1 package org.objectweb.celtix.jbi.transport; 2 3 import java.io.BufferedReader ; 4 import java.io.ByteArrayInputStream ; 5 import java.io.ByteArrayOutputStream ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.io.InputStreamReader ; 9 import java.lang.reflect.Method ; 10 11 import javax.jbi.messaging.DeliveryChannel; 12 import javax.jbi.messaging.InOut; 13 import javax.jbi.messaging.MessageExchangeFactory; 14 import javax.jbi.messaging.NormalizedMessage; 15 import javax.jws.WebService; 16 import javax.xml.namespace.QName ; 17 import javax.xml.transform.Source ; 18 import javax.xml.transform.stream.StreamSource ; 19 import javax.xml.ws.handler.MessageContext; 20 21 import junit.framework.TestCase; 22 23 import org.easymock.classextension.EasyMock; 24 import org.objectweb.celtix.bindings.ClientBinding; 25 import org.objectweb.celtix.bindings.ResponseCallback; 26 import org.objectweb.celtix.context.InputStreamMessageContext; 27 import org.objectweb.celtix.context.ObjectMessageContextImpl; 28 import org.objectweb.celtix.context.OutputStreamMessageContext; 29 import org.objectweb.celtix.ws.addressing.EndpointReferenceType; 30 import org.objectweb.celtix.wsdl.EndpointReferenceUtils; 31 import org.objectweb.hello_world_soap_http.Greeter; 32 33 34 public class JBIClientTransportTest extends TestCase { 35 36 private static final String TEST_MESSAGE = "<message>this is the test message</message>"; 37 private static final String XML_DECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 38 39 private final DeliveryChannel channel = EasyMock.createMock(DeliveryChannel.class); 40 private EndpointReferenceType endpointRef; 42 private final ClientBinding clientBinding = EasyMock.createMock(ClientBinding.class); 44 45 46 private JBIOutputStreamMessageContext outCtx; 47 48 49 private JBIClientTransport clientTransport; 50 private QName serviceName; 51 private Method targetMethod; 52 53 public void setUp() throws Exception { 54 55 initFixture(); 56 clientTransport = new JBIClientTransport(channel, endpointRef, clientBinding); 57 58 ObjectMessageContextImpl context = new ObjectMessageContextImpl(); 59 60 outCtx = (JBIOutputStreamMessageContext)clientTransport.createOutputStreamContext(context); 61 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 62 baos.write(TEST_MESSAGE.getBytes()); 63 outCtx.setOutputStream(baos); 64 65 targetMethod = Greeter.class.getMethod("sayHi"); 66 assertNotNull("could not set up target method", targetMethod); 67 context.setMethod(targetMethod); 68 69 WebService ws = Greeter.class.getAnnotation(WebService.class); 70 assertNotNull(ws); 71 serviceName = new QName (ws.targetNamespace(), ws.name()); 72 initFixture(); 73 } 74 75 76 public void testJBIClientTransport() { 77 78 79 JBIClientTransport ct = new JBIClientTransport(channel, endpointRef, clientBinding); 80 assertNotNull("server transport must not be null", ct); 81 assertSame("transport must JBIClientTransport", JBIClientTransport.class, ct.getClass()); 82 EasyMock.verify(clientBinding); 83 84 } 85 86 87 public void testInvokeOneway() { 88 89 } 90 91 92 public void testinvoke() throws Exception { 93 94 MessageExchangeFactory factory = EasyMock.createMock(MessageExchangeFactory.class); 95 InOut exchange = EasyMock.createMock(InOut.class); 96 NormalizedMessage message = EasyMock.createMock(NormalizedMessage.class); 97 ByteArrayInputStream messageStream = new ByteArrayInputStream (TEST_MESSAGE.getBytes()); 98 99 channel.createExchangeFactoryForService(serviceName); 100 EasyMock.expectLastCall().andReturn(factory); 101 factory.createInOutExchange(); 102 EasyMock.expectLastCall().andReturn(exchange); 103 exchange.createMessage(); 104 EasyMock.expectLastCall().andReturn(message); 105 exchange.getEndpoint(); 106 EasyMock.expectLastCall().andReturn(null); 107 message.setContent((Source )EasyMock.notNull()); 108 exchange.setService(serviceName); 109 exchange.setInterfaceName(new QName ("http://objectweb.org/hello_world_soap_http", "Greeter")); 110 exchange.setOperation(new QName (targetMethod.getName())); 111 exchange.setInMessage(message); 112 channel.sendSync(exchange); 113 EasyMock.expectLastCall().andReturn(Boolean.TRUE); 114 exchange.getOutMessage(); 115 EasyMock.expectLastCall().andReturn(message); 116 message.getContent(); 117 EasyMock.expectLastCall().andReturn(new StreamSource (messageStream)); 118 119 EasyMock.replay(channel); 120 EasyMock.replay(factory); 121 EasyMock.replay(exchange); 122 EasyMock.replay(message); 123 124 InputStreamMessageContext ret = clientTransport.invoke(outCtx); 125 assertNotNull("invoke must not return null", ret); 126 assertNotNull("invoke must not return an emtpy context", ret.getInputStream()); 127 128 EasyMock.verify(channel); 129 EasyMock.verify(factory); 130 EasyMock.verify(exchange); 131 EasyMock.verify(message); 132 133 BufferedReader reader = new BufferedReader (new InputStreamReader (ret.getInputStream())); 134 String s = reader.readLine(); 136 assertNotNull(s); 137 139 assertTrue(s.startsWith(XML_DECLARATION)); 140 String result = s.substring(XML_DECLARATION.length()); 141 assertEquals("returned message incorrect", TEST_MESSAGE, result); 142 } 143 144 145 public void testGetMessageContent() throws IOException { 146 147 Source ret = clientTransport.getMessageContent(outCtx); 148 149 assertNotNull(ret); 150 assertEquals("incorrect return type", StreamSource .class, ret.getClass()); 151 InputStream in = ((StreamSource )ret).getInputStream(); 152 assertNotNull(in); 153 154 BufferedReader reader = new BufferedReader (new InputStreamReader (in)); 155 assertEquals(TEST_MESSAGE, reader.readLine()); 156 } 157 158 159 public void testInvokeAsync() { 160 161 } 162 163 164 public void testFinalPrepareOutputStreamContext() { 165 166 } 167 168 169 public void testShutdown() { 170 171 } 172 173 174 public void testCreateOutputStreamContext() throws IOException { 175 176 MessageContext messageContext = EasyMock.createMock(MessageContext.class); 177 OutputStreamMessageContext ret = clientTransport.createOutputStreamContext(messageContext); 178 179 assertNotNull("OutputStreamMessageContext must not be null", ret); 180 assertEquals("OutputStreamMessageContext of incorrect type", 181 JBIOutputStreamMessageContext.class, ret.getClass()); 182 } 183 184 185 private void initFixture() { 186 187 EasyMock.reset(clientBinding); 188 189 endpointRef = new EndpointReferenceType(); 190 EndpointReferenceUtils.setServiceAndPortName(endpointRef, 191 new QName ("http://objectweb.org/hello_world_soap_http", 192 "Greeter"), 193 "SOAPPort"); 194 ResponseCallback responseCallback = EasyMock.createMock(ResponseCallback.class); 195 clientBinding.createResponseCallback(); 196 EasyMock.expectLastCall().andReturn(responseCallback); 197 EasyMock.replay(clientBinding); 198 199 } 200 } 201 | Popular Tags |