| 1 package org.objectweb.celtix.bus.bindings.soap; 2 3 import java.io.BufferedReader ; 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.io.InputStreamReader ; 7 import java.lang.reflect.Method ; 8 import java.net.URL ; 9 import java.util.concurrent.Executor ; 10 import java.util.concurrent.Future ; 11 12 import javax.wsdl.Port; 13 import javax.wsdl.WSDLException; 14 import javax.xml.namespace.QName ; 15 import javax.xml.soap.MessageFactory ; 16 import javax.xml.soap.SOAPMessage ; 17 import javax.xml.ws.handler.MessageContext; 18 import javax.xml.ws.handler.soap.SOAPMessageContext; 19 20 import junit.framework.TestCase; 21 22 import org.objectweb.celtix.Bus; 23 import org.objectweb.celtix.bindings.DataBindingCallback; 24 import org.objectweb.celtix.bindings.ResponseCallback; 25 import org.objectweb.celtix.bus.bindings.TestInputStreamContext; 26 import org.objectweb.celtix.bus.bindings.TestOutputStreamContext; 27 import org.objectweb.celtix.bus.jaxws.JAXBDataBindingCallback; 28 import org.objectweb.celtix.context.GenericMessageContext; 29 import org.objectweb.celtix.context.InputStreamMessageContext; 30 import org.objectweb.celtix.context.ObjectMessageContext; 31 import org.objectweb.celtix.context.OutputStreamMessageContext; 32 import org.objectweb.celtix.transports.ClientTransport; 33 import org.objectweb.celtix.ws.addressing.EndpointReferenceType; 34 import org.objectweb.celtix.wsdl.EndpointReferenceUtils; 35 import org.objectweb.hello_world_soap_http.Greeter; 36 37 public class SOAPClientBindingTest extends TestCase { 38 Bus bus; 39 EndpointReferenceType epr; 40 41 public SOAPClientBindingTest(String arg0) { 42 super(arg0); 43 } 44 45 public static void main(String [] args) { 46 junit.textui.TestRunner.run(SOAPClientBindingTest.class); 47 } 48 49 protected void setUp() throws Exception { 50 super.setUp(); 51 bus = Bus.init(); 52 epr = new EndpointReferenceType(); 53 54 URL wsdlUrl = getClass().getResource("/wsdl/hello_world.wsdl"); 55 QName serviceName = new QName ("http://objectweb.org/hello_world_soap_http", "SOAPService"); 56 epr = EndpointReferenceUtils.getEndpointReference(wsdlUrl, serviceName, "SoapPort"); 57 } 58 59 public void testGetBinding() throws Exception { 60 SOAPClientBinding clientBinding = new SOAPClientBinding(bus, epr); 61 assertNotNull(clientBinding.getBinding()); 62 } 63 64 public void testCreateObjectContext() throws Exception { 65 SOAPClientBinding clientBinding = new SOAPClientBinding(bus, epr); 66 assertNotNull(clientBinding.createObjectContext()); 67 } 68 69 public void testInvokeOneWay() throws Exception { 70 TestClientBinding clientBinding = new TestClientBinding(bus, epr); 71 ObjectMessageContext objContext = clientBinding.createObjectContext(); 72 assertNotNull(objContext); 73 Method method = SOAPMessageUtil.getMethod(Greeter.class, "greetMe"); 74 75 String arg0 = new String ("TestSOAPInputPMessage"); 76 objContext.setMessageObjects(arg0); 77 78 clientBinding.invokeOneWay(objContext, 79 new JAXBDataBindingCallback(method, 80 DataBindingCallback.Mode.PARTS, null)); 81 } 82 83 public void testhasFault() throws Exception { 84 TestClientBinding clientBinding = new TestClientBinding(bus, epr); 85 SOAPMessageContext soapCtx = new SOAPMessageContextImpl(new GenericMessageContext()); 86 87 InputStream is = getClass().getResourceAsStream("resources/NoSuchCodeDocLiteral.xml"); 88 MessageFactory msgFactory = MessageFactory.newInstance(); 89 SOAPMessage faultMsg = msgFactory.createMessage(null, is); 90 soapCtx.setMessage(faultMsg); 91 assertTrue(clientBinding.getBindingImpl().hasFault(soapCtx)); 92 93 is = getClass().getResourceAsStream("resources/GreetMeDocLiteralReq.xml"); 94 faultMsg = msgFactory.createMessage(null, is); 95 soapCtx.setMessage(faultMsg); 96 assertFalse(clientBinding.getBindingImpl().hasFault(soapCtx)); 97 } 98 99 public void testRead() throws Exception { 100 TestClientBinding clientBinding = new TestClientBinding(bus, epr); 101 InputStream is = getClass().getResourceAsStream("resources/GreetMeDocLiteralResp.xml"); 102 TestInputStreamContext tisc = new TestInputStreamContext(null); 103 tisc.setInputStream(is); 104 105 SOAPMessageContext soapCtx = new SOAPMessageContextImpl(new GenericMessageContext()); 106 clientBinding.getBindingImpl().read(tisc, soapCtx); 107 assertNotNull(soapCtx.getMessage()); 108 } 109 110 public void testWrite() throws Exception { 111 TestClientBinding clientBinding = new TestClientBinding(bus, epr); 112 113 InputStream is = getClass().getResourceAsStream("resources/GreetMeDocLiteralReq.xml"); 114 115 MessageFactory msgFactory = MessageFactory.newInstance(); 116 SOAPMessage greetMeMsg = msgFactory.createMessage(null, is); 117 is.close(); 118 119 BufferedReader br = 120 new BufferedReader ( 121 new InputStreamReader ( 122 getClass().getResourceAsStream("resources/GreetMeDocLiteralReq.xml"))); 123 124 SOAPMessageContext soapCtx = new SOAPMessageContextImpl(new GenericMessageContext()); 125 soapCtx.setMessage(greetMeMsg); 126 127 TestOutputStreamContext tosc = new TestOutputStreamContext(null, soapCtx); 128 clientBinding.getBindingImpl().write(soapCtx, tosc); 129 130 byte[] bArray = tosc.getOutputStreamBytes(); 131 assertEquals(br.readLine(), (new String (bArray)).trim()); 132 } 133 134 class TestClientBinding extends SOAPClientBinding { 135 136 public TestClientBinding(Bus b, EndpointReferenceType ref) 137 throws WSDLException, IOException { 138 super(b, ref); 139 } 140 141 protected ClientTransport createTransport(EndpointReferenceType ref) 142 throws WSDLException, IOException { 143 return new TestClientTransport(bus, ref); 145 } 146 147 } 148 149 class TestClientTransport implements ClientTransport { 150 151 public TestClientTransport(Bus b, EndpointReferenceType ref) { 152 } 153 154 public EndpointReferenceType getTargetEndpoint() { 155 return null; 156 } 157 158 public EndpointReferenceType getDecoupledEndpoint() throws IOException { 159 return null; 160 } 161 162 public Port getPort() { 163 return null; 164 } 165 166 public OutputStreamMessageContext createOutputStreamContext(MessageContext context) 167 throws IOException { 168 return new TestOutputStreamContext(null, context); 169 } 170 171 public void finalPrepareOutputStreamContext(OutputStreamMessageContext context) throws IOException { 172 } 173 174 public void invokeOneway(OutputStreamMessageContext context) throws IOException { 175 } 177 178 public InputStreamMessageContext invoke(OutputStreamMessageContext context) throws IOException { 179 return context.getCorrespondingInputStreamContext(); 180 } 181 182 public Future <InputStreamMessageContext> invokeAsync(OutputStreamMessageContext context, Executor ex) 183 throws IOException { 184 return null; 185 } 186 187 public ResponseCallback getResponseCallback() { 188 return null; 189 } 190 191 public void shutdown() { 192 } 194 } 195 196 197 } 198 | Popular Tags |