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.Source ; 12 import javax.xml.transform.dom.DOMSource ; 13 import javax.xml.transform.sax.SAXSource ; 14 import javax.xml.transform.stream.StreamSource ; 15 16 import org.xml.sax.InputSource ; 17 18 import junit.framework.TestCase; 19 20 import org.objectweb.celtix.bindings.DataBindingCallback.Mode; 21 22 public class SOAPBodyDataReaderTest<T> extends TestCase { 23 24 private SOAPMessage soapMsg; 25 private InputStream is; 26 27 public void setUp() throws IOException , SOAPException { 28 is = getClass().getResourceAsStream("GreetMeDocLiteralReq.xml"); 29 soapMsg = MessageFactory.newInstance().createMessage(null, is); 30 assertNotNull(soapMsg); 31 } 32 33 public void tearDown() throws IOException { 34 is.close(); 35 } 36 37 public void testDOMSourceRead() throws Exception { 38 TestDynamicDataBindingCallback callback = 39 new TestDynamicDataBindingCallback(DOMSource .class, Mode.PAYLOAD); 40 SOAPBodyDataReader<SOAPBody > soapBodyDataReader = new SOAPBodyDataReader<SOAPBody >(callback); 41 DOMSource obj = (DOMSource )soapBodyDataReader.read(0, soapMsg.getSOAPBody()); 42 assertNotNull(obj); 43 assertEquals("Message should contain TestSOAPInputMessage", 44 obj.getNode().getFirstChild().getTextContent(), "TestSOAPInputMessage"); 45 46 } 47 48 public void testSAXSourceRead() throws Exception { 49 TestDynamicDataBindingCallback callback = 50 new TestDynamicDataBindingCallback(SAXSource .class, Mode.PAYLOAD); 51 SOAPBodyDataReader<SOAPBody > soapBodyDataReader = new SOAPBodyDataReader<SOAPBody >(callback); 52 SAXSource obj = (SAXSource )soapBodyDataReader.read(0, soapMsg.getSOAPBody()); 53 assertNotNull(obj); 54 checkSource("TestSOAPInputMessage", obj); 55 } 56 57 public void testStreamSourceRead() throws Exception { 58 TestDynamicDataBindingCallback callback = 59 new TestDynamicDataBindingCallback(StreamSource .class, Mode.PAYLOAD); 60 SOAPBodyDataReader<SOAPBody > soapBodyDataReader = new SOAPBodyDataReader<SOAPBody >(callback); 61 StreamSource obj = (StreamSource )soapBodyDataReader.read(0, soapMsg.getSOAPBody()); 62 assertNotNull(obj); 63 checkSource("TestSOAPInputMessage", obj); 64 } 65 66 public void testJAXBObjectRead() throws Exception { 67 68 JAXBContext jc = JAXBContext.newInstance("org.objectweb.hello_world_soap_http.types"); 69 TestDynamicDataBindingCallback callback = 70 new TestDynamicDataBindingCallback(jc, Mode.PAYLOAD); 71 SOAPBodyDataReader<SOAPBody > soapBodyDataReader = new SOAPBodyDataReader<SOAPBody >(callback); 72 Object obj = soapBodyDataReader.read(0, soapMsg.getSOAPBody()); 73 assertNotNull(obj); 74 assertTrue("Expect class of type org.objectweb.hello_world_soap_http.types.GreetMe", 75 obj.getClass().getName().equals("org.objectweb.hello_world_soap_http.types.GreetMe")); 76 } 77 78 private void checkSource(String expected, Source source) { 79 80 InputStream inputStream = null; 81 82 if (source.getClass().isAssignableFrom(SAXSource .class)) { 83 InputSource inputSource = ((SAXSource )source).getInputSource(); 84 inputStream = inputSource.getByteStream(); 85 } else if (source.getClass().isAssignableFrom(StreamSource .class)) { 86 inputStream = ((StreamSource )source).getInputStream(); 87 } 88 89 int i = 0; 90 StringBuilder sb = new StringBuilder (); 91 try { 92 while (i != -1) { 93 i = inputStream.read(); 94 sb.append((char)i); 95 } 96 } catch (IOException e) { 97 e.printStackTrace(); 99 } 100 String received = sb.toString(); 101 assertTrue("Expected: " + expected, received.contains(expected)); 102 103 } 104 105 } 106 | Popular Tags |