From cc8244203340c9bc6d8f0355309456e7c5dc2d3c Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Wed, 29 Jan 2025 12:28:17 +0300 Subject: [PATCH 1/5] feat(#146): add more tests to StEndless --- .../com/yegor256/xsline/StEndlessTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/test/java/com/yegor256/xsline/StEndlessTest.java b/src/test/java/com/yegor256/xsline/StEndlessTest.java index 064dd5d..920a105 100644 --- a/src/test/java/com/yegor256/xsline/StEndlessTest.java +++ b/src/test/java/com/yegor256/xsline/StEndlessTest.java @@ -24,9 +24,11 @@ package com.yegor256.xsline; import com.jcabi.matchers.XhtmlMatchers; +import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; import com.jcabi.xml.XSLDocument; import java.io.IOException; +import java.util.concurrent.atomic.AtomicInteger; import org.hamcrest.MatcherAssert; import org.junit.jupiter.api.Test; @@ -49,4 +51,44 @@ void simpleScenario() throws IOException { ); } + @Test + void changesXmlOnce() { + MatcherAssert.assertThat( + "We expect a shift is applied twice", + new StEndless(new Twice()).apply(0, new XMLDocument("")), + XhtmlMatchers.hasXPaths("/dummy") + ); + } + + /** + * A dummy shift that does nothing and returns a constant XML. + * However, it can be applied only twice, and then it throws an exception. + * + * @since 0.34 + */ + private static class Twice implements Shift { + + /** + * How many times are allowed to transform. + */ + private final AtomicInteger attempts; + + Twice() { + this.attempts = new AtomicInteger(2); + } + + @Override + public String uid() { + return "twice-dummy"; + } + + @Override + public XML apply(final int position, final XML xml) { + if (this.attempts.decrementAndGet() >= 0) { + return new XMLDocument("I just do nothing"); + } + throw new IllegalStateException("This shift was already used, but it shouldn't"); + } + } + } From 1eaae7662d689b30325b8f6d0340825f92f846e2 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Wed, 29 Jan 2025 13:01:42 +0300 Subject: [PATCH 2/5] feat(#146): add a bit more tests --- .../java/com/yegor256/xsline/StEndless.java | 3 +- .../com/yegor256/xsline/StEndlessTest.java | 36 ++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/yegor256/xsline/StEndless.java b/src/main/java/com/yegor256/xsline/StEndless.java index d43eb24..b95cef8 100644 --- a/src/main/java/com/yegor256/xsline/StEndless.java +++ b/src/main/java/com/yegor256/xsline/StEndless.java @@ -68,7 +68,8 @@ public StEndless(final Shift shift) { boolean more; do { after = shift.apply(position, before); - more = !after.toString().equals(before.toString()); +// more = !after.toString().equals(before.toString()); + more = !after.inner().isEqualNode(before.inner()); before = after; } while (more); return after; diff --git a/src/test/java/com/yegor256/xsline/StEndlessTest.java b/src/test/java/com/yegor256/xsline/StEndlessTest.java index 920a105..ea3c6da 100644 --- a/src/test/java/com/yegor256/xsline/StEndlessTest.java +++ b/src/test/java/com/yegor256/xsline/StEndlessTest.java @@ -53,9 +53,23 @@ void simpleScenario() throws IOException { @Test void changesXmlOnce() { + MatcherAssert.assertThat( + "We expect a shift is used twice", + new StEndless(new Dummy(2)).apply(0, new XMLDocument("")), + XhtmlMatchers.hasXPaths("/dummy") + ); + } + + @Test + void understandsDifferenceBetweenDocumentAndFirstNode() { MatcherAssert.assertThat( "We expect a shift is applied twice", - new StEndless(new Twice()).apply(0, new XMLDocument("")), + new StEndless(new Dummy(2)).apply( + 0, + new XMLDocument( + new XMLDocument("I just do nothing").inner().getFirstChild() + ) + ), XhtmlMatchers.hasXPaths("/dummy") ); } @@ -66,15 +80,29 @@ void changesXmlOnce() { * * @since 0.34 */ - private static class Twice implements Shift { + private static class Dummy implements Shift { /** * How many times are allowed to transform. */ private final AtomicInteger attempts; - Twice() { - this.attempts = new AtomicInteger(2); + /** + * Ctor. + * + * @param attempts How many times are allowed to transform. + */ + Dummy(final int attempts) { + this(new AtomicInteger(attempts)); + } + + /** + * Ctor. + * + * @param attempts How many times are allowed to transform. + */ + private Dummy(final AtomicInteger attempts) { + this.attempts = attempts; } @Override From c4549f490a4ae15be6df08718753c32438a55a1e Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Wed, 29 Jan 2025 13:16:25 +0300 Subject: [PATCH 3/5] feat(#146): fast comparision of large XML files --- .../java/com/yegor256/xsline/StEndless.java | 1 - .../com/yegor256/xsline/StEndlessTest.java | 49 +++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/yegor256/xsline/StEndless.java b/src/main/java/com/yegor256/xsline/StEndless.java index b95cef8..e82c983 100644 --- a/src/main/java/com/yegor256/xsline/StEndless.java +++ b/src/main/java/com/yegor256/xsline/StEndless.java @@ -68,7 +68,6 @@ public StEndless(final Shift shift) { boolean more; do { after = shift.apply(position, before); -// more = !after.toString().equals(before.toString()); more = !after.inner().isEqualNode(before.inner()); before = after; } while (more); diff --git a/src/test/java/com/yegor256/xsline/StEndlessTest.java b/src/test/java/com/yegor256/xsline/StEndlessTest.java index ea3c6da..f52e6af 100644 --- a/src/test/java/com/yegor256/xsline/StEndlessTest.java +++ b/src/test/java/com/yegor256/xsline/StEndlessTest.java @@ -74,6 +74,32 @@ void understandsDifferenceBetweenDocumentAndFirstNode() { ); } + @Test + void changesLargeXmlDocs() { + final String initial = StEndlessTest.largeXml("initial"); + final String updated = StEndlessTest.largeXml("updated"); + MatcherAssert.assertThat( + "We expect large XMLs are transformed fast", + new StEndless(new Dummy(2, updated)).apply(0, new XMLDocument(initial)), + XhtmlMatchers.hasXPaths("/updated") + ); + } + + /** + * Generate large XML. + * + * @return Large XML. + */ + private static String largeXml(final String root) { + final StringBuilder xml = new StringBuilder(0); + xml.append('<').append(root).append('>'); + for (int idx = 0; idx < 10_000; ++idx) { + xml.append("").append(idx).append(""); + } + xml.append("'); + return xml.toString(); + } + /** * A dummy shift that does nothing and returns a constant XML. * However, it can be applied only twice, and then it throws an exception. @@ -87,22 +113,39 @@ private static class Dummy implements Shift { */ private final AtomicInteger attempts; + /** + * XML to return. + */ + private final String xml; + /** * Ctor. * * @param attempts How many times are allowed to transform. */ Dummy(final int attempts) { - this(new AtomicInteger(attempts)); + this(attempts, "I just do nothing"); + } + + /** + * Ctor. + * + * @param attempts How many times are allowed to transform. + * @param xml XML to return. + */ + Dummy(final int attempts, final String xml) { + this(new AtomicInteger(attempts), xml); } /** * Ctor. * * @param attempts How many times are allowed to transform. + * @param xml XML to return. */ - private Dummy(final AtomicInteger attempts) { + private Dummy(final AtomicInteger attempts, final String xml) { this.attempts = attempts; + this.xml = xml; } @Override @@ -113,7 +156,7 @@ public String uid() { @Override public XML apply(final int position, final XML xml) { if (this.attempts.decrementAndGet() >= 0) { - return new XMLDocument("I just do nothing"); + return new XMLDocument(this.xml); } throw new IllegalStateException("This shift was already used, but it shouldn't"); } From 680a8d9d3e2694046aa66e6a941219b9fed2bf2a Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Wed, 29 Jan 2025 13:20:08 +0300 Subject: [PATCH 4/5] feat(#146): fix all the code offences --- src/test/java/com/yegor256/xsline/StEndlessTest.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/yegor256/xsline/StEndlessTest.java b/src/test/java/com/yegor256/xsline/StEndlessTest.java index f52e6af..851731a 100644 --- a/src/test/java/com/yegor256/xsline/StEndlessTest.java +++ b/src/test/java/com/yegor256/xsline/StEndlessTest.java @@ -88,12 +88,14 @@ void changesLargeXmlDocs() { /** * Generate large XML. * + * @param root Root element. * @return Large XML. */ private static String largeXml(final String root) { - final StringBuilder xml = new StringBuilder(0); + final int capacity = 10_000; + final StringBuilder xml = new StringBuilder(capacity); xml.append('<').append(root).append('>'); - for (int idx = 0; idx < 10_000; ++idx) { + for (int idx = 0; idx < capacity; ++idx) { xml.append("").append(idx).append(""); } xml.append("'); @@ -154,7 +156,7 @@ public String uid() { } @Override - public XML apply(final int position, final XML xml) { + public XML apply(final int position, final XML node) { if (this.attempts.decrementAndGet() >= 0) { return new XMLDocument(this.xml); } From 87733c59d7c25a33d20d3991f34d02c81697b36e Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Wed, 29 Jan 2025 17:46:31 +0300 Subject: [PATCH 5/5] feat(#146): update jcabi-xml to version '0.33.5' --- pom.xml | 2 +- src/main/java/com/yegor256/xsline/StSchema.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index a200452..46b4275 100644 --- a/pom.xml +++ b/pom.xml @@ -85,7 +85,7 @@ SOFTWARE. com.jcabi jcabi-xml - 0.33.3 + 0.33.5 xml-apis diff --git a/src/main/java/com/yegor256/xsline/StSchema.java b/src/main/java/com/yegor256/xsline/StSchema.java index 8d1d84f..3464504 100644 --- a/src/main/java/com/yegor256/xsline/StSchema.java +++ b/src/main/java/com/yegor256/xsline/StSchema.java @@ -24,6 +24,7 @@ package com.yegor256.xsline; import com.jcabi.log.Logger; +import com.jcabi.xml.ClasspathResolver; import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; import java.io.FileNotFoundException; @@ -32,7 +33,6 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Objects; import org.xml.sax.SAXParseException; @@ -103,7 +103,7 @@ public StSchema(final XML schema) { private static XML validate(final XML schema, final XML xml) { final Collection violations; if (Objects.isNull(schema)) { - violations = Collections.emptyList(); + violations = xml.validate(new ClasspathResolver()); } else { violations = xml.validate(schema); }