1 17 18 package org.apache.geronimo.jetty; 19 20 import java.net.HttpURLConnection ; 21 import java.net.URL ; 22 import java.util.HashSet ; 23 import java.util.Set ; 24 import java.io.BufferedReader ; 25 import java.io.InputStreamReader ; 26 import javax.management.ObjectName ; 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 40 public class ContainerTest extends TestCase { 41 private ClassLoader cl = this.getClass().getClassLoader(); 42 private Kernel kernel; 43 private GBeanData container; 44 private ObjectName containerName; 45 private Set containerPatterns; 46 private ObjectName connectorName; 47 48 public void testServer() throws Exception { 49 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(containerName)); 50 } 51 52 public void testHTTPConnector() throws Exception { 53 GBeanData connector = new GBeanData(connectorName, HTTPConnector.GBEAN_INFO); 54 connector.setAttribute("port", new Integer (5678)); 55 connector.setReferencePatterns("JettyContainer", containerPatterns); 56 start(connector); 57 58 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(connectorName)); 59 60 HttpURLConnection connection = (HttpURLConnection ) new URL ("http://localhost:5678").openConnection(); 61 try { 62 connection.getInputStream(); 63 fail(); 64 } catch (Exception e) { 65 assertEquals(HttpURLConnection.HTTP_NOT_FOUND, connection.getResponseCode()); 67 connection.disconnect(); 68 } 69 stop(connectorName); 70 } 71 72 public void testWebServiceHandler() throws Exception { 73 GBeanData connector = new GBeanData(connectorName, HTTPConnector.GBEAN_INFO); 74 connector.setAttribute("port", new Integer (5678)); 75 connector.setReferencePatterns("JettyContainer", containerPatterns); 76 start(connector); 77 78 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(connectorName)); 79 80 String contextPath = "/foo/webservice.ws"; 81 MockWebServiceContainer webServiceInvoker = new MockWebServiceContainer(); 82 kernel.invoke(containerName, "addWebService", new Object [] {contextPath, webServiceInvoker}, new String [] {String .class.getName(), WebServiceContainer.class.getName()}); 83 84 HttpURLConnection connection = (HttpURLConnection ) new URL ("http://localhost:5678" + contextPath).openConnection(); 85 try { 86 BufferedReader reader = new BufferedReader (new InputStreamReader (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 [] {contextPath}, new String [] {String .class.getName()}); 93 connection = (HttpURLConnection ) new URL ("http://localhost:5678" + contextPath).openConnection(); 94 try { 95 connection.getInputStream(); 96 fail(); 97 } catch (Exception e) { 98 assertEquals(HttpURLConnection.HTTP_NOT_FOUND, connection.getResponseCode()); 100 connection.disconnect(); 101 } 102 stop(connectorName); 103 } 104 105 private void start(GBeanData instance) throws Exception { 106 kernel.loadGBean(instance, cl); 107 kernel.startGBean(instance.getName()); 108 } 109 110 private void stop(ObjectName name) throws Exception { 111 kernel.stopGBean(name); 112 kernel.unloadGBean(name); 113 } 114 115 protected void setUp() throws Exception { 116 containerName = new ObjectName ("geronimo.jetty:role=Container"); 117 containerPatterns = new HashSet (); 118 containerPatterns.add(containerName); 119 connectorName = new ObjectName ("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 { 127 stop(containerName); 128 kernel.shutdown(); 129 } 130 } 131 | Popular Tags |