From a338be306b8aa8d067cb2e0ea79464d48fbfd568 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Thu, 4 Nov 2010 12:10:19 +0000 Subject: [PATCH] Moved DefaultXPathExpectations over to support.matcher, as XPathExpectationsHelper. --- .../ws/test/client/RequestMatchers.java | 21 +-- .../WebServiceMessageMatcherAdapter.java | 44 ++++++ .../XPathExpectationsHelperAdapter.java | 61 ++++++++ .../matcher/XPathExpectationsHelper.java} | 65 +++++---- .../matcher/XPathExpectationsHelperTest.java} | 136 ++++++++++-------- 5 files changed, 220 insertions(+), 107 deletions(-) create mode 100644 test/src/main/java/org/springframework/ws/test/client/WebServiceMessageMatcherAdapter.java create mode 100644 test/src/main/java/org/springframework/ws/test/client/XPathExpectationsHelperAdapter.java rename test/src/main/java/org/springframework/ws/test/{client/DefaultXPathExpectations.java => support/matcher/XPathExpectationsHelper.java} (61%) rename test/src/test/java/org/springframework/ws/test/{client/DefaultXPathExpectationsTest.java => support/matcher/XPathExpectationsHelperTest.java} (63%) diff --git a/test/src/main/java/org/springframework/ws/test/client/RequestMatchers.java b/test/src/main/java/org/springframework/ws/test/client/RequestMatchers.java index 782dc6f9..c5c3f6a8 100644 --- a/test/src/main/java/org/springframework/ws/test/client/RequestMatchers.java +++ b/test/src/main/java/org/springframework/ws/test/client/RequestMatchers.java @@ -28,7 +28,6 @@ import org.springframework.ws.WebServiceMessage; import org.springframework.ws.test.support.matcher.PayloadDiffMatcher; import org.springframework.ws.test.support.matcher.SchemaValidatingMatcher; import org.springframework.ws.test.support.matcher.SoapHeaderMatcher; -import org.springframework.ws.test.support.matcher.WebServiceMessageMatcher; import org.springframework.xml.transform.ResourceSource; /** @@ -95,7 +94,7 @@ public abstract class RequestMatchers { * @return the XPath expectations, to be further configured */ public static XPathExpectations xpath(String xpathExpression) { - return new DefaultXPathExpectations(xpathExpression, null); + return new XPathExpectationsHelperAdapter(xpathExpression, null); } /** @@ -106,7 +105,7 @@ public abstract class RequestMatchers { * @return the XPath expectations, to be further configured */ public static XPathExpectations xpath(String xpathExpression, Map namespaceMapping) { - return new DefaultXPathExpectations(xpathExpression, namespaceMapping); + return new XPathExpectationsHelperAdapter(xpathExpression, namespaceMapping); } /** @@ -142,20 +141,4 @@ public abstract class RequestMatchers { return new UriMatcher(uri); } - /** - * Adapts a {@link WebServiceMessageMatcher} to the {@link RequestMatcher} contract. - */ - private static class WebServiceMessageMatcherAdapter implements RequestMatcher { - - private final WebServiceMessageMatcher adaptee; - - private WebServiceMessageMatcherAdapter(WebServiceMessageMatcher adaptee) { - this.adaptee = adaptee; - } - - public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError { - adaptee.match(request); - } - } - } diff --git a/test/src/main/java/org/springframework/ws/test/client/WebServiceMessageMatcherAdapter.java b/test/src/main/java/org/springframework/ws/test/client/WebServiceMessageMatcherAdapter.java new file mode 100644 index 00000000..a6528943 --- /dev/null +++ b/test/src/main/java/org/springframework/ws/test/client/WebServiceMessageMatcherAdapter.java @@ -0,0 +1,44 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ws.test.client; + +import java.io.IOException; +import java.net.URI; + +import org.springframework.util.Assert; +import org.springframework.ws.WebServiceMessage; +import org.springframework.ws.test.support.matcher.WebServiceMessageMatcher; + +/** + * Adapts a {@link WebServiceMessageMatcher} to the {@link RequestMatcher} contract. + * + * @author Arjen Poutsma + * @since 2.0 + */ +class WebServiceMessageMatcherAdapter implements RequestMatcher { + + private final WebServiceMessageMatcher adaptee; + + WebServiceMessageMatcherAdapter(WebServiceMessageMatcher adaptee) { + Assert.notNull(adaptee, "'adaptee' must not be null"); + this.adaptee = adaptee; + } + + public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError { + adaptee.match(request); + } +} diff --git a/test/src/main/java/org/springframework/ws/test/client/XPathExpectationsHelperAdapter.java b/test/src/main/java/org/springframework/ws/test/client/XPathExpectationsHelperAdapter.java new file mode 100644 index 00000000..668922ff --- /dev/null +++ b/test/src/main/java/org/springframework/ws/test/client/XPathExpectationsHelperAdapter.java @@ -0,0 +1,61 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ws.test.client; + +import java.util.Map; + +import org.springframework.ws.test.support.matcher.XPathExpectationsHelper; + +/** + * Adapts {@link XPathExpectationsHelper} into the {@link XPathExpectations} contract. + * + * @author Arjen Poutsma + * @since 2.0 + */ +class XPathExpectationsHelperAdapter implements XPathExpectations { + + private final XPathExpectationsHelper helper; + + XPathExpectationsHelperAdapter(String expression, Map namespaces) { + helper = new XPathExpectationsHelper(expression, namespaces); + } + + public RequestMatcher exists() { + return new WebServiceMessageMatcherAdapter(helper.exists()); + } + + public RequestMatcher doesNotExist() { + return new WebServiceMessageMatcherAdapter(helper.doesNotExist()); + } + + public RequestMatcher evaluatesTo(boolean expectedValue) { + return new WebServiceMessageMatcherAdapter(helper.evaluatesTo(expectedValue)); + } + + public RequestMatcher evaluatesTo(int expectedValue) { + return new WebServiceMessageMatcherAdapter(helper.evaluatesTo(expectedValue)); + } + + public RequestMatcher evaluatesTo(double expectedValue) { + return new WebServiceMessageMatcherAdapter(helper.evaluatesTo(expectedValue)); + } + + public RequestMatcher evaluatesTo(String expectedValue) { + return new WebServiceMessageMatcherAdapter(helper.evaluatesTo(expectedValue)); + } + +} diff --git a/test/src/main/java/org/springframework/ws/test/client/DefaultXPathExpectations.java b/test/src/main/java/org/springframework/ws/test/support/matcher/XPathExpectationsHelper.java similarity index 61% rename from test/src/main/java/org/springframework/ws/test/client/DefaultXPathExpectations.java rename to test/src/main/java/org/springframework/ws/test/support/matcher/XPathExpectationsHelper.java index 67d456da..fb952443 100644 --- a/test/src/main/java/org/springframework/ws/test/client/DefaultXPathExpectations.java +++ b/test/src/main/java/org/springframework/ws/test/support/matcher/XPathExpectationsHelper.java @@ -14,10 +14,9 @@ * limitations under the License. */ -package org.springframework.ws.test.client; +package org.springframework.ws.test.support.matcher; import java.io.IOException; -import java.net.URI; import java.util.Map; import javax.xml.transform.TransformerException; import javax.xml.transform.dom.DOMResult; @@ -34,13 +33,13 @@ import static org.springframework.ws.test.support.AssertionErrors.assertEquals; import static org.springframework.ws.test.support.AssertionErrors.fail; /** - * Default implementation of {@link XPathExpectations}. + * Helper class for dealing with XPath expectations. * * @author Lukas Krecan * @author Arjen Poutsma * @since 2.0 */ -class DefaultXPathExpectations implements XPathExpectations { +public class XPathExpectationsHelper { private final XPathExpression expression; @@ -48,16 +47,30 @@ class DefaultXPathExpectations implements XPathExpectations { private final TransformerHelper transformerHelper = new TransformerHelper(); - DefaultXPathExpectations(String expression, Map namespaces) { + /** + * Creates a new instance of the {@code XPathExpectationsSupport} with the given XPath expression. + * + * @param expression the XPath expression + */ + public XPathExpectationsHelper(String expression) { + this(expression, null); + } + /** + * Creates a new instance of the {@code XPathExpectationsSupport} with the given XPath expression and namespaces. + * + * @param expression the XPath expression + * @param namespaces the namespaces, can be empty or {@code null} + */ + public XPathExpectationsHelper(String expression, Map namespaces) { Assert.hasLength(expression, "'expression' must not be empty"); this.expression = XPathExpressionFactory.createXPathExpression(expression, namespaces); this.expressionString = expression; } - public RequestMatcher exists() { - return new RequestMatcher() { - public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError { - Node payload = transformToNode(request); + public WebServiceMessageMatcher exists() { + return new WebServiceMessageMatcher() { + public void match(WebServiceMessage message) throws IOException, AssertionError { + Node payload = transformToNode(message); Node result = expression.evaluateAsNode(payload); if (result == null) { fail("No match for \"" + expressionString + "\" found"); @@ -66,10 +79,10 @@ class DefaultXPathExpectations implements XPathExpectations { }; } - public RequestMatcher doesNotExist() { - return new RequestMatcher() { - public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError { - Node payload = transformToNode(request); + public WebServiceMessageMatcher doesNotExist() { + return new WebServiceMessageMatcher() { + public void match(WebServiceMessage message) throws IOException, AssertionError { + Node payload = transformToNode(message); Node result = expression.evaluateAsNode(payload); if (result != null) { fail("Match for \"" + expressionString + "\" found"); @@ -78,10 +91,10 @@ class DefaultXPathExpectations implements XPathExpectations { }; } - public RequestMatcher evaluatesTo(final boolean expectedValue) { - return new RequestMatcher() { - public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError { - Node payload = transformToNode(request); + public WebServiceMessageMatcher evaluatesTo(final boolean expectedValue) { + return new WebServiceMessageMatcher() { + public void match(WebServiceMessage message) throws IOException, AssertionError { + Node payload = transformToNode(message); boolean result = expression.evaluateAsBoolean(payload); assertEquals("Evaluation of XPath expression \"" + expressionString + "\" failed.", expectedValue, result); @@ -90,14 +103,14 @@ class DefaultXPathExpectations implements XPathExpectations { }; } - public RequestMatcher evaluatesTo(int expectedValue) { + public WebServiceMessageMatcher evaluatesTo(int expectedValue) { return evaluatesTo((double) expectedValue); } - public RequestMatcher evaluatesTo(final double expectedValue) { - return new RequestMatcher() { - public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError { - Node payload = transformToNode(request); + public WebServiceMessageMatcher evaluatesTo(final double expectedValue) { + return new WebServiceMessageMatcher() { + public void match(WebServiceMessage message) throws IOException, AssertionError { + Node payload = transformToNode(message); double result = expression.evaluateAsNumber(payload); assertEquals("Evaluation of XPath expression \"" + expressionString + "\" failed.", expectedValue, result); @@ -106,11 +119,11 @@ class DefaultXPathExpectations implements XPathExpectations { }; } - public RequestMatcher evaluatesTo(final String expectedValue) { + public WebServiceMessageMatcher evaluatesTo(final String expectedValue) { Assert.notNull(expectedValue, "'expectedValue' must not be null"); - return new RequestMatcher() { - public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError { - Node payload = transformToNode(request); + return new WebServiceMessageMatcher() { + public void match(WebServiceMessage message) throws IOException, AssertionError { + Node payload = transformToNode(message); String result = expression.evaluateAsString(payload); assertEquals("Evaluation of XPath expression \"" + expressionString + "\" failed.", expectedValue, result); diff --git a/test/src/test/java/org/springframework/ws/test/client/DefaultXPathExpectationsTest.java b/test/src/test/java/org/springframework/ws/test/support/matcher/XPathExpectationsHelperTest.java similarity index 63% rename from test/src/test/java/org/springframework/ws/test/client/DefaultXPathExpectationsTest.java rename to test/src/test/java/org/springframework/ws/test/support/matcher/XPathExpectationsHelperTest.java index e2d8ce11..2a52266d 100644 --- a/test/src/test/java/org/springframework/ws/test/client/DefaultXPathExpectationsTest.java +++ b/test/src/test/java/org/springframework/ws/test/support/matcher/XPathExpectationsHelperTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ws.test.client; +package org.springframework.ws.test.support.matcher; import java.io.IOException; import java.util.Collections; @@ -28,125 +28,164 @@ import org.junit.Test; import static org.easymock.EasyMock.*; import static org.junit.Assert.assertNotNull; -public class DefaultXPathExpectationsTest { +public class XPathExpectationsHelperTest { @Test public void existsMatch() throws IOException, AssertionError { - RequestMatcher requestMatcher = RequestMatchers.xpath("//b").exists(); - assertNotNull(requestMatcher); + XPathExpectationsHelper helper = new XPathExpectationsHelper("//b"); + WebServiceMessageMatcher matcher = helper.exists(); + assertNotNull(matcher); WebServiceMessage message = createMock(WebServiceMessage.class); expect(message.getPayloadSource()).andReturn(new StringSource("")); replay(message); - requestMatcher.match(null, message); + matcher.match(message); verify(message); } @Test(expected = AssertionError.class) public void existsNonMatch() throws IOException, AssertionError { - RequestMatcher requestMatcher = RequestMatchers.xpath("//c").exists(); - assertNotNull(requestMatcher); + XPathExpectationsHelper helper = new XPathExpectationsHelper("//c"); + WebServiceMessageMatcher matcher = helper.exists(); + assertNotNull(matcher); WebServiceMessage message = createMock(WebServiceMessage.class); expect(message.getPayloadSource()).andReturn(new StringSource("")); replay(message); - requestMatcher.match(null, message); + matcher.match(message); } @Test - public void notExistsMatch() throws IOException, AssertionError { - RequestMatcher requestMatcher = RequestMatchers.xpath("//c").doesNotExist(); - assertNotNull(requestMatcher); + public void doesNotExistMatch() throws IOException, AssertionError { + XPathExpectationsHelper helper = new XPathExpectationsHelper("//c"); + WebServiceMessageMatcher matcher = helper.doesNotExist(); + assertNotNull(matcher); WebServiceMessage message = createMock(WebServiceMessage.class); expect(message.getPayloadSource()).andReturn(new StringSource("")); replay(message); - requestMatcher.match(null, message); + matcher.match(message); verify(message); } @Test(expected = AssertionError.class) - public void notExistsNonMatch() throws IOException, AssertionError { - RequestMatcher requestMatcher = RequestMatchers.xpath("//a").doesNotExist(); - assertNotNull(requestMatcher); + public void doesNotExistNonMatch() throws IOException, AssertionError { + XPathExpectationsHelper helper = new XPathExpectationsHelper("//a"); + WebServiceMessageMatcher matcher = helper.doesNotExist(); + assertNotNull(matcher); WebServiceMessage message = createMock(WebServiceMessage.class); expect(message.getPayloadSource()).andReturn(new StringSource("")); replay(message); - requestMatcher.match(null, message); + matcher.match(message); } @Test public void evaluatesToTrueMatch() throws IOException, AssertionError { - RequestMatcher requestMatcher = RequestMatchers.xpath("//b=1").evaluatesTo(true); - assertNotNull(requestMatcher); + XPathExpectationsHelper helper = new XPathExpectationsHelper("//b=1"); + WebServiceMessageMatcher matcher = helper.evaluatesTo(true); + assertNotNull(matcher); WebServiceMessage message = createMock(WebServiceMessage.class); expect(message.getPayloadSource()).andReturn(new StringSource("1")); replay(message); - requestMatcher.match(null, message); + matcher.match(message); verify(message); } @Test(expected = AssertionError.class) public void evaluatesToTrueNonMatch() throws IOException, AssertionError { - RequestMatcher requestMatcher = RequestMatchers.xpath("//b=2").evaluatesTo(true); - assertNotNull(requestMatcher); + XPathExpectationsHelper helper = new XPathExpectationsHelper("//b=2"); + WebServiceMessageMatcher matcher = helper.evaluatesTo(true); + assertNotNull(matcher); WebServiceMessage message = createMock(WebServiceMessage.class); expect(message.getPayloadSource()).andReturn(new StringSource("1")); replay(message); - requestMatcher.match(null, message); + matcher.match(message); } @Test public void evaluatesToFalseMatch() throws IOException, AssertionError { - RequestMatcher requestMatcher = RequestMatchers.xpath("//b!=1").evaluatesTo(false); - assertNotNull(requestMatcher); + XPathExpectationsHelper helper = new XPathExpectationsHelper("//b!=1"); + WebServiceMessageMatcher matcher = helper.evaluatesTo(false); + assertNotNull(matcher); WebServiceMessage message = createMock(WebServiceMessage.class); expect(message.getPayloadSource()).andReturn(new StringSource("1")); replay(message); - requestMatcher.match(null, message); + matcher.match(message); verify(message); } @Test(expected = AssertionError.class) public void evaluatesToFalseNonMatch() throws IOException, AssertionError { - RequestMatcher requestMatcher = RequestMatchers.xpath("//b!=2").evaluatesTo(false); - assertNotNull(requestMatcher); + XPathExpectationsHelper helper = new XPathExpectationsHelper("//b!=2"); + WebServiceMessageMatcher matcher = helper.evaluatesTo(false); + assertNotNull(matcher); WebServiceMessage message = createMock(WebServiceMessage.class); expect(message.getPayloadSource()).andReturn(new StringSource("1")); replay(message); - requestMatcher.match(null, message); + matcher.match(message); + } + + @Test + public void evaluatesToIntegerMatch() throws IOException, AssertionError { + XPathExpectationsHelper helper = new XPathExpectationsHelper("//b"); + WebServiceMessageMatcher matcher = helper.evaluatesTo(1); + assertNotNull(matcher); + + WebServiceMessage message = createMock(WebServiceMessage.class); + expect(message.getPayloadSource()).andReturn(new StringSource("1")); + + replay(message); + + matcher.match(message); + + verify(message); + } + + @Test(expected = AssertionError.class) + public void evaluatesToIntegerNonMatch() throws IOException, AssertionError { + XPathExpectationsHelper helper = new XPathExpectationsHelper("//b"); + WebServiceMessageMatcher matcher = helper.evaluatesTo(2); + assertNotNull(matcher); + + WebServiceMessage message = createMock(WebServiceMessage.class); + expect(message.getPayloadSource()).andReturn(new StringSource("1")); + + replay(message); + + matcher.match(message); } @Test public void existsWithNamespacesMatch() throws IOException, AssertionError { Map ns = Collections.singletonMap("x", "http://example.org"); - RequestMatcher requestMatcher = RequestMatchers.xpath("//x:b", ns).exists(); - assertNotNull(requestMatcher); + XPathExpectationsHelper helper = new XPathExpectationsHelper("//x:b", ns); + WebServiceMessageMatcher matcher = helper.exists(); + assertNotNull(matcher); WebServiceMessage message = createMock(WebServiceMessage.class); expect(message.getPayloadSource()) @@ -154,7 +193,7 @@ public class DefaultXPathExpectationsTest { replay(message); - requestMatcher.match(null, message); + matcher.match(message); verify(message); } @@ -162,8 +201,9 @@ public class DefaultXPathExpectationsTest { @Test(expected = AssertionError.class) public void existsWithNamespacesNonMatch() throws IOException, AssertionError { Map ns = Collections.singletonMap("x", "http://example.org"); - RequestMatcher requestMatcher = RequestMatchers.xpath("//b", ns).exists(); - assertNotNull(requestMatcher); + XPathExpectationsHelper helper = new XPathExpectationsHelper("//b", ns); + WebServiceMessageMatcher matcher = helper.exists(); + assertNotNull(matcher); WebServiceMessage message = createMock(WebServiceMessage.class); expect(message.getPayloadSource()) @@ -171,35 +211,7 @@ public class DefaultXPathExpectationsTest { replay(message); - requestMatcher.match(null, message); - } - - @Test - public void evaluatesToIntegerMatch() throws IOException, AssertionError { - RequestMatcher requestMatcher = RequestMatchers.xpath("//b").evaluatesTo(1); - assertNotNull(requestMatcher); - - WebServiceMessage message = createMock(WebServiceMessage.class); - expect(message.getPayloadSource()).andReturn(new StringSource("1")); - - replay(message); - - requestMatcher.match(null, message); - - verify(message); - } - - @Test(expected = AssertionError.class) - public void evaluatesToIntegerNonMatch() throws IOException, AssertionError { - RequestMatcher requestMatcher = RequestMatchers.xpath("//b").evaluatesTo(2); - assertNotNull(requestMatcher); - - WebServiceMessage message = createMock(WebServiceMessage.class); - expect(message.getPayloadSource()).andReturn(new StringSource("1")); - - replay(message); - - requestMatcher.match(null, message); + matcher.match(message); } }