001: package org.objectweb.celtix.bus.bindings.soap;
002:
003: import java.io.InputStream;
004: import java.lang.reflect.Method;
005:
006: import javax.xml.soap.SOAPMessage;
007: import javax.xml.ws.Holder;
008:
009: import org.w3c.dom.Element;
010: import org.w3c.dom.NodeList;
011: import org.w3c.dom.Text;
012:
013: import junit.framework.TestCase;
014:
015: import org.objectweb.celtix.bindings.DataBindingCallback;
016: import org.objectweb.celtix.bus.jaxws.JAXBDataBindingCallback;
017: import org.objectweb.celtix.context.ObjectMessageContext;
018: import org.objectweb.celtix.context.ObjectMessageContextImpl;
019: import org.objectweb.header_test.TestHeader;
020: import org.objectweb.header_test.types.TestHeader1;
021: import org.objectweb.header_test.types.TestHeader2Response;
022: import org.objectweb.header_test.types.TestHeader3;
023: import org.objectweb.header_test.types.TestHeader5;
024:
025: public class SoapBindingImplHeaderTest extends TestCase {
026: private SOAPBindingImpl binding;
027: private ObjectMessageContextImpl objContext;
028: private SOAPMessageContextImpl soapContext;
029:
030: public SoapBindingImplHeaderTest(String arg0) {
031: super (arg0);
032: }
033:
034: public static void main(String[] args) {
035: junit.textui.TestRunner.run(SoapBindingImplHeaderTest.class);
036: }
037:
038: protected void setUp() throws Exception {
039: super .setUp();
040:
041: binding = new SOAPBindingImpl(false);
042: objContext = new ObjectMessageContextImpl();
043: soapContext = new SOAPMessageContextImpl(objContext);
044: }
045:
046: public void testMarshalHeaderDocLitInputMessage() throws Exception {
047: //Test The InputMessage of testHeader1 Operation
048: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
049: Method testHeader1 = SOAPMessageUtil.getMethod(
050: TestHeader.class, "testHeader1");
051: assertNotNull(testHeader1);
052: objContext.setMethod(testHeader1);
053: Object arg = new TestHeader1();
054: objContext.setMessageObjects(arg, arg);
055:
056: binding.marshal(objContext, soapContext,
057: new JAXBDataBindingCallback(testHeader1,
058: DataBindingCallback.Mode.PARTS, null));
059: SOAPMessage msg = soapContext.getMessage();
060:
061: assertNotNull(msg);
062: //Test the Header Part Only
063: assertNotNull(msg.getSOAPHeader());
064: assertTrue(msg.getSOAPHeader().hasChildNodes());
065: NodeList list = msg.getSOAPHeader().getChildNodes();
066: assertEquals(1, list.getLength());
067: Element headerElement = (Element) list.item(0);
068: assertEquals("true", headerElement.getAttributeNS(
069: SOAPConstants.HEADER_MUSTUNDERSTAND.getNamespaceURI(),
070: SOAPConstants.HEADER_MUSTUNDERSTAND.getLocalPart()));
071: //TestHeader1 has no child elements.
072: assertFalse(headerElement.hasChildNodes());
073:
074: //TestHeader3 InOutHeader
075: Method testHeader3 = SOAPMessageUtil.getMethod(
076: TestHeader.class, "testHeader3");
077: assertNotNull(testHeader3);
078: objContext.setMethod(testHeader3);
079: TestHeader3 arg0 = new TestHeader3();
080: TestHeader3 arg1 = new TestHeader3();
081: arg1.setRequestType("HeaderVal");
082: Object[] args = SOAPMessageUtil.getMessageObjects(testHeader3,
083: arg0, arg1);
084: objContext.setMessageObjects(args);
085: //Write soap headers for testHeader3 operation - tests inout headers
086: binding.marshal(objContext, soapContext,
087: new JAXBDataBindingCallback(testHeader3,
088: DataBindingCallback.Mode.PARTS, null));
089:
090: msg = soapContext.getMessage();
091: assertNotNull(msg);
092:
093: //Test the Header Part Only
094: assertNotNull(msg.getSOAPHeader());
095: assertTrue(msg.getSOAPHeader().hasChildNodes());
096: list = msg.getSOAPHeader().getChildNodes();
097: assertEquals(1, list.getLength());
098: headerElement = (Element) list.item(0);
099: //TestHeader3 has no child elements
100: assertTrue(headerElement.hasChildNodes());
101: list = headerElement.getChildNodes();
102: assertEquals(1, list.getLength());
103: headerElement = (Element) list.item(0);
104: assertTrue(headerElement.hasChildNodes());
105: list = headerElement.getChildNodes();
106: assertEquals(1, list.getLength());
107: Text text = (Text) list.item(0);
108: assertEquals(arg1.getRequestType(), text.getData());
109: }
110:
111: public void testMarshalHeaderDocLitOutputMessage() throws Exception {
112: //Test The InputMessage of testHeader1 Operation
113: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, true);
114: Method testHeader2 = SOAPMessageUtil.getMethod(
115: TestHeader.class, "testHeader2");
116: assertNotNull(testHeader2);
117: objContext.setMethod(testHeader2);
118: TestHeader2Response arg0 = new TestHeader2Response();
119: arg0.setResponseType("BodyVal2");
120: TestHeader2Response arg1 = new TestHeader2Response();
121: arg1.setResponseType("HeaderVal2");
122:
123: Object[] args = SOAPMessageUtil.getMessageObjects(testHeader2,
124: null, arg0, arg1);
125: objContext.setMessageObjects(args);
126: //Write the SOAP Headers for testHeader2 operation - tests out headers
127: binding.marshal(objContext, soapContext,
128: new JAXBDataBindingCallback(testHeader2,
129: DataBindingCallback.Mode.PARTS, null));
130:
131: SOAPMessage msg = soapContext.getMessage();
132: assertNotNull(msg);
133:
134: //Test the Header Part Only
135: assertNotNull(msg.getSOAPHeader());
136: assertTrue(msg.getSOAPHeader().hasChildNodes());
137: NodeList list = msg.getSOAPHeader().getChildNodes();
138: assertEquals(1, list.getLength());
139: Element headerElement = (Element) list.item(0);
140: //Check for mustUndrstand Attribute
141: assertEquals("true", headerElement.getAttributeNS(
142: SOAPConstants.HEADER_MUSTUNDERSTAND.getNamespaceURI(),
143: SOAPConstants.HEADER_MUSTUNDERSTAND.getLocalPart()));
144:
145: //TestHeader3 has child elements.
146: assertTrue(headerElement.hasChildNodes());
147: list = headerElement.getChildNodes();
148: assertEquals(1, list.getLength());
149: assertEquals(arg1.getResponseType(), list.item(0)
150: .getFirstChild().getNodeValue());
151:
152: //TestHeader5 return Header
153: Method testHeader5 = SOAPMessageUtil.getMethod(
154: TestHeader.class, "testHeader5");
155: assertNotNull(testHeader5);
156: objContext.setMethod(testHeader5);
157:
158: TestHeader5 arg2 = new TestHeader5();
159: arg2.setRequestType("HeaderVal5");
160:
161: objContext.setMessageObjects(new Object[0]);
162: objContext.setReturn(arg2);
163: //Write the SOAP Headers for testHeader5 operation - tests headers as return.
164: binding.marshal(objContext, soapContext,
165: new JAXBDataBindingCallback(testHeader5,
166: DataBindingCallback.Mode.PARTS, null));
167:
168: msg = soapContext.getMessage();
169: assertNotNull(msg);
170:
171: //Test the Header Part Only
172: assertNotNull(msg.getSOAPHeader());
173: assertTrue(msg.getSOAPHeader().hasChildNodes());
174: list = msg.getSOAPHeader().getChildNodes();
175: assertEquals(1, list.getLength());
176: headerElement = (Element) list.item(0);
177: //Check for mustUndrstand Attribute
178: assertEquals("true", headerElement.getAttributeNS(
179: SOAPConstants.HEADER_MUSTUNDERSTAND.getNamespaceURI(),
180: SOAPConstants.HEADER_MUSTUNDERSTAND.getLocalPart()));
181:
182: //TestHeader5 has child elements.
183: assertTrue(headerElement.hasChildNodes());
184: list = headerElement.getChildNodes();
185: assertEquals(1, list.getLength());
186: assertEquals(arg2.getRequestType(), list.item(0)
187: .getFirstChild().getNodeValue());
188:
189: }
190:
191: public void testUnmarshalHeaderDocLitInputMessage()
192: throws Exception {
193:
194: Method testHeader3 = SOAPMessageUtil.getMethod(
195: TestHeader.class, "testHeader3");
196: assertNotNull(testHeader3);
197: objContext.setMethod(testHeader3);
198: objContext.setMessageObjects(SOAPMessageUtil
199: .getMessageObjects(testHeader3));
200:
201: InputStream is = getClass().getResourceAsStream(
202: "resources/TestHeader3DocLitReq.xml");
203: assertNotNull(binding.getMessageFactory());
204: SOAPMessage headerMsg = binding.getMessageFactory()
205: .createMessage(null, is);
206: assertNotNull(headerMsg);
207: soapContext.setMessage(headerMsg);
208: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, false);
209:
210: //Test The InputMessage of testHeader3 Operation
211: binding.unmarshal(soapContext, objContext,
212: new JAXBDataBindingCallback(testHeader3,
213: DataBindingCallback.Mode.PARTS, null));
214:
215: //Read the headers of testHeader3 Operation - tests inout headers
216: Object[] params = objContext.getMessageObjects();
217: assertNotNull(params);
218: assertNull(objContext.getReturn());
219: assertEquals(2, params.length);
220: assertTrue(params[1].getClass().isAssignableFrom(Holder.class));
221: Holder<?> holder = (Holder<?>) params[1];
222: assertNotNull(holder.value);
223: assertTrue(holder.value.getClass().isAssignableFrom(
224: TestHeader3.class));
225: TestHeader3 header3 = (TestHeader3) holder.value;
226: assertNull(header3.getRequestType());
227: }
228:
229: public void testUnmarshalHeaderDocLitOutputMessage()
230: throws Exception {
231:
232: Method testHeader2 = SOAPMessageUtil.getMethod(
233: TestHeader.class, "testHeader2");
234: assertNotNull(testHeader2);
235: objContext.setMethod(testHeader2);
236: objContext.setMessageObjects(SOAPMessageUtil
237: .getMessageObjects(testHeader2));
238:
239: InputStream is = getClass().getResourceAsStream(
240: "resources/TestHeader2DocLitResp.xml");
241: assertNotNull(binding.getMessageFactory());
242: SOAPMessage headerMsg = binding.getMessageFactory()
243: .createMessage(null, is);
244: soapContext.setMessage(headerMsg);
245: soapContext.put(ObjectMessageContext.MESSAGE_INPUT, true);
246:
247: binding.unmarshal(soapContext, objContext,
248: new JAXBDataBindingCallback(testHeader2,
249: DataBindingCallback.Mode.PARTS, null));
250:
251: //Read the SOAP Headers
252: Object[] params = objContext.getMessageObjects();
253: assertNotNull(params);
254: assertNull(objContext.getReturn());
255: assertEquals(3, params.length);
256: //Test the Header method paramaters
257: assertTrue(params[2].getClass().isAssignableFrom(Holder.class));
258: Holder<?> holder = (Holder<?>) params[2];
259: assertNotNull(holder.value);
260: assertTrue(holder.value.getClass().isAssignableFrom(
261: TestHeader2Response.class));
262: TestHeader2Response header2 = (TestHeader2Response) holder.value;
263: assertEquals("HeaderVal2", header2.getResponseType());
264:
265: //test for return header using TestHeader5 operation
266: Method testHeader5 = SOAPMessageUtil.getMethod(
267: TestHeader.class, "testHeader5");
268: assertNotNull(testHeader5);
269: objContext.setMethod(testHeader5);
270: objContext.setMessageObjects(SOAPMessageUtil
271: .getMessageObjects(testHeader5));
272:
273: is = getClass().getResourceAsStream(
274: "resources/TestHeader5DocLitResp.xml");
275: headerMsg = binding.getMessageFactory().createMessage(null, is);
276: soapContext.setMessage(headerMsg);
277:
278: //Test The InputMessage of testHeader3 Operation
279: binding.unmarshal(soapContext, objContext,
280: new JAXBDataBindingCallback(testHeader5,
281: DataBindingCallback.Mode.PARTS, null));
282:
283: params = objContext.getMessageObjects();
284: assertNotNull(params);
285: assertEquals(1, params.length);
286:
287: //Test the Header method paramaters
288: assertNotNull(objContext.getReturn());
289: TestHeader5 header5 = (TestHeader5) objContext.getReturn();
290: assertEquals("HeaderVal5", header5.getRequestType());
291:
292: }
293: }
|