Skip to content

Commit 90bdf17

Browse files
committed
Refactored some tests
1 parent 5c3de45 commit 90bdf17

File tree

5 files changed

+185
-168
lines changed

5 files changed

+185
-168
lines changed

test/src/main/java/org/springframework/ws/test/client/ResponseCreators.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.util.Assert;
2626
import org.springframework.ws.WebServiceMessage;
2727
import org.springframework.ws.WebServiceMessageFactory;
28+
import org.springframework.ws.soap.SoapBody;
2829
import org.springframework.ws.test.support.creator.PayloadMessageCreator;
2930
import org.springframework.ws.test.support.creator.WebServiceMessageCreator;
3031
import org.springframework.xml.transform.ResourceSource;
@@ -103,11 +104,16 @@ public static ResponseCreator withException(RuntimeException ex) {
103104
*
104105
* @param faultStringOrReason the SOAP 1.1 fault string or SOAP 1.2 reason text
105106
* @param locale the language of faultStringOrReason. Optional for SOAP 1.1
106-
* @see org.springframework.ws.soap.SoapBody#addMustUnderstandFault(String, java.util.Locale)
107+
* @see SoapBody#addMustUnderstandFault(String, java.util.Locale)
107108
*/
108-
public static ResponseCreator withMustUnderstandFault(String faultStringOrReason, Locale locale) {
109+
public static ResponseCreator withMustUnderstandFault(final String faultStringOrReason, final Locale locale) {
109110
Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty");
110-
return SoapFaultResponseCreator.createMustUnderstandFault(faultStringOrReason, locale);
111+
return new SoapFaultResponseCreator() {
112+
@Override
113+
public void addSoapFault(SoapBody soapBody) {
114+
soapBody.addMustUnderstandFault(faultStringOrReason, locale);
115+
}
116+
};
111117
}
112118

113119
/**
@@ -117,9 +123,14 @@ public static ResponseCreator withMustUnderstandFault(String faultStringOrReason
117123
* @param locale the language of faultStringOrReason. Optional for SOAP 1.1
118124
* @see org.springframework.ws.soap.SoapBody#addClientOrSenderFault(String, Locale)
119125
*/
120-
public static ResponseCreator withClientOrSenderFault(String faultStringOrReason, Locale locale) {
126+
public static ResponseCreator withClientOrSenderFault(final String faultStringOrReason, final Locale locale) {
121127
Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty");
122-
return SoapFaultResponseCreator.createClientOrSenderFault(faultStringOrReason, locale);
128+
return new SoapFaultResponseCreator() {
129+
@Override
130+
public void addSoapFault(SoapBody soapBody) {
131+
soapBody.addClientOrSenderFault(faultStringOrReason, locale);
132+
}
133+
};
123134
}
124135

125136
/**
@@ -129,9 +140,14 @@ public static ResponseCreator withClientOrSenderFault(String faultStringOrReason
129140
* @param locale the language of faultStringOrReason. Optional for SOAP 1.1
130141
* @see org.springframework.ws.soap.SoapBody#addServerOrReceiverFault(String, Locale)
131142
*/
132-
public static ResponseCreator withServerOrReceiverFault(String faultStringOrReason, Locale locale) {
143+
public static ResponseCreator withServerOrReceiverFault(final String faultStringOrReason, final Locale locale) {
133144
Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty");
134-
return SoapFaultResponseCreator.createServerOrReceiverFault(faultStringOrReason, locale);
145+
return new SoapFaultResponseCreator() {
146+
@Override
147+
public void addSoapFault(SoapBody soapBody) {
148+
soapBody.addServerOrReceiverFault(faultStringOrReason, locale);
149+
}
150+
};
135151
}
136152

137153
/**
@@ -141,9 +157,14 @@ public static ResponseCreator withServerOrReceiverFault(String faultStringOrReas
141157
* @param locale the language of faultStringOrReason. Optional for SOAP 1.1
142158
* @see org.springframework.ws.soap.SoapBody#addVersionMismatchFault(String, Locale)
143159
*/
144-
public static ResponseCreator withVersionMismatchFault(String faultStringOrReason, Locale locale) {
160+
public static ResponseCreator withVersionMismatchFault(final String faultStringOrReason, final Locale locale) {
145161
Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty");
146-
return SoapFaultResponseCreator.createVersionMismatchFault(faultStringOrReason, locale);
162+
return new SoapFaultResponseCreator() {
163+
@Override
164+
public void addSoapFault(SoapBody soapBody) {
165+
soapBody.addVersionMismatchFault(faultStringOrReason, locale);
166+
}
167+
};
147168
}
148169

149170
/**

test/src/main/java/org/springframework/ws/test/client/SoapFaultResponseCreator.java

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.IOException;
2020
import java.net.URI;
21-
import java.util.Locale;
2221

2322
import org.springframework.ws.WebServiceMessage;
2423
import org.springframework.ws.soap.SoapBody;
@@ -48,50 +47,10 @@ protected void doWithResponse(URI uri, WebServiceMessage request, WebServiceMess
4847
addSoapFault(responseBody);
4948
}
5049

51-
public abstract void addSoapFault(SoapBody soapBody);
52-
53-
public static SoapFaultResponseCreator createMustUnderstandFault(final String faultStringOrReason,
54-
final Locale locale) {
55-
return new SoapFaultResponseCreator() {
56-
@Override
57-
public void addSoapFault(SoapBody soapBody) {
58-
soapBody.addMustUnderstandFault(faultStringOrReason, locale);
59-
}
60-
};
61-
62-
}
63-
64-
public static SoapFaultResponseCreator createClientOrSenderFault(final String faultStringOrReason,
65-
final Locale locale) {
66-
return new SoapFaultResponseCreator() {
67-
@Override
68-
public void addSoapFault(SoapBody soapBody) {
69-
soapBody.addClientOrSenderFault(faultStringOrReason, locale);
70-
}
71-
};
72-
}
73-
74-
public static SoapFaultResponseCreator createServerOrReceiverFault(final String faultStringOrReason,
75-
final Locale locale) {
76-
return new SoapFaultResponseCreator() {
77-
@Override
78-
public void addSoapFault(SoapBody soapBody) {
79-
soapBody.addServerOrReceiverFault(faultStringOrReason, locale);
80-
}
81-
};
82-
83-
}
84-
85-
public static SoapFaultResponseCreator createVersionMismatchFault(final String faultStringOrReason,
86-
final Locale locale) {
87-
return new SoapFaultResponseCreator() {
88-
@Override
89-
public void addSoapFault(SoapBody soapBody) {
90-
soapBody.addVersionMismatchFault(faultStringOrReason, locale);
91-
}
92-
};
93-
94-
}
95-
96-
50+
/**
51+
* Abstract template method that allows subclasses to add a SOAP Fault to the given Body.
52+
*
53+
* @param soapBody the body to attach a fault to
54+
*/
55+
protected abstract void addSoapFault(SoapBody soapBody);
9756
}

test/src/test/java/org/springframework/ws/test/client/MockWebServiceServerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public void fault() throws Exception {
304304
StringResult result = new StringResult();
305305
template.sendSourceAndReceiveToResult(request, result);
306306
}
307-
307+
308308
public static class MyClient extends WebServiceGatewaySupport {
309309

310310
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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.test.client;
18+
19+
import java.io.IOException;
20+
import java.util.Locale;
21+
import javax.xml.namespace.QName;
22+
import javax.xml.transform.Result;
23+
import javax.xml.transform.TransformerException;
24+
25+
import org.springframework.core.io.ByteArrayResource;
26+
import org.springframework.ws.WebServiceMessage;
27+
import org.springframework.ws.soap.SoapMessage;
28+
import org.springframework.ws.soap.SoapVersion;
29+
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
30+
import org.springframework.ws.soap.soap11.Soap11Fault;
31+
import org.springframework.xml.transform.StringResult;
32+
import org.springframework.xml.transform.StringSource;
33+
import org.springframework.xml.transform.TransformerHelper;
34+
35+
import org.junit.Before;
36+
import org.junit.Test;
37+
38+
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
39+
import static org.junit.Assert.*;
40+
41+
public class ResponseCreatorsTest {
42+
43+
private final TransformerHelper transformerHelper = new TransformerHelper();
44+
45+
private SaajSoapMessageFactory messageFactory;
46+
47+
@Before
48+
public void createMessageFactory() {
49+
messageFactory = new SaajSoapMessageFactory();
50+
messageFactory.afterPropertiesSet();
51+
}
52+
53+
@Test
54+
public void withPayloadSource() throws Exception {
55+
String payload = "<payload xmlns='http://springframework.org'/>";
56+
ResponseCreator responseCreator = ResponseCreators.withPayload(new StringSource(payload));
57+
58+
WebServiceMessage response = responseCreator.createResponse(null, null, messageFactory);
59+
60+
assertXMLEqual(payload, getPayloadAsString(response));
61+
62+
}
63+
64+
@Test
65+
public void withPayloadResource() throws Exception {
66+
String payload = "<payload xmlns='http://springframework.org'/>";
67+
ResponseCreator responseCreator =
68+
ResponseCreators.withPayload(new ByteArrayResource(payload.getBytes("UTF-8")));
69+
70+
WebServiceMessage response = responseCreator.createResponse(null, null, messageFactory);
71+
72+
assertXMLEqual(payload, getPayloadAsString(response));
73+
}
74+
75+
@Test
76+
public void withIOException() throws Exception {
77+
IOException expected = new IOException("Foo");
78+
ResponseCreator responseCreator = ResponseCreators.withException(expected);
79+
80+
try {
81+
responseCreator.createResponse(null, null, null);
82+
}
83+
catch (IOException actual) {
84+
assertSame(expected, actual);
85+
}
86+
}
87+
88+
@Test
89+
public void withRuntimeException() throws Exception {
90+
RuntimeException expected = new RuntimeException("Foo");
91+
ResponseCreator responseCreator = ResponseCreators.withException(expected);
92+
93+
try {
94+
responseCreator.createResponse(null, null, null);
95+
}
96+
catch (RuntimeException actual) {
97+
assertSame(expected, actual);
98+
}
99+
}
100+
101+
@Test
102+
public void withMustUnderstandFault() throws Exception {
103+
String faultString = "Foo";
104+
ResponseCreator responseCreator = ResponseCreators.withMustUnderstandFault(faultString, Locale.ENGLISH);
105+
106+
testFault(responseCreator, faultString, SoapVersion.SOAP_11.getMustUnderstandFaultName());
107+
}
108+
109+
@Test
110+
public void withClientOrSenderFault() throws Exception {
111+
String faultString = "Foo";
112+
ResponseCreator responseCreator = ResponseCreators.withClientOrSenderFault(faultString, Locale.ENGLISH);
113+
114+
testFault(responseCreator, faultString, SoapVersion.SOAP_11.getClientOrSenderFaultName());
115+
}
116+
117+
@Test
118+
public void withServerOrReceiverFault() throws Exception {
119+
String faultString = "Foo";
120+
ResponseCreator responseCreator = ResponseCreators.withServerOrReceiverFault(faultString, Locale.ENGLISH);
121+
122+
testFault(responseCreator, faultString, SoapVersion.SOAP_11.getServerOrReceiverFaultName());
123+
}
124+
125+
@Test
126+
public void withVersionMismatchFault() throws Exception {
127+
String faultString = "Foo";
128+
ResponseCreator responseCreator = ResponseCreators.withVersionMismatchFault(faultString, Locale.ENGLISH);
129+
130+
testFault(responseCreator, faultString, SoapVersion.SOAP_11.getVersionMismatchFaultName());
131+
}
132+
133+
private void testFault(ResponseCreator responseCreator, String faultString, QName faultCode) throws IOException {
134+
SoapMessage response = (SoapMessage) responseCreator.createResponse(null, null, messageFactory);
135+
136+
assertTrue("Response has no fault", response.hasFault());
137+
Soap11Fault soapFault = (Soap11Fault) response.getSoapBody().getFault();
138+
assertEquals("Response has invalid fault code", faultCode, soapFault.getFaultCode());
139+
assertEquals("Response has invalid fault string", faultString, soapFault.getFaultStringOrReason());
140+
assertEquals("Response has invalid fault locale", Locale.ENGLISH, soapFault.getFaultStringLocale());
141+
}
142+
143+
private String getPayloadAsString(WebServiceMessage message) throws TransformerException {
144+
Result result = new StringResult();
145+
transformerHelper.transform(message.getPayloadSource(), result);
146+
return result.toString();
147+
}
148+
}

0 commit comments

Comments
 (0)