001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.transport.http_jetty;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.InputStream;
022: import java.net.HttpURLConnection;
023: import java.net.URL;
024: import java.net.URLConnection;
025: import java.util.ArrayList;
026: import java.util.List;
027:
028: import org.apache.cxf.Bus;
029: import org.apache.cxf.configuration.Configurer;
030: import org.apache.cxf.configuration.jsse.TLSServerParameters;
031: import org.apache.cxf.configuration.spring.ConfigurerImpl;
032: import org.apache.cxf.helpers.IOUtils;
033: import org.easymock.classextension.EasyMock;
034: import org.easymock.classextension.IMocksControl;
035: import org.junit.Assert;
036: import org.junit.Before;
037: import org.junit.Test;
038: import org.mortbay.jetty.Connector;
039: import org.mortbay.jetty.Handler;
040: import org.mortbay.jetty.handler.ContextHandler;
041: import org.mortbay.jetty.nio.SelectChannelConnector;
042: import org.mortbay.jetty.security.SslSocketConnector;
043:
044: public class JettyHTTPServerEngineTest extends Assert {
045:
046: private Bus bus;
047: private IMocksControl control;
048: private JettyHTTPServerEngineFactory factory;
049:
050: @Before
051: public void setUp() throws Exception {
052: control = EasyMock.createNiceControl();
053: bus = control.createMock(Bus.class);
054: factory = new JettyHTTPServerEngineFactory();
055: factory.setBus(bus);
056:
057: Configurer configurer = new ConfigurerImpl();
058:
059: bus.getExtension(Configurer.class);
060: EasyMock.expectLastCall().andReturn(configurer).anyTimes();
061: control.replay();
062: }
063:
064: @Test
065: public void testEngineRetrieval() throws Exception {
066: JettyHTTPServerEngine engine = factory
067: .createJettyHTTPServerEngine(1234, "http");
068:
069: assertTrue(
070: "Engine references for the same port should point to the same instance",
071: engine == factory.retrieveJettyHTTPServerEngine(1234));
072:
073: factory.destroyForPort(1234);
074: }
075:
076: @Test
077: public void testHttpAndHttps() throws Exception {
078: JettyHTTPServerEngine engine = factory
079: .createJettyHTTPServerEngine(1234, "http");
080:
081: assertTrue("Protocol must be http", "http".equals(engine
082: .getProtocol()));
083:
084: engine = new JettyHTTPServerEngine();
085: engine.setPort(1235);
086: engine.setTlsServerParameters(new TLSServerParameters());
087: engine.finalizeConfig();
088:
089: List<JettyHTTPServerEngine> list = new ArrayList<JettyHTTPServerEngine>();
090: list.add(engine);
091: factory.setEnginesList(list);
092:
093: engine = factory.createJettyHTTPServerEngine(1235, "https");
094:
095: assertTrue("Protocol must be https", "https".equals(engine
096: .getProtocol()));
097:
098: factory.destroyForPort(1234);
099: factory.destroyForPort(1235);
100: }
101:
102: @Test
103: public void testSetConnector() throws Exception {
104: JettyHTTPServerEngine engine = new JettyHTTPServerEngine();
105: Connector conn = new SslSocketConnector();
106: engine.setConnector(conn);
107: engine.setPort(9000);
108: try {
109: engine.finalizeConfig();
110: fail("We should get the connector not set with TSLServerParament exception ");
111: } catch (Exception ex) {
112: // expect the excepion
113: }
114:
115: engine = new JettyHTTPServerEngine();
116: conn = new SelectChannelConnector();
117: conn.setPort(9002);
118: engine.setConnector(conn);
119: engine.setPort(9000);
120: try {
121: engine.finalizeConfig();
122: fail("We should get the connector not set right port exception ");
123: } catch (Exception ex) {
124: // expect the excepion
125: }
126:
127: engine = new JettyHTTPServerEngine();
128: conn = new SslSocketConnector();
129: conn.setPort(9003);
130: engine.setConnector(conn);
131: engine.setPort(9003);
132: engine.setTlsServerParameters(new TLSServerParameters());
133: try {
134: engine.finalizeConfig();
135: } catch (Exception ex) {
136: fail("we should not throw exception here");
137: }
138: }
139:
140: @Test
141: public void testaddServants() throws Exception {
142: String urlStr = "http://localhost:1234/hello/test";
143: JettyHTTPServerEngine engine = factory
144: .createJettyHTTPServerEngine(1234, "http");
145: JettyHTTPTestHandler handler1 = new JettyHTTPTestHandler(
146: "string1");
147: JettyHTTPTestHandler handler2 = new JettyHTTPTestHandler(
148: "string2");
149: engine.addServant(new URL(urlStr), handler1);
150: String response = null;
151: try {
152: response = getResponse(urlStr);
153: } catch (Exception ex) {
154: fail("Can't get the reponse from the server " + ex);
155: }
156: assertEquals("the jetty http handler did not take effect",
157: response, "string1");
158:
159: engine.addServant(new URL(urlStr), handler2);
160: try {
161: response = getResponse(urlStr);
162: } catch (Exception ex) {
163: fail("Can't get the reponse from the server " + ex);
164: }
165: assertEquals("the jetty http handler did not take effect",
166: response, "string1string2");
167:
168: // set the get request
169: factory.destroyForPort(1234);
170:
171: }
172:
173: @Test
174: public void testSetHandlers() throws Exception {
175: URL url = new URL("http://localhost:1245/hello/test");
176: JettyHTTPTestHandler handler1 = new JettyHTTPTestHandler(
177: "string1");
178: JettyHTTPTestHandler handler2 = new JettyHTTPTestHandler(
179: "string2");
180:
181: JettyHTTPServerEngine engine = new JettyHTTPServerEngine();
182: engine.setPort(1245);
183:
184: List<Handler> handlers = new ArrayList<Handler>();
185: handlers.add(handler1);
186: engine.setHandlers(handlers);
187: engine.finalizeConfig();
188:
189: engine.addServant(url, handler2);
190: String response = null;
191: try {
192: response = getResponse(url.toString());
193: assertEquals("the jetty http handler1 did not take effect",
194: response, "string1string2");
195: } catch (Exception ex) {
196: fail("Can't get the reponse from the server " + ex);
197: }
198: engine.stop();
199: }
200:
201: @Test
202: public void testGetContextHandler() throws Exception {
203: String urlStr = "http://localhost:1234/hello/test";
204: JettyHTTPServerEngine engine = factory
205: .createJettyHTTPServerEngine(1234, "http");
206: ContextHandler contextHandler = engine
207: .getContextHandler(new URL(urlStr));
208: // can't find the context handler here
209: assertNull(contextHandler);
210: JettyHTTPTestHandler handler1 = new JettyHTTPTestHandler(
211: "string1");
212: JettyHTTPTestHandler handler2 = new JettyHTTPTestHandler(
213: "string2");
214: engine.addServant(new URL(urlStr), handler1);
215:
216: contextHandler = engine.getContextHandler(new URL(urlStr));
217: contextHandler.setHandler(handler2);
218: contextHandler.start();
219:
220: String response = null;
221: try {
222: response = getResponse(urlStr);
223: } catch (Exception ex) {
224: fail("Can't get the reponse from the server " + ex);
225: }
226: assertEquals("the jetty http handler did not take effect",
227: response, "string2");
228: factory.destroyForPort(1234);
229: }
230:
231: private String getResponse(String target) throws Exception {
232: URL url = new URL(target);
233:
234: URLConnection connection = url.openConnection();
235:
236: assertTrue(connection instanceof HttpURLConnection);
237: connection.connect();
238: InputStream in = connection.getInputStream();
239: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
240: IOUtils.copy(in, buffer);
241: return buffer.toString();
242: }
243:
244: }
|