Skip to content

Commit 33ec9ee

Browse files
committed
SWS-544 - Add test framework for Spring WS client
1 parent f770046 commit 33ec9ee

23 files changed

+1622
-0
lines changed

test/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,9 @@
4949
<artifactId>xmlunit</artifactId>
5050
<scope>compile</scope>
5151
</dependency>
52+
<dependency>
53+
<groupId>org.springframework</groupId>
54+
<artifactId>spring-test</artifactId>
55+
</dependency>
5256
</dependencies>
5357
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2005-2010 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.mock.client2;
18+
19+
import java.io.IOException;
20+
import java.net.URI;
21+
22+
import org.springframework.ws.WebServiceMessage;
23+
import org.springframework.xml.transform.TransformerObjectSupport;
24+
25+
import org.custommonkey.xmlunit.Diff;
26+
27+
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
28+
import static org.custommonkey.xmlunit.XMLAssert.fail;
29+
30+
/**
31+
* Implementation of {@link RequestMatcher} based on XMLUnit's {@link Diff}.
32+
*
33+
* @author Arjen Poutsma
34+
* @since 2.0
35+
*/
36+
abstract class DiffMatcher extends TransformerObjectSupport implements RequestMatcher {
37+
38+
public final void match(URI uri, WebServiceMessage request) throws IOException, AssertionError {
39+
try {
40+
Diff diff = createDiff(request);
41+
assertXMLEqual(diff, true);
42+
}
43+
catch (IOException ex) {
44+
throw ex;
45+
}
46+
catch (Exception ex) {
47+
fail("Could not create Diff: " + ex.getMessage());
48+
}
49+
}
50+
51+
/**
52+
* Creates a {@link org.custommonkey.xmlunit.Diff} for the given request message.
53+
*
54+
* @param request the request message
55+
* @return the diff
56+
* @throws Exception in case of errors
57+
*/
58+
protected abstract Diff createDiff(WebServiceMessage request) throws Exception;
59+
60+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2005-2010 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.mock.client2;
18+
19+
import java.io.IOException;
20+
21+
import org.springframework.ws.WebServiceMessage;
22+
23+
/**
24+
* Implementation of {@link ResponseCallback} that holds an error message.
25+
*
26+
* @author Arjen Poutsma
27+
* @author Lukas Krecan
28+
* @since 2.0
29+
*/
30+
class ErrorResponseCallback implements ResponseCallback {
31+
32+
private final String errorMessage;
33+
34+
ErrorResponseCallback(String errorMessage) {
35+
this.errorMessage = errorMessage;
36+
}
37+
38+
public void doWithResponse(WebServiceMessage response, WebServiceMessage request) throws IOException {
39+
// Do nothing
40+
}
41+
42+
String getErrorMessage() {
43+
return errorMessage;
44+
}
45+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2005-2010 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.mock.client2;
18+
19+
import java.io.IOException;
20+
21+
import org.springframework.ws.WebServiceMessage;
22+
23+
/**
24+
* Implementation of {@link ResponseCallback} that responds by throwing either an {@link IOException} or a {@link
25+
* RuntimeException}.
26+
*
27+
* @author Arjen Poutsma
28+
* @since 2.0
29+
*/
30+
class ExceptionResponseCallback implements ResponseCallback {
31+
32+
private final Exception exception;
33+
34+
ExceptionResponseCallback(IOException exception) {
35+
this.exception = exception;
36+
}
37+
38+
ExceptionResponseCallback(RuntimeException exception) {
39+
this.exception = exception;
40+
}
41+
42+
public void doWithResponse(WebServiceMessage request, WebServiceMessage response) throws IOException {
43+
if (exception instanceof IOException) {
44+
throw (IOException) exception;
45+
}
46+
else {
47+
throw (RuntimeException) exception;
48+
}
49+
}
50+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright 2005-2010 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.mock.client2;
18+
19+
import java.io.IOException;
20+
import java.net.URI;
21+
import java.util.LinkedList;
22+
import java.util.List;
23+
24+
import org.springframework.util.Assert;
25+
import org.springframework.ws.WebServiceMessage;
26+
import org.springframework.ws.WebServiceMessageFactory;
27+
import org.springframework.ws.transport.FaultAwareWebServiceConnection;
28+
29+
/**
30+
* Mock implementation of {@link FaultAwareWebServiceConnection}. Implements {@link ResponseActions} to form a fluent
31+
* API.
32+
*
33+
* @author Arjen Poutsma
34+
* @author Lukas Krecan
35+
* @since 2.0
36+
*/
37+
class MockSenderConnection implements FaultAwareWebServiceConnection, ResponseActions {
38+
39+
private final List<RequestMatcher> requestMatchers = new LinkedList<RequestMatcher>();
40+
41+
private URI uri;
42+
43+
private boolean lastConnection = false;
44+
45+
private WebServiceMessage request;
46+
47+
private ResponseCallback responseCallback;
48+
49+
void addRequestMatcher(RequestMatcher requestMatcher) {
50+
Assert.notNull(requestMatcher, "'requestMatcher' must not be null");
51+
requestMatchers.add(requestMatcher);
52+
}
53+
54+
void setUri(URI uri) {
55+
Assert.notNull(uri, "'uri' must not be null");
56+
this.uri = uri;
57+
}
58+
59+
void lastConnection() {
60+
lastConnection = true;
61+
}
62+
63+
// ResponseActions implementation
64+
65+
public ResponseActions andExpect(RequestMatcher requestMatcher) {
66+
addRequestMatcher(requestMatcher);
67+
return this;
68+
}
69+
70+
public void andRespond(ResponseCallback responseCallback) {
71+
Assert.notNull(responseCallback, "'responseCallback' must not be null");
72+
this.responseCallback = responseCallback;
73+
}
74+
75+
// FaultAwareWebServiceConnection implementation
76+
77+
public void send(WebServiceMessage message) throws IOException {
78+
if (!requestMatchers.isEmpty()) {
79+
for (RequestMatcher requestMatcher : requestMatchers) {
80+
requestMatcher.match(uri, message);
81+
}
82+
}
83+
else {
84+
throw new AssertionError("Unexpected send() for [" + message + "]");
85+
}
86+
this.request = message;
87+
}
88+
89+
public WebServiceMessage receive(WebServiceMessageFactory messageFactory) throws IOException {
90+
if (responseCallback != null) {
91+
WebServiceMessage response = messageFactory.createWebServiceMessage();
92+
responseCallback.doWithResponse(request, response);
93+
return response;
94+
}
95+
else {
96+
return null;
97+
}
98+
}
99+
100+
public URI getUri() {
101+
return uri;
102+
}
103+
104+
public boolean hasError() throws IOException {
105+
return responseCallback instanceof ErrorResponseCallback;
106+
}
107+
108+
public String getErrorMessage() throws IOException {
109+
if (responseCallback instanceof ErrorResponseCallback) {
110+
return ((ErrorResponseCallback) responseCallback).getErrorMessage();
111+
}
112+
else {
113+
return null;
114+
}
115+
}
116+
117+
public boolean hasFault() throws IOException {
118+
return responseCallback instanceof SoapFaultResponseCallback;
119+
}
120+
121+
public void setFault(boolean fault) throws IOException {
122+
// Do nothing
123+
}
124+
125+
public void close() throws IOException {
126+
requestMatchers.clear();
127+
request = null;
128+
responseCallback = null;
129+
uri = null;
130+
if (lastConnection) {
131+
MockWebServiceMessageSenderHolder.clear();
132+
}
133+
}
134+
135+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2005-2010 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.mock.client2;
18+
19+
import java.io.IOException;
20+
import java.net.URI;
21+
import java.util.Iterator;
22+
import java.util.LinkedList;
23+
import java.util.List;
24+
25+
import org.springframework.util.Assert;
26+
import org.springframework.ws.transport.WebServiceMessageSender;
27+
28+
import static org.junit.Assert.assertTrue;
29+
30+
class MockWebServiceMessageSender implements WebServiceMessageSender {
31+
32+
private final List<MockSenderConnection> expectedConnections = new LinkedList<MockSenderConnection>();
33+
34+
private Iterator<MockSenderConnection> connectionIterator;
35+
36+
public MockSenderConnection createConnection(URI uri) throws IOException {
37+
Assert.notNull(uri, "'uri' must not be null");
38+
if (connectionIterator == null) {
39+
connectionIterator = expectedConnections.iterator();
40+
}
41+
assertTrue("No further connections expected", connectionIterator.hasNext());
42+
43+
MockSenderConnection currentConnection = connectionIterator.next();
44+
currentConnection.setUri(uri);
45+
if (!connectionIterator.hasNext()) {
46+
currentConnection.lastConnection();
47+
}
48+
return currentConnection;
49+
}
50+
51+
/** Always returns {@code true}. */
52+
public boolean supports(URI uri) {
53+
return true;
54+
}
55+
56+
MockSenderConnection expectNewConnection() {
57+
MockSenderConnection connection = new MockSenderConnection();
58+
expectedConnections.add(connection);
59+
return connection;
60+
}
61+
62+
}

0 commit comments

Comments
 (0)