Skip to content

Commit

Permalink
#119 implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 5, 2024
1 parent d22a7ee commit b389590
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# OF THE POSSIBILITY OF SUCH DAMAGE.
---
name: codecov
on:
'on':
push:
branches:
- master
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mvn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# OF THE POSSIBILITY OF SUCH DAMAGE.
---
name: mvn
on:
'on':
push:
branches:
- master
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pdd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# OF THE POSSIBILITY OF SUCH DAMAGE.
---
name: pdd
on:
'on':
push:
branches:
- master
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/xcop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# OF THE POSSIBILITY OF SUCH DAMAGE.
---
name: xcop
on:
'on':
push:
branches:
- master
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/com/jcabi/matchers/XhtmlMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -164,12 +165,22 @@ public static <T> Matcher<T> hasXPath(final String query,
* @return Matcher suitable for JUnit/Hamcrest matching
*/
public static <T> Matcher<T> hasXPaths(final String...xpaths) {
final Collection<Matcher<? super T>> list =
new ArrayList<>(xpaths.length);
return XhtmlMatchers.hasXPaths(Arrays.asList(xpaths));
}

/**
* Matches content against list of XPaths.
* @param xpaths The query
* @param <T> Type of XML content provided
* @return Matcher suitable for JUnit/Hamcrest matching
* @since 1.8.0
*/
public static <T> Matcher<T> hasXPaths(final Iterable<String> xpaths) {
final Collection<Matcher<? super T>> list = new LinkedList<>();
for (final String xpath : xpaths) {
list.add(XhtmlMatchers.<T>hasXPath(xpath));
list.add(XhtmlMatchers.hasXPath(xpath));
}
return new AllOfThatPrintsOnlyWrongMatchers<T>(list);
return new AllOfThatPrintsOnlyWrongMatchers<>(list);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/jcabi/matchers/StringSourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
final class StringSourceTest {

@Test
void formatsIncomingXmlDocument() throws Exception {
void formatsIncomingXmlDocument() {
final String xml = "<a><b>\u0443\u0440\u0430!</b></a>";
MatcherAssert.assertThat(
new StringSource(xml).toString(),
Expand Down
29 changes: 16 additions & 13 deletions src/test/java/com/jcabi/matchers/XhtmlMatchersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,23 +52,23 @@
final class XhtmlMatchersTest {

@Test
void matchesWithCustomNamespace() throws Exception {
void matchesWithCustomNamespace() {
MatcherAssert.assertThat(
"<a xmlns='foo'><file>abc.txt</file></a>",
XhtmlMatchers.hasXPath("/ns1:a/ns1:file[.='abc.txt']", "foo")
);
}

@Test
void doesntMatch() throws Exception {
void doesntMatch() {
MatcherAssert.assertThat(
"<a/>",
Matchers.not(XhtmlMatchers.hasXPath("/foo"))
);
}

@Test
void matchesPlainString() throws Exception {
void matchesPlainString() {
MatcherAssert.assertThat(
"<b xmlns='bar'><file>abc.txt</file></b>",
XhtmlMatchers.hasXPath("/ns1:b/ns1:file[.='abc.txt']", "bar")
Expand All @@ -76,7 +77,7 @@ void matchesPlainString() throws Exception {
}

@Test
void matchesInputStreamAndReader() throws Exception {
void matchesInputStreamAndReader() {
MatcherAssert.assertThat(
IOUtils.toInputStream(
"<b><file>foo.txt</file></b>",
Expand Down Expand Up @@ -105,7 +106,7 @@ void matchesAfterJaxbConverter() throws Exception {
}

@Test
void matchesWithGenericType() throws Exception {
void matchesWithGenericType() {
final Foo foo = new Foo();
MatcherAssert.assertThat(
foo,
Expand All @@ -117,7 +118,7 @@ void matchesWithGenericType() throws Exception {
}

@Test
void convertsTextToXml() throws Exception {
void convertsTextToXml() {
MatcherAssert.assertThat(
StringUtils.join(
"<html xmlns='http://www.w3.org/1999/xhtml'><body>",
Expand All @@ -128,15 +129,15 @@ void convertsTextToXml() throws Exception {
}

@Test
void convertsTextToXmlWithUnicode() throws Exception {
void convertsTextToXmlWithUnicode() {
MatcherAssert.assertThat(
"<a>\u8514 &#8250;</a>",
XhtmlMatchers.hasXPath("/a")
);
}

@Test
void preservesProcessingInstructions() throws Exception {
void preservesProcessingInstructions() {
MatcherAssert.assertThat(
"<?xml version='1.0'?><?pi name='foo'?><a/>",
XhtmlMatchers.hasXPath(
Expand All @@ -146,7 +147,7 @@ void preservesProcessingInstructions() throws Exception {
}

@Test
void processesDocumentsWithDoctype() throws Exception {
void processesDocumentsWithDoctype() {
final String text =
// @checkstyle StringLiteralsConcatenation (6 lines)
"<?xml version='1.0'?>"
Expand Down Expand Up @@ -184,7 +185,7 @@ void convertsNodeToXml() throws Exception {
}

@Test
void hasXPaths() throws Exception {
void hasXPaths() {
MatcherAssert.assertThat(
"<b><file>def.txt</file><file>ghi.txt</file></b>",
XhtmlMatchers.hasXPaths(
Expand All @@ -200,9 +201,11 @@ void hasXPathsPrintsOnlyWrongXPaths() {
MatcherAssert.assertThat(
"<b><file>some.txt</file><file>gni.txt</file></b>",
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) {
Expand Down

0 comments on commit b389590

Please sign in to comment.