ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ange...@apache.org
Subject svn commit: r1063839 - in /incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http: ./ EchoServlet.java MockHttpService.java ServletConfiguratorIntegrationTest.java
Date Wed, 26 Jan 2011 19:22:32 GMT
Author: angelos
Date: Wed Jan 26 19:22:32 2011
New Revision: 1063839

URL: http://svn.apache.org/viewvc?rev=1063839&view=rev
Log:
ACE-79 Ported over the http service test.

Added:
    incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/
    incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/EchoServlet.java
    incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/MockHttpService.java
    incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/ServletConfiguratorIntegrationTest.java

Added: incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/EchoServlet.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/EchoServlet.java?rev=1063839&view=auto
==============================================================================
--- incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/EchoServlet.java
(added)
+++ incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/EchoServlet.java
Wed Jan 26 19:22:32 2011
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.it.http;
+
+import java.io.IOException;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class EchoServlet extends HttpServlet {
+
+    private static final long serialVersionUID = 1L;
+
+    @Override
+    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
+        ServletOutputStream output = null;
+        try {
+            output = response.getOutputStream();
+            output.println(request.getQueryString());
+        }
+        catch (IOException e) {
+            // not much we can do, the test will fail anyway
+        } finally {
+            if (output != null) {
+                try {
+                    output.close();
+                }
+                catch (IOException e) {
+                    // not much we can do, the test will fail anyway
+                }
+            }
+        }
+    }
+}

Added: incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/MockHttpService.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/MockHttpService.java?rev=1063839&view=auto
==============================================================================
--- incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/MockHttpService.java
(added)
+++ incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/MockHttpService.java
Wed Jan 26 19:22:32 2011
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.it.http;
+
+import java.util.Dictionary;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+
+import org.osgi.service.http.HttpContext;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+
+public class MockHttpService implements HttpService {
+
+    private boolean m_registerCalled = false;
+    private boolean m_unregisterCalled = false;
+
+    public HttpContext createDefaultHttpContext() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public void registerResources(String arg0, String arg1, HttpContext arg2) throws NamespaceException
{
+        // TODO Auto-generated method stub
+
+    }
+
+    @SuppressWarnings("unchecked")
+    public void registerServlet(String arg0, Servlet arg1, Dictionary arg2, HttpContext arg3)
throws ServletException, NamespaceException {
+        m_registerCalled = true;
+    }
+
+    public void unregister(String arg0) {
+        m_unregisterCalled = true;
+    }
+
+    public boolean isRegisterCalled() {
+        return m_registerCalled;
+    }
+
+    public boolean isUnregisterCalled() {
+        return m_unregisterCalled;
+    }
+}

Added: incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/ServletConfiguratorIntegrationTest.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/ServletConfiguratorIntegrationTest.java?rev=1063839&view=auto
==============================================================================
--- incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/ServletConfiguratorIntegrationTest.java
(added)
+++ incubator/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/http/ServletConfiguratorIntegrationTest.java
Wed Jan 26 19:22:32 2011
@@ -0,0 +1,190 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.it.http;
+
+import org.apache.ace.http.listener.constants.HttpConstants;
+import org.apache.ace.it.IntegrationTestBase;
+import org.apache.ace.test.constants.TestConstants;
+import org.apache.felix.dm.Component;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.container.def.options.VMOption;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.log.LogService;
+
+import javax.servlet.http.HttpServlet;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import static org.apache.ace.it.Options.*;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.provision;
+import static org.ops4j.pax.exam.CoreOptions.systemProperty;
+
+@RunWith(JUnit4TestRunner.class)
+public class ServletConfiguratorIntegrationTest extends IntegrationTestBase {
+
+    @Configuration
+    public Option[] configuration() {
+        return options(
+            systemProperty("org.osgi.service.http.port").value("" + TestConstants.PORT),
+            new VMOption("-ea"),
+            provision(
+                Osgi.compendium(),
+                Felix.dependencyManager(),
+                jetty(),
+                Felix.configAdmin(),
+                Ace.httplistener()
+            )
+        );
+    }
+
+    protected void before() throws IOException {
+        m_echoServlet = new EchoServlet();
+        Dictionary<String, String> dictionary = new Hashtable<String, String>();
+        dictionary.put(HttpConstants.ENDPOINT, "/echoServlet");
+        m_echoServletService = m_dependencyManager.createComponent()
+            .setImplementation(m_echoServlet)
+            .setInterface(HttpServlet.class.getName(), dictionary);
+
+        m_mockHttp = new MockHttpService();
+        m_mockHttpService = m_dependencyManager.createComponent()
+            .setImplementation(m_mockHttp)
+            .setInterface(HttpService.class.getName(), null);
+    }
+
+    protected Component[] getDependencies() {
+        return new Component[] {
+            createComponent()
+                .setImplementation(ServletConfiguratorIntegrationTest.class)
+                .add(createServiceDependency()
+                    .setService(HttpService.class)
+                    .setRequired(true))
+                .add(createServiceDependency()
+                    .setService(LogService.class)
+                    .setRequired(false))
+            };
+    }           
+
+    // the echo servlet
+    private HttpServlet m_echoServlet;
+    // echo servlet service-reference
+    private Component m_echoServletService;
+    // mock http service
+    private MockHttpService m_mockHttp;
+
+    //mock http service-reference
+    private Component m_mockHttpService;
+
+    /**
+     * Start the http service and then register a servlet and see if it works
+     * After that, try to unregister
+     */
+    @Test
+    public void testRegisterServlet() throws Exception {
+        m_dependencyManager.add(m_echoServletService);
+        assert waitForEchoServlet(true) : "TestValue not echo'd back";
+
+        m_dependencyManager.remove(m_echoServletService);
+        assert !waitForEchoServlet(false) : "The servlet should not be available anymore";
+    }
+
+    /**
+     * Register a servlet with 2 http services, try to unregister and see if it is removed
from both
+     */
+    @Test
+    public void testServletOnTwoHttpServices() throws Exception {
+        // also use the mock version
+        m_dependencyManager.add(m_mockHttpService);
+        m_dependencyManager.add(m_echoServletService);
+        assert waitForEchoServlet(true) : "TestValue not echo'd back";
+        assert m_mockHttp.isRegisterCalled() : "Servlet not registered with the mock service";
+
+
+        m_dependencyManager.remove(m_echoServletService);
+        assert !waitForEchoServlet(false) : "The servlet should not be available anymore";
+        assert m_mockHttp.isUnregisterCalled() : "Servlet not unregistered with the mock
service";
+    }
+
+    /**
+     * Now the server should be made available at
+     * http://SERVER:PORT/echoservlet and if it is not available after
+     * some time, the test is failed anyway.
+     *
+     * The expectSuccess parameter indicated if this method should expect a working echoservlet
or a non-working one.
+     *
+     * This method returns whether the echo servlet worked. So if you expect
+     * it to work (and it does), true will be returned.
+     * If you expect it to NOT work (and it doesn't), false will be returned.
+     */
+    private boolean waitForEchoServlet(boolean expectedResult) {
+        BufferedReader bufReader = null;
+
+        long startTimeMillis = System.currentTimeMillis();
+        // make sure we iterate at least once
+        boolean success = !expectedResult;
+        try {
+            while ((expectedResult != success) && (System.currentTimeMillis() <
startTimeMillis + 30000)) {
+                URL echoServletUrl = new URL("http://localhost:" + TestConstants.PORT + "/echoServlet?test");
+                String echoString = null;
+                try {
+                    bufReader = new BufferedReader(new InputStreamReader(echoServletUrl.openStream()));
+                    echoString = bufReader.readLine();
+                } catch (IOException ioe) {
+                    // let's wait and try again.
+                }
+                boolean resultFromServlet = (echoString != null) && echoString.equals("test");
+                if (resultFromServlet == expectedResult) {
+                    success = expectedResult;
+                }
+                if ((expectedResult != success)) {
+                    Thread.sleep(100);
+                }
+            }
+        }catch (MalformedURLException e) {
+            e.printStackTrace();
+            assert false : "No MalformedURLException expected";
+        }
+        catch (InterruptedException e) {
+            e.printStackTrace();
+            assert false : "No interruptedException expected";
+        } catch (Throwable t) {
+            t.printStackTrace();
+        } finally {
+            if (bufReader != null) {
+                try {
+                    bufReader.close();
+                }
+                catch (Exception ex) {
+                    // not much we can do
+                }
+            }
+        }
+        return success;
+    }
+
+}



Mime
View raw message