1 package org.objectweb.celtix.bus.jaxws.io; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 6 import javax.xml.bind.JAXBContext; 7 import javax.xml.soap.MessageFactory ; 8 import javax.xml.soap.SOAPBody ; 9 import javax.xml.soap.SOAPException ; 10 import javax.xml.soap.SOAPMessage ; 11 import javax.xml.transform.dom.DOMSource ; 12 import javax.xml.transform.sax.SAXSource ; 13 import javax.xml.transform.stream.StreamSource ; 14 15 import org.xml.sax.InputSource ; 16 17 import junit.framework.TestCase; 18 19 import org.objectweb.celtix.bindings.DataBindingCallback.Mode; 20 import org.objectweb.celtix.bus.jaxws.DynamicDataBindingCallback; 21 import org.objectweb.hello_world_soap_http.types.GreetMe; 22 23 public class SOAPBodyDataWriterTest<T> extends TestCase { 24 25 26 private SOAPMessage soapMsg; 27 private InputStream is; 28 private InputStream is2; 29 private InputStream is3; 30 private Object obj; 31 private DOMSource domSource; 32 private SAXSource saxSource; 33 private StreamSource streamSource; 34 35 public void setUp() throws IOException , SOAPException { 36 is = getClass().getResourceAsStream("GreetMeDocLiteralReq.xml"); 37 is2 = getClass().getResourceAsStream("GreetMeDocLiteralSOAPBodyReq.xml"); 38 is3 = getClass().getResourceAsStream("GreetMeDocLiteralSOAPBodyReq.xml"); 39 SOAPMessage msg = MessageFactory.newInstance().createMessage(null, is); 40 domSource = new DOMSource (msg.getSOAPBody().extractContentAsDocument()); 41 saxSource = new SAXSource (new InputSource (is2)); 42 streamSource = new StreamSource (is3); 43 soapMsg = MessageFactory.newInstance().createMessage(); 44 assertNotNull(soapMsg); 45 } 46 47 public void tearDown() throws IOException { 48 } 50 51 public void testDOMSourceWrite() throws Exception { 52 TestDynamicDataBindingCallback callback = 53 new TestDynamicDataBindingCallback(DOMSource .class, Mode.PAYLOAD); 54 TestSOAPBodyDataWriter<SOAPBody > soapBodyDataWriter = new TestSOAPBodyDataWriter<SOAPBody >(callback); 55 obj = domSource; 56 soapBodyDataWriter.write(obj, soapMsg.getSOAPBody()); 57 SOAPBody soapBody = soapBodyDataWriter.getTestSOAPBody(); 58 assertTrue("TextContent should be TestSOAPInputMessage", 59 "TestSOAPInputMessage".equals(soapBody.getFirstChild().getTextContent())); 60 61 } 62 63 public void testSAXSourceWrite() throws Exception { 64 TestDynamicDataBindingCallback callback = 65 new TestDynamicDataBindingCallback(SAXSource .class, Mode.PAYLOAD); 66 TestSOAPBodyDataWriter<SOAPBody > soapBodyDataWriter = new TestSOAPBodyDataWriter<SOAPBody >(callback); 67 obj = saxSource; 68 soapBodyDataWriter.write(obj, soapMsg.getSOAPBody()); 69 SOAPBody soapBody = soapBodyDataWriter.getTestSOAPBody(); 70 assertTrue("TextContent should be TestSOAPInputMessage", 71 "TestSOAPInputMessage".equals(soapBody.getFirstChild().getTextContent())); 72 73 } 74 75 public void testStreamSourceWrite() throws Exception { 76 TestDynamicDataBindingCallback callback = 77 new TestDynamicDataBindingCallback(StreamSource .class, Mode.PAYLOAD); 78 TestSOAPBodyDataWriter<SOAPBody > soapBodyDataWriter = new TestSOAPBodyDataWriter<SOAPBody >(callback); 79 obj = streamSource; 80 soapBodyDataWriter.write(obj, soapMsg.getSOAPBody()); 81 SOAPBody soapBody = soapBodyDataWriter.getTestSOAPBody(); 82 assertTrue("TextContent should be TestSOAPInputMessage", 83 "TestSOAPInputMessage".equals(soapBody.getFirstChild().getTextContent())); 84 85 } 86 87 public void testJAXBObjectWrite() throws Exception { 88 89 JAXBContext jc = JAXBContext.newInstance("org.objectweb.hello_world_soap_http.types"); 90 TestDynamicDataBindingCallback callback = 91 new TestDynamicDataBindingCallback(jc, Mode.PAYLOAD); 92 TestSOAPBodyDataWriter<SOAPBody > soapBodyDataWriter = new TestSOAPBodyDataWriter<SOAPBody >(callback); 93 GreetMe greetMe = new GreetMe(); 94 greetMe.setRequestType("DIPLO"); 95 soapBodyDataWriter.write(greetMe, soapMsg.getSOAPBody()); 96 SOAPBody soapBody = soapBodyDataWriter.getTestSOAPBody(); 97 assertTrue("TextContent should be DIPLO", 98 "DIPLO".equals(soapBody.getFirstChild().getTextContent())); 99 100 } 101 102 private class TestSOAPBodyDataWriter<X> extends SOAPBodyDataWriter<X> { 103 104 public TestSOAPBodyDataWriter(DynamicDataBindingCallback cb) { 105 super(cb); 106 } 107 108 public SOAPBody getTestSOAPBody() { 109 return dest; 110 } 111 112 } 113 114 } 115 | Popular Tags |