Moved SoapHeaderMatcher over to support.matcher.

This commit is contained in:
Arjen Poutsma
2010-11-04 11:32:01 +00:00
parent 5500ae79b8
commit b0934813aa
4 changed files with 73 additions and 22 deletions

View File

@@ -27,6 +27,7 @@ import org.springframework.util.Assert;
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;
@@ -116,7 +117,7 @@ public abstract class RequestMatchers {
*/
public static RequestMatcher soapHeader(QName soapHeaderName) {
Assert.notNull(soapHeaderName, "'soapHeaderName' must not be null");
return new SoapHeaderMatcher(soapHeaderName);
return new WebServiceMessageMatcherAdapter(new SoapHeaderMatcher(soapHeaderName));
}
/**
@@ -142,7 +143,7 @@ public abstract class RequestMatchers {
}
/**
* Adapts a {@link org.springframework.ws.test.support.matcher.WebServiceMessageMatcher} to the {@link RequestMatcher} contract.
* Adapts a {@link WebServiceMessageMatcher} to the {@link RequestMatcher} contract.
*/
private static class WebServiceMessageMatcherAdapter implements RequestMatcher {

View File

@@ -0,0 +1,51 @@
/*
* 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.support.matcher;
import java.io.IOException;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.soap.SoapMessage;
import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
/**
* Abstract base class for SOAP-specific {@link WebServiceMessageMatcher} implementations.
* <p/>
* Asserts that the message given to {@link #match(WebServiceMessage)} is a {@link SoapMessage}, and invokes {@link
* #match(SoapMessage)} with it if so.
*
* @author Arjen Poutsma
* @since 2.0
*/
public abstract class AbstractSoapMessageMatcher implements WebServiceMessageMatcher {
public final void match(WebServiceMessage message) throws IOException, AssertionError {
assertTrue("Message is not a SOAP message", message instanceof SoapMessage);
match((SoapMessage) message);
}
/**
* Abstract template method that gets invoked from {@link #match(WebServiceMessage)} if the given message is a
* {@link SoapMessage}.
*
* @param soapMessage the soap message
* @throws IOException in case of I/O errors
* @throws AssertionError if expectations are not met
*/
protected abstract void match(SoapMessage soapMessage) throws IOException, AssertionError;
}

View File

@@ -14,20 +14,18 @@
* 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.Iterator;
import javax.xml.namespace.QName;
import org.springframework.ws.WebServiceMessage;
import org.springframework.util.Assert;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapHeaderElement;
import org.springframework.ws.soap.SoapMessage;
import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
import static org.springframework.ws.test.support.AssertionErrors.fail;
/**
* Matches SOAP headers.
@@ -35,25 +33,26 @@ import static org.springframework.ws.test.support.AssertionErrors.fail;
* @author Arjen Poutsma
* @since 2.0
*/
class SoapHeaderMatcher implements RequestMatcher {
public class SoapHeaderMatcher extends AbstractSoapMessageMatcher {
private final QName soapHeaderName;
SoapHeaderMatcher(QName soapHeaderName) {
/**
* Creates a new instance of the {@code SoapHeaderMatcher} that checks for the presence of the given SOAP header
* name.
*
* @param soapHeaderName the header name to check for
*/
public SoapHeaderMatcher(QName soapHeaderName) {
Assert.notNull(soapHeaderName, "'soapHeaderName' must not be null");
this.soapHeaderName = soapHeaderName;
}
public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError {
if (!(request instanceof SoapMessage)) {
fail("Request message is not a SOAP message");
return;
}
SoapMessage soapMessage = (SoapMessage) request;
@Override
protected void match(SoapMessage soapMessage) throws IOException, AssertionError {
SoapHeader soapHeader = soapMessage.getSoapHeader();
if (soapHeader == null) {
fail("SOAP message [" + soapMessage + "] does not contain SOAP header");
return;
}
assertTrue("SOAP message [" + soapMessage + "] does not contain SOAP header", soapHeader != null);
Iterator<SoapHeaderElement> soapHeaderElementIterator = soapHeader.examineAllHeaderElements();
boolean found = false;
while (soapHeaderElementIterator.hasNext()) {

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.ws.test.client;
package org.springframework.ws.test.support.matcher;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
@@ -48,7 +48,7 @@ public class SoapHeaderMatcherTest {
saajMessage.getSOAPHeader().addHeaderElement(expectedHeaderName);
SoapMessage soapMessage = new SaajSoapMessage(saajMessage);
matcher.match(null, soapMessage);
matcher.match(soapMessage);
}
@Test(expected = AssertionError.class)
@@ -57,14 +57,14 @@ public class SoapHeaderMatcherTest {
SOAPMessage saajMessage = messageFactory.createMessage();
SoapMessage soapMessage = new SaajSoapMessage(saajMessage);
matcher.match(null, soapMessage);
matcher.match(soapMessage);
}
@Test(expected = AssertionError.class)
public void nonSoap() throws Exception {
WebServiceMessage message = createMock(WebServiceMessage.class);
matcher.match(null, message);
matcher.match(message);
}
}