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