| 1 package org.objectweb.celtix.bus.jaxws.io; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 6 import javax.xml.soap.MessageFactory ; 7 import javax.xml.soap.SOAPException ; 8 import javax.xml.soap.SOAPMessage ; 9 import javax.xml.transform.Source ; 10 import javax.xml.transform.dom.DOMSource ; 11 import javax.xml.transform.sax.SAXSource ; 12 import javax.xml.transform.stream.StreamSource ; 13 14 import org.xml.sax.InputSource ; 15 16 import junit.framework.TestCase; 17 18 import org.objectweb.celtix.bindings.DataBindingCallback.Mode; 19 20 public class SOAPMessageDataReaderTest<T> extends TestCase { 21 22 private SOAPMessage soapMsg; 23 private InputStream is; 24 25 public void setUp() throws IOException , SOAPException { 26 is = getClass().getResourceAsStream("GreetMeDocLiteralReq.xml"); 27 soapMsg = MessageFactory.newInstance().createMessage(null, is); 28 assertNotNull(soapMsg); 29 } 30 31 public void tearDown() throws IOException { 32 is.close(); 33 } 34 35 public void testDOMSourceRead() throws Exception { 36 TestDynamicDataBindingCallback callback = 37 new TestDynamicDataBindingCallback(DOMSource .class, Mode.MESSAGE); 38 SOAPMessageDataReader<SOAPMessage > soapMessageDataReader = 39 new SOAPMessageDataReader<SOAPMessage >(callback); 40 DOMSource obj = (DOMSource )soapMessageDataReader.read(0, soapMsg); 41 assertNotNull(obj); 42 assertEquals("Message should contain TestSOAPInputMessage", 43 obj.getNode().getFirstChild().getTextContent(), "TestSOAPInputMessage"); 44 45 } 46 47 public void testSAXSourceRead() throws Exception { 48 TestDynamicDataBindingCallback callback = 49 new TestDynamicDataBindingCallback(SAXSource .class, Mode.MESSAGE); 50 SOAPMessageDataReader<SOAPMessage > soapMessageDataReader = 51 new SOAPMessageDataReader<SOAPMessage >(callback); 52 SAXSource obj = (SAXSource )soapMessageDataReader.read(0, soapMsg); 53 assertNotNull(obj); 54 checkSource("TestSOAPInputMessage", obj); 55 } 56 57 public void testStreamSourceRead() throws Exception { 58 TestDynamicDataBindingCallback callback = 59 new TestDynamicDataBindingCallback(StreamSource .class, Mode.MESSAGE); 60 SOAPMessageDataReader<SOAPMessage > soapMessageDataReader = 61 new SOAPMessageDataReader<SOAPMessage >(callback); 62 StreamSource obj = (StreamSource )soapMessageDataReader.read(0, soapMsg); 63 assertNotNull(obj); 64 checkSource("TestSOAPInputMessage", obj); 65 } 66 67 68 private void checkSource(String expected, Source source) { 69 70 InputStream inputStream = null; 71 72 if (source.getClass().isAssignableFrom(SAXSource .class)) { 73 InputSource inputSource = ((SAXSource )source).getInputSource(); 74 inputStream = inputSource.getByteStream(); 75 } else if (source.getClass().isAssignableFrom(StreamSource .class)) { 76 inputStream = ((StreamSource )source).getInputStream(); 77 } 78 79 int i = 0; 80 StringBuilder sb = new StringBuilder (); 81 try { 82 while (i != -1) { 83 i = inputStream.read(); 84 sb.append((char)i); 85 } 86 } catch (IOException e) { 87 e.printStackTrace(); 89 } 90 String received = sb.toString(); 91 assertTrue("Expected: " + expected, received.contains(expected)); 92 93 } 94 95 } 96 | Popular Tags |