001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.soap.saaj;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.util.Iterator;
021: import java.util.Locale;
022: import javax.activation.DataHandler;
023: import javax.xml.namespace.QName;
024: import javax.xml.soap.AttachmentPart;
025: import javax.xml.soap.Detail;
026: import javax.xml.soap.DetailEntry;
027: import javax.xml.soap.MessageFactory;
028: import javax.xml.soap.Name;
029: import javax.xml.soap.SOAPBody;
030: import javax.xml.soap.SOAPEnvelope;
031: import javax.xml.soap.SOAPException;
032: import javax.xml.soap.SOAPFault;
033: import javax.xml.soap.SOAPHeader;
034: import javax.xml.soap.SOAPHeaderElement;
035: import javax.xml.soap.SOAPMessage;
036: import javax.xml.transform.Source;
037: import javax.xml.transform.Transformer;
038: import javax.xml.transform.TransformerFactory;
039:
040: import org.custommonkey.xmlunit.XMLTestCase;
041: import org.springframework.ws.soap.SoapVersion;
042: import org.springframework.xml.transform.StringResult;
043: import org.springframework.xml.transform.StringSource;
044:
045: public abstract class AbstractSaajImplementationTestCase extends
046: XMLTestCase {
047:
048: private SaajImplementation implementation;
049:
050: private SOAPMessage message;
051:
052: private SOAPEnvelope envelope;
053:
054: private SOAPBody body;
055:
056: private SOAPHeader header;
057:
058: protected final void setUp() throws Exception {
059: implementation = createSaajImplementation();
060: MessageFactory messageFactory = MessageFactory.newInstance();
061: message = messageFactory.createMessage();
062: envelope = message.getSOAPPart().getEnvelope();
063: body = envelope.getBody();
064: header = envelope.getHeader();
065: }
066:
067: protected abstract SaajImplementation createSaajImplementation();
068:
069: public void testGetName() throws Exception {
070: QName name = implementation.getName(message.getSOAPPart()
071: .getEnvelope());
072: assertEquals("Invalid name", SoapVersion.SOAP_11
073: .getEnvelopeName(), name);
074: }
075:
076: public void testGetSource() throws Exception {
077: Source source = implementation.getSource(message.getSOAPPart()
078: .getEnvelope().getBody());
079: assertNotNull("No source returned", source);
080: Transformer transformer = TransformerFactory.newInstance()
081: .newTransformer();
082: StringResult result = new StringResult();
083: transformer.transform(source, result);
084: assertXMLEqual(
085: "<Body xmlns='http://schemas.xmlsoap.org/soap/envelope/'/>",
086: result.toString());
087: }
088:
089: public void testGetResult() throws Exception {
090: Source source = new StringSource(
091: "<content xmlns='http://springframework.org/spring-ws'/>");
092: Transformer transformer = TransformerFactory.newInstance()
093: .newTransformer();
094: transformer.transform(source, implementation.getResult(message
095: .getSOAPPart().getEnvelope().getBody()));
096: }
097:
098: public void testGetEnvelope() throws Exception {
099: SOAPEnvelope envelope = implementation.getEnvelope(message);
100: assertEquals("Invalid envelope", message.getSOAPPart()
101: .getEnvelope(), envelope);
102: }
103:
104: public void testGetHeader() throws Exception {
105: SOAPHeader header = implementation.getHeader(message
106: .getSOAPPart().getEnvelope());
107: assertEquals("Invalid header", message.getSOAPPart()
108: .getEnvelope().getHeader(), header);
109: }
110:
111: public void testGetBody() throws Exception {
112: SOAPBody body = implementation.getBody(message.getSOAPPart()
113: .getEnvelope());
114: assertEquals("Invalid body", message.getSOAPPart()
115: .getEnvelope().getBody(), body);
116: }
117:
118: public void testExampleAllHeaderElements() throws Exception {
119: Iterator iterator = implementation
120: .examineAllHeaderElements(header);
121: assertFalse("Header elements present", iterator.hasNext());
122: createHeaderElement();
123: iterator = implementation.examineAllHeaderElements(header);
124: assertTrue("No header elements present", iterator.hasNext());
125: }
126:
127: public void testExampleMustUnderstandHeaderElements()
128: throws Exception {
129: SOAPHeaderElement headerElement = createHeaderElement();
130: headerElement.setMustUnderstand(true);
131: Iterator iterator = implementation
132: .examineAllHeaderElements(header);
133: assertTrue("No header elements present", iterator.hasNext());
134: }
135:
136: public void testAddHeaderElement() throws Exception {
137: SOAPHeaderElement headerElement = implementation
138: .addHeaderElement(header, new QName(
139: "http://springframework.org/spring-ws",
140: "Header"));
141: assertNotNull("No header element returned", headerElement);
142: assertEquals("Invalid namespace",
143: "http://springframework.org/spring-ws", headerElement
144: .getElementName().getURI());
145: assertEquals("Invalid local name", "Header", headerElement
146: .getElementName().getLocalName());
147: }
148:
149: public void testGetActorOrRole() throws Exception {
150: SOAPHeaderElement headerElement = createHeaderElement();
151: String actor = "http://springframework.org/spring-ws/Actor";
152: headerElement.setActor(actor);
153: assertEquals("Invalid actor", actor, implementation
154: .getActorOrRole(headerElement));
155: }
156:
157: private SOAPHeaderElement createHeaderElement()
158: throws SOAPException {
159: Name name = envelope.createName("Header", "",
160: "http://springframework.org/spring-ws");
161: return header.addHeaderElement(name);
162: }
163:
164: public void testSetActorOrRole() throws Exception {
165: SOAPHeaderElement headerElement = createHeaderElement();
166: String actor = "http://springframework.org/spring-ws/Actor";
167: implementation.setActorOrRole(headerElement, actor);
168: assertEquals("Invalid actor", headerElement.getActor(), actor);
169: }
170:
171: public void testGetMustUnderstand() throws Exception {
172: SOAPHeaderElement headerElement = createHeaderElement();
173: headerElement.setMustUnderstand(true);
174: assertTrue("Invalid mustUnderstand", implementation
175: .getMustUnderstand(headerElement));
176: }
177:
178: public void testSetMustUnderstand() throws Exception {
179: SOAPHeaderElement headerElement = createHeaderElement();
180: implementation.setMustUnderstand(headerElement, true);
181: assertTrue("Invalid mustUnderstand", headerElement
182: .getMustUnderstand());
183: }
184:
185: public void testHasFault() throws Exception {
186: assertFalse("Body has fault", implementation.hasFault(body));
187: body.addFault();
188: assertTrue("Body has no fault", implementation.hasFault(body));
189: }
190:
191: public void testGetFault() throws Exception {
192: assertNull("Body has fault", implementation.getFault(body));
193: body.addFault();
194: assertNotNull("Body has no fault", implementation
195: .getFault(body));
196: }
197:
198: public void testAddFault() throws Exception {
199: implementation.addFault(body, new QName(
200: "http://springframework.org/spring-ws", "Fault"),
201: "Fault", Locale.ENGLISH);
202: assertTrue("No Fault added", body.hasFault());
203: }
204:
205: public void testGetFaultCode() throws Exception {
206: SOAPFault fault = createFault();
207: assertEquals("Invalid fault code", new QName(
208: "http://springframework.org/spring-ws", "Fault"),
209: implementation.getFaultCode(fault));
210: }
211:
212: private SOAPFault createFault() throws SOAPException {
213: return body.addFault(new QName(
214: "http://springframework.org/spring-ws", "Fault"),
215: "Fault", Locale.ENGLISH);
216: }
217:
218: public void testGetFaultActor() throws Exception {
219: SOAPFault fault = createFault();
220: String actor = "http://springframework.org/spring-ws/Actor";
221: fault.setFaultActor(actor);
222: assertEquals("Invalid actor", actor, implementation
223: .getFaultActor(fault));
224: }
225:
226: public void testSetFaultActor() throws Exception {
227: SOAPFault fault = createFault();
228: String actor = "http://springframework.org/spring-ws/Actor";
229: implementation.setFaultActor(fault, actor);
230: assertEquals("Invalid actor", actor, fault.getFaultActor());
231: }
232:
233: public void testGetFaultString() throws Exception {
234: SOAPFault fault = createFault();
235: String faultString = "FaultString";
236: fault.setFaultString(faultString);
237: assertEquals("Invalid fault string", faultString,
238: implementation.getFaultString(fault));
239: }
240:
241: public void testGetFaultStringLocale() throws Exception {
242: SOAPFault fault = createFault();
243: assertEquals("Invalid fault string", Locale.ENGLISH,
244: implementation.getFaultStringLocale(fault));
245: }
246:
247: public void testGetFaultDetail() throws Exception {
248: SOAPFault fault = createFault();
249: assertNull("Fault Detail returned", implementation
250: .getFaultDetail(fault));
251: fault.addDetail();
252: assertNotNull("No Fault Detail returned", implementation
253: .getFaultDetail(fault));
254: }
255:
256: public void testAddFaultDetail() throws Exception {
257: SOAPFault fault = createFault();
258: Detail detail = implementation.addFaultDetail(fault);
259: assertEquals("Invalid fault detail", fault.getDetail(), detail);
260: }
261:
262: public void testAddDetailEntry() throws Exception {
263: SOAPFault fault = createFault();
264: Detail detail = fault.addDetail();
265: DetailEntry detailEntry = implementation.addDetailEntry(detail,
266: new QName("http://springframework.org/spring-ws",
267: "DetailEntry"));
268: assertNotNull("No detail entry", detailEntry);
269: Iterator iterator = detail.getDetailEntries();
270: assertTrue("No detail entried", iterator.hasNext());
271: assertEquals("Invalid detail entry", detailEntry, iterator
272: .next());
273: }
274:
275: public void testAddTextNode() throws Exception {
276: SOAPFault fault = createFault();
277: Detail detail = fault.addDetail();
278: Name name = envelope.createName("DetailEntry", "",
279: "http://springframework.org/spring-ws");
280: DetailEntry detailEntry = detail.addDetailEntry(name);
281: implementation.addTextNode(detailEntry, "text");
282: assertEquals("Invalid text", "text", detailEntry.getValue());
283: }
284:
285: public void testGetDetailEntries() throws Exception {
286: SOAPFault fault = createFault();
287: Detail detail = fault.addDetail();
288: Iterator iterator = implementation.getDetailEntries(detail);
289: assertFalse("Detail entries found", iterator.hasNext());
290: Name name = envelope.createName("DetailEntry", "",
291: "http://springframework.org/spring-ws");
292: DetailEntry detailEntry = detail.addDetailEntry(name);
293: iterator = implementation.getDetailEntries(detail);
294: assertTrue("No detail entries found", iterator.hasNext());
295: assertEquals("Invalid detail entry found", detailEntry,
296: iterator.next());
297: assertFalse("Detail entries found", iterator.hasNext());
298: }
299:
300: public void testWriteTo() throws Exception {
301: ByteArrayOutputStream os = new ByteArrayOutputStream();
302: implementation.writeTo(message, os);
303: assertTrue("Nothing written", os.toByteArray().length > 0);
304: }
305:
306: public void testGetAttachments() throws Exception {
307: Iterator iterator = implementation.getAttachments(message);
308: assertFalse("Message has attachments", iterator.hasNext());
309: AttachmentPart attachmentPart = message.createAttachmentPart();
310: message.addAttachmentPart(attachmentPart);
311: iterator = implementation.getAttachments(message);
312: assertTrue("Message has no attachments", iterator.hasNext());
313: assertEquals("Invalid attachment part", attachmentPart,
314: iterator.next());
315: }
316:
317: public void testAddAttachmentPart() throws Exception {
318: DataHandler dataHandler = new DataHandler("data", "text/plain");
319: AttachmentPart attachmentPart = implementation
320: .addAttachmentPart(message, dataHandler);
321: assertNotNull("No attachment part", attachmentPart);
322: }
323:
324: public void testRemoveContents() throws Exception {
325: body.addBodyElement(new QName("foo", "bar"));
326:
327: assertTrue("Body has child nodes", body.hasChildNodes());
328: implementation.removeContents(message.getSOAPBody());
329: assertFalse("Body has child nodes", body.hasChildNodes());
330: }
331:
332: }
|