001: package org.objectweb.celtix.jbi.transport;
002:
003: import java.io.BufferedReader;
004: import java.io.ByteArrayInputStream;
005: import java.io.ByteArrayOutputStream;
006: import java.io.IOException;
007: import java.io.InputStream;
008: import java.io.InputStreamReader;
009: import java.lang.reflect.Method;
010:
011: import javax.jbi.messaging.DeliveryChannel;
012: import javax.jbi.messaging.InOut;
013: import javax.jbi.messaging.MessageExchangeFactory;
014: import javax.jbi.messaging.NormalizedMessage;
015: import javax.jws.WebService;
016: import javax.xml.namespace.QName;
017: import javax.xml.transform.Source;
018: import javax.xml.transform.stream.StreamSource;
019: import javax.xml.ws.handler.MessageContext;
020:
021: import junit.framework.TestCase;
022:
023: import org.easymock.classextension.EasyMock;
024: import org.objectweb.celtix.bindings.ClientBinding;
025: import org.objectweb.celtix.bindings.ResponseCallback;
026: import org.objectweb.celtix.context.InputStreamMessageContext;
027: import org.objectweb.celtix.context.ObjectMessageContextImpl;
028: import org.objectweb.celtix.context.OutputStreamMessageContext;
029: import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
030: import org.objectweb.celtix.wsdl.EndpointReferenceUtils;
031: import org.objectweb.hello_world_soap_http.Greeter;
032:
033: public class JBIClientTransportTest extends TestCase {
034:
035: private static final String TEST_MESSAGE = "<message>this is the test message</message>";
036: private static final String XML_DECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
037:
038: private final DeliveryChannel channel = EasyMock
039: .createMock(DeliveryChannel.class);
040: //private final EndpointReferenceType endpointRef = EasyMock.createMock(EndpointReferenceType.class);
041: private EndpointReferenceType endpointRef;
042: //private final MetadataType metaData = EasyMock.createMock(MetadataType.class);
043: private final ClientBinding clientBinding = EasyMock
044: .createMock(ClientBinding.class);
045:
046: private JBIOutputStreamMessageContext outCtx;
047:
048: private JBIClientTransport clientTransport;
049: private QName serviceName;
050: private Method targetMethod;
051:
052: public void setUp() throws Exception {
053:
054: initFixture();
055: clientTransport = new JBIClientTransport(channel, endpointRef,
056: clientBinding);
057:
058: ObjectMessageContextImpl context = new ObjectMessageContextImpl();
059:
060: outCtx = (JBIOutputStreamMessageContext) clientTransport
061: .createOutputStreamContext(context);
062: ByteArrayOutputStream baos = new ByteArrayOutputStream();
063: baos.write(TEST_MESSAGE.getBytes());
064: outCtx.setOutputStream(baos);
065:
066: targetMethod = Greeter.class.getMethod("sayHi");
067: assertNotNull("could not set up target method", targetMethod);
068: context.setMethod(targetMethod);
069:
070: WebService ws = Greeter.class.getAnnotation(WebService.class);
071: assertNotNull(ws);
072: serviceName = new QName(ws.targetNamespace(), ws.name());
073: initFixture();
074: }
075:
076: public void testJBIClientTransport() {
077:
078: JBIClientTransport ct = new JBIClientTransport(channel,
079: endpointRef, clientBinding);
080: assertNotNull("server transport must not be null", ct);
081: assertSame("transport must JBIClientTransport",
082: JBIClientTransport.class, ct.getClass());
083: EasyMock.verify(clientBinding);
084:
085: }
086:
087: public void testInvokeOneway() {
088:
089: }
090:
091: public void testinvoke() throws Exception {
092:
093: MessageExchangeFactory factory = EasyMock
094: .createMock(MessageExchangeFactory.class);
095: InOut exchange = EasyMock.createMock(InOut.class);
096: NormalizedMessage message = EasyMock
097: .createMock(NormalizedMessage.class);
098: ByteArrayInputStream messageStream = new ByteArrayInputStream(
099: TEST_MESSAGE.getBytes());
100:
101: channel.createExchangeFactoryForService(serviceName);
102: EasyMock.expectLastCall().andReturn(factory);
103: factory.createInOutExchange();
104: EasyMock.expectLastCall().andReturn(exchange);
105: exchange.createMessage();
106: EasyMock.expectLastCall().andReturn(message);
107: exchange.getEndpoint();
108: EasyMock.expectLastCall().andReturn(null);
109: message.setContent((Source) EasyMock.notNull());
110: exchange.setService(serviceName);
111: exchange
112: .setInterfaceName(new QName(
113: "http://objectweb.org/hello_world_soap_http",
114: "Greeter"));
115: exchange.setOperation(new QName(targetMethod.getName()));
116: exchange.setInMessage(message);
117: channel.sendSync(exchange);
118: EasyMock.expectLastCall().andReturn(Boolean.TRUE);
119: exchange.getOutMessage();
120: EasyMock.expectLastCall().andReturn(message);
121: message.getContent();
122: EasyMock.expectLastCall().andReturn(
123: new StreamSource(messageStream));
124:
125: EasyMock.replay(channel);
126: EasyMock.replay(factory);
127: EasyMock.replay(exchange);
128: EasyMock.replay(message);
129:
130: InputStreamMessageContext ret = clientTransport.invoke(outCtx);
131: assertNotNull("invoke must not return null", ret);
132: assertNotNull("invoke must not return an emtpy context", ret
133: .getInputStream());
134:
135: EasyMock.verify(channel);
136: EasyMock.verify(factory);
137: EasyMock.verify(exchange);
138: EasyMock.verify(message);
139:
140: BufferedReader reader = new BufferedReader(
141: new InputStreamReader(ret.getInputStream()));
142: // throw away prolog
143: String s = reader.readLine();
144: assertNotNull(s);
145: // chop off the XML declaration
146:
147: assertTrue(s.startsWith(XML_DECLARATION));
148: String result = s.substring(XML_DECLARATION.length());
149: assertEquals("returned message incorrect", TEST_MESSAGE, result);
150: }
151:
152: public void testGetMessageContent() throws IOException {
153:
154: Source ret = clientTransport.getMessageContent(outCtx);
155:
156: assertNotNull(ret);
157: assertEquals("incorrect return type", StreamSource.class, ret
158: .getClass());
159: InputStream in = ((StreamSource) ret).getInputStream();
160: assertNotNull(in);
161:
162: BufferedReader reader = new BufferedReader(
163: new InputStreamReader(in));
164: assertEquals(TEST_MESSAGE, reader.readLine());
165: }
166:
167: public void testInvokeAsync() {
168:
169: }
170:
171: public void testFinalPrepareOutputStreamContext() {
172:
173: }
174:
175: public void testShutdown() {
176:
177: }
178:
179: public void testCreateOutputStreamContext() throws IOException {
180:
181: MessageContext messageContext = EasyMock
182: .createMock(MessageContext.class);
183: OutputStreamMessageContext ret = clientTransport
184: .createOutputStreamContext(messageContext);
185:
186: assertNotNull("OutputStreamMessageContext must not be null",
187: ret);
188: assertEquals("OutputStreamMessageContext of incorrect type",
189: JBIOutputStreamMessageContext.class, ret.getClass());
190: }
191:
192: private void initFixture() {
193:
194: EasyMock.reset(clientBinding);
195:
196: endpointRef = new EndpointReferenceType();
197: EndpointReferenceUtils.setServiceAndPortName(endpointRef,
198: new QName("http://objectweb.org/hello_world_soap_http",
199: "Greeter"), "SOAPPort");
200: ResponseCallback responseCallback = EasyMock
201: .createMock(ResponseCallback.class);
202: clientBinding.createResponseCallback();
203: EasyMock.expectLastCall().andReturn(responseCallback);
204: EasyMock.replay(clientBinding);
205:
206: }
207: }
|