Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 24, 2024
2 parents 851862b + 475c890 commit 2107ecf
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 9 deletions.
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,26 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<source>8</source>
</configuration>
</plugin>
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<configuration>
<!--
@todo #1:90min Enable Groovy Integration Test
The Groovy integration test is disabled because it fails.
The problem is that the XMLDocument#equals method is
indentation-aware, but it shouldn't be.
We need to fix it and enable the test.
-->
<pomExcludes>groovy/pom.xml</pomExcludes>
</configuration>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<profiles>
Expand Down
12 changes: 12 additions & 0 deletions src/it/groovy/README.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions src/it/groovy/first.xmir
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<program>
<indentation/>
</program>
1 change: 1 addition & 0 deletions src/it/groovy/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = clean
48 changes: 48 additions & 0 deletions src/it/groovy/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jcabi</groupId>
<artifactId>jcabi</artifactId>
<version>1.39.0</version>
</parent>
<groupId>com.jcabi.xml</groupId>
<artifactId>groovy</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>
</project>
4 changes: 4 additions & 0 deletions src/it/groovy/same.xmir
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<program>
<indentation/>
</program>
38 changes: 38 additions & 0 deletions src/it/groovy/verify.groovy
Original file line number Diff line number Diff line change
@@ -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.equals(same)

assert log.text.contains("BUILD SUCCESS")
72 changes: 63 additions & 9 deletions src/main/java/com/jcabi/xml/XMLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public final class XMLDocument implements XML {
*/
private final transient Node cache;

/**
* Transformer factory to use for {@link #toString()}.
*/
private final transient TransformerFactory tfactory;

static {
if (XMLDocument.DFACTORY.getClass().getName().contains("xerces")) {
try {
Expand Down Expand Up @@ -155,6 +160,34 @@ public XMLDocument(final String text) {
);
}

/**
* Public ctor, from XML as a text.
*
* <p>The object is created with a default implementation of
* {@link NamespaceContext}, which already defines a
* number of namespaces, for convenience, including:
*
* <pre> 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</pre>
*
* <p>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, final TransformerFactory factory) {
this(
new DomParser(XMLDocument.DFACTORY, text).document(),
new XPathContext(),
false,
factory
);
}

/**
* Public ctor, from XML as a text.
*
Expand Down Expand Up @@ -307,16 +340,37 @@ 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);
}

/**
* 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,
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
Expand Down Expand Up @@ -505,18 +559,18 @@ private <T> T fetch(final String query, final Class<T> 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
);
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/jcabi/xml/XMLDocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,6 +49,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;
Expand Down Expand Up @@ -381,6 +383,27 @@ 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
// 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",
Thread.currentThread().getContextClassLoader()
);
MatcherAssert.assertThat(
"Different indentations should be ignored",
new XMLDocument("<program>\n <indentation/>\n</program>", factory),
Matchers.equalTo(
new XMLDocument("<program>\n <indentation/>\n</program>\n", factory)
)
);
}

@Test
void preservesXmlNamespaces() {
final String xml = "<a xmlns='http://www.w3.org/1999/xhtml'><b/></a>";
Expand Down

2 comments on commit 2107ecf

@0pdd
Copy link

@0pdd 0pdd commented on 2107ecf May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1-ee5e6b2b discovered in pom.xml) and submitted as #248. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 2107ecf May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1-c7571585 discovered in src/test/java/com/jcabi/xml/XMLDocumentTest.java) and submitted as #249. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.