001: package org.objectweb.celtix.geronimo.container;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.io.OutputStream;
006: import java.io.Serializable;
007: import java.net.URI;
008: import java.util.HashMap;
009: import java.util.List;
010: import java.util.Map;
011: import java.util.concurrent.Executor;
012:
013: import javax.xml.namespace.QName;
014: import javax.xml.ws.Holder;
015: import javax.xml.ws.handler.MessageContext;
016:
017: import junit.framework.TestCase;
018:
019: import org.apache.geronimo.webservices.WebServiceContainer.Request;
020: import org.apache.geronimo.webservices.WebServiceContainer.Response;
021: import org.easymock.classextension.EasyMock;
022: import org.objectweb.celtix.Bus;
023: import org.objectweb.celtix.configuration.Configuration;
024: import org.objectweb.celtix.context.InputStreamMessageContext;
025: import org.objectweb.celtix.context.OutputStreamMessageContext;
026: import org.objectweb.celtix.geronimo.MockBusFactory;
027: import org.objectweb.celtix.transports.ServerTransport;
028: import org.objectweb.celtix.transports.ServerTransportCallback;
029: import org.objectweb.celtix.ws.addressing.AttributedURIType;
030: import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
031: import org.objectweb.celtix.wsdl.EndpointReferenceUtils;
032:
033: public class GeronimoServerTransportTest extends TestCase {
034:
035: private Bus mockBus;
036: private EndpointReferenceType address;
037: private Holder<InputStreamMessageContext> ctxHolder = new Holder<InputStreamMessageContext>();
038: private Holder<ServerTransport> transportHolder = new Holder<ServerTransport>();
039: private Holder<Boolean> invokedHolder = new Holder<Boolean>();
040: private MockBusFactory busFactory = new MockBusFactory();
041: private GeronimoServerTransport transport;
042:
043: public void setUp() throws Exception {
044:
045: mockBus = busFactory.createMockBus();
046:
047: address = new EndpointReferenceType();
048: AttributedURIType uri = new AttributedURIType();
049: uri.setValue("http://foo.bar.iona.com/wibbly/wobbly/wonder");
050: address.setAddress(uri);
051: QName serviceName = new QName("http://www.w3.org/2004/08/wsdl",
052: "testServiceName");
053: EndpointReferenceUtils.setServiceAndPortName(address,
054: serviceName, "");
055:
056: Configuration child = busFactory
057: .addChildConfig(
058: "http://celtix.objectweb.org/bus/jaxws/endpoint-config",
059: null, null);
060: EasyMock.replay(child);
061: busFactory.replay();
062:
063: transport = new GeronimoServerTransport(mockBus, address);
064:
065: }
066:
067: public void testTransportIsSerializable() {
068:
069: assertTrue("transport must be serializable", Serializable.class
070: .isAssignableFrom(GeronimoServerTransport.class));
071: }
072:
073: public void testCopyHeaders() {
074:
075: DummyRequest req = new DummyRequest();
076: req.setHeader("Accept", "text/plain, text/html");
077: req.setHeader("Host", "localhost");
078: req.setHeader("Content-Type", "text/html");
079:
080: GeronimoInputStreamMessageContext ctx = transport
081: .createInputStreamMessageContext();
082: ctx.setRequest(req);
083:
084: Map<String, List<String>> headerMap = new HashMap<String, List<String>>();
085: assertTrue(ctx instanceof MessageContext);
086: transport.copyRequestHeaders(ctx, headerMap);
087:
088: assertEquals("unexpected number of headers", 3, headerMap
089: .size());
090: checkHeader(headerMap, "Host", "localhost");
091: checkHeader(headerMap, "Content-Type", "text/html");
092: checkHeader(headerMap, "Accept", "text/plain", "text/html");
093: }
094:
095: private void checkHeader(Map<String, List<String>> headers,
096: String name, String... expectedValues) {
097:
098: List<String> values = headers.get(name);
099:
100: int valueCount = 0;
101:
102: for (String expectedValue : expectedValues) {
103: assertTrue("unexpected header value: [" + expectedValue
104: + "] " + values, values.contains(expectedValue));
105: ++valueCount;
106: }
107: assertEquals("unexpected number of header values", valueCount,
108: values.size());
109: }
110:
111: public void testInvoke() throws Exception {
112:
113: final Request request = EasyMock.createMock(Request.class);
114: final Response response = EasyMock.createMock(Response.class);
115:
116: transport.activate(new ServerTransportCallback() {
117:
118: public void dispatch(InputStreamMessageContext ctx,
119: ServerTransport tport) {
120: invokedHolder.value = Boolean.TRUE;
121: ctxHolder.value = ctx;
122: transportHolder.value = tport;
123: }
124:
125: public Executor getExecutor() {
126: return null;
127: }
128:
129: });
130:
131: transport.invoke(request, response);
132:
133: assertTrue("ServerTransportCallback.dispatch was not called",
134: invokedHolder.value);
135: assertSame("transport not passed to ServerTransportCallback",
136: transport, transportHolder.value);
137:
138: assertNotNull("no context passed from transport",
139: ctxHolder.value);
140: InputStreamMessageContext ctx = ctxHolder.value;
141: assertTrue("context is of incorrect type: " + ctx.getClass(),
142: ctx instanceof GeronimoInputStreamMessageContext);
143: GeronimoInputStreamMessageContext inctx = (GeronimoInputStreamMessageContext) ctx;
144: assertNotNull("request not available in context", inctx
145: .getRequest());
146:
147: assertNotNull("response not available in context", inctx
148: .getResponse());
149: }
150:
151: public void testCreateOutputStreamContext() throws IOException {
152:
153: MessageContext mc = EasyMock.createMock(MessageContext.class);
154: Response resp = EasyMock.createNiceMock(Response.class);
155: resp.setStatusCode(200);
156: EasyMock.expect(resp.getOutputStream()).andReturn(
157: EasyMock.createNiceMock(OutputStream.class));
158:
159: EasyMock.expect(
160: mc.get(GeronimoInputStreamMessageContext.RESPONSE))
161: .andReturn(resp);
162:
163: EasyMock.expect(mc.get("foo")).andReturn("ret");
164: EasyMock.replay(mc);
165: EasyMock.replay(resp);
166:
167: OutputStreamMessageContext outctx = transport
168: .createOutputStreamContext(mc);
169:
170: assertNotNull("received null context from the transoprt",
171: outctx);
172: assertTrue(
173: "incorrect type for context" + outctx.getClass(),
174: outctx instanceof GeronimoOutputStreamServerMessageContext);
175:
176: assertNotNull("no outputstream in context", outctx
177: .getOutputStream());
178:
179: // check that context passed in has been wrapped.
180:
181: outctx.get("foo");
182: EasyMock.verify(mc);
183:
184: }
185:
186: static class DummyRequest implements Request {
187:
188: private Map<String, String> headers = new HashMap<String, String>();
189:
190: public void setHeader(String header, String value) {
191: headers.put(header, value);
192: }
193:
194: public String getHeader(String header) {
195: return headers.get(header);
196: }
197:
198: public URI getURI() {
199: // TODO Auto-generated method stub
200: return null;
201: }
202:
203: public int getContentLength() {
204: // TODO Auto-generated method stub
205: return 0;
206: }
207:
208: public String getContentType() {
209: // TODO Auto-generated method stub
210: return null;
211: }
212:
213: public InputStream getInputStream() throws IOException {
214: // TODO Auto-generated method stub
215: return null;
216: }
217:
218: public int getMethod() {
219: // TODO Auto-generated method stub
220: return 0;
221: }
222:
223: public String getParameter(String arg0) {
224: // TODO Auto-generated method stub
225: return null;
226: }
227:
228: public Map getParameters() {
229: // TODO Auto-generated method stub
230: return null;
231: }
232:
233: public Object getAttribute(String arg0) {
234: // TODO Auto-generated method stub
235: return null;
236: }
237:
238: public void setAttribute(String arg0, Object arg1) {
239: // TODO Auto-generated method stub
240:
241: }
242:
243: }
244:
245: }
|