1 package org.objectweb.celtix.geronimo.container; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.io.OutputStream ; 6 import java.io.Serializable ; 7 import java.net.URI ; 8 import java.util.HashMap ; 9 import java.util.List ; 10 import java.util.Map ; 11 import java.util.concurrent.Executor ; 12 13 import javax.xml.namespace.QName ; 14 import javax.xml.ws.Holder; 15 import javax.xml.ws.handler.MessageContext; 16 17 import junit.framework.TestCase; 18 19 import org.apache.geronimo.webservices.WebServiceContainer.Request; 20 import org.apache.geronimo.webservices.WebServiceContainer.Response; 21 import org.easymock.classextension.EasyMock; 22 import org.objectweb.celtix.Bus; 23 import org.objectweb.celtix.configuration.Configuration; 24 import org.objectweb.celtix.context.InputStreamMessageContext; 25 import org.objectweb.celtix.context.OutputStreamMessageContext; 26 import org.objectweb.celtix.geronimo.MockBusFactory; 27 import org.objectweb.celtix.transports.ServerTransport; 28 import org.objectweb.celtix.transports.ServerTransportCallback; 29 import org.objectweb.celtix.ws.addressing.AttributedURIType; 30 import org.objectweb.celtix.ws.addressing.EndpointReferenceType; 31 import org.objectweb.celtix.wsdl.EndpointReferenceUtils; 32 33 public class GeronimoServerTransportTest extends TestCase { 34 35 private Bus mockBus; 36 private EndpointReferenceType address; 37 private Holder<InputStreamMessageContext> ctxHolder = new Holder<InputStreamMessageContext>(); 38 private Holder<ServerTransport> transportHolder = new Holder<ServerTransport>(); 39 private Holder<Boolean > invokedHolder = new Holder<Boolean >(); 40 private MockBusFactory busFactory = new MockBusFactory(); 41 private GeronimoServerTransport transport; 42 43 44 public void setUp() throws Exception { 45 46 mockBus = busFactory.createMockBus(); 47 48 address = new EndpointReferenceType(); 49 AttributedURIType uri = new AttributedURIType(); 50 uri.setValue("http://foo.bar.iona.com/wibbly/wobbly/wonder"); 51 address.setAddress(uri); 52 QName serviceName = new QName ("http://www.w3.org/2004/08/wsdl", "testServiceName"); 53 EndpointReferenceUtils.setServiceAndPortName(address, serviceName, ""); 54 55 Configuration child = 56 busFactory.addChildConfig("http://celtix.objectweb.org/bus/jaxws/endpoint-config", null, null); 57 EasyMock.replay(child); 58 busFactory.replay(); 59 60 transport = new GeronimoServerTransport(mockBus, address); 61 62 } 63 64 public void testTransportIsSerializable() { 65 66 assertTrue("transport must be serializable", 67 Serializable .class.isAssignableFrom(GeronimoServerTransport.class)); 68 } 69 70 71 public void testCopyHeaders() { 72 73 74 DummyRequest req = new DummyRequest(); 75 req.setHeader("Accept", "text/plain, text/html"); 76 req.setHeader("Host", "localhost"); 77 req.setHeader("Content-Type", "text/html"); 78 79 GeronimoInputStreamMessageContext ctx = transport.createInputStreamMessageContext(); 80 ctx.setRequest(req); 81 82 Map <String , List <String >> headerMap = new HashMap <String , List <String >>(); 83 assertTrue(ctx instanceof MessageContext); 84 transport.copyRequestHeaders(ctx, headerMap); 85 86 assertEquals("unexpected number of headers", 3, headerMap.size()); 87 checkHeader(headerMap, "Host", "localhost"); 88 checkHeader(headerMap, "Content-Type", "text/html"); 89 checkHeader(headerMap, "Accept", "text/plain", "text/html"); 90 } 91 92 93 private void checkHeader(Map <String , List <String >> headers, String name, String ... expectedValues) { 94 95 List <String > values = headers.get(name); 96 97 int valueCount = 0; 98 99 for (String expectedValue : expectedValues) { 100 assertTrue("unexpected header value: [" + expectedValue + "] " + values, 101 values.contains(expectedValue)); 102 ++valueCount; 103 } 104 assertEquals("unexpected number of header values", valueCount, values.size()); 105 } 106 107 public void testInvoke() throws Exception { 108 109 final Request request = EasyMock.createMock(Request.class); 110 final Response response = EasyMock.createMock(Response.class); 111 112 113 transport.activate(new ServerTransportCallback() { 114 115 public void dispatch(InputStreamMessageContext ctx, ServerTransport tport) { 116 invokedHolder.value = Boolean.TRUE; 117 ctxHolder.value = ctx; 118 transportHolder.value = tport; 119 } 120 121 public Executor getExecutor() { 122 return null; 123 } 124 125 }); 126 127 transport.invoke(request, response); 128 129 assertTrue("ServerTransportCallback.dispatch was not called", invokedHolder.value); 130 assertSame("transport not passed to ServerTransportCallback", transport, transportHolder.value); 131 132 assertNotNull("no context passed from transport", ctxHolder.value); 133 InputStreamMessageContext ctx = ctxHolder.value; 134 assertTrue("context is of incorrect type: " + ctx.getClass(), 135 ctx instanceof GeronimoInputStreamMessageContext); 136 GeronimoInputStreamMessageContext inctx = (GeronimoInputStreamMessageContext)ctx; 137 assertNotNull("request not available in context", inctx.getRequest()); 138 139 assertNotNull("response not available in context", inctx.getResponse()); 140 } 141 142 public void testCreateOutputStreamContext() throws IOException { 143 144 MessageContext mc = EasyMock.createMock(MessageContext.class); 145 Response resp = EasyMock.createNiceMock(Response.class); 146 resp.setStatusCode(200); 147 EasyMock.expect(resp.getOutputStream()).andReturn(EasyMock.createNiceMock(OutputStream .class)); 148 149 EasyMock.expect(mc.get(GeronimoInputStreamMessageContext.RESPONSE)) 150 .andReturn(resp); 151 152 EasyMock.expect(mc.get("foo")).andReturn("ret"); 153 EasyMock.replay(mc); 154 EasyMock.replay(resp); 155 156 OutputStreamMessageContext outctx = transport.createOutputStreamContext(mc); 157 158 assertNotNull("received null context from the transoprt", outctx); 159 assertTrue("incorrect type for context" + outctx.getClass(), 160 outctx instanceof GeronimoOutputStreamServerMessageContext); 161 162 assertNotNull("no outputstream in context", outctx.getOutputStream()); 163 164 166 outctx.get("foo"); 167 EasyMock.verify(mc); 168 169 } 170 171 172 static class DummyRequest implements Request { 173 174 private Map <String , String > headers = new HashMap <String , String >(); 175 176 public void setHeader(String header, String value) { 177 headers.put(header, value); 178 } 179 public String getHeader(String header) { 180 return headers.get(header); 181 } 182 183 public URI getURI() { 184 return null; 186 } 187 188 public int getContentLength() { 189 return 0; 191 } 192 193 public String getContentType() { 194 return null; 196 } 197 198 public InputStream getInputStream() throws IOException { 199 return null; 201 } 202 203 public int getMethod() { 204 return 0; 206 } 207 208 public String getParameter(String arg0) { 209 return null; 211 } 212 213 public Map getParameters() { 214 return null; 216 } 217 218 public Object getAttribute(String arg0) { 219 return null; 221 } 222 223 public void setAttribute(String arg0, Object arg1) { 224 226 } 227 228 } 229 230 } 231 | Popular Tags |