View Javadoc

1   /*
2    * Copyright 2005-2011 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Helper class for dealing with XPath expectations.
37   *
38   * @author Lukas Krecan
39   * @author Arjen Poutsma
40   * @since 2.0
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       * Creates a new instance of the [email protected] XPathExpectationsSupport} with the given XPath expression.
52       *
53       * @param expression the XPath expression
54       */
55      public XPathExpectationsHelper(String expression) {
56          this(expression, null);
57      }
58      /**
59       * Creates a new instance of the [email protected] XPathExpectationsSupport} with the given XPath expression and namespaces.
60       *
61       * @param expression the XPath expression
62       * @param namespaces the namespaces, can be empty or [email protected] null}
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 }