Skip to content

Commit

Permalink
#272 java version
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 7, 2024
1 parent 65e46fd commit d96e331
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 21 deletions.
6 changes: 6 additions & 0 deletions src/it/utf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<groupId>com.jcabi.xml</groupId>
<artifactId>utf</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<skipITs/>
</properties>
<dependencies>
<dependency>
<groupId>@project.groupId@</groupId>
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/jcabi/xml/XMLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,16 @@ public int hashCode() {

@Override
public Node node() {
final Node casted = this.cache;
final Node answer;
if (casted instanceof Document) {
answer = casted.cloneNode(true);
} else {
answer = XMLDocument.createImportedNode(casted);
synchronized (XML.class) {
final Node casted = this.cache;
final Node answer;
if (casted instanceof Document) {
answer = casted.cloneNode(true);
} else {
answer = XMLDocument.createImportedNode(casted);
}
return answer;
}
return answer;
}

@Override
Expand Down
34 changes: 20 additions & 14 deletions src/test/java/com/jcabi/xml/StrictXMLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Collections;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -125,9 +126,6 @@ void rejectsInvalidXmlUsingXsiSchemaLocation() {

@Test
void validatesMultipleXmlsInThreads() throws Exception {
final int timeout = 10;
final int numrun = 100;
final int loop = 50;
final XSD xsd = new XSDDocument(
StringUtils.join(
"<xs:schema xmlns:xs ='http://www.w3.org/2001/XMLSchema' >",
Expand All @@ -145,9 +143,9 @@ void validatesMultipleXmlsInThreads() throws Exception {
Iterables.concat(
Collections.singleton("<r>"),
Iterables.transform(
Collections.nCopies(timeout, 0),
Collections.nCopies(10, 0),
pos -> String.format(
"<x>%d</x>", rnd.nextInt(numrun)
"<x>%d</x>", rnd.nextInt(100)
)
),
Collections.singleton("<x>101</x></r>")
Expand All @@ -156,25 +154,33 @@ void validatesMultipleXmlsInThreads() throws Exception {
)
);
final AtomicInteger done = new AtomicInteger();
final int threads = 50;
final CountDownLatch latch = new CountDownLatch(threads);
final Callable<Void> callable = () -> {
try {
new StrictXML(xml, xsd);
} catch (final IllegalArgumentException ex) {
done.incrementAndGet();
} finally {
latch.countDown();
}
return null;
};
final ExecutorService service = Executors.newFixedThreadPool(5);
for (int count = 0; count < loop; count += 1) {
service.submit(callable);
try {
for (int count = 0; count < threads; count += 1) {
service.submit(callable);
}
latch.await(1L, TimeUnit.SECONDS);
MatcherAssert.assertThat(done.get(), Matchers.equalTo(threads));
} finally {
service.shutdown();
MatcherAssert.assertThat(
service.awaitTermination(10L, TimeUnit.SECONDS),
Matchers.is(true)
);
service.shutdownNow();
}
service.shutdown();
MatcherAssert.assertThat(
service.awaitTermination(timeout, TimeUnit.SECONDS),
Matchers.is(true)
);
service.shutdownNow();
MatcherAssert.assertThat(done.get(), Matchers.equalTo(loop));
}

@Test
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/com/jcabi/xml/XMLDocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
*/
package com.jcabi.xml;

import com.google.common.collect.Iterables;
import com.jcabi.matchers.XhtmlMatchers;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -342,6 +344,52 @@ void printsInMultipleThreads() throws Exception {
service.shutdownNow();
}

@Test
void takesNodeInMultipleThreads() throws Exception {
final int threads = 50;
final XML xml = new XMLDocument(
StringUtils.join(
Iterables.concat(
Collections.singleton("<r>"),
Iterables.transform(
Collections.nCopies(100, 0),
pos -> String.format("<x>%d</x>", pos)
),
Collections.singleton("<x>5555</x></r>")
),
" "
)
);
final AtomicInteger done = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(threads);
final Runnable runnable = () -> {
try {
MatcherAssert.assertThat(
new XMLDocument(xml.node()).toString(),
Matchers.containsString(">5555<")
);
done.incrementAndGet();
} finally {
latch.countDown();
}
};
final ExecutorService service = Executors.newFixedThreadPool(threads);
try {
for (int thread = 0; thread < threads; ++thread) {
service.submit(runnable);
}
latch.await(1L, TimeUnit.SECONDS);
MatcherAssert.assertThat(done.get(), Matchers.equalTo(threads));
} finally {
service.shutdown();
MatcherAssert.assertThat(
service.awaitTermination(10L, TimeUnit.SECONDS),
Matchers.is(true)
);
service.shutdownNow();
}
}

@Test
void performsXpathCalculations() {
final XML xml = new XMLDocument("<x><a/><a/><a/></x>");
Expand Down

0 comments on commit d96e331

Please sign in to comment.