KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > http > ServerManagerTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.http;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.net.HttpURLConnection JavaDoc;
23 import java.net.URL JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import junit.framework.TestCase;
29
30 import org.apache.commons.httpclient.HttpClient;
31 import org.apache.commons.httpclient.HttpMethod;
32 import org.apache.commons.httpclient.methods.GetMethod;
33 import org.apache.commons.httpclient.methods.PostMethod;
34 import org.apache.commons.httpclient.methods.StringRequestEntity;
35 import org.apache.servicemix.components.http.InvalidStatusResponseException;
36 import org.apache.servicemix.http.jetty.JettyContextManager;
37 import org.apache.servicemix.jbi.util.FileUtil;
38 import org.mortbay.thread.BoundedThreadPool;
39
40 public class ServerManagerTest extends TestCase {
41
42     protected JettyContextManager server;
43     protected HttpConfiguration configuration;
44     
45     protected void setUp() throws Exception JavaDoc {
46         System.setProperty("DEBUG", "true");
47         System.setProperty("java.protocol.handler.pkgs", "HTTPClient");
48         configuration = new HttpConfiguration();
49         server = new JettyContextManager();
50         server.setConfiguration(configuration);
51     }
52     
53     protected void tearDown() throws Exception JavaDoc {
54         server.shutDown();
55     }
56     
57     public void test() throws Exception JavaDoc {
58         server.init();
59         server.start();
60         
61         // Test first context
62
checkFail("http://localhost:8192/Service1/echo", null);
63         Object JavaDoc ctx1 = server.createContext("http://localhost:8192/Service1", new TestHttpProcessor());
64         request("http://localhost:8192/Service1/echo", null);
65         server.remove(ctx1);
66         checkFail("http://localhost:8192/Service1/echo", null);
67         
68         // Test second context on the same host/port
69
checkFail("http://localhost:8192/Service2/echo", null);
70         Object JavaDoc ctx2 = server.createContext("http://localhost:8192/Service2", new TestHttpProcessor());
71         request("http://localhost:8192/Service2/echo", null);
72         server.remove(ctx2);
73         checkFail("http://localhost:8192/Service2/echo", null);
74
75         // Test third context on another port
76
checkFail("http://localhost:8193/echo", null);
77         Object JavaDoc ctx3 = server.createContext("http://localhost:8193", new TestHttpProcessor());
78         request("http://localhost:8193/echo", null);
79         server.remove(ctx3);
80         checkFail("http://localhost:8193/echo", null);
81     }
82     
83     public void testOverlappingPath() throws Exception JavaDoc {
84         server.init();
85         server.start();
86         
87         server.createContext("http://localhost:8192/Service1/test1", new TestHttpProcessor());
88         
89         try {
90             server.createContext("http://localhost:8192/Service1/test1", new TestHttpProcessor());
91             fail("Contexts are overlapping, an exception should have been thrown");
92         } catch (Exception JavaDoc e) {
93             // ok
94
}
95         
96         try {
97             server.createContext("http://localhost:8192/Service1/test1/test", new TestHttpProcessor());
98             fail("Contexts are overlapping, an exception should have been thrown");
99         } catch (Exception JavaDoc e) {
100             // ok
101
}
102         
103         try {
104             server.createContext("http://localhost:8192/Service1", new TestHttpProcessor());
105             fail("Contexts are overlapping, an exception should have been thrown");
106         } catch (Exception JavaDoc e) {
107             // ok
108
}
109     }
110     
111     public void testSetMaxThreads() throws Exception JavaDoc {
112         int maxThreads = 512;
113         configuration.setJettyThreadPoolSize(maxThreads);
114         server.init();
115         assertTrue(server.getThreadPool() instanceof BoundedThreadPool);
116         int threads = ((BoundedThreadPool) server.getThreadPool()).getMaxThreads();
117         assertEquals("The max number of threads is incorrect!", maxThreads, threads);
118     }
119     
120     protected void checkFail(String JavaDoc url, String JavaDoc content) {
121         try {
122             request(url, content);
123             fail("Request should have failed: " + url);
124         } catch (Exception JavaDoc e) {
125             //System.out.println(e);
126
}
127     }
128     
129     protected String JavaDoc request(String JavaDoc url, String JavaDoc content) throws Exception JavaDoc {
130         if (true) {
131             return requestWithHttpClient(url, content);
132         } else {
133             return requestWithUrlConnection(url, content);
134         }
135     }
136     
137     private String JavaDoc requestWithHttpClient(String JavaDoc url, String JavaDoc content) throws Exception JavaDoc {
138         HttpMethod method;
139         if (content != null) {
140             PostMethod post = new PostMethod(url);
141             post.setRequestEntity(new StringRequestEntity(content));
142             method = post;
143         } else {
144             GetMethod get = new GetMethod(url);
145             method = get;
146         }
147         new HttpClient().executeMethod(method);
148         if (method.getStatusCode() != 200) {
149             throw new InvalidStatusResponseException(method.getStatusCode());
150         }
151         return method.getResponseBodyAsString();
152     }
153     
154     private String JavaDoc requestWithUrlConnection(String JavaDoc url, String JavaDoc content) throws Exception JavaDoc {
155         HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc) new URL JavaDoc(url).openConnection();
156         try {
157             connection.setDoInput(true);
158             if (content != null) {
159                 connection.setDoOutput(true);
160                 OutputStream JavaDoc os = connection.getOutputStream();
161                 os.write(content.getBytes());
162                 os.close();
163             }
164             InputStream JavaDoc is = connection.getInputStream();
165             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
166             FileUtil.copyInputStream(is, baos);
167             return baos.toString();
168         } finally {
169             connection.disconnect();
170         }
171     }
172
173     public static class TestHttpProcessor implements HttpProcessor {
174         public SslParameters getSsl() {
175             return null;
176         }
177         public String JavaDoc getAuthMethod() {
178             return null;
179         }
180         public void process(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
181             System.out.println(request);
182         }
183         
184     }
185 }
186
Popular Tags