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.Collections;
21  import java.util.Map;
22  
23  import org.springframework.ws.WebServiceMessage;
24  import org.springframework.xml.transform.StringSource;
25  
26  import org.junit.Test;
27  
28  import static org.easymock.EasyMock.*;
29  import static org.junit.Assert.assertNotNull;
30  
31  public class XPathExpectationsHelperTest {
32  
33      @Test
34      public void existsMatch() throws IOException, AssertionError {
35          XPathExpectationsHelper helper = new XPathExpectationsHelper("//b");
36          WebServiceMessageMatcher matcher = helper.exists();
37          assertNotNull(matcher);
38  
39          WebServiceMessage message = createMock(WebServiceMessage.class);
40          expect(message.getPayloadSource()).andReturn(new StringSource("<a><b/></a>"));
41  
42          replay(message);
43  
44          matcher.match(message);
45  
46          verify(message);
47      }
48  
49      @Test(expected = AssertionError.class)
50      public void existsNonMatch() throws IOException, AssertionError {
51          XPathExpectationsHelper helper = new XPathExpectationsHelper("//c");
52          WebServiceMessageMatcher matcher = helper.exists();
53          assertNotNull(matcher);
54  
55          WebServiceMessage message = createMock(WebServiceMessage.class);
56          expect(message.getPayloadSource()).andReturn(new StringSource("<a><b/></a>")).times(2);
57  
58          replay(message);
59  
60          matcher.match(message);
61      }
62  
63      @Test
64      public void doesNotExistMatch() throws IOException, AssertionError {
65          XPathExpectationsHelper helper = new XPathExpectationsHelper("//c");
66          WebServiceMessageMatcher matcher = helper.doesNotExist();
67          assertNotNull(matcher);
68  
69          WebServiceMessage message = createMock(WebServiceMessage.class);
70          expect(message.getPayloadSource()).andReturn(new StringSource("<a><b/></a>"));
71  
72          replay(message);
73  
74          matcher.match(message);
75  
76          verify(message);
77      }
78  
79      @Test(expected = AssertionError.class)
80      public void doesNotExistNonMatch() throws IOException, AssertionError {
81          XPathExpectationsHelper helper = new XPathExpectationsHelper("//a");
82          WebServiceMessageMatcher matcher = helper.doesNotExist();
83          assertNotNull(matcher);
84  
85          WebServiceMessage message = createMock(WebServiceMessage.class);
86          expect(message.getPayloadSource()).andReturn(new StringSource("<a><b/></a>")).times(2);
87  
88          replay(message);
89  
90          matcher.match(message);
91      }
92  
93      @Test
94      public void evaluatesToTrueMatch() throws IOException, AssertionError {
95          XPathExpectationsHelper helper = new XPathExpectationsHelper("//b=1");
96          WebServiceMessageMatcher matcher = helper.evaluatesTo(true);
97          assertNotNull(matcher);
98  
99          WebServiceMessage message = createMock(WebServiceMessage.class);
100         expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>")).times(2);
101 
102         replay(message);
103 
104         matcher.match(message);
105 
106         verify(message);
107     }
108 
109     @Test(expected = AssertionError.class)
110     public void evaluatesToTrueNonMatch() throws IOException, AssertionError {
111         XPathExpectationsHelper helper = new XPathExpectationsHelper("//b=2");
112         WebServiceMessageMatcher matcher = helper.evaluatesTo(true);
113         assertNotNull(matcher);
114 
115         WebServiceMessage message = createMock(WebServiceMessage.class);
116         expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>")).times(2);
117 
118         replay(message);
119 
120         matcher.match(message);
121     }
122 
123     @Test
124     public void evaluatesToFalseMatch() throws IOException, AssertionError {
125         XPathExpectationsHelper helper = new XPathExpectationsHelper("//b!=1");
126         WebServiceMessageMatcher matcher = helper.evaluatesTo(false);
127         assertNotNull(matcher);
128 
129         WebServiceMessage message = createMock(WebServiceMessage.class);
130         expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>")).times(2);
131 
132         replay(message);
133 
134         matcher.match(message);
135 
136         verify(message);
137     }
138 
139     @Test(expected = AssertionError.class)
140     public void evaluatesToFalseNonMatch() throws IOException, AssertionError {
141         XPathExpectationsHelper helper = new XPathExpectationsHelper("//b!=2");
142         WebServiceMessageMatcher matcher = helper.evaluatesTo(false);
143         assertNotNull(matcher);
144 
145         WebServiceMessage message = createMock(WebServiceMessage.class);
146         expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>")).times(2);
147 
148         replay(message);
149 
150         matcher.match(message);
151     }
152 
153     @Test
154     public void evaluatesToIntegerMatch() throws IOException, AssertionError {
155         XPathExpectationsHelper helper = new XPathExpectationsHelper("//b");
156         WebServiceMessageMatcher matcher = helper.evaluatesTo(1);
157         assertNotNull(matcher);
158 
159         WebServiceMessage message = createMock(WebServiceMessage.class);
160         expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>")).times(2);
161 
162         replay(message);
163 
164         matcher.match(message);
165 
166         verify(message);
167     }
168 
169     @Test(expected = AssertionError.class)
170     public void evaluatesToIntegerNonMatch() throws IOException, AssertionError {
171         XPathExpectationsHelper helper = new XPathExpectationsHelper("//b");
172         WebServiceMessageMatcher matcher = helper.evaluatesTo(2);
173         assertNotNull(matcher);
174 
175         WebServiceMessage message = createMock(WebServiceMessage.class);
176         expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>")).times(2);
177 
178         replay(message);
179 
180         matcher.match(message);
181     }
182 
183     @Test
184     public void existsWithNamespacesMatch() throws IOException, AssertionError {
185         Map<String, String> ns = Collections.singletonMap("x", "http://example.org");
186         XPathExpectationsHelper helper = new XPathExpectationsHelper("//x:b", ns);
187         WebServiceMessageMatcher matcher = helper.exists();
188         assertNotNull(matcher);
189 
190         WebServiceMessage message = createMock(WebServiceMessage.class);
191         expect(message.getPayloadSource())
192                 .andReturn(new StringSource("<a:a xmlns:a=\"http://example.org\"><a:b/></a:a>"));
193 
194         replay(message);
195 
196         matcher.match(message);
197 
198         verify(message);
199     }
200 
201     @Test(expected = AssertionError.class)
202     public void existsWithNamespacesNonMatch() throws IOException, AssertionError {
203         Map<String, String> ns = Collections.singletonMap("x", "http://example.org");
204         XPathExpectationsHelper helper = new XPathExpectationsHelper("//b", ns);
205         WebServiceMessageMatcher matcher = helper.exists();
206         assertNotNull(matcher);
207 
208         WebServiceMessage message = createMock(WebServiceMessage.class);
209         expect(message.getPayloadSource())
210                 .andReturn(new StringSource("<a:a xmlns:a=\"http://example.org\"><a:b/></a:a>")).times(2);
211 
212         replay(message);
213 
214         matcher.match(message);
215     }
216 
217 }