Skip to content

Commit

Permalink
Merge pull request #3758 from maxonfjvipon/bug/#3726/synchronized-str…
Browse files Browse the repository at this point in the history
…ict-xmir

bug(#3726): Updated jcabi-xml and made `StrictXmir` synchronized
  • Loading branch information
yegor256 authored Dec 25, 2024
2 parents 71e3409 + b408f9b commit 3e3caa8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 45 deletions.
40 changes: 24 additions & 16 deletions eo-parser/src/main/java/org/eolang/parser/StrictXmir.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
import org.cactoos.bytes.IoCheckedBytes;
import org.cactoos.io.InputOf;
import org.cactoos.io.ResourceOf;
import org.cactoos.scalar.Sticky;
import org.cactoos.scalar.Synced;
import org.cactoos.scalar.Unchecked;
import org.w3c.dom.Node;
import org.w3c.dom.ls.LSResourceResolver;
import org.xembly.Directives;
import org.xembly.Xembler;
import org.xml.sax.SAXParseException;
Expand Down Expand Up @@ -71,7 +75,7 @@ public final class StrictXmir implements XML {
/**
* The XML.
*/
private final XML xml;
private final Unchecked<XML> xml;

/**
* Ctor.
Expand All @@ -91,36 +95,40 @@ public StrictXmir(final XML src) {
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public StrictXmir(final XML before, final Path tmp) {
synchronized (before) {
this.xml = new StrictXML(
StrictXmir.reset(before, tmp)
);
}
this.xml = new Unchecked<>(
new Synced<>(
new Sticky<>(
() -> new StrictXML(
StrictXmir.reset(before, tmp)
)
)
)
);
}

@Override
public String toString() {
return this.xml.toString();
return this.xml.value().toString();
}

@Override
public List<String> xpath(final String query) {
return this.xml.xpath(query);
return this.xml.value().xpath(query);
}

@Override
public List<XML> nodes(final String query) {
return this.xml.nodes(query);
return this.xml.value().nodes(query);
}

@Override
public XML registerNs(final String prefix, final Object uri) {
return this.xml.registerNs(prefix, uri);
return this.xml.value().registerNs(prefix, uri);
}

@Override
public XML merge(final NamespaceContext context) {
return this.xml.merge(context);
return this.xml.value().merge(context);
}

@Override
Expand All @@ -131,22 +139,22 @@ public Node node() {

@Override
public Node inner() {
return this.xml.inner();
return this.xml.value().inner();
}

@Override
public Node deepCopy() {
return this.xml.deepCopy();
return this.xml.value().deepCopy();
}

@Override
public Collection<SAXParseException> validate() {
return this.xml.validate();
public Collection<SAXParseException> validate(final LSResourceResolver resolver) {
return this.xml.value().validate(resolver);
}

@Override
public Collection<SAXParseException> validate(final XML schema) {
return this.xml.validate(schema);
return this.xml.value().validate(schema);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions eo-parser/src/main/java/org/eolang/parser/Xmir.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.logging.Level;
import javax.xml.namespace.NamespaceContext;
import org.w3c.dom.Node;
import org.w3c.dom.ls.LSResourceResolver;
import org.xml.sax.SAXParseException;

/**
Expand Down Expand Up @@ -163,8 +164,8 @@ public Node deepCopy() {
}

@Override
public Collection<SAXParseException> validate() {
return this.xml.validate();
public Collection<SAXParseException> validate(final LSResourceResolver resolver) {
return this.xml.validate(resolver);
}

@Override
Expand Down
24 changes: 13 additions & 11 deletions eo-parser/src/test/java/org/eolang/parser/DrProgramTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
package org.eolang.parser;

import com.jcabi.matchers.XhtmlMatchers;
import com.jcabi.xml.StrictXML;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.nio.file.Paths;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
Expand Down Expand Up @@ -83,17 +85,17 @@ void setsSchemaLocation() throws Exception {

@Test
void validatesAgainstSchema() {
MatcherAssert.assertThat(
"XMIR document validates correctly",
new XMLDocument(
new Xembler(
new Directives().append(new DrProgram("foo"))
.add("listing").set("hello, world!").up()
.add("objects").add("o").attr("name", "bar")
).domQuietly()
).validate(),
Matchers.emptyIterable()
Assertions.assertDoesNotThrow(
new StrictXML(
new XMLDocument(
new Xembler(
new Directives().append(new DrProgram("foo"))
.add("listing").set("hello, world!").up()
.add("objects").add("o").attr("name", "bar")
).domQuietly()
)
)::inner,
"XMIR document validates correctly"
);
}

}
28 changes: 14 additions & 14 deletions eo-parser/src/test/java/org/eolang/parser/StrictXmirTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ final class StrictXmirTest {
@ExtendWith(MktmpResolver.class)
@ExtendWith(WeAreOnline.class)
void validatesXmir(@Mktmp final Path tmp) {
MatcherAssert.assertThat(
"validation should pass as normal",
Assertions.assertDoesNotThrow(
new StrictXmir(
StrictXmirTest.xmir("https://www.eolang.org/XMIR.xsd"),
tmp
).validate(),
Matchers.emptyIterable()
)::inner,
"validation should pass as normal"
);
MatcherAssert.assertThat(
"temporary XSD file created",
Expand All @@ -75,7 +74,7 @@ void doesNotFailWithDifferentXmlInMultipleThreads(@Mktmp final Path tmp) {
new Together<>(
thread -> {
final XML xml = StrictXmirTest.xmir("https://www.eolang.org/XMIR.xsd");
return new StrictXmir(xml, tmp);
return new StrictXmir(xml, tmp).inner();
}
)::asList,
"StrictXmir should not fail in different threads with different xmls"
Expand All @@ -85,11 +84,13 @@ void doesNotFailWithDifferentXmlInMultipleThreads(@Mktmp final Path tmp) {
@RepeatedTest(20)
@ExtendWith(WeAreOnline.class)
@ExtendWith(MktmpResolver.class)
void doesNotFailWithSameXmlInMultipleThreads(@Mktmp final Path tmp) {
final XML xml = StrictXmirTest.xmir("https://www.eolang.org/XMIR.xsd");
void doesNotFailOnTheSameOperation(@Mktmp final Path tmp) {
final XML xmir = new StrictXmir(
StrictXmirTest.xmir("https://www.eolang.org/XMIR.xsd"), tmp
);
Assertions.assertDoesNotThrow(
new Together<>(
thread -> new StrictXmir(xml, tmp)
thread -> xmir.inner()
)::asList,
"StrictXmir should not fail in different threads with the same xml"
);
Expand All @@ -114,8 +115,7 @@ void refersToAbsoluteFileName(@Mktmp final Path tmp) {
@Test
@ExtendWith(MktmpResolver.class)
void validatesXmirWithLocalSchema(@Mktmp final Path tmp) {
MatcherAssert.assertThat(
"validation should pass as normal",
Assertions.assertDoesNotThrow(
new StrictXmir(
new Xmir(
StrictXmirTest.xmir(
Expand All @@ -126,8 +126,8 @@ void validatesXmirWithLocalSchema(@Mktmp final Path tmp) {
)
),
tmp
).validate(),
Matchers.emptyIterable()
)::inner,
"validation should pass as normal"
);
MatcherAssert.assertThat(
"temporary XSD file created",
Expand All @@ -143,12 +143,12 @@ void validatesXmirWithLocalSchema(@Mktmp final Path tmp) {
void validatesXmirWithBrokenUri(@Mktmp final Path tmp) {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> new StrictXmir(
new StrictXmir(
new Xmir(
StrictXmirTest.xmir("https://www.invalid-website-uri/XMIR.xsd")
),
tmp
).validate(),
)::inner,
"validation should fail because of broken URI"
);
}
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ SOFTWARE.
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-xml</artifactId>
<version>0.33.1</version>
<version>0.33.3</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
Expand Down Expand Up @@ -482,7 +482,7 @@ SOFTWARE.
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-xml</artifactId>
<version>0.33.1</version>
<version>0.33.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
Expand Down

0 comments on commit 3e3caa8

Please sign in to comment.