28
28
import javax .xml .namespace .QName ;
29
29
import javax .xml .parsers .DocumentBuilder ;
30
30
import javax .xml .parsers .DocumentBuilderFactory ;
31
- import javax .xml .parsers .ParserConfigurationException ;
32
31
import javax .xml .soap .MessageFactory ;
33
32
import javax .xml .soap .MimeHeader ;
34
33
import javax .xml .soap .MimeHeaders ;
35
34
import javax .xml .soap .SOAPBody ;
36
35
import javax .xml .soap .SOAPException ;
37
36
import javax .xml .soap .SOAPMessage ;
37
+ import javax .xml .transform .Result ;
38
+ import javax .xml .transform .Source ;
38
39
import javax .xml .transform .Transformer ;
40
+ import javax .xml .transform .TransformerConfigurationException ;
41
+ import javax .xml .transform .TransformerException ;
39
42
import javax .xml .transform .TransformerFactory ;
40
43
import javax .xml .transform .dom .DOMSource ;
41
44
import javax .xml .transform .stream .StreamResult ;
50
53
import org .w3c .dom .Document ;
51
54
import org .xml .sax .SAXException ;
52
55
56
+ import org .springframework .oxm .Marshaller ;
57
+ import org .springframework .oxm .Unmarshaller ;
58
+ import org .springframework .oxm .XmlMappingException ;
53
59
import org .springframework .util .StringUtils ;
54
60
import org .springframework .ws .client .WebServiceTransportException ;
55
61
import org .springframework .ws .pox .dom .DomPoxMessageFactory ;
@@ -67,16 +73,18 @@ public class WebServiceTemplateIntegrationTest extends XMLTestCase {
67
73
68
74
private static Server jettyServer ;
69
75
76
+ private String messagePayload ;
77
+
70
78
public static Test suite () {
71
79
return new ServerTestSetup (new TestSuite (WebServiceTemplateIntegrationTest .class ));
72
80
}
73
81
74
82
public void testAxiom () throws Exception {
75
- testSoap (new AxiomSoapMessageFactory ());
83
+ doSoap (new AxiomSoapMessageFactory ());
76
84
}
77
85
78
86
public void testWithSaaj () throws Exception {
79
- testSoap (new SaajSoapMessageFactory (MessageFactory .newInstance ()));
87
+ doSoap (new SaajSoapMessageFactory (MessageFactory .newInstance ()));
80
88
}
81
89
82
90
public void testPox () throws Exception {
@@ -104,44 +112,131 @@ public void testPox() throws Exception {
104
112
}
105
113
}
106
114
107
- private void testSoap (SoapMessageFactory messageFactory )
108
- throws SAXException , IOException , ParserConfigurationException {
115
+ private void doSoap (SoapMessageFactory messageFactory ) throws Exception {
109
116
template = new WebServiceTemplate (messageFactory );
110
117
template .setMessageSender (new CommonsHttpMessageSender ());
111
- String content = "<root xmlns='http://springframework.org/spring-ws'><child/></root>" ;
118
+ sendSourceAndReceiveToResult ();
119
+ sendSourceAndReceiveToResultNoResponse ();
120
+ marshalSendAndReceiveResponse ();
121
+ marshalSendAndReceiveNoResponse ();
122
+ notFound ();
123
+ fault ();
124
+ faultNonCompliant ();
125
+ }
126
+
127
+ private void sendSourceAndReceiveToResult () throws SAXException , IOException {
128
+ messagePayload = "<root xmlns='http://springframework.org/spring-ws'><child/></root>" ;
112
129
StringResult result = new StringResult ();
113
- template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/echo" , new StringSource (content ), result );
114
- assertXMLEqual (content , result .toString ());
130
+ boolean b = template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/echo" ,
131
+ new StringSource (messagePayload ), result );
132
+ assertTrue ("Invalid result" , b );
133
+ assertXMLEqual (messagePayload , result .toString ());
134
+ }
135
+
136
+ private void sendSourceAndReceiveToResultNoResponse () {
115
137
boolean b = template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/noResponse" ,
116
- new StringSource (content ), new StringResult ());
138
+ new StringSource (messagePayload ), new StringResult ());
117
139
assertFalse ("Invalid result" , b );
140
+ }
141
+
142
+ private void marshalSendAndReceiveResponse () throws TransformerConfigurationException {
143
+ final Transformer transformer = TransformerFactory .newInstance ().newTransformer ();
144
+ final Object requestObject = new Object ();
145
+ Marshaller marshaller = new Marshaller () {
146
+
147
+ public void marshal (Object graph , Result result ) throws XmlMappingException , IOException {
148
+ assertEquals ("Invalid object" , graph , requestObject );
149
+ try {
150
+ transformer .transform (new StringSource (messagePayload ), result );
151
+ }
152
+ catch (TransformerException e ) {
153
+ fail (e .getMessage ());
154
+ }
155
+ }
156
+
157
+ public boolean supports (Class clazz ) {
158
+ assertEquals ("Invalid class" , Object .class , clazz );
159
+ return true ;
160
+ }
161
+ };
162
+ final Object responseObject = new Object ();
163
+ Unmarshaller unmarshaller = new Unmarshaller () {
164
+
165
+ public Object unmarshal (Source source ) throws XmlMappingException , IOException {
166
+ return responseObject ;
167
+ }
168
+
169
+ public boolean supports (Class clazz ) {
170
+ assertEquals ("Invalid class" , Object .class , clazz );
171
+ return true ;
172
+ }
173
+ };
174
+ template .setMarshaller (marshaller );
175
+ template .setUnmarshaller (unmarshaller );
176
+ Object result = template .marshalSendAndReceive ("http://localhost:8888/soap/echo" , requestObject );
177
+ assertEquals ("Invalid response object" , responseObject , result );
178
+ }
179
+
180
+ private void marshalSendAndReceiveNoResponse () throws TransformerConfigurationException {
181
+ final Transformer transformer = TransformerFactory .newInstance ().newTransformer ();
182
+ final Object requestObject = new Object ();
183
+ Marshaller marshaller = new Marshaller () {
184
+
185
+ public void marshal (Object graph , Result result ) throws XmlMappingException , IOException {
186
+ assertEquals ("Invalid object" , graph , requestObject );
187
+ try {
188
+ transformer .transform (new StringSource (messagePayload ), result );
189
+ }
190
+ catch (TransformerException e ) {
191
+ fail (e .getMessage ());
192
+ }
193
+ }
194
+
195
+ public boolean supports (Class clazz ) {
196
+ assertEquals ("Invalid class" , Object .class , clazz );
197
+ return true ;
198
+ }
199
+ };
200
+ template .setMarshaller (marshaller );
201
+ Object result = template .marshalSendAndReceive ("http://localhost:8888/soap/noResponse" , requestObject );
202
+ assertNull ("Invalid response object" , result );
203
+ }
204
+
205
+ private void notFound () {
118
206
try {
119
- template .sendSourceAndReceiveToResult ("http://localhost:8888/errors/notfound" , new StringSource ( content ),
120
- new StringResult ());
207
+ template .sendSourceAndReceiveToResult ("http://localhost:8888/errors/notfound" ,
208
+ new StringSource ( messagePayload ), new StringResult ());
121
209
fail ("WebServiceTransportException expected" );
122
210
}
123
211
catch (WebServiceTransportException ex ) {
124
212
//expected
125
213
}
214
+ }
215
+
216
+ private void fault () {
217
+ Result result = new StringResult ();
126
218
try {
127
- template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/fault" , new StringSource (content ),
219
+ template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/fault" , new StringSource (messagePayload ),
128
220
result );
129
221
fail ("SoapFaultClientException expected" );
130
222
}
131
223
catch (SoapFaultClientException ex ) {
132
224
//expected
133
225
}
226
+ }
227
+
228
+ private void faultNonCompliant () {
229
+ Result result = new StringResult ();
134
230
template .setCheckConnectionForFault (false );
135
231
template .setCheckConnectionForError (false );
136
232
try {
137
233
template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/badRequestFault" ,
138
- new StringSource (content ), result );
234
+ new StringSource (messagePayload ), result );
139
235
fail ("SoapFaultClientException expected" );
140
236
}
141
237
catch (SoapFaultClientException ex ) {
142
238
//expected
143
239
}
144
- template .setCheckConnectionForFault (true );
145
240
}
146
241
147
242
/** Servlet that returns and error message for a given status code. */
0 commit comments