001: package org.objectweb.celtix.bus.jaxws.io;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005:
006: import javax.xml.soap.MessageFactory;
007: import javax.xml.soap.SOAPException;
008: import javax.xml.soap.SOAPMessage;
009: import javax.xml.transform.Source;
010: import javax.xml.transform.dom.DOMSource;
011: import javax.xml.transform.sax.SAXSource;
012: import javax.xml.transform.stream.StreamSource;
013:
014: import org.xml.sax.InputSource;
015:
016: import junit.framework.TestCase;
017:
018: import org.objectweb.celtix.bindings.DataBindingCallback.Mode;
019:
020: public class SOAPMessageDataReaderTest<T> extends TestCase {
021:
022: private SOAPMessage soapMsg;
023: private InputStream is;
024:
025: public void setUp() throws IOException, SOAPException {
026: is = getClass().getResourceAsStream("GreetMeDocLiteralReq.xml");
027: soapMsg = MessageFactory.newInstance().createMessage(null, is);
028: assertNotNull(soapMsg);
029: }
030:
031: public void tearDown() throws IOException {
032: is.close();
033: }
034:
035: public void testDOMSourceRead() throws Exception {
036: TestDynamicDataBindingCallback callback = new TestDynamicDataBindingCallback(
037: DOMSource.class, Mode.MESSAGE);
038: SOAPMessageDataReader<SOAPMessage> soapMessageDataReader = new SOAPMessageDataReader<SOAPMessage>(
039: callback);
040: DOMSource obj = (DOMSource) soapMessageDataReader.read(0,
041: soapMsg);
042: assertNotNull(obj);
043: assertEquals("Message should contain TestSOAPInputMessage", obj
044: .getNode().getFirstChild().getTextContent(),
045: "TestSOAPInputMessage");
046:
047: }
048:
049: public void testSAXSourceRead() throws Exception {
050: TestDynamicDataBindingCallback callback = new TestDynamicDataBindingCallback(
051: SAXSource.class, Mode.MESSAGE);
052: SOAPMessageDataReader<SOAPMessage> soapMessageDataReader = new SOAPMessageDataReader<SOAPMessage>(
053: callback);
054: SAXSource obj = (SAXSource) soapMessageDataReader.read(0,
055: soapMsg);
056: assertNotNull(obj);
057: checkSource("TestSOAPInputMessage", obj);
058: }
059:
060: public void testStreamSourceRead() throws Exception {
061: TestDynamicDataBindingCallback callback = new TestDynamicDataBindingCallback(
062: StreamSource.class, Mode.MESSAGE);
063: SOAPMessageDataReader<SOAPMessage> soapMessageDataReader = new SOAPMessageDataReader<SOAPMessage>(
064: callback);
065: StreamSource obj = (StreamSource) soapMessageDataReader.read(0,
066: soapMsg);
067: assertNotNull(obj);
068: checkSource("TestSOAPInputMessage", obj);
069: }
070:
071: private void checkSource(String expected, Source source) {
072:
073: InputStream inputStream = null;
074:
075: if (source.getClass().isAssignableFrom(SAXSource.class)) {
076: InputSource inputSource = ((SAXSource) source)
077: .getInputSource();
078: inputStream = inputSource.getByteStream();
079: } else if (source.getClass().isAssignableFrom(
080: StreamSource.class)) {
081: inputStream = ((StreamSource) source).getInputStream();
082: }
083:
084: int i = 0;
085: StringBuilder sb = new StringBuilder();
086: try {
087: while (i != -1) {
088: i = inputStream.read();
089: sb.append((char) i);
090: }
091: } catch (IOException e) {
092: // TODO Auto-generated catch block
093: e.printStackTrace();
094: }
095: String received = sb.toString();
096: assertTrue("Expected: " + expected, received.contains(expected));
097:
098: }
099:
100: }
|