From b3895901399bad38e992c2437a8b45bbfd8c2828 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Thu, 5 Dec 2024 14:26:04 +0300 Subject: [PATCH] #119 implemented --- .github/workflows/codecov.yml | 2 +- .github/workflows/mvn.yml | 2 +- .github/workflows/pdd.yml | 2 +- .github/workflows/xcop.yml | 2 +- .../com/jcabi/matchers/XhtmlMatchers.java | 21 ++++++++++---- .../com/jcabi/matchers/StringSourceTest.java | 2 +- .../com/jcabi/matchers/XhtmlMatchersTest.java | 29 ++++++++++--------- 7 files changed, 37 insertions(+), 23 deletions(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 9fd7c91..4096a76 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -27,7 +27,7 @@ # OF THE POSSIBILITY OF SUCH DAMAGE. --- name: codecov -on: +'on': push: branches: - master diff --git a/.github/workflows/mvn.yml b/.github/workflows/mvn.yml index 9b34e54..172157b 100644 --- a/.github/workflows/mvn.yml +++ b/.github/workflows/mvn.yml @@ -27,7 +27,7 @@ # OF THE POSSIBILITY OF SUCH DAMAGE. --- name: mvn -on: +'on': push: branches: - master diff --git a/.github/workflows/pdd.yml b/.github/workflows/pdd.yml index e12d477..826de3e 100644 --- a/.github/workflows/pdd.yml +++ b/.github/workflows/pdd.yml @@ -27,7 +27,7 @@ # OF THE POSSIBILITY OF SUCH DAMAGE. --- name: pdd -on: +'on': push: branches: - master diff --git a/.github/workflows/xcop.yml b/.github/workflows/xcop.yml index b964b7d..5cc95e0 100644 --- a/.github/workflows/xcop.yml +++ b/.github/workflows/xcop.yml @@ -27,7 +27,7 @@ # OF THE POSSIBILITY OF SUCH DAMAGE. --- name: xcop -on: +'on': push: branches: - master diff --git a/src/main/java/com/jcabi/matchers/XhtmlMatchers.java b/src/main/java/com/jcabi/matchers/XhtmlMatchers.java index ffd3a5d..e5b1dcd 100644 --- a/src/main/java/com/jcabi/matchers/XhtmlMatchers.java +++ b/src/main/java/com/jcabi/matchers/XhtmlMatchers.java @@ -34,8 +34,9 @@ import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; -import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.Scanner; import javax.xml.namespace.NamespaceContext; import javax.xml.transform.Source; @@ -164,12 +165,22 @@ public static Matcher hasXPath(final String query, * @return Matcher suitable for JUnit/Hamcrest matching */ public static Matcher hasXPaths(final String...xpaths) { - final Collection> list = - new ArrayList<>(xpaths.length); + return XhtmlMatchers.hasXPaths(Arrays.asList(xpaths)); + } + + /** + * Matches content against list of XPaths. + * @param xpaths The query + * @param Type of XML content provided + * @return Matcher suitable for JUnit/Hamcrest matching + * @since 1.8.0 + */ + public static Matcher hasXPaths(final Iterable xpaths) { + final Collection> list = new LinkedList<>(); for (final String xpath : xpaths) { - list.add(XhtmlMatchers.hasXPath(xpath)); + list.add(XhtmlMatchers.hasXPath(xpath)); } - return new AllOfThatPrintsOnlyWrongMatchers(list); + return new AllOfThatPrintsOnlyWrongMatchers<>(list); } /** diff --git a/src/test/java/com/jcabi/matchers/StringSourceTest.java b/src/test/java/com/jcabi/matchers/StringSourceTest.java index a779e22..3707c6c 100644 --- a/src/test/java/com/jcabi/matchers/StringSourceTest.java +++ b/src/test/java/com/jcabi/matchers/StringSourceTest.java @@ -45,7 +45,7 @@ final class StringSourceTest { @Test - void formatsIncomingXmlDocument() throws Exception { + void formatsIncomingXmlDocument() { final String xml = "\u0443\u0440\u0430!"; MatcherAssert.assertThat( new StringSource(xml).toString(), diff --git a/src/test/java/com/jcabi/matchers/XhtmlMatchersTest.java b/src/test/java/com/jcabi/matchers/XhtmlMatchersTest.java index f208615..a5b3be1 100644 --- a/src/test/java/com/jcabi/matchers/XhtmlMatchersTest.java +++ b/src/test/java/com/jcabi/matchers/XhtmlMatchersTest.java @@ -31,6 +31,7 @@ import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; +import java.util.Arrays; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlType; @@ -51,7 +52,7 @@ final class XhtmlMatchersTest { @Test - void matchesWithCustomNamespace() throws Exception { + void matchesWithCustomNamespace() { MatcherAssert.assertThat( "abc.txt", XhtmlMatchers.hasXPath("/ns1:a/ns1:file[.='abc.txt']", "foo") @@ -59,7 +60,7 @@ void matchesWithCustomNamespace() throws Exception { } @Test - void doesntMatch() throws Exception { + void doesntMatch() { MatcherAssert.assertThat( "", Matchers.not(XhtmlMatchers.hasXPath("/foo")) @@ -67,7 +68,7 @@ void doesntMatch() throws Exception { } @Test - void matchesPlainString() throws Exception { + void matchesPlainString() { MatcherAssert.assertThat( "abc.txt", XhtmlMatchers.hasXPath("/ns1:b/ns1:file[.='abc.txt']", "bar") @@ -76,7 +77,7 @@ void matchesPlainString() throws Exception { } @Test - void matchesInputStreamAndReader() throws Exception { + void matchesInputStreamAndReader() { MatcherAssert.assertThat( IOUtils.toInputStream( "foo.txt", @@ -105,7 +106,7 @@ void matchesAfterJaxbConverter() throws Exception { } @Test - void matchesWithGenericType() throws Exception { + void matchesWithGenericType() { final Foo foo = new Foo(); MatcherAssert.assertThat( foo, @@ -117,7 +118,7 @@ void matchesWithGenericType() throws Exception { } @Test - void convertsTextToXml() throws Exception { + void convertsTextToXml() { MatcherAssert.assertThat( StringUtils.join( "", @@ -128,7 +129,7 @@ void convertsTextToXml() throws Exception { } @Test - void convertsTextToXmlWithUnicode() throws Exception { + void convertsTextToXmlWithUnicode() { MatcherAssert.assertThat( "\u8514 ›", XhtmlMatchers.hasXPath("/a") @@ -136,7 +137,7 @@ void convertsTextToXmlWithUnicode() throws Exception { } @Test - void preservesProcessingInstructions() throws Exception { + void preservesProcessingInstructions() { MatcherAssert.assertThat( "", XhtmlMatchers.hasXPath( @@ -146,7 +147,7 @@ void preservesProcessingInstructions() throws Exception { } @Test - void processesDocumentsWithDoctype() throws Exception { + void processesDocumentsWithDoctype() { final String text = // @checkstyle StringLiteralsConcatenation (6 lines) "" @@ -184,7 +185,7 @@ void convertsNodeToXml() throws Exception { } @Test - void hasXPaths() throws Exception { + void hasXPaths() { MatcherAssert.assertThat( "def.txtghi.txt", XhtmlMatchers.hasXPaths( @@ -200,9 +201,11 @@ void hasXPathsPrintsOnlyWrongXPaths() { MatcherAssert.assertThat( "some.txtgni.txt", XhtmlMatchers.hasXPaths( - "/b/file[.='some.txt']", - "/b/file[.='gnx.txt']", - "/b/file[.='gni.txt']" + Arrays.asList( + "/b/file[.='some.txt']", + "/b/file[.='gnx.txt']", + "/b/file[.='gni.txt']" + ) ) ); } catch (final AssertionError error) {