1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.test.support.matcher;
18
19 import java.io.IOException;
20 import java.util.Map;
21 import javax.xml.transform.TransformerException;
22 import javax.xml.transform.dom.DOMResult;
23
24 import org.springframework.util.Assert;
25 import org.springframework.ws.WebServiceMessage;
26 import org.springframework.xml.transform.TransformerHelper;
27 import org.springframework.xml.xpath.XPathExpression;
28 import org.springframework.xml.xpath.XPathExpressionFactory;
29
30 import org.w3c.dom.Node;
31
32 import static org.springframework.ws.test.support.AssertionErrors.assertEquals;
33 import static org.springframework.ws.test.support.AssertionErrors.fail;
34
35
36
37
38
39
40
41
42 public class XPathExpectationsHelper {
43
44 private final XPathExpression expression;
45
46 private final String expressionString;
47
48 private final TransformerHelper transformerHelper = new TransformerHelper();
49
50
51
52
53
54
55 public XPathExpectationsHelper(String expression) {
56 this(expression, null);
57 }
58
59
60
61
62
63
64 public XPathExpectationsHelper(String expression, Map<String, String> namespaces) {
65 Assert.hasLength(expression, "'expression' must not be empty");
66 this.expression = XPathExpressionFactory.createXPathExpression(expression, namespaces);
67 this.expressionString = expression;
68 }
69
70 public WebServiceMessageMatcher exists() {
71 return new WebServiceMessageMatcher() {
72 public void match(WebServiceMessage message) throws IOException, AssertionError {
73 Node payload = transformToNode(message);
74 Node result = expression.evaluateAsNode(payload);
75 if (result == null) {
76 fail("No match for \"" + expressionString + "\" found", "Payload", message.getPayloadSource());
77 }
78 }
79 };
80 }
81
82 public WebServiceMessageMatcher doesNotExist() {
83 return new WebServiceMessageMatcher() {
84 public void match(WebServiceMessage message) throws IOException, AssertionError {
85 Node payload = transformToNode(message);
86 Node result = expression.evaluateAsNode(payload);
87 if (result != null) {
88 fail("Match for \"" + expressionString + "\" found", "Payload", message.getPayloadSource());
89 }
90 }
91 };
92 }
93
94 public WebServiceMessageMatcher evaluatesTo(final boolean expectedValue) {
95 return new WebServiceMessageMatcher() {
96 public void match(WebServiceMessage message) throws IOException, AssertionError {
97 Node payload = transformToNode(message);
98 boolean result = expression.evaluateAsBoolean(payload);
99 assertEquals("Evaluation of XPath expression \"" + expressionString + "\" failed.", expectedValue,
100 result, "Payload", message.getPayloadSource());
101
102 }
103 };
104 }
105
106 public WebServiceMessageMatcher evaluatesTo(int expectedValue) {
107 return evaluatesTo((double) expectedValue);
108 }
109
110 public WebServiceMessageMatcher evaluatesTo(final double expectedValue) {
111 return new WebServiceMessageMatcher() {
112 public void match(WebServiceMessage message) throws IOException, AssertionError {
113 Node payload = transformToNode(message);
114 double result = expression.evaluateAsNumber(payload);
115 assertEquals("Evaluation of XPath expression \"" + expressionString + "\" failed.", expectedValue,
116 result, "Payload", message.getPayloadSource());
117
118 }
119 };
120 }
121
122 public WebServiceMessageMatcher evaluatesTo(final String expectedValue) {
123 Assert.notNull(expectedValue, "'expectedValue' must not be null");
124 return new WebServiceMessageMatcher() {
125 public void match(WebServiceMessage message) throws IOException, AssertionError {
126 Node payload = transformToNode(message);
127 String result = expression.evaluateAsString(payload);
128 assertEquals("Evaluation of XPath expression \"" + expressionString + "\" failed.", expectedValue,
129 result, "Payload", message.getPayloadSource());
130 }
131 };
132 }
133
134 private Node transformToNode(WebServiceMessage request) {
135 DOMResult domResult = new DOMResult();
136 try {
137 transformerHelper.transform(request.getPayloadSource(), domResult);
138 return domResult.getNode();
139 }
140 catch (TransformerException ex) {
141 fail("Could not transform request payload: " + ex.getMessage());
142 return null;
143 }
144 }
145
146
147 }