6
6
import org .hamcrest .TypeSafeDiagnosingMatcher ;
7
7
import org .hamcrest .core .IsAnything ;
8
8
import org .w3c .dom .Node ;
9
+ import org .w3c .dom .NodeList ;
9
10
10
11
import javax .xml .namespace .NamespaceContext ;
11
12
import javax .xml .namespace .QName ;
12
13
import javax .xml .xpath .*;
13
14
15
+ import java .util .ArrayList ;
16
+ import java .util .List ;
17
+
18
+ import static javax .xml .xpath .XPathConstants .NODESET ;
14
19
import static javax .xml .xpath .XPathConstants .STRING ;
15
- import static org .hamcrest .Condition .matched ;
16
- import static org .hamcrest .Condition .notMatched ;
20
+ import static org .hamcrest .Condition .*;
17
21
18
22
/**
19
23
* Applies a Matcher to a given XML Node in an existing XML Node tree, specified by an XPath expression.
@@ -58,9 +62,22 @@ private HasXPath(String xPathExpression, NamespaceContext namespaceContext, Matc
58
62
59
63
@ Override
60
64
public boolean matchesSafely (Node item , Description mismatch ) {
61
- return evaluated (item , mismatch )
62
- .and (NODE_EXISTS )
63
- .matching (valueMatcher );
65
+ if (this .evaluationMode == NODESET )
66
+ {
67
+ List <Condition <Object >> match_list = evaluatedList (item , mismatch );
68
+
69
+ for (Condition <Object > match : match_list ) {
70
+ if (match .and (NODE_EXISTS ).matching (valueMatcher )) {
71
+ return true ;
72
+ }
73
+ }
74
+ return false ;
75
+ }
76
+ else {
77
+ return evaluated (item , mismatch )
78
+ .and (NODE_EXISTS )
79
+ .matching (valueMatcher );
80
+ }
64
81
}
65
82
66
83
@ Override
@@ -80,6 +97,23 @@ private Condition<Object> evaluated(Node item, Description mismatch) {
80
97
return notMatched ();
81
98
}
82
99
100
+ private List <Condition <Object >> evaluatedList (Node item , Description mismatch ) {
101
+ List <Condition <Object >> match_list = new ArrayList <>();
102
+ try {
103
+ NodeList list = (NodeList ) compiledXPath .evaluate (item , evaluationMode );
104
+ Object obj = compiledXPath .evaluate (item , STRING );
105
+ for (int i = 0 ; i < list .getLength (); i ++) {
106
+ Node node = list .item (i );
107
+ match_list .add ((matched ((Object )node .getTextContent (), mismatch )));
108
+ }
109
+ return match_list ;
110
+ } catch (XPathExpressionException e ) {
111
+ mismatch .appendText (e .getMessage ());
112
+ }
113
+ match_list .add (notMatched ());
114
+ return match_list ;
115
+ }
116
+
83
117
private static Condition .Step <Object , String > nodeExists () {
84
118
return new Condition .Step <Object , String >() {
85
119
@ Override
@@ -136,7 +170,7 @@ public static Matcher<Node> hasXPath(String xPath, Matcher<String> valueMatcher)
136
170
* matcher for the value at the specified xpath
137
171
*/
138
172
public static Matcher <Node > hasXPath (String xPath , NamespaceContext namespaceContext , Matcher <String > valueMatcher ) {
139
- return new HasXPath (xPath , namespaceContext , valueMatcher , STRING );
173
+ return new HasXPath (xPath , namespaceContext , valueMatcher , NODESET );
140
174
}
141
175
142
176
/**
0 commit comments