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.jws.WebService;
008: import javax.xml.namespace.QName;
009: import javax.xml.soap.SOAPMessage;
010:
011: import org.w3c.dom.Node;
012: import org.w3c.dom.NodeList;
013:
014: import junit.framework.TestCase;
015:
016: import org.objectweb.celtix.bindings.DataBindingCallback;
017: import org.objectweb.celtix.bus.bindings.TestInputStreamContext;
018: import org.objectweb.celtix.bus.jaxws.JAXBDataBindingCallback;
019: import org.objectweb.celtix.context.GenericMessageContext;
020: import org.objectweb.celtix.context.ObjectMessageContext;
021: import org.objectweb.celtix.context.ObjectMessageContextImpl;
022: import org.objectweb.hello_world_rpclit.GreeterRPCLit;
023:
024: public class SoapBindingImplRPCLitTest extends TestCase {
025:
026: private SOAPBindingImpl binding;
027: private ObjectMessageContextImpl objContext;
028: private SOAPMessageContextImpl soapContext;
029:
030: public SoapBindingImplRPCLitTest(String arg0) {
031: super (arg0);
032: }
033:
034: /**
035: * @param args
036: */
037: public static void main(String[] args) {
038: junit.textui.TestRunner.run(SoapBindingImplRPCLitTest.class);
039: }
040:
041: protected void setUp() throws Exception {
042: super .setUp();
043:
044: binding = new SOAPBindingImpl(false);
045: objContext = new ObjectMessageContextImpl();
046: soapContext = new SOAPMessageContextImpl(
047: new GenericMessageContext());
048:
049: Method greetMe = SOAPMessageUtil.getMethod(GreeterRPCLit.class,
050: "greetMe");
051: objContext.setMethod(greetMe);
052: }
053:
054: public void testMarshalRPCLitInputMessage() throws Exception {
055: //Test The InputMessage of GreetMe Operation
056: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
057:
058: String arg0 = new String("TestSOAPInputPMessage");
059: objContext.setMessageObjects(arg0);
060:
061: binding.marshal(objContext, soapContext,
062: new JAXBDataBindingCallback(objContext.getMethod(),
063: DataBindingCallback.Mode.PARTS, null));
064: SOAPMessage msg = soapContext.getMessage();
065: assertNotNull(msg);
066: assertTrue(msg.getSOAPBody().hasChildNodes());
067: NodeList list = msg.getSOAPBody().getChildNodes();
068: assertEquals(1, list.getLength());
069: Node operationNode = list.item(0);
070: assertEquals(objContext.getMethod().getName(), operationNode
071: .getLocalName());
072:
073: WebService wsAnnotation = objContext.getMethod()
074: .getDeclaringClass().getAnnotation(WebService.class);
075: String expectedNameSpace = wsAnnotation.targetNamespace();
076: assertTrue(expectedNameSpace.equals(operationNode
077: .getNamespaceURI()));
078:
079: assertTrue(operationNode.hasChildNodes());
080: assertEquals(arg0, operationNode.getFirstChild()
081: .getFirstChild().getNodeValue());
082: }
083:
084: public void testMarshalRPCLitOutputMessage() throws Exception {
085: //Test The Output of GreetMe Operation
086: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, true);
087:
088: String arg0 = new String("TestSOAPOutputMessage");
089: objContext.setReturn(arg0);
090:
091: binding.marshal(objContext, soapContext,
092: new JAXBDataBindingCallback(objContext.getMethod(),
093: DataBindingCallback.Mode.PARTS, null));
094: SOAPMessage msg = soapContext.getMessage();
095: assertNotNull(msg);
096: assertTrue(msg.getSOAPBody().hasChildNodes());
097: NodeList list = msg.getSOAPBody().getChildNodes();
098: assertEquals(1, list.getLength());
099: Node operationNode = list.item(0);
100: assertEquals(objContext.getMethod().getName() + "Response",
101: operationNode.getLocalName());
102:
103: WebService wsAnnotation = objContext.getMethod()
104: .getDeclaringClass().getAnnotation(WebService.class);
105: String expectedNameSpace = wsAnnotation.targetNamespace();
106: assertTrue(expectedNameSpace.equals(operationNode
107: .getNamespaceURI()));
108:
109: assertTrue(operationNode.hasChildNodes());
110: assertEquals(arg0, operationNode.getFirstChild()
111: .getFirstChild().getNodeValue());
112: }
113:
114: public void testParseRPCLitInputMessage() throws Exception {
115:
116: QName opName = new QName(
117: "http://objectweb.org/hello_world_rpclit", "greetMe");
118: QName elName = new QName(
119: "http://objectweb.org/hello_world_rpclit", "in");
120: String data = new String("TestSOAPInputMessage");
121: String str = SOAPMessageUtil.createRPCLitSOAPMessage(opName,
122: elName, data);
123:
124: TestInputStreamContext inCtx = new TestInputStreamContext(str
125: .getBytes());
126: binding.read(inCtx, soapContext);
127:
128: SOAPMessage msg = soapContext.getMessage();
129: assertNotNull(msg);
130: assertTrue(msg.getSOAPBody().hasChildNodes());
131: NodeList list = msg.getSOAPBody().getChildNodes();
132:
133: assertEquals(1, list.getLength());
134: Node operationNode = list.item(0);
135: assertEquals(objContext.getMethod().getName(), operationNode
136: .getLocalName());
137: assertTrue(operationNode.hasChildNodes());
138: assertEquals(data, operationNode.getFirstChild()
139: .getFirstChild().getNodeValue());
140: }
141:
142: public void testUnmarshalRPCLitInputMessage() throws Exception {
143: //Test The InputMessage of GreetMe Operation
144: InputStream is = getClass().getResourceAsStream(
145: "resources/GreetMeRpcLiteralReq.xml");
146: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
147:
148: assertNotNull(binding.getMessageFactory());
149: SOAPMessage soapMessage = binding.getMessageFactory()
150: .createMessage(null, is);
151: soapContext.setMessage(soapMessage);
152: //GreetMe has a IN parameter
153: objContext.setMessageObjects(new Object[] { null });
154:
155: binding.unmarshal(soapContext, objContext,
156: new JAXBDataBindingCallback(objContext.getMethod(),
157: DataBindingCallback.Mode.PARTS, null));
158:
159: assertNull(objContext.getReturn());
160: Object[] params = objContext.getMessageObjects();
161: assertNotNull(params);
162: assertEquals(1, params.length);
163: assertEquals("TestSOAPRPCLitInputMessage", (String) params[0]);
164: }
165:
166: public void testUnmarshalRPCLitOutputMessage() throws Exception {
167: //Test The InputMessage of GreetMe Operation
168: QName opName = new QName(
169: "http://objectweb.org/hello_world_rpclit", "greetMe");
170: QName elName = new QName(
171: "http://objectweb.org/hello_world_rpclit", "out");
172: String data = new String("TestSOAPOutputMessage");
173: String str = SOAPMessageUtil.createRPCLitSOAPMessage(opName,
174: elName, data);
175: ByteArrayInputStream in = new ByteArrayInputStream(str
176: .getBytes());
177:
178: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, true);
179: assertNotNull(binding.getMessageFactory());
180: SOAPMessage soapMessage = binding.getMessageFactory()
181: .createMessage(null, in);
182: soapContext.setMessage(soapMessage);
183:
184: binding.unmarshal(soapContext, objContext,
185: new JAXBDataBindingCallback(objContext.getMethod(),
186: DataBindingCallback.Mode.PARTS, null));
187:
188: assertNull(objContext.getMessageObjects());
189: assertNotNull(objContext.getReturn());
190: assertEquals(data, (String) objContext.getReturn());
191: }
192:
193: }
|