99
1010import javax .xml .namespace .NamespaceContext ;
1111import 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
1417import static javax .xml .xpath .XPathConstants .STRING ;
1518import static org .hamcrest .Condition .matched ;
2326 */
2427public 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}
0 commit comments