Skip to content

Commit 2eb4069

Browse files
committed
Moved factory methods into MatchXml
1 parent 24e0330 commit 2eb4069

File tree

4 files changed

+21
-74
lines changed

4 files changed

+21
-74
lines changed

hamcrest-library/src/main/java/org/hamcrest/Matchers.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.hamcrest;
22

33
import org.hamcrest.text.MatchStrings;
4+
import org.hamcrest.xml.MatchXml;
45

56
@SuppressWarnings("UnusedDeclaration")
67
public class Matchers {
@@ -1454,7 +1455,7 @@ public static <T> org.hamcrest.Matcher<T> samePropertyValuesAs(T expectedBean) {
14541455
* @param valueMatcher matcher for the value at the specified xpath
14551456
*/
14561457
public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, org.hamcrest.Matcher<java.lang.String> valueMatcher) {
1457-
return org.hamcrest.xml.HasXPath.hasXPath(xPath, valueMatcher);
1458+
return MatchXml.hasXPath(xPath, valueMatcher);
14581459
}
14591460

14601461
/**
@@ -1469,7 +1470,7 @@ public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String x
14691470
* @param valueMatcher matcher for the value at the specified xpath
14701471
*/
14711472
public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext, org.hamcrest.Matcher<java.lang.String> valueMatcher) {
1472-
return org.hamcrest.xml.HasXPath.hasXPath(xPath, namespaceContext, valueMatcher);
1473+
return MatchXml.hasXPath(xPath, namespaceContext, valueMatcher);
14731474
}
14741475

14751476
/**
@@ -1481,7 +1482,7 @@ public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String x
14811482
* @param xPath the target xpath
14821483
*/
14831484
public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath) {
1484-
return org.hamcrest.xml.HasXPath.hasXPath(xPath);
1485+
return MatchXml.hasXPath(xPath);
14851486
}
14861487

14871488
/**
@@ -1494,7 +1495,7 @@ public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String x
14941495
* @param namespaceContext the namespace for matching nodes
14951496
*/
14961497
public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext) {
1497-
return org.hamcrest.xml.HasXPath.hasXPath(xPath, namespaceContext);
1498+
return MatchXml.hasXPath(xPath, namespaceContext);
14981499
}
14991500

15001501
}

hamcrest-library/src/main/java/org/hamcrest/xml/HasXPath.java

Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
import javax.xml.namespace.NamespaceContext;
1111
import javax.xml.namespace.QName;
12-
import javax.xml.xpath.*;
12+
import javax.xml.xpath.XPath;
13+
import javax.xml.xpath.XPathExpression;
14+
import javax.xml.xpath.XPathExpressionException;
15+
import javax.xml.xpath.XPathFactory;
1316

1417
import static javax.xml.xpath.XPathConstants.STRING;
1518
import static org.hamcrest.Condition.matched;
@@ -23,7 +26,7 @@
2326
*/
2427
public class HasXPath extends TypeSafeDiagnosingMatcher<Node> {
2528
public static final NamespaceContext NO_NAMESPACE_CONTEXT = null;
26-
private static final IsAnything<String> WITH_ANY_CONTENT = new IsAnything<>("");
29+
public static final IsAnything<String> WITH_ANY_CONTENT = new IsAnything<>("");
2730
private static final Condition.Step<Object,String> NODE_EXISTS = nodeExists();
2831
private final Matcher<String> valueMatcher;
2932
private final XPathExpression compiledXPath;
@@ -49,7 +52,7 @@ public HasXPath(String xPathExpression, NamespaceContext namespaceContext, Match
4952
this(xPathExpression, namespaceContext, valueMatcher, STRING);
5053
}
5154

52-
private HasXPath(String xPathExpression, NamespaceContext namespaceContext, Matcher<String> valueMatcher, QName mode) {
55+
public HasXPath(String xPathExpression, NamespaceContext namespaceContext, Matcher<String> valueMatcher, QName mode) {
5356
this.compiledXPath = compiledXPath(xPathExpression, namespaceContext);
5457
this.xpathString = xPathExpression;
5558
this.valueMatcher = valueMatcher;
@@ -104,66 +107,4 @@ private static XPathExpression compiledXPath(String xPathExpression, NamespaceCo
104107
throw new IllegalArgumentException("Invalid XPath : " + xPathExpression, e);
105108
}
106109
}
107-
108-
109-
/**
110-
* Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node has a value at the
111-
* specified <code>xPath</code> that satisfies the specified <code>valueMatcher</code>.
112-
* For example:
113-
* <pre>assertThat(xml, hasXPath("/root/something[2]/cheese", equalTo("Cheddar")))</pre>
114-
*
115-
* @param xPath
116-
* the target xpath
117-
* @param valueMatcher
118-
* matcher for the value at the specified xpath
119-
*/
120-
public static Matcher<Node> hasXPath(String xPath, Matcher<String> valueMatcher) {
121-
return hasXPath(xPath, NO_NAMESPACE_CONTEXT, valueMatcher);
122-
}
123-
124-
/**
125-
* Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node has a value at the
126-
* specified <code>xPath</code>, within the specified <code>namespaceContext</code>, that satisfies
127-
* the specified <code>valueMatcher</code>.
128-
* For example:
129-
* <pre>assertThat(xml, hasXPath("/root/something[2]/cheese", myNs, equalTo("Cheddar")))</pre>
130-
*
131-
* @param xPath
132-
* the target xpath
133-
* @param namespaceContext
134-
* the namespace for matching nodes
135-
* @param valueMatcher
136-
* matcher for the value at the specified xpath
137-
*/
138-
public static Matcher<Node> hasXPath(String xPath, NamespaceContext namespaceContext, Matcher<String> valueMatcher) {
139-
return new HasXPath(xPath, namespaceContext, valueMatcher, STRING);
140-
}
141-
142-
/**
143-
* Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node contains a node
144-
* at the specified <code>xPath</code>, with any content.
145-
* For example:
146-
* <pre>assertThat(xml, hasXPath("/root/something[2]/cheese"))</pre>
147-
*
148-
* @param xPath
149-
* the target xpath
150-
*/
151-
public static Matcher<Node> hasXPath(String xPath) {
152-
return hasXPath(xPath, NO_NAMESPACE_CONTEXT);
153-
}
154-
155-
/**
156-
* Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node contains a node
157-
* at the specified <code>xPath</code> within the specified namespace context, with any content.
158-
* For example:
159-
* <pre>assertThat(xml, hasXPath("/root/something[2]/cheese", myNs))</pre>
160-
*
161-
* @param xPath
162-
* the target xpath
163-
* @param namespaceContext
164-
* the namespace for matching nodes
165-
*/
166-
public static Matcher<Node> hasXPath(String xPath, NamespaceContext namespaceContext) {
167-
return new HasXPath(xPath, namespaceContext, WITH_ANY_CONTENT, XPathConstants.NODE);
168-
}
169110
}

hamcrest-library/src/main/java/org/hamcrest/xml/MatchXml.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package org.hamcrest.xml;
22

3+
import javax.xml.xpath.XPathConstants;
4+
5+
import static javax.xml.xpath.XPathConstants.STRING;
6+
import static org.hamcrest.xml.HasXPath.NO_NAMESPACE_CONTEXT;
7+
38
public class MatchXml {
49
/**
510
* Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node has a value at the
@@ -11,7 +16,7 @@ public class MatchXml {
1116
* @param valueMatcher matcher for the value at the specified xpath
1217
*/
1318
public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, org.hamcrest.Matcher<java.lang.String> valueMatcher) {
14-
return org.hamcrest.xml.HasXPath.hasXPath(xPath, valueMatcher);
19+
return new HasXPath(xPath, NO_NAMESPACE_CONTEXT, valueMatcher, STRING);
1520
}
1621

1722
/**
@@ -26,7 +31,7 @@ public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String x
2631
* @param valueMatcher matcher for the value at the specified xpath
2732
*/
2833
public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext, org.hamcrest.Matcher<java.lang.String> valueMatcher) {
29-
return org.hamcrest.xml.HasXPath.hasXPath(xPath, namespaceContext, valueMatcher);
34+
return new HasXPath(xPath, namespaceContext, valueMatcher, STRING);
3035
}
3136

3237
/**
@@ -38,7 +43,7 @@ public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String x
3843
* @param xPath the target xpath
3944
*/
4045
public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath) {
41-
return org.hamcrest.xml.HasXPath.hasXPath(xPath);
46+
return new HasXPath(xPath, NO_NAMESPACE_CONTEXT, HasXPath.WITH_ANY_CONTENT, XPathConstants.NODE);
4247
}
4348

4449
/**
@@ -51,7 +56,7 @@ public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String x
5156
* @param namespaceContext the namespace for matching nodes
5257
*/
5358
public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext) {
54-
return org.hamcrest.xml.HasXPath.hasXPath(xPath, namespaceContext);
59+
return new HasXPath(xPath, namespaceContext, HasXPath.WITH_ANY_CONTENT, XPathConstants.NODE);
5560
}
5661

5762
}

hamcrest-library/src/test/java/org/hamcrest/xml/HasXPathTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
import java.util.Iterator;
1414

1515
import static org.hamcrest.AbstractMatcherTest.*;
16+
import static org.hamcrest.Matchers.hasXPath;
1617
import static org.hamcrest.core.IsEqual.equalTo;
1718
import static org.hamcrest.core.IsNot.not;
1819
import static org.hamcrest.core.StringContains.containsString;
19-
import static org.hamcrest.xml.HasXPath.hasXPath;
2020
import static org.junit.Assert.fail;
2121

2222
/**

0 commit comments

Comments
 (0)