Moved DefaultXPathExpectations over to support.matcher, as XPathExpectationsHelper.

This commit is contained in:
Arjen Poutsma
2010-11-04 12:10:19 +00:00
parent b9f5a75a30
commit a338be306b
5 changed files with 220 additions and 107 deletions

View File

@@ -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<String, String> 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);
}
}
}

View File

@@ -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);
}
}

View File

@@ -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<String, String> 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));
}
}

View File

@@ -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<String, String> 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<String, String> 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);

View File

@@ -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("<a><b/></a>"));
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("<a><b/></a>"));
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("<a><b/></a>"));
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("<a><b/></a>"));
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("<a><b>1</b></a>"));
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("<a><b>1</b></a>"));
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("<a><b>1</b></a>"));
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("<a><b>1</b></a>"));
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("<a><b>1</b></a>"));
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("<a><b>1</b></a>"));
replay(message);
matcher.match(message);
}
@Test
public void existsWithNamespacesMatch() throws IOException, AssertionError {
Map<String, String> 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<String, String> 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("<a><b>1</b></a>"));
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("<a><b>1</b></a>"));
replay(message);
requestMatcher.match(null, message);
matcher.match(message);
}
}