From 9bb9266081fa09e5e8c8bf74ae838abbe54aef4c Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Fri, 3 May 2024 16:13:47 +0300 Subject: [PATCH 1/8] bug: indentify the bug with XMLDocument comparison --- pom.xml | 20 ++++++++ src/it/groovy/README.md | 12 +++++ src/it/groovy/first.xmir | 4 ++ src/it/groovy/invoker.properties | 1 + src/it/groovy/pom.xml | 48 +++++++++++++++++++ src/it/groovy/same.xmir | 4 ++ src/it/groovy/verify.groovy | 38 +++++++++++++++ .../java/com/jcabi/xml/XMLDocumentTest.java | 15 ++++++ 8 files changed, 142 insertions(+) create mode 100644 src/it/groovy/README.md create mode 100644 src/it/groovy/first.xmir create mode 100644 src/it/groovy/invoker.properties create mode 100644 src/it/groovy/pom.xml create mode 100644 src/it/groovy/same.xmir create mode 100644 src/it/groovy/verify.groovy diff --git a/pom.xml b/pom.xml index aa88513..9a8d0ea 100644 --- a/pom.xml +++ b/pom.xml @@ -161,6 +161,26 @@ OF THE POSSIBILITY OF SUCH DAMAGE. 8 + + maven-invoker-plugin + + + groovy/pom.xml + + + + ${project.groupId} + ${project.artifactId} + ${project.version} + + + diff --git a/src/it/groovy/README.md b/src/it/groovy/README.md new file mode 100644 index 0000000..eff85a9 --- /dev/null +++ b/src/it/groovy/README.md @@ -0,0 +1,12 @@ +# Groovy Integration Test + +This module contains integration tests for `jcabi-xml` usage in Groovy scripts. + +To run the tests, execute the following command: + +```shell +mvn clean integration-test invoker:run -Dinvoker.test=groovy -DskipTests +``` + +The testing Groovy code you can find in the [verify.groovy](verify.groovy) +script. \ No newline at end of file diff --git a/src/it/groovy/first.xmir b/src/it/groovy/first.xmir new file mode 100644 index 0000000..f692288 --- /dev/null +++ b/src/it/groovy/first.xmir @@ -0,0 +1,4 @@ + + + + diff --git a/src/it/groovy/invoker.properties b/src/it/groovy/invoker.properties new file mode 100644 index 0000000..2329e6c --- /dev/null +++ b/src/it/groovy/invoker.properties @@ -0,0 +1 @@ +invoker.goals = clean \ No newline at end of file diff --git a/src/it/groovy/pom.xml b/src/it/groovy/pom.xml new file mode 100644 index 0000000..774df8a --- /dev/null +++ b/src/it/groovy/pom.xml @@ -0,0 +1,48 @@ + + + + 4.0.0 + + com.jcabi + jcabi + 1.39.0 + + com.jcabi.xml + groovy + 1.0-SNAPSHOT + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + diff --git a/src/it/groovy/same.xmir b/src/it/groovy/same.xmir new file mode 100644 index 0000000..950c93f --- /dev/null +++ b/src/it/groovy/same.xmir @@ -0,0 +1,4 @@ + + + + diff --git a/src/it/groovy/verify.groovy b/src/it/groovy/verify.groovy new file mode 100644 index 0000000..490e352 --- /dev/null +++ b/src/it/groovy/verify.groovy @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2012-2022, jcabi.com + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: 1) Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. 2) Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. 3) Neither the name of the jcabi.com nor + * the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +import com.jcabi.xml.XMLDocument + +def log = new File(basedir, 'build.log') +def fist = new XMLDocument(new File(basedir, 'first.xmir')) +def same = new XMLDocument(new File(basedir, 'same.xmir')) + +assert fist == same + +assert log.text.contains("BUILD SUCCESS") \ No newline at end of file diff --git a/src/test/java/com/jcabi/xml/XMLDocumentTest.java b/src/test/java/com/jcabi/xml/XMLDocumentTest.java index 2d7c3b7..4f7a9bd 100644 --- a/src/test/java/com/jcabi/xml/XMLDocumentTest.java +++ b/src/test/java/com/jcabi/xml/XMLDocumentTest.java @@ -48,6 +48,7 @@ import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.hamcrest.core.IsEqual; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -381,6 +382,20 @@ void comparesToAnotherDocument() { ); } + @Test + @Disabled + void comparesDocumentsWithDifferentIndentations() { + // @todo #1:90min Implement comparison of XML documents with different indentations. + // The current implementation of XMLDocument does not ignore different indentations + // when comparing two XML documents. We need to implement a comparison that ignores + // different indentations. Don't forget to remove the @Disabled annotation from this test. + MatcherAssert.assertThat( + "Different indentations should be ignored", + new XMLDocument("\n "), + Matchers.equalTo(new XMLDocument("\n ")) + ); + } + @Test void preservesXmlNamespaces() { final String xml = ""; From 4077325883f12e7b3f30392927c6be49bb0b4418 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Fri, 3 May 2024 16:21:34 +0300 Subject: [PATCH 2/8] chore: suppress MethodBodyCommentsCheck --- src/test/java/com/jcabi/xml/XMLDocumentTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/jcabi/xml/XMLDocumentTest.java b/src/test/java/com/jcabi/xml/XMLDocumentTest.java index 4f7a9bd..8a34900 100644 --- a/src/test/java/com/jcabi/xml/XMLDocumentTest.java +++ b/src/test/java/com/jcabi/xml/XMLDocumentTest.java @@ -385,6 +385,7 @@ void comparesToAnotherDocument() { @Test @Disabled void comparesDocumentsWithDifferentIndentations() { + // @checkstyle MethodBodyCommentsCheck (4 lines) // @todo #1:90min Implement comparison of XML documents with different indentations. // The current implementation of XMLDocument does not ignore different indentations // when comparing two XML documents. We need to implement a comparison that ignores From cb296b408d08d9fb164862f37e2a3a8917e5ed2f Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 6 May 2024 15:42:51 +0300 Subject: [PATCH 3/8] bug(#247): rename xmir -> xml --- src/it/groovy/{first.xmir => first.xml} | 0 src/it/groovy/{same.xmir => same.xml} | 0 src/it/groovy/verify.groovy | 4 ++-- 3 files changed, 2 insertions(+), 2 deletions(-) rename src/it/groovy/{first.xmir => first.xml} (100%) rename src/it/groovy/{same.xmir => same.xml} (100%) diff --git a/src/it/groovy/first.xmir b/src/it/groovy/first.xml similarity index 100% rename from src/it/groovy/first.xmir rename to src/it/groovy/first.xml diff --git a/src/it/groovy/same.xmir b/src/it/groovy/same.xml similarity index 100% rename from src/it/groovy/same.xmir rename to src/it/groovy/same.xml diff --git a/src/it/groovy/verify.groovy b/src/it/groovy/verify.groovy index 490e352..c49c508 100644 --- a/src/it/groovy/verify.groovy +++ b/src/it/groovy/verify.groovy @@ -30,8 +30,8 @@ import com.jcabi.xml.XMLDocument def log = new File(basedir, 'build.log') -def fist = new XMLDocument(new File(basedir, 'first.xmir')) -def same = new XMLDocument(new File(basedir, 'same.xmir')) +def fist = new XMLDocument(new File(basedir, 'first.xml')) +def same = new XMLDocument(new File(basedir, 'same.xml')) assert fist == same From 8ea331269c13380eafa9db9f5f84827320c093e0 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 6 May 2024 15:49:33 +0300 Subject: [PATCH 4/8] bug(#247): rename xml -> xmir --- src/it/groovy/{first.xml => first.xmir} | 0 src/it/groovy/{same.xml => same.xmir} | 0 src/it/groovy/verify.groovy | 4 ++-- 3 files changed, 2 insertions(+), 2 deletions(-) rename src/it/groovy/{first.xml => first.xmir} (100%) rename src/it/groovy/{same.xml => same.xmir} (100%) diff --git a/src/it/groovy/first.xml b/src/it/groovy/first.xmir similarity index 100% rename from src/it/groovy/first.xml rename to src/it/groovy/first.xmir diff --git a/src/it/groovy/same.xml b/src/it/groovy/same.xmir similarity index 100% rename from src/it/groovy/same.xml rename to src/it/groovy/same.xmir diff --git a/src/it/groovy/verify.groovy b/src/it/groovy/verify.groovy index c49c508..490e352 100644 --- a/src/it/groovy/verify.groovy +++ b/src/it/groovy/verify.groovy @@ -30,8 +30,8 @@ import com.jcabi.xml.XMLDocument def log = new File(basedir, 'build.log') -def fist = new XMLDocument(new File(basedir, 'first.xml')) -def same = new XMLDocument(new File(basedir, 'same.xml')) +def fist = new XMLDocument(new File(basedir, 'first.xmir')) +def same = new XMLDocument(new File(basedir, 'same.xmir')) assert fist == same From 56a53b96c255410b2cc228524c4a2ab17cee9af1 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 6 May 2024 15:51:32 +0300 Subject: [PATCH 5/8] bug(#247): use 'equals' instead of '==' --- src/it/groovy/verify.groovy | 2 +- src/test/java/com/jcabi/xml/XMLDocumentTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/it/groovy/verify.groovy b/src/it/groovy/verify.groovy index 490e352..8eb2ed6 100644 --- a/src/it/groovy/verify.groovy +++ b/src/it/groovy/verify.groovy @@ -33,6 +33,6 @@ def log = new File(basedir, 'build.log') def fist = new XMLDocument(new File(basedir, 'first.xmir')) def same = new XMLDocument(new File(basedir, 'same.xmir')) -assert fist == same +assert fist.equals(same) assert log.text.contains("BUILD SUCCESS") \ No newline at end of file diff --git a/src/test/java/com/jcabi/xml/XMLDocumentTest.java b/src/test/java/com/jcabi/xml/XMLDocumentTest.java index 8a34900..f54f62e 100644 --- a/src/test/java/com/jcabi/xml/XMLDocumentTest.java +++ b/src/test/java/com/jcabi/xml/XMLDocumentTest.java @@ -383,7 +383,7 @@ void comparesToAnotherDocument() { } @Test - @Disabled +// @Disabled void comparesDocumentsWithDifferentIndentations() { // @checkstyle MethodBodyCommentsCheck (4 lines) // @todo #1:90min Implement comparison of XML documents with different indentations. From d2fb21b74687a17af99a8e2fc6e3252734ac8262 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 6 May 2024 15:53:21 +0300 Subject: [PATCH 6/8] bug(#247): disable unworking unit test --- src/test/java/com/jcabi/xml/XMLDocumentTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/jcabi/xml/XMLDocumentTest.java b/src/test/java/com/jcabi/xml/XMLDocumentTest.java index f54f62e..90a82a7 100644 --- a/src/test/java/com/jcabi/xml/XMLDocumentTest.java +++ b/src/test/java/com/jcabi/xml/XMLDocumentTest.java @@ -383,7 +383,7 @@ void comparesToAnotherDocument() { } @Test -// @Disabled + @Disabled void comparesDocumentsWithDifferentIndentations() { // @checkstyle MethodBodyCommentsCheck (4 lines) // @todo #1:90min Implement comparison of XML documents with different indentations. @@ -392,8 +392,8 @@ void comparesDocumentsWithDifferentIndentations() { // different indentations. Don't forget to remove the @Disabled annotation from this test. MatcherAssert.assertThat( "Different indentations should be ignored", - new XMLDocument("\n "), - Matchers.equalTo(new XMLDocument("\n ")) + new XMLDocument("\n \n"), + Matchers.equalTo(new XMLDocument("\n \n")) ); } From 46a5182e7cb28539ef1c5621aacc3f9b88508486 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 6 May 2024 17:09:56 +0300 Subject: [PATCH 7/8] bug(#247): identify the problem in the test --- src/main/java/com/jcabi/xml/XMLDocument.java | 62 ++++++++++++++++--- .../java/com/jcabi/xml/XMLDocumentTest.java | 11 +++- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/jcabi/xml/XMLDocument.java b/src/main/java/com/jcabi/xml/XMLDocument.java index e5b14d1..17a543a 100644 --- a/src/main/java/com/jcabi/xml/XMLDocument.java +++ b/src/main/java/com/jcabi/xml/XMLDocument.java @@ -115,6 +115,8 @@ public final class XMLDocument implements XML { */ private final transient Node cache; + private final transient TransformerFactory tfactory; + static { if (XMLDocument.DFACTORY.getClass().getName().contains("xerces")) { try { @@ -155,6 +157,35 @@ public XMLDocument(final String text) { ); } + + /** + * Public ctor, from XML as a text. + * + *

The object is created with a default implementation of + * {@link NamespaceContext}, which already defines a + * number of namespaces, for convenience, including: + * + *

 xhtml: http://www.w3.org/1999/xhtml
+     * xs: http://www.w3.org/2001/XMLSchema
+     * xsi: http://www.w3.org/2001/XMLSchema-instance
+     * xsl: http://www.w3.org/1999/XSL/Transform
+     * svg: http://www.w3.org/2000/svg
+ * + *

In future versions we will add more namespaces (submit a ticket if + * you need more of them defined here). + * + * @param text XML document body + * @param factory Transformer factory + */ + public XMLDocument(final String text, TransformerFactory factory) { + this( + new DomParser(XMLDocument.DFACTORY, text).document(), + new XPathContext(), + false, + factory + ); + } + /** * Public ctor, from XML as a text. * @@ -307,16 +338,29 @@ public XMLDocument(final InputStream stream) throws IOException { * @param ctx Namespace context * @param lfe Is it a leaf node? */ - private XMLDocument(final Node node, final XPathContext ctx, - final boolean lfe) { - this.context = ctx; - this.leaf = lfe; - this.cache = node; + private XMLDocument( + final Node node, + final XPathContext ctx, + final boolean lfe + ) { + this(node, ctx, lfe, XMLDocument.TFACTORY); + } + + public XMLDocument( + final Node cache, + final XPathContext context, + final boolean leaf, + final TransformerFactory tfactory + ) { + this.context = context; + this.leaf = leaf; + this.cache = cache; + this.tfactory = tfactory; } @Override public String toString() { - return XMLDocument.asString(this.cache); + return this.asString(this.cache); } @Override @@ -505,18 +549,18 @@ private T fetch(final String query, final Class type) * @param node The DOM node. * @return String representation */ - private static String asString(final Node node) { + private String asString(final Node node) { final StringWriter writer = new StringWriter(); final Transformer trans; try { synchronized (XMLDocument.class) { - trans = XMLDocument.TFACTORY.newTransformer(); + trans = this.tfactory.newTransformer(); } } catch (final TransformerConfigurationException ex) { throw new IllegalArgumentException( String.format( "Failed to create transformer by %s", - XMLDocument.TFACTORY.getClass().getName() + this.tfactory.getClass().getName() ), ex ); diff --git a/src/test/java/com/jcabi/xml/XMLDocumentTest.java b/src/test/java/com/jcabi/xml/XMLDocumentTest.java index 90a82a7..b3ffa0b 100644 --- a/src/test/java/com/jcabi/xml/XMLDocumentTest.java +++ b/src/test/java/com/jcabi/xml/XMLDocumentTest.java @@ -40,6 +40,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import javax.xml.transform.TransformerFactory; import org.apache.commons.lang3.StringUtils; import org.cactoos.io.ResourceOf; import org.cactoos.io.TeeInput; @@ -390,10 +391,16 @@ void comparesDocumentsWithDifferentIndentations() { // The current implementation of XMLDocument does not ignore different indentations // when comparing two XML documents. We need to implement a comparison that ignores // different indentations. Don't forget to remove the @Disabled annotation from this test. + final TransformerFactory factory = TransformerFactory.newInstance( + "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", + this.getClass().getClassLoader() + ); MatcherAssert.assertThat( "Different indentations should be ignored", - new XMLDocument("\n \n"), - Matchers.equalTo(new XMLDocument("\n \n")) + new XMLDocument("\n \n", factory), + Matchers.equalTo( + new XMLDocument("\n \n\n", factory) + ) ); } From 475c890121caf645a5111eec653d0c955e436c41 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 6 May 2024 17:14:56 +0300 Subject: [PATCH 8/8] bug(#247): fix all qulice suggestions --- src/main/java/com/jcabi/xml/XMLDocument.java | 14 ++++++++++++-- src/test/java/com/jcabi/xml/XMLDocumentTest.java | 6 +++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/jcabi/xml/XMLDocument.java b/src/main/java/com/jcabi/xml/XMLDocument.java index 17a543a..fa16c27 100644 --- a/src/main/java/com/jcabi/xml/XMLDocument.java +++ b/src/main/java/com/jcabi/xml/XMLDocument.java @@ -115,6 +115,9 @@ public final class XMLDocument implements XML { */ private final transient Node cache; + /** + * Transformer factory to use for {@link #toString()}. + */ private final transient TransformerFactory tfactory; static { @@ -157,7 +160,6 @@ public XMLDocument(final String text) { ); } - /** * Public ctor, from XML as a text. * @@ -177,7 +179,7 @@ public XMLDocument(final String text) { * @param text XML document body * @param factory Transformer factory */ - public XMLDocument(final String text, TransformerFactory factory) { + public XMLDocument(final String text, final TransformerFactory factory) { this( new DomParser(XMLDocument.DFACTORY, text).document(), new XPathContext(), @@ -346,6 +348,14 @@ private XMLDocument( this(node, ctx, lfe, XMLDocument.TFACTORY); } + /** + * Private ctor. + * @param cache The source + * @param context Namespace context + * @param leaf Is it a leaf node? + * @param tfactory Transformer factory + * @checkstyle ParameterNumberCheck (5 lines) + */ public XMLDocument( final Node cache, final XPathContext context, diff --git a/src/test/java/com/jcabi/xml/XMLDocumentTest.java b/src/test/java/com/jcabi/xml/XMLDocumentTest.java index b3ffa0b..c40e1c5 100644 --- a/src/test/java/com/jcabi/xml/XMLDocumentTest.java +++ b/src/test/java/com/jcabi/xml/XMLDocumentTest.java @@ -393,13 +393,13 @@ void comparesDocumentsWithDifferentIndentations() { // different indentations. Don't forget to remove the @Disabled annotation from this test. final TransformerFactory factory = TransformerFactory.newInstance( "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", - this.getClass().getClassLoader() + Thread.currentThread().getContextClassLoader() ); MatcherAssert.assertThat( "Different indentations should be ignored", - new XMLDocument("\n \n", factory), + new XMLDocument("\n \n", factory), Matchers.equalTo( - new XMLDocument("\n \n\n", factory) + new XMLDocument("\n \n\n", factory) ) ); }