Skip to content

Commit d96e331

Browse files
committed
#272 java version
1 parent 65e46fd commit d96e331

4 files changed

Lines changed: 83 additions & 21 deletions

File tree

src/it/utf/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
3333
<groupId>com.jcabi.xml</groupId>
3434
<artifactId>utf</artifactId>
3535
<version>1.0-SNAPSHOT</version>
36+
<properties>
37+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
38+
<maven.compiler.source>1.8</maven.compiler.source>
39+
<maven.compiler.target>1.8</maven.compiler.target>
40+
<skipITs/>
41+
</properties>
3642
<dependencies>
3743
<dependency>
3844
<groupId>@project.groupId@</groupId>

src/main/java/com/jcabi/xml/XMLDocument.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,16 @@ public int hashCode() {
307307

308308
@Override
309309
public Node node() {
310-
final Node casted = this.cache;
311-
final Node answer;
312-
if (casted instanceof Document) {
313-
answer = casted.cloneNode(true);
314-
} else {
315-
answer = XMLDocument.createImportedNode(casted);
310+
synchronized (XML.class) {
311+
final Node casted = this.cache;
312+
final Node answer;
313+
if (casted instanceof Document) {
314+
answer = casted.cloneNode(true);
315+
} else {
316+
answer = XMLDocument.createImportedNode(casted);
317+
}
318+
return answer;
316319
}
317-
return answer;
318320
}
319321

320322
@Override

src/test/java/com/jcabi/xml/StrictXMLTest.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.Collections;
3838
import java.util.Random;
3939
import java.util.concurrent.Callable;
40+
import java.util.concurrent.CountDownLatch;
4041
import java.util.concurrent.ExecutorService;
4142
import java.util.concurrent.Executors;
4243
import java.util.concurrent.TimeUnit;
@@ -125,9 +126,6 @@ void rejectsInvalidXmlUsingXsiSchemaLocation() {
125126

126127
@Test
127128
void validatesMultipleXmlsInThreads() throws Exception {
128-
final int timeout = 10;
129-
final int numrun = 100;
130-
final int loop = 50;
131129
final XSD xsd = new XSDDocument(
132130
StringUtils.join(
133131
"<xs:schema xmlns:xs ='http://www.w3.org/2001/XMLSchema' >",
@@ -145,9 +143,9 @@ void validatesMultipleXmlsInThreads() throws Exception {
145143
Iterables.concat(
146144
Collections.singleton("<r>"),
147145
Iterables.transform(
148-
Collections.nCopies(timeout, 0),
146+
Collections.nCopies(10, 0),
149147
pos -> String.format(
150-
"<x>%d</x>", rnd.nextInt(numrun)
148+
"<x>%d</x>", rnd.nextInt(100)
151149
)
152150
),
153151
Collections.singleton("<x>101</x></r>")
@@ -156,25 +154,33 @@ void validatesMultipleXmlsInThreads() throws Exception {
156154
)
157155
);
158156
final AtomicInteger done = new AtomicInteger();
157+
final int threads = 50;
158+
final CountDownLatch latch = new CountDownLatch(threads);
159159
final Callable<Void> callable = () -> {
160160
try {
161161
new StrictXML(xml, xsd);
162162
} catch (final IllegalArgumentException ex) {
163163
done.incrementAndGet();
164+
} finally {
165+
latch.countDown();
164166
}
165167
return null;
166168
};
167169
final ExecutorService service = Executors.newFixedThreadPool(5);
168-
for (int count = 0; count < loop; count += 1) {
169-
service.submit(callable);
170+
try {
171+
for (int count = 0; count < threads; count += 1) {
172+
service.submit(callable);
173+
}
174+
latch.await(1L, TimeUnit.SECONDS);
175+
MatcherAssert.assertThat(done.get(), Matchers.equalTo(threads));
176+
} finally {
177+
service.shutdown();
178+
MatcherAssert.assertThat(
179+
service.awaitTermination(10L, TimeUnit.SECONDS),
180+
Matchers.is(true)
181+
);
182+
service.shutdownNow();
170183
}
171-
service.shutdown();
172-
MatcherAssert.assertThat(
173-
service.awaitTermination(timeout, TimeUnit.SECONDS),
174-
Matchers.is(true)
175-
);
176-
service.shutdownNow();
177-
MatcherAssert.assertThat(done.get(), Matchers.equalTo(loop));
178184
}
179185

180186
@Test

src/test/java/com/jcabi/xml/XMLDocumentTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@
2929
*/
3030
package com.jcabi.xml;
3131

32+
import com.google.common.collect.Iterables;
3233
import com.jcabi.matchers.XhtmlMatchers;
3334
import java.io.ByteArrayInputStream;
3435
import java.io.File;
3536
import java.nio.file.Files;
3637
import java.util.Arrays;
3738
import java.util.Collections;
3839
import java.util.List;
40+
import java.util.concurrent.CountDownLatch;
3941
import java.util.concurrent.ExecutorService;
4042
import java.util.concurrent.Executors;
4143
import java.util.concurrent.TimeUnit;
@@ -342,6 +344,52 @@ void printsInMultipleThreads() throws Exception {
342344
service.shutdownNow();
343345
}
344346

347+
@Test
348+
void takesNodeInMultipleThreads() throws Exception {
349+
final int threads = 50;
350+
final XML xml = new XMLDocument(
351+
StringUtils.join(
352+
Iterables.concat(
353+
Collections.singleton("<r>"),
354+
Iterables.transform(
355+
Collections.nCopies(100, 0),
356+
pos -> String.format("<x>%d</x>", pos)
357+
),
358+
Collections.singleton("<x>5555</x></r>")
359+
),
360+
" "
361+
)
362+
);
363+
final AtomicInteger done = new AtomicInteger();
364+
final CountDownLatch latch = new CountDownLatch(threads);
365+
final Runnable runnable = () -> {
366+
try {
367+
MatcherAssert.assertThat(
368+
new XMLDocument(xml.node()).toString(),
369+
Matchers.containsString(">5555<")
370+
);
371+
done.incrementAndGet();
372+
} finally {
373+
latch.countDown();
374+
}
375+
};
376+
final ExecutorService service = Executors.newFixedThreadPool(threads);
377+
try {
378+
for (int thread = 0; thread < threads; ++thread) {
379+
service.submit(runnable);
380+
}
381+
latch.await(1L, TimeUnit.SECONDS);
382+
MatcherAssert.assertThat(done.get(), Matchers.equalTo(threads));
383+
} finally {
384+
service.shutdown();
385+
MatcherAssert.assertThat(
386+
service.awaitTermination(10L, TimeUnit.SECONDS),
387+
Matchers.is(true)
388+
);
389+
service.shutdownNow();
390+
}
391+
}
392+
345393
@Test
346394
void performsXpathCalculations() {
347395
final XML xml = new XMLDocument("<x><a/><a/><a/></x>");

0 commit comments

Comments
 (0)