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.Source;
012: import javax.xml.transform.dom.DOMSource;
013: import javax.xml.transform.sax.SAXSource;
014: import javax.xml.transform.stream.StreamSource;
015:
016: import org.xml.sax.InputSource;
017:
018: import junit.framework.TestCase;
019:
020: import org.objectweb.celtix.bindings.DataBindingCallback.Mode;
021:
022: public class SOAPBodyDataReaderTest<T> extends TestCase {
023:
024: private SOAPMessage soapMsg;
025: private InputStream is;
026:
027: public void setUp() throws IOException, SOAPException {
028: is = getClass().getResourceAsStream("GreetMeDocLiteralReq.xml");
029: soapMsg = MessageFactory.newInstance().createMessage(null, is);
030: assertNotNull(soapMsg);
031: }
032:
033: public void tearDown() throws IOException {
034: is.close();
035: }
036:
037: public void testDOMSourceRead() throws Exception {
038: TestDynamicDataBindingCallback callback = new TestDynamicDataBindingCallback(
039: DOMSource.class, Mode.PAYLOAD);
040: SOAPBodyDataReader<SOAPBody> soapBodyDataReader = new SOAPBodyDataReader<SOAPBody>(
041: callback);
042: DOMSource obj = (DOMSource) soapBodyDataReader.read(0, soapMsg
043: .getSOAPBody());
044: assertNotNull(obj);
045: assertEquals("Message should contain TestSOAPInputMessage", obj
046: .getNode().getFirstChild().getTextContent(),
047: "TestSOAPInputMessage");
048:
049: }
050:
051: public void testSAXSourceRead() throws Exception {
052: TestDynamicDataBindingCallback callback = new TestDynamicDataBindingCallback(
053: SAXSource.class, Mode.PAYLOAD);
054: SOAPBodyDataReader<SOAPBody> soapBodyDataReader = new SOAPBodyDataReader<SOAPBody>(
055: callback);
056: SAXSource obj = (SAXSource) soapBodyDataReader.read(0, soapMsg
057: .getSOAPBody());
058: assertNotNull(obj);
059: checkSource("TestSOAPInputMessage", obj);
060: }
061:
062: public void testStreamSourceRead() throws Exception {
063: TestDynamicDataBindingCallback callback = new TestDynamicDataBindingCallback(
064: StreamSource.class, Mode.PAYLOAD);
065: SOAPBodyDataReader<SOAPBody> soapBodyDataReader = new SOAPBodyDataReader<SOAPBody>(
066: callback);
067: StreamSource obj = (StreamSource) soapBodyDataReader.read(0,
068: soapMsg.getSOAPBody());
069: assertNotNull(obj);
070: checkSource("TestSOAPInputMessage", obj);
071: }
072:
073: public void testJAXBObjectRead() throws Exception {
074:
075: JAXBContext jc = JAXBContext
076: .newInstance("org.objectweb.hello_world_soap_http.types");
077: TestDynamicDataBindingCallback callback = new TestDynamicDataBindingCallback(
078: jc, Mode.PAYLOAD);
079: SOAPBodyDataReader<SOAPBody> soapBodyDataReader = new SOAPBodyDataReader<SOAPBody>(
080: callback);
081: Object obj = soapBodyDataReader.read(0, soapMsg.getSOAPBody());
082: assertNotNull(obj);
083: assertTrue(
084: "Expect class of type org.objectweb.hello_world_soap_http.types.GreetMe",
085: obj
086: .getClass()
087: .getName()
088: .equals(
089: "org.objectweb.hello_world_soap_http.types.GreetMe"));
090: }
091:
092: private void checkSource(String expected, Source source) {
093:
094: InputStream inputStream = null;
095:
096: if (source.getClass().isAssignableFrom(SAXSource.class)) {
097: InputSource inputSource = ((SAXSource) source)
098: .getInputSource();
099: inputStream = inputSource.getByteStream();
100: } else if (source.getClass().isAssignableFrom(
101: StreamSource.class)) {
102: inputStream = ((StreamSource) source).getInputStream();
103: }
104:
105: int i = 0;
106: StringBuilder sb = new StringBuilder();
107: try {
108: while (i != -1) {
109: i = inputStream.read();
110: sb.append((char) i);
111: }
112: } catch (IOException e) {
113: // TODO Auto-generated catch block
114: e.printStackTrace();
115: }
116: String received = sb.toString();
117: assertTrue("Expected: " + expected, received.contains(expected));
118:
119: }
120:
121: }
|