001: package org.objectweb.celtix.bus.jaxws.io;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005:
006: import javax.xml.bind.JAXBContext;
007: import javax.xml.soap.MessageFactory;
008: import javax.xml.soap.SOAPBody;
009: import javax.xml.soap.SOAPException;
010: import javax.xml.soap.SOAPMessage;
011: import javax.xml.transform.dom.DOMSource;
012: import javax.xml.transform.sax.SAXSource;
013: import javax.xml.transform.stream.StreamSource;
014:
015: import org.xml.sax.InputSource;
016:
017: import junit.framework.TestCase;
018:
019: import org.objectweb.celtix.bindings.DataBindingCallback.Mode;
020: import org.objectweb.celtix.bus.jaxws.DynamicDataBindingCallback;
021: import org.objectweb.hello_world_soap_http.types.GreetMe;
022:
023: public class SOAPBodyDataWriterTest<T> extends TestCase {
024:
025: private SOAPMessage soapMsg;
026: private InputStream is;
027: private InputStream is2;
028: private InputStream is3;
029: private Object obj;
030: private DOMSource domSource;
031: private SAXSource saxSource;
032: private StreamSource streamSource;
033:
034: public void setUp() throws IOException, SOAPException {
035: is = getClass().getResourceAsStream("GreetMeDocLiteralReq.xml");
036: is2 = getClass().getResourceAsStream(
037: "GreetMeDocLiteralSOAPBodyReq.xml");
038: is3 = getClass().getResourceAsStream(
039: "GreetMeDocLiteralSOAPBodyReq.xml");
040: SOAPMessage msg = MessageFactory.newInstance().createMessage(
041: null, is);
042: domSource = new DOMSource(msg.getSOAPBody()
043: .extractContentAsDocument());
044: saxSource = new SAXSource(new InputSource(is2));
045: streamSource = new StreamSource(is3);
046: soapMsg = MessageFactory.newInstance().createMessage();
047: assertNotNull(soapMsg);
048: }
049:
050: public void tearDown() throws IOException {
051: //is.close();
052: }
053:
054: public void testDOMSourceWrite() throws Exception {
055: TestDynamicDataBindingCallback callback = new TestDynamicDataBindingCallback(
056: DOMSource.class, Mode.PAYLOAD);
057: TestSOAPBodyDataWriter<SOAPBody> soapBodyDataWriter = new TestSOAPBodyDataWriter<SOAPBody>(
058: callback);
059: obj = domSource;
060: soapBodyDataWriter.write(obj, soapMsg.getSOAPBody());
061: SOAPBody soapBody = soapBodyDataWriter.getTestSOAPBody();
062: assertTrue("TextContent should be TestSOAPInputMessage",
063: "TestSOAPInputMessage".equals(soapBody.getFirstChild()
064: .getTextContent()));
065:
066: }
067:
068: public void testSAXSourceWrite() throws Exception {
069: TestDynamicDataBindingCallback callback = new TestDynamicDataBindingCallback(
070: SAXSource.class, Mode.PAYLOAD);
071: TestSOAPBodyDataWriter<SOAPBody> soapBodyDataWriter = new TestSOAPBodyDataWriter<SOAPBody>(
072: callback);
073: obj = saxSource;
074: soapBodyDataWriter.write(obj, soapMsg.getSOAPBody());
075: SOAPBody soapBody = soapBodyDataWriter.getTestSOAPBody();
076: assertTrue("TextContent should be TestSOAPInputMessage",
077: "TestSOAPInputMessage".equals(soapBody.getFirstChild()
078: .getTextContent()));
079:
080: }
081:
082: public void testStreamSourceWrite() throws Exception {
083: TestDynamicDataBindingCallback callback = new TestDynamicDataBindingCallback(
084: StreamSource.class, Mode.PAYLOAD);
085: TestSOAPBodyDataWriter<SOAPBody> soapBodyDataWriter = new TestSOAPBodyDataWriter<SOAPBody>(
086: callback);
087: obj = streamSource;
088: soapBodyDataWriter.write(obj, soapMsg.getSOAPBody());
089: SOAPBody soapBody = soapBodyDataWriter.getTestSOAPBody();
090: assertTrue("TextContent should be TestSOAPInputMessage",
091: "TestSOAPInputMessage".equals(soapBody.getFirstChild()
092: .getTextContent()));
093:
094: }
095:
096: public void testJAXBObjectWrite() throws Exception {
097:
098: JAXBContext jc = JAXBContext
099: .newInstance("org.objectweb.hello_world_soap_http.types");
100: TestDynamicDataBindingCallback callback = new TestDynamicDataBindingCallback(
101: jc, Mode.PAYLOAD);
102: TestSOAPBodyDataWriter<SOAPBody> soapBodyDataWriter = new TestSOAPBodyDataWriter<SOAPBody>(
103: callback);
104: GreetMe greetMe = new GreetMe();
105: greetMe.setRequestType("DIPLO");
106: soapBodyDataWriter.write(greetMe, soapMsg.getSOAPBody());
107: SOAPBody soapBody = soapBodyDataWriter.getTestSOAPBody();
108: assertTrue("TextContent should be DIPLO", "DIPLO"
109: .equals(soapBody.getFirstChild().getTextContent()));
110:
111: }
112:
113: private class TestSOAPBodyDataWriter<X> extends
114: SOAPBodyDataWriter<X> {
115:
116: public TestSOAPBodyDataWriter(DynamicDataBindingCallback cb) {
117: super (cb);
118: }
119:
120: public SOAPBody getTestSOAPBody() {
121: return dest;
122: }
123:
124: }
125:
126: }
|