001: package org.objectweb.celtix.bus.bindings.soap;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.InputStream;
005: import java.lang.reflect.Method;
006:
007: import javax.xml.namespace.QName;
008: import javax.xml.soap.Detail;
009: import javax.xml.soap.SOAPException;
010: import javax.xml.soap.SOAPFault;
011: import javax.xml.soap.SOAPMessage;
012: import javax.xml.ws.WebFault;
013: import javax.xml.ws.soap.SOAPFaultException;
014:
015: import org.w3c.dom.Node;
016: import org.w3c.dom.NodeList;
017:
018: import junit.framework.TestCase;
019:
020: import org.objectweb.celtix.bindings.DataBindingCallback;
021: import org.objectweb.celtix.bus.bindings.TestInputStreamContext;
022: import org.objectweb.celtix.bus.bindings.TestOutputStreamContext;
023: import org.objectweb.celtix.bus.jaxws.JAXBDataBindingCallback;
024: import org.objectweb.celtix.context.GenericMessageContext;
025: import org.objectweb.celtix.context.ObjectMessageContext;
026: import org.objectweb.celtix.context.ObjectMessageContextImpl;
027: import org.objectweb.hello_world_soap_http.BadRecordLitFault;
028: import org.objectweb.hello_world_soap_http.Greeter;
029: import org.objectweb.hello_world_soap_http.NoSuchCodeLitFault;
030: import org.objectweb.hello_world_soap_http.types.ErrorCode;
031: import org.objectweb.hello_world_soap_http.types.NoSuchCodeLit;
032: import org.objectweb.type_test.doc.TypeTestPortType;
033:
034: public class SoapBindingImplTest extends TestCase {
035: private SOAPBindingImpl binding;
036: private ObjectMessageContextImpl objContext;
037: private SOAPMessageContextImpl soapContext;
038:
039: public SoapBindingImplTest(String arg0) {
040: super (arg0);
041: }
042:
043: public static void main(String[] args) {
044: junit.textui.TestRunner.run(SoapBindingImplTest.class);
045: }
046:
047: protected void setUp() throws Exception {
048: super .setUp();
049:
050: binding = new SOAPBindingImpl(false);
051: objContext = new ObjectMessageContextImpl();
052: soapContext = new SOAPMessageContextImpl(
053: new GenericMessageContext());
054:
055: objContext.setMethod(SOAPMessageUtil.getMethod(Greeter.class,
056: "greetMe"));
057: }
058:
059: public void testCreateBindingMessageContext() throws Exception {
060: binding = new SOAPBindingImpl(false);
061: byte[] bArray = new byte[0];
062: TestInputStreamContext inCtx = new TestInputStreamContext(
063: bArray);
064: assertNotNull(binding.createBindingMessageContext(inCtx));
065: }
066:
067: public void testGetMessageFactory() throws Exception {
068: assertNotNull(binding.getSOAPFactory());
069: }
070:
071: public void testMarshalWrapDocLitInputMessage() throws Exception {
072: //Test The InputMessage of GreetMe Operation
073: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
074:
075: String arg0 = new String("TestSOAPInputPMessage");
076: objContext.setMessageObjects(arg0);
077:
078: binding.marshal(objContext, soapContext,
079: new JAXBDataBindingCallback(objContext.getMethod(),
080: DataBindingCallback.Mode.PARTS, null));
081: SOAPMessage msg = soapContext.getMessage();
082: assertNotNull(msg);
083:
084: assertTrue(msg.getSOAPBody().hasChildNodes());
085: NodeList list = msg.getSOAPBody().getChildNodes();
086: assertEquals(1, list.getLength());
087: Node wrappedNode = list.item(0).getFirstChild();
088: assertTrue(wrappedNode.hasChildNodes());
089: assertEquals(arg0, wrappedNode.getFirstChild().getNodeValue());
090: }
091:
092: public void testMarshalWrapDocLitMessageForInOutVar()
093: throws Exception {
094: //Test The InputMessage of GreetMe Operation
095: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
096: Method testInt = SOAPMessageUtil.getMethod(
097: TypeTestPortType.class, "testInt");
098: assertNotNull(testInt);
099: objContext.setMethod(testInt);
100:
101: Object[] methodArg = SOAPMessageUtil.getMessageObjects(testInt);
102: int arg0 = 5;
103: methodArg[0] = arg0;
104: //INOUT Variable
105: methodArg[1].getClass().getField("value").set(methodArg[1],
106: arg0);
107: objContext.setMessageObjects(methodArg);
108:
109: binding.marshal(objContext, soapContext,
110: new JAXBDataBindingCallback(objContext.getMethod(),
111: DataBindingCallback.Mode.PARTS, null));
112: SOAPMessage msg = soapContext.getMessage();
113: assertNotNull(msg);
114:
115: assertTrue(msg.getSOAPBody().hasChildNodes());
116: NodeList list = msg.getSOAPBody().getChildNodes();
117: assertEquals(1, list.getLength());
118: list = list.item(0).getChildNodes();
119: assertEquals(2, list.getLength());
120: Node wrappedNode = list.item(0);
121: assertEquals(String.valueOf(arg0), wrappedNode.getFirstChild()
122: .getNodeValue());
123:
124: wrappedNode = list.item(1);
125: assertEquals(String.valueOf(arg0), wrappedNode.getFirstChild()
126: .getNodeValue());
127: }
128:
129: public void testMarshalWrapDocLitOutputMessage() throws Exception {
130: //Test The Output of GreetMe Operation
131: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, true);
132: objContext.setMessageObjects(new Object[0]);
133:
134: String arg0 = new String("TestSOAPOutputMessage");
135: objContext.setReturn(arg0);
136:
137: binding.marshal(objContext, soapContext,
138: new JAXBDataBindingCallback(objContext.getMethod(),
139: DataBindingCallback.Mode.PARTS, null));
140: SOAPMessage msg = soapContext.getMessage();
141: assertNotNull(msg);
142: assertTrue(msg.getSOAPBody().hasChildNodes());
143: NodeList list = msg.getSOAPBody().getChildNodes();
144: assertEquals(1, list.getLength());
145: Node wrappedNode = list.item(0).getFirstChild();
146: assertTrue(wrappedNode.hasChildNodes());
147: assertEquals(arg0, wrappedNode.getFirstChild().getNodeValue());
148: }
149:
150: public void testParseWrapDocLitInputMessage() throws Exception {
151: //Test The InputMessage of GreetMe Operation
152: //Assumption the Wrapper element and the inner element are in the same namespace
153: //elementFormDefault is qualified
154:
155: QName wrapName = new QName(
156: "http://objectweb.org/hello_world_soap_http/types",
157: "greetMe");
158: QName elName = new QName(
159: "http://objectweb.org/hello_world_soap_http/types",
160: "requestType");
161: String data = new String("TestSOAPInputMessage");
162: String str = SOAPMessageUtil.createWrapDocLitSOAPMessage(
163: wrapName, elName, data);
164:
165: TestInputStreamContext inCtx = new TestInputStreamContext(str
166: .getBytes());
167: binding.read(inCtx, soapContext);
168:
169: SOAPMessage msg = soapContext.getMessage();
170: assertNotNull(msg);
171: assertTrue(msg.getSOAPBody().hasChildNodes());
172: NodeList list = msg.getSOAPBody().getChildNodes();
173:
174: assertEquals(1, list.getLength());
175: Node wrappedNode = list.item(0).getFirstChild();
176: assertTrue(wrappedNode.hasChildNodes());
177: assertEquals(data, wrappedNode.getFirstChild().getNodeValue());
178:
179: //Parse SOAP 1.2 message
180: InputStream is = getClass().getResourceAsStream(
181: "resources/Soap12message.xml");
182: inCtx.setInputStream(is);
183: try {
184: binding.read(inCtx, soapContext);
185: fail("Should have received a SOAP FaultException");
186: } catch (SOAPFaultException sfe) {
187: SOAPFault sf = sfe.getFault();
188: assertNotNull("Should have a non null soap fault", sf);
189: assertEquals(SOAPConstants.FAULTCODE_VERSIONMISMATCH, sf
190: .getFaultCodeAsQName());
191: }
192: }
193:
194: public void testUnmarshalWrapDocLitInputMessage() throws Exception {
195: //Test The InputMessage of GreetMe Operation
196: QName wrapName = new QName(
197: "http://objectweb.org/hello_world_soap_http/types",
198: "greetMe");
199: QName elName = new QName(
200: "http://objectweb.org/hello_world_soap_http/types",
201: "requestType");
202: String data = new String("TestSOAPInputMessage");
203: String str = SOAPMessageUtil.createWrapDocLitSOAPMessage(
204: wrapName, elName, data);
205: InputStream in = new ByteArrayInputStream(str.getBytes());
206: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
207:
208: assertNotNull(binding.getMessageFactory());
209: SOAPMessage soapMessage = binding.getMessageFactory()
210: .createMessage(null, in);
211: soapContext.setMessage(soapMessage);
212:
213: //GreetMe method has a IN parameter
214: objContext.setMessageObjects(new Object[1]);
215: binding.unmarshal(soapContext, objContext,
216: new JAXBDataBindingCallback(objContext.getMethod(),
217: DataBindingCallback.Mode.PARTS, null));
218:
219: Object[] params = objContext.getMessageObjects();
220: assertNotNull(params);
221: assertNull(objContext.getReturn());
222: assertEquals(1, params.length);
223: assertEquals(data, (String) params[0]);
224: }
225:
226: public void testUnmarshalWrapDocLitMessageWithInOutVar()
227: throws Exception {
228: //Test The testInt Operation of TypeTestPortType SEI
229: Method testInt = SOAPMessageUtil.getMethod(
230: TypeTestPortType.class, "testInt");
231: assertNotNull(testInt);
232: objContext.setMethod(testInt);
233:
234: InputStream is = getClass().getResourceAsStream(
235: "resources/TestIntDocLitTypeTestReq.xml");
236: assertNotNull(binding.getMessageFactory());
237: SOAPMessage faultMsg = binding.getMessageFactory()
238: .createMessage(null, is);
239: soapContext.setMessage(faultMsg);
240: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
241:
242: Object[] methodArg = SOAPMessageUtil.getMessageObjects(testInt);
243: assertNotNull(methodArg);
244: objContext.setMessageObjects(methodArg);
245:
246: binding.unmarshal(soapContext, objContext,
247: new JAXBDataBindingCallback(objContext.getMethod(),
248: DataBindingCallback.Mode.PARTS, null));
249:
250: assertNotNull(objContext.getMessageObjects());
251: methodArg = objContext.getMessageObjects();
252:
253: assertNull(objContext.getReturn());
254: assertEquals(3, methodArg.length);
255: assertEquals("5", String.valueOf(methodArg[0]));
256: }
257:
258: public void testUnmarshalWrapDocLiteralOutputMessage()
259: throws Exception {
260:
261: QName wrapName = new QName(
262: "http://objectweb.org/hello_world_soap_http/types",
263: "greetMeResponse");
264: QName elName = new QName(
265: "http://objectweb.org/hello_world_soap_http/types",
266: "responseType");
267: String data = new String("TestSOAPOutputMessage");
268: String str = SOAPMessageUtil.createWrapDocLitSOAPMessage(
269: wrapName, elName, data);
270: ByteArrayInputStream in = new ByteArrayInputStream(str
271: .getBytes());
272:
273: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, true);
274: assertNotNull(binding.getMessageFactory());
275: SOAPMessage soapMessage = binding.getMessageFactory()
276: .createMessage(null, in);
277: soapContext.setMessage(soapMessage);
278:
279: binding.unmarshal(soapContext, objContext,
280: new JAXBDataBindingCallback(objContext.getMethod(),
281: DataBindingCallback.Mode.PARTS, null));
282:
283: assertNull(objContext.getMessageObjects());
284: assertNotNull(objContext.getReturn());
285: assertEquals(data, (String) objContext.getReturn());
286: }
287:
288: public void testMarshalDocLiteralUserFaults() throws Exception {
289: //Test The InputMessage of GreetMe Operation
290: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
291:
292: String exMessage = new String("Test Exception");
293: ErrorCode ec = new ErrorCode();
294: ec.setMajor((short) 1);
295: ec.setMinor((short) 1);
296: NoSuchCodeLit nscl = new NoSuchCodeLit();
297: nscl.setCode(ec);
298: NoSuchCodeLitFault ex = new NoSuchCodeLitFault(exMessage, nscl);
299: objContext.setException(ex);
300:
301: binding.marshalFault(objContext, soapContext,
302: new JAXBDataBindingCallback(objContext.getMethod(),
303: DataBindingCallback.Mode.PARTS, null));
304: SOAPMessage msg = soapContext.getMessage();
305:
306: assertNotNull(msg);
307: Node xmlNode = msg.getSOAPBody();
308: assertNotNull(xmlNode);
309: assertEquals(1, xmlNode.getChildNodes().getLength());
310: assertTrue(msg.getSOAPBody().hasFault());
311: SOAPFault fault = msg.getSOAPBody().getFault();
312: assertNotNull(fault);
313: assertEquals(getExceptionString(ex, exMessage), fault
314: .getFaultString());
315: assertTrue(fault.hasChildNodes());
316: Detail detail = fault.getDetail();
317: assertNotNull(detail);
318:
319: NodeList list = detail.getChildNodes();
320: assertEquals(1, list.getLength());
321:
322: WebFault wfAnnotation = ex.getClass().getAnnotation(
323: WebFault.class);
324: assertEquals(wfAnnotation.targetNamespace(), list.item(0)
325: .getNamespaceURI());
326: assertEquals(wfAnnotation.name(), list.item(0).getLocalName());
327: }
328:
329: public void testMarshalSystemFaults() throws Exception {
330: //Test The InputMessage of GreetMe Operation
331: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
332:
333: SOAPException se = new SOAPException("SAAJ Exception");
334: objContext.setException(se);
335:
336: binding.marshalFault(objContext, soapContext,
337: new JAXBDataBindingCallback(objContext.getMethod(),
338: DataBindingCallback.Mode.PARTS, null));
339: SOAPMessage msg = soapContext.getMessage();
340:
341: assertNotNull(msg);
342: Node xmlNode = msg.getSOAPBody();
343: assertNotNull(xmlNode);
344: assertEquals(1, xmlNode.getChildNodes().getLength());
345:
346: assertTrue(msg.getSOAPBody().hasFault());
347: SOAPFault fault = msg.getSOAPBody().getFault();
348: assertNotNull(fault);
349: assertEquals(getExceptionString(se, se.getMessage()), fault
350: .getFaultString());
351: assertTrue(fault.hasChildNodes());
352: NodeList list = fault.getChildNodes();
353: assertEquals(2, list.getLength());
354: }
355:
356: public void testUnmarshalDocLiteralUserFaults() throws Exception {
357: //Test The InputMessage of GreetMe Operation
358: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, true);
359: objContext.setMethod(SOAPMessageUtil.getMethod(Greeter.class,
360: "testDocLitFault"));
361:
362: InputStream is = getClass().getResourceAsStream(
363: "resources/NoSuchCodeDocLiteral.xml");
364: SOAPMessage faultMsg = binding.getMessageFactory()
365: .createMessage(null, is);
366: soapContext.setMessage(faultMsg);
367:
368: binding.unmarshalFault(soapContext, objContext,
369: new JAXBDataBindingCallback(objContext.getMethod(),
370: DataBindingCallback.Mode.PARTS, null));
371: assertNotNull(objContext.getException());
372: Object faultEx = objContext.getException();
373: assertTrue(NoSuchCodeLitFault.class.isAssignableFrom(faultEx
374: .getClass()));
375: NoSuchCodeLitFault nscf = (NoSuchCodeLitFault) faultEx;
376: assertNotNull(nscf.getFaultInfo());
377: NoSuchCodeLit faultInfo = nscf.getFaultInfo();
378:
379: assertNotNull(faultInfo.getCode());
380: ErrorCode ec = faultInfo.getCode();
381: assertEquals(ec.getMajor(), (short) 666);
382: assertEquals(ec.getMinor(), (short) 999);
383:
384: assertEquals(nscf.getMessage(), "Test Exception");
385:
386: is = getClass().getResourceAsStream(
387: "resources/BadRecordDocLiteral.xml");
388: faultMsg = binding.getMessageFactory().createMessage(null, is);
389: soapContext.setMessage(faultMsg);
390: binding.unmarshalFault(soapContext, objContext,
391: new JAXBDataBindingCallback(objContext.getMethod(),
392: DataBindingCallback.Mode.PARTS, null));
393: assertNotNull(objContext.getException());
394: faultEx = objContext.getException();
395: assertTrue(BadRecordLitFault.class.isAssignableFrom(faultEx
396: .getClass()));
397: BadRecordLitFault brlf = (BadRecordLitFault) faultEx;
398: assertEquals(brlf.getFaultInfo(), "BadRecordTested");
399:
400: is = getClass()
401: .getResourceAsStream("resources/SystemFault.xml");
402: faultMsg = binding.getMessageFactory().createMessage(null, is);
403: soapContext.setMessage(faultMsg);
404: binding.unmarshalFault(soapContext, objContext,
405: new JAXBDataBindingCallback(objContext.getMethod(),
406: DataBindingCallback.Mode.PARTS, null));
407: assertNotNull(objContext.getException());
408: faultEx = objContext.getException();
409: assertTrue("Should be a SOAPFaultException",
410: SOAPFaultException.class.isAssignableFrom(faultEx
411: .getClass()));
412: SOAPFaultException sfe = (SOAPFaultException) faultEx;
413: SOAPFault sf = sfe.getFault();
414: assertNotNull(sf);
415: }
416:
417: //Bare Doc Literal Tests
418: public void testMarshalBareDocLitInputMessage() throws Exception {
419: //Test The InputMessage of testDocLitBare Operation
420: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
421: objContext.setMethod(SOAPMessageUtil.getMethod(Greeter.class,
422: "testDocLitBare"));
423:
424: String arg0 = new String("DocLitBareDocumentInputMessage");
425: objContext.setMessageObjects(arg0);
426:
427: binding.marshal(objContext, soapContext,
428: new JAXBDataBindingCallback(objContext.getMethod(),
429: DataBindingCallback.Mode.PARTS, null));
430: SOAPMessage msg = soapContext.getMessage();
431: assertNotNull(msg);
432: //msg.writeTo(System.out);
433: assertTrue(msg.getSOAPBody().hasChildNodes());
434: NodeList list = msg.getSOAPBody().getChildNodes();
435: assertEquals(1, list.getLength());
436: Node msgnode = list.item(0).getFirstChild();
437: assertTrue(!msgnode.hasChildNodes());
438: assertEquals(arg0, msgnode.getNodeValue());
439: }
440:
441: public void testUnmarshalBareDocLitInputMessage() throws Exception {
442: //Test The InputMessage of testDocLitBare Operation
443: objContext.setMethod(SOAPMessageUtil.getMethod(Greeter.class,
444: "testDocLitBare"));
445: QName elName = new QName(
446: "http://objectweb.org/hello_world_soap_http/types",
447: "BareDocument");
448:
449: String data = new String("DocLitBareDocumentInputMessage");
450: String str = SOAPMessageUtil.createBareDocLitSOAPMessage(
451: elName, data);
452:
453: ByteArrayInputStream in = new ByteArrayInputStream(str
454: .getBytes());
455: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
456:
457: assertNotNull(binding.getMessageFactory());
458: SOAPMessage soapMessage = binding.getMessageFactory()
459: .createMessage(null, in);
460: soapContext.setMessage(soapMessage);
461:
462: //testDocLitBare method has a IN parameter
463: objContext.setMessageObjects(new Object[1]);
464: binding.unmarshal(soapContext, objContext,
465: new JAXBDataBindingCallback(objContext.getMethod(),
466: DataBindingCallback.Mode.PARTS, null));
467:
468: Object[] params = objContext.getMessageObjects();
469: assertNotNull(params);
470: assertNull(objContext.getReturn());
471: assertEquals(1, params.length);
472: assertEquals(data, (String) params[0]);
473: }
474:
475: public void testMarshalEmptyBody() throws Exception {
476:
477: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
478:
479: binding.marshal(objContext, soapContext, null);
480: SOAPMessage msg = soapContext.getMessage();
481: assertNotNull(msg);
482: assertTrue(!msg.getSOAPBody().hasChildNodes());
483:
484: TestOutputStreamContext ostreamCtx = new TestOutputStreamContext(
485: null, soapContext);
486:
487: binding.write(soapContext, ostreamCtx);
488:
489: }
490:
491: public void testUnmarshalEmptyBody() throws Exception {
492: TestInputStreamContext inCtx = new TestInputStreamContext(null);
493: InputStream is = getClass().getResourceAsStream(
494: "resources/EmptyBody.xml");
495: inCtx.setInputStream(is);
496: binding.read(inCtx, soapContext);
497:
498: SOAPMessage msg = soapContext.getMessage();
499: assertNotNull(msg);
500: assertTrue(!msg.getSOAPBody().hasChildNodes());
501:
502: assertNull(objContext.getMessageObjects());
503: assertNull(objContext.getReturn());
504: binding.unmarshal(soapContext, objContext, null);
505: assertNull(objContext.getMessageObjects());
506: assertNull(objContext.getReturn());
507: }
508:
509: private String getExceptionString(Exception ex, String faultString) {
510: StringBuffer str = new StringBuffer();
511: if (ex != null) {
512: str.append(ex.getClass().getName());
513: str.append(": ");
514: }
515: str.append(faultString);
516:
517: if (!ex.getClass().isAnnotationPresent(WebFault.class)) {
518: str.append("\n");
519: for (StackTraceElement s : ex.getStackTrace()) {
520: str.append(s.toString());
521: str.append("\n");
522: }
523: }
524: return str.toString();
525: }
526: }
|