2828import javax .xml .namespace .QName ;
2929import javax .xml .parsers .DocumentBuilder ;
3030import javax .xml .parsers .DocumentBuilderFactory ;
31- import javax .xml .parsers .ParserConfigurationException ;
3231import javax .xml .soap .MessageFactory ;
3332import javax .xml .soap .MimeHeader ;
3433import javax .xml .soap .MimeHeaders ;
3534import javax .xml .soap .SOAPBody ;
3635import javax .xml .soap .SOAPException ;
3736import javax .xml .soap .SOAPMessage ;
37+ import javax .xml .transform .Result ;
38+ import javax .xml .transform .Source ;
3839import javax .xml .transform .Transformer ;
40+ import javax .xml .transform .TransformerConfigurationException ;
41+ import javax .xml .transform .TransformerException ;
3942import javax .xml .transform .TransformerFactory ;
4043import javax .xml .transform .dom .DOMSource ;
4144import javax .xml .transform .stream .StreamResult ;
5053import org .w3c .dom .Document ;
5154import org .xml .sax .SAXException ;
5255
56+ import org .springframework .oxm .Marshaller ;
57+ import org .springframework .oxm .Unmarshaller ;
58+ import org .springframework .oxm .XmlMappingException ;
5359import org .springframework .util .StringUtils ;
5460import org .springframework .ws .client .WebServiceTransportException ;
5561import org .springframework .ws .pox .dom .DomPoxMessageFactory ;
@@ -67,16 +73,18 @@ public class WebServiceTemplateIntegrationTest extends XMLTestCase {
6773
6874 private static Server jettyServer ;
6975
76+ private String messagePayload ;
77+
7078 public static Test suite () {
7179 return new ServerTestSetup (new TestSuite (WebServiceTemplateIntegrationTest .class ));
7280 }
7381
7482 public void testAxiom () throws Exception {
75- testSoap (new AxiomSoapMessageFactory ());
83+ doSoap (new AxiomSoapMessageFactory ());
7684 }
7785
7886 public void testWithSaaj () throws Exception {
79- testSoap (new SaajSoapMessageFactory (MessageFactory .newInstance ()));
87+ doSoap (new SaajSoapMessageFactory (MessageFactory .newInstance ()));
8088 }
8189
8290 public void testPox () throws Exception {
@@ -104,44 +112,131 @@ public void testPox() throws Exception {
104112 }
105113 }
106114
107- private void testSoap (SoapMessageFactory messageFactory )
108- throws SAXException , IOException , ParserConfigurationException {
115+ private void doSoap (SoapMessageFactory messageFactory ) throws Exception {
109116 template = new WebServiceTemplate (messageFactory );
110117 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>" ;
112129 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 () {
115137 boolean b = template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/noResponse" ,
116- new StringSource (content ), new StringResult ());
138+ new StringSource (messagePayload ), new StringResult ());
117139 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 () {
118206 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 ());
121209 fail ("WebServiceTransportException expected" );
122210 }
123211 catch (WebServiceTransportException ex ) {
124212 //expected
125213 }
214+ }
215+
216+ private void fault () {
217+ Result result = new StringResult ();
126218 try {
127- template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/fault" , new StringSource (content ),
219+ template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/fault" , new StringSource (messagePayload ),
128220 result );
129221 fail ("SoapFaultClientException expected" );
130222 }
131223 catch (SoapFaultClientException ex ) {
132224 //expected
133225 }
226+ }
227+
228+ private void faultNonCompliant () {
229+ Result result = new StringResult ();
134230 template .setCheckConnectionForFault (false );
135231 template .setCheckConnectionForError (false );
136232 try {
137233 template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/badRequestFault" ,
138- new StringSource (content ), result );
234+ new StringSource (messagePayload ), result );
139235 fail ("SoapFaultClientException expected" );
140236 }
141237 catch (SoapFaultClientException ex ) {
142238 //expected
143239 }
144- template .setCheckConnectionForFault (true );
145240 }
146241
147242 /** Servlet that returns and error message for a given status code. */
0 commit comments