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.soap.saaj;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.util.Iterator;
21  import java.util.Locale;
22  import javax.activation.DataHandler;
23  import javax.xml.namespace.QName;
24  import javax.xml.soap.AttachmentPart;
25  import javax.xml.soap.Detail;
26  import javax.xml.soap.DetailEntry;
27  import javax.xml.soap.MessageFactory;
28  import javax.xml.soap.Name;
29  import javax.xml.soap.SOAPBody;
30  import javax.xml.soap.SOAPEnvelope;
31  import javax.xml.soap.SOAPException;
32  import javax.xml.soap.SOAPFault;
33  import javax.xml.soap.SOAPHeader;
34  import javax.xml.soap.SOAPHeaderElement;
35  import javax.xml.soap.SOAPMessage;
36  import javax.xml.transform.Source;
37  import javax.xml.transform.Transformer;
38  import javax.xml.transform.TransformerFactory;
39  
40  import org.springframework.ws.soap.SoapVersion;
41  import org.springframework.xml.transform.StringResult;
42  import org.springframework.xml.transform.StringSource;
43  
44  import org.junit.Assert;
45  import org.junit.Before;
46  import org.junit.Test;
47  
48  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
49  
50  public abstract class AbstractSaajImplementationTestCase {
51  
52      private SaajImplementation implementation;
53  
54      private SOAPMessage message;
55  
56      private SOAPEnvelope envelope;
57  
58      private SOAPBody body;
59  
60      private SOAPHeader header;
61  
62      @Before
63      public final void setUp() throws Exception {
64          implementation = createSaajImplementation();
65          MessageFactory messageFactory = MessageFactory.newInstance();
66          message = messageFactory.createMessage();
67          envelope = message.getSOAPPart().getEnvelope();
68          body = envelope.getBody();
69          header = envelope.getHeader();
70      }
71  
72      protected abstract SaajImplementation createSaajImplementation();
73  
74      @Test
75      public void testGetName() throws Exception {
76          QName name = implementation.getName(message.getSOAPPart().getEnvelope());
77          Assert.assertEquals("Invalid name", SoapVersion.SOAP_11.getEnvelopeName(), name);
78      }
79  
80      @Test
81      public void testGetSource() throws Exception {
82          Source source = implementation.getSource(message.getSOAPPart().getEnvelope().getBody());
83          Assert.assertNotNull("No source returned", source);
84          Transformer transformer = TransformerFactory.newInstance().newTransformer();
85          StringResult result = new StringResult();
86          transformer.transform(source, result);
87          assertXMLEqual("<Body xmlns='http://schemas.xmlsoap.org/soap/envelope/'/>", result.toString());
88      }
89  
90      @Test
91      public void testGetResult() throws Exception {
92          Source source = new StringSource("<content xmlns='http://springframework.org/spring-ws'/>");
93          Transformer transformer = TransformerFactory.newInstance().newTransformer();
94          transformer.transform(source, implementation.getResult(message.getSOAPPart().getEnvelope().getBody()));
95      }
96  
97      @Test
98      public void testGetEnvelope() throws Exception {
99          SOAPEnvelope envelope = implementation.getEnvelope(message);
100         Assert.assertEquals("Invalid envelope", message.getSOAPPart().getEnvelope(), envelope);
101     }
102 
103     @Test
104     public void testGetHeader() throws Exception {
105         SOAPHeader header = implementation.getHeader(message.getSOAPPart().getEnvelope());
106         Assert.assertEquals("Invalid header", message.getSOAPPart().getEnvelope().getHeader(), header);
107     }
108 
109     @Test
110     public void testGetBody() throws Exception {
111         SOAPBody body = implementation.getBody(message.getSOAPPart().getEnvelope());
112         Assert.assertEquals("Invalid body", message.getSOAPPart().getEnvelope().getBody(), body);
113     }
114 
115     @Test
116     public void testExampleAllHeaderElements() throws Exception {
117         Iterator<SOAPHeaderElement> iterator = implementation.examineAllHeaderElements(header);
118         Assert.assertFalse("Header elements present", iterator.hasNext());
119         createHeaderElement();
120         iterator = implementation.examineAllHeaderElements(header);
121         Assert.assertTrue("No header elements present", iterator.hasNext());
122     }
123 
124     @Test
125     public void testExampleMustUnderstandHeaderElements() throws Exception {
126         SOAPHeaderElement headerElement = createHeaderElement();
127         headerElement.setMustUnderstand(true);
128         Iterator<SOAPHeaderElement> iterator = implementation.examineAllHeaderElements(header);
129         Assert.assertTrue("No header elements present", iterator.hasNext());
130     }
131 
132     @Test
133     public void testAddHeaderElement() throws Exception {
134         SOAPHeaderElement headerElement = implementation
135                 .addHeaderElement(header, new QName("http://springframework.org/spring-ws", "Header"));
136         Assert.assertNotNull("No header element returned", headerElement);
137         Assert.assertEquals("Invalid namespace", "http://springframework.org/spring-ws",
138                 headerElement.getElementName().getURI());
139         Assert.assertEquals("Invalid local name", "Header", headerElement.getElementName().getLocalName());
140     }
141 
142     @Test
143     public void testGetActorOrRole() throws Exception {
144         SOAPHeaderElement headerElement = createHeaderElement();
145         String actor = "http://springframework.org/spring-ws/Actor";
146         headerElement.setActor(actor);
147         Assert.assertEquals("Invalid actor", actor, implementation.getActorOrRole(headerElement));
148     }
149 
150     private SOAPHeaderElement createHeaderElement() throws SOAPException {
151         Name name = envelope.createName("Header", "", "http://springframework.org/spring-ws");
152         return header.addHeaderElement(name);
153     }
154 
155     @Test
156     public void testSetActorOrRole() throws Exception {
157         SOAPHeaderElement headerElement = createHeaderElement();
158         String actor = "http://springframework.org/spring-ws/Actor";
159         implementation.setActorOrRole(headerElement, actor);
160         Assert.assertEquals("Invalid actor", headerElement.getActor(), actor);
161     }
162 
163     @Test
164     public void testGetMustUnderstand() throws Exception {
165         SOAPHeaderElement headerElement = createHeaderElement();
166         headerElement.setMustUnderstand(true);
167         Assert.assertTrue("Invalid mustUnderstand", implementation.getMustUnderstand(headerElement));
168     }
169 
170     @Test
171     public void testSetMustUnderstand() throws Exception {
172         SOAPHeaderElement headerElement = createHeaderElement();
173         implementation.setMustUnderstand(headerElement, true);
174         Assert.assertTrue("Invalid mustUnderstand", headerElement.getMustUnderstand());
175     }
176 
177     @Test
178     public void testHasFault() throws Exception {
179         Assert.assertFalse("Body has fault", implementation.hasFault(body));
180         body.addFault();
181         Assert.assertTrue("Body has no fault", implementation.hasFault(body));
182     }
183 
184     @Test
185     public void testGetFault() throws Exception {
186         Assert.assertNull("Body has fault", implementation.getFault(body));
187         body.addFault();
188         Assert.assertNotNull("Body has no fault", implementation.getFault(body));
189     }
190 
191     @Test
192     public void testAddFault() throws Exception {
193         implementation
194                 .addFault(body, new QName("http://springframework.org/spring-ws", "Fault"), "Fault", Locale.ENGLISH);
195         Assert.assertTrue("No Fault added", body.hasFault());
196     }
197 
198     @Test
199     public void testGetFaultCode() throws Exception {
200         SOAPFault fault = createFault();
201         Assert.assertEquals("Invalid fault code", new QName("http://springframework.org/spring-ws", "Fault"),
202                 implementation.getFaultCode(fault));
203     }
204 
205     private SOAPFault createFault() throws SOAPException {
206         return body.addFault(new QName("http://springframework.org/spring-ws", "Fault"), "Fault", Locale.ENGLISH);
207     }
208 
209     @Test
210     public void testGetFaultActor() throws Exception {
211         SOAPFault fault = createFault();
212         String actor = "http://springframework.org/spring-ws/Actor";
213         fault.setFaultActor(actor);
214         Assert.assertEquals("Invalid actor", actor, implementation.getFaultActor(fault));
215     }
216 
217     @Test
218     public void testSetFaultActor() throws Exception {
219         SOAPFault fault = createFault();
220         String actor = "http://springframework.org/spring-ws/Actor";
221         implementation.setFaultActor(fault, actor);
222         Assert.assertEquals("Invalid actor", actor, fault.getFaultActor());
223     }
224 
225     @Test
226     public void testGetFaultString() throws Exception {
227         SOAPFault fault = createFault();
228         String faultString = "FaultString";
229         fault.setFaultString(faultString);
230         Assert.assertEquals("Invalid fault string", faultString, implementation.getFaultString(fault));
231     }
232 
233     @Test
234     public void testGetFaultStringLocale() throws Exception {
235         SOAPFault fault = createFault();
236         Assert.assertEquals("Invalid fault string", Locale.ENGLISH, implementation.getFaultStringLocale(fault));
237     }
238 
239     @Test
240     public void testGetFaultDetail() throws Exception {
241         SOAPFault fault = createFault();
242         Assert.assertNull("Fault Detail returned", implementation.getFaultDetail(fault));
243         fault.addDetail();
244         Assert.assertNotNull("No Fault Detail returned", implementation.getFaultDetail(fault));
245     }
246 
247     @Test
248     public void testAddFaultDetail() throws Exception {
249         SOAPFault fault = createFault();
250         Detail detail = implementation.addFaultDetail(fault);
251         Assert.assertEquals("Invalid fault detail", fault.getDetail(), detail);
252     }
253 
254     @Test
255     public void testAddDetailEntry() throws Exception {
256         SOAPFault fault = createFault();
257         Detail detail = fault.addDetail();
258         DetailEntry detailEntry =
259                 implementation.addDetailEntry(detail, new QName("http://springframework.org/spring-ws", "DetailEntry"));
260         Assert.assertNotNull("No detail entry", detailEntry);
261         Iterator<?> iterator = detail.getDetailEntries();
262         Assert.assertTrue("No detail entry", iterator.hasNext());
263         Assert.assertEquals("Invalid detail entry", detailEntry, iterator.next());
264     }
265 
266     @Test
267     public void testAddTextNode() throws Exception {
268         SOAPFault fault = createFault();
269         Detail detail = fault.addDetail();
270         Name name = envelope.createName("DetailEntry", "", "http://springframework.org/spring-ws");
271         DetailEntry detailEntry = detail.addDetailEntry(name);
272         implementation.addTextNode(detailEntry, "text");
273         Assert.assertEquals("Invalid text", "text", detailEntry.getValue());
274     }
275 
276     @Test
277     public void testGetDetailEntries() throws Exception {
278         SOAPFault fault = createFault();
279         Detail detail = fault.addDetail();
280         Iterator<DetailEntry> iterator = implementation.getDetailEntries(detail);
281         Assert.assertFalse("Detail entries found", iterator.hasNext());
282         Name name = envelope.createName("DetailEntry", "", "http://springframework.org/spring-ws");
283         DetailEntry detailEntry = detail.addDetailEntry(name);
284         iterator = implementation.getDetailEntries(detail);
285         Assert.assertTrue("No detail entries found", iterator.hasNext());
286         Assert.assertEquals("Invalid detail entry found", detailEntry, iterator.next());
287         Assert.assertFalse("Detail entries found", iterator.hasNext());
288     }
289 
290     @Test
291     public void testWriteTo() throws Exception {
292         ByteArrayOutputStream os = new ByteArrayOutputStream();
293         implementation.writeTo(message, os);
294         Assert.assertTrue("Nothing written", os.toByteArray().length > 0);
295     }
296 
297     @Test
298     public void testGetAttachments() throws Exception {
299         Iterator<AttachmentPart> iterator = implementation.getAttachments(message);
300         Assert.assertFalse("Message has attachments", iterator.hasNext());
301         AttachmentPart attachmentPart = message.createAttachmentPart();
302         message.addAttachmentPart(attachmentPart);
303         iterator = implementation.getAttachments(message);
304         Assert.assertTrue("Message has no attachments", iterator.hasNext());
305         Assert.assertEquals("Invalid attachment part", attachmentPart, iterator.next());
306     }
307 
308     @Test
309     public void testAddAttachmentPart() throws Exception {
310         DataHandler dataHandler = new DataHandler("data", "text/plain");
311         AttachmentPart attachmentPart = implementation.addAttachmentPart(message, dataHandler);
312         Assert.assertNotNull("No attachment part", attachmentPart);
313     }
314 
315     @Test
316     public void testRemoveContents() throws Exception {
317         body.addBodyElement(new QName("foo", "bar"));
318 
319         Assert.assertTrue("Body has child nodes", body.hasChildNodes());
320         implementation.removeContents(message.getSOAPBody());
321         Assert.assertFalse("Body has child nodes", body.hasChildNodes());
322     }
323 
324 }