KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty > ContainerTest


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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
18 package org.apache.geronimo.jetty;
19
20 import java.net.HttpURLConnection JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.io.BufferedReader JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27
28 import junit.framework.TestCase;
29 import org.apache.geronimo.gbean.GBeanData;
30 import org.apache.geronimo.jetty.connector.HTTPConnector;
31 import org.apache.geronimo.jetty.app.MockWebServiceContainer;
32 import org.apache.geronimo.kernel.KernelFactory;
33 import org.apache.geronimo.kernel.Kernel;
34 import org.apache.geronimo.kernel.management.State;
35 import org.apache.geronimo.webservices.WebServiceContainer;
36
37 /**
38  * @version $Rev: 169154 $ $Date: 2005-05-08 12:35:23 -0700 (Sun, 08 May 2005) $
39  */

40 public class ContainerTest extends TestCase {
41     private ClassLoader JavaDoc cl = this.getClass().getClassLoader();
42     private Kernel kernel;
43     private GBeanData container;
44     private ObjectName JavaDoc containerName;
45     private Set JavaDoc containerPatterns;
46     private ObjectName JavaDoc connectorName;
47
48     public void testServer() throws Exception JavaDoc {
49         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(containerName));
50     }
51
52     public void testHTTPConnector() throws Exception JavaDoc {
53         GBeanData connector = new GBeanData(connectorName, HTTPConnector.GBEAN_INFO);
54         connector.setAttribute("port", new Integer JavaDoc(5678));
55         connector.setReferencePatterns("JettyContainer", containerPatterns);
56         start(connector);
57
58         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(connectorName));
59
60         HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc) new URL JavaDoc("http://localhost:5678").openConnection();
61         try {
62             connection.getInputStream();
63             fail();
64         } catch (Exception JavaDoc e) {
65             // 404 proves we spoke to the server even if we didn't get anything
66
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, connection.getResponseCode());
67             connection.disconnect();
68         }
69         stop(connectorName);
70     }
71
72     public void testWebServiceHandler() throws Exception JavaDoc {
73         GBeanData connector = new GBeanData(connectorName, HTTPConnector.GBEAN_INFO);
74         connector.setAttribute("port", new Integer JavaDoc(5678));
75         connector.setReferencePatterns("JettyContainer", containerPatterns);
76         start(connector);
77
78         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(connectorName));
79
80         String JavaDoc contextPath = "/foo/webservice.ws";
81         MockWebServiceContainer webServiceInvoker = new MockWebServiceContainer();
82         kernel.invoke(containerName, "addWebService", new Object JavaDoc[] {contextPath, webServiceInvoker}, new String JavaDoc[] {String JavaDoc.class.getName(), WebServiceContainer.class.getName()});
83
84         HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc) new URL JavaDoc("http://localhost:5678" + contextPath).openConnection();
85         try {
86             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(connection.getInputStream()));
87             assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
88             assertEquals("Hello World", reader.readLine());
89         } finally {
90             connection.disconnect();
91         }
92         kernel.invoke(containerName, "removeWebService", new Object JavaDoc[] {contextPath}, new String JavaDoc[] {String JavaDoc.class.getName()});
93         connection = (HttpURLConnection JavaDoc) new URL JavaDoc("http://localhost:5678" + contextPath).openConnection();
94         try {
95             connection.getInputStream();
96             fail();
97         } catch (Exception JavaDoc e) {
98             // see if we removed the ws.
99
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, connection.getResponseCode());
100             connection.disconnect();
101         }
102         stop(connectorName);
103     }
104
105     private void start(GBeanData instance) throws Exception JavaDoc {
106         kernel.loadGBean(instance, cl);
107         kernel.startGBean(instance.getName());
108     }
109
110     private void stop(ObjectName JavaDoc name) throws Exception JavaDoc {
111         kernel.stopGBean(name);
112         kernel.unloadGBean(name);
113     }
114
115     protected void setUp() throws Exception JavaDoc {
116         containerName = new ObjectName JavaDoc("geronimo.jetty:role=Container");
117         containerPatterns = new HashSet JavaDoc();
118         containerPatterns.add(containerName);
119         connectorName = new ObjectName JavaDoc("geronimo.jetty:role=Connector");
120         kernel = KernelFactory.newInstance().createKernel("test.kernel");
121         kernel.boot();
122         container = new GBeanData(containerName, JettyContainerImpl.GBEAN_INFO);
123         start(container);
124     }
125
126     protected void tearDown() throws Exception JavaDoc {
127         stop(containerName);
128         kernel.shutdown();
129     }
130 }
131
Popular Tags