You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2015/06/25 00:52:34 UTC

svn commit: r1687390 - in /felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest: BaseIntegrationTest.java EventListenerTest.java

Author: cziegeler
Date: Wed Jun 24 22:52:33 2015
New Revision: 1687390

URL: http://svn.apache.org/r1687390
Log:
Add listener tests

Modified:
    felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java
    felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/EventListenerTest.java

Modified: felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java?rev=1687390&r1=1687389&r2=1687390&view=diff
==============================================================================
--- felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java (original)
+++ felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java Wed Jun 24 22:52:33 2015
@@ -313,15 +313,15 @@ public abstract class BaseIntegrationTes
             when( localRepo.length() > 0 ).useOptions(
                     systemProperty("org.ops4j.pax.url.mvn.localRepository").value(localRepo)
             ),
-//                        CoreOptions.vmOption("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8787"),
+//            CoreOptions.vmOption("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8787"),
+
+            mavenBundle("org.slf4j", "slf4j-api", "1.7.5"),
+            mavenBundle("org.slf4j", "jcl-over-slf4j", "1.7.5"),
+            mavenBundle("org.slf4j", "log4j-over-slf4j", "1.7.5"),
 
             mavenBundle("org.apache.sling", "org.apache.sling.commons.log", "4.0.0"),
             mavenBundle("org.apache.sling", "org.apache.sling.commons.logservice", "1.0.2"),
 
-            mavenBundle("org.slf4j", "slf4j-api", "1.6.4"),
-            mavenBundle("org.slf4j", "jcl-over-slf4j", "1.6.4"),
-            mavenBundle("org.slf4j", "log4j-over-slf4j", "1.6.4"),
-
             mavenBundle("org.apache.felix", "org.apache.felix.http.api").startLevel(START_LEVEL_SYSTEM_BUNDLES),
             mavenBundle("org.apache.felix", "org.apache.felix.http.servlet-api").startLevel(START_LEVEL_SYSTEM_BUNDLES),
             mavenBundle("org.apache.felix", ORG_APACHE_FELIX_HTTP_JETTY).startLevel(START_LEVEL_SYSTEM_BUNDLES),

Modified: felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/EventListenerTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/EventListenerTest.java?rev=1687390&r1=1687389&r2=1687390&view=diff
==============================================================================
--- felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/EventListenerTest.java (original)
+++ felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/EventListenerTest.java Wed Jun 24 22:52:33 2015
@@ -18,15 +18,20 @@
  */
 package org.apache.felix.http.itest;
 
+import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
 import static javax.servlet.http.HttpServletResponse.SC_OK;
 import static javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE;
 import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
-import java.net.URL;
+import java.util.Dictionary;
+import java.util.Hashtable;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
+import javax.servlet.Servlet;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -42,6 +47,7 @@ import org.ops4j.pax.exam.junit.PaxExam;
 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
 import org.ops4j.pax.exam.spi.reactors.PerMethod;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
 
 /**
  * Test cases for all supported event listeners.
@@ -52,11 +58,27 @@ import org.osgi.framework.ServiceRegistr
 @ExamReactorStrategy(PerMethod.class)
 public class EventListenerTest extends BaseIntegrationTest
 {
+    private Dictionary<String, Object> getListenerProps()
+    {
+        final Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, "true");
+
+        return props;
+    }
+
+    private Dictionary<String, Object> getServletProps(final String pattern)
+    {
+        final Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, pattern);
+
+        return props;
+    }
+
     /**
      * Tests that {@link HttpSessionListener}s are called whenever a session is created or destroyed.
      */
     @Test
-    public void testHttpSessionListenerOk() throws Exception
+    public void testHttpSessionListenerOldWhiteboardOk() throws Exception
     {
         final CountDownLatch createdLatch = new CountDownLatch(1);
         final CountDownLatch destroyedLatch = new CountDownLatch(1);
@@ -76,7 +98,7 @@ public class EventListenerTest extends B
             }
         };
 
-        ServiceRegistration reg = m_context.registerService(HttpSessionListener.class.getName(), listener, null);
+        ServiceRegistration<HttpSessionListener> reg = m_context.registerService(HttpSessionListener.class, listener, null);
 
         register("/session", new TestServlet()
         {
@@ -93,12 +115,12 @@ public class EventListenerTest extends B
 
         try
         {
-            assertContent(SC_OK, null, new URL("http://localhost:8080/session"));
+            assertContent(SC_OK, null, createURL("/session"));
 
             // Session should been created...
             assertTrue(createdLatch.await(50, TimeUnit.SECONDS));
 
-            assertContent(SC_OK, null, new URL("http://localhost:8080/session"));
+            assertContent(SC_OK, null, createURL("/session"));
 
             // Session should timeout automatically...
             assertTrue(destroyedLatch.await(50, TimeUnit.SECONDS));
@@ -113,7 +135,7 @@ public class EventListenerTest extends B
      * Tests that {@link HttpSessionAttributeListener}s are called whenever a session attribute is added, changed or removed.
      */
     @Test
-    public void testHttpSessionAttributeListenerOk() throws Exception
+    public void testHttpSessionAttributeListenerOldWhiteboardOk() throws Exception
     {
         final CountDownLatch addedLatch = new CountDownLatch(1);
         final CountDownLatch removedLatch = new CountDownLatch(1);
@@ -140,7 +162,7 @@ public class EventListenerTest extends B
             }
         };
 
-        ServiceRegistration reg = m_context.registerService(HttpSessionAttributeListener.class.getName(), listener, null);
+        ServiceRegistration<HttpSessionAttributeListener> reg = m_context.registerService(HttpSessionAttributeListener.class, listener, null);
 
         register("/session", new TestServlet()
         {
@@ -165,6 +187,11 @@ public class EventListenerTest extends B
 
                     resp.setStatus(SC_OK);
                 }
+                catch (AssertionError ae)
+                {
+                    resp.sendError(SC_INTERNAL_SERVER_ERROR, ae.getMessage());
+                    throw ae;
+                }
                 catch (InterruptedException e)
                 {
                     resp.sendError(SC_SERVICE_UNAVAILABLE, e.getMessage());
@@ -178,11 +205,191 @@ public class EventListenerTest extends B
 
         try
         {
-            assertContent(SC_OK, null, new URL("http://localhost:8080/session"));
+            assertContent(SC_OK, null, createURL("/session"));
+        }
+        finally
+        {
+            reg.unregister();
+        }
+    }
+
+    /**
+     * Tests that {@link HttpSessionListener}s are called whenever a session is created or destroyed.
+     */
+    @Test
+    public void testHttpSessionListenerOk() throws Exception
+    {
+        final CountDownLatch createdLatch = new CountDownLatch(1);
+        final CountDownLatch destroyedLatch = new CountDownLatch(1);
+
+        HttpSessionListener listener = new HttpSessionListener()
+        {
+            @Override
+            public void sessionDestroyed(HttpSessionEvent se)
+            {
+                destroyedLatch.countDown();
+            }
+
+            @Override
+            public void sessionCreated(HttpSessionEvent se)
+            {
+                createdLatch.countDown();
+            }
+        };
+
+        ServiceRegistration<HttpSessionListener> reg = m_context.registerService(HttpSessionListener.class, listener, getListenerProps());
+        ServiceRegistration<Servlet> regS = m_context.registerService(Servlet.class,
+            new TestServlet()
+            {
+                @Override
+                protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+                {
+                    HttpSession session = req.getSession();
+                    session.setMaxInactiveInterval(2);
+
+                    resp.setStatus(SC_OK);
+                    resp.flushBuffer();
+                }
+            }, getServletProps("/session"));
+
+        try
+        {
+            assertContent(SC_OK, null, createURL("/session"));
+
+            // Session should been created...
+            assertTrue(createdLatch.await(50, TimeUnit.SECONDS));
+
+            assertContent(SC_OK, null, createURL("/session"));
+
+            // Session should timeout automatically...
+            assertTrue(destroyedLatch.await(50, TimeUnit.SECONDS));
+        }
+        finally
+        {
+            reg.unregister();
+            regS.unregister();
+        }
+    }
+
+    /**
+     * Tests that {@link HttpSessionAttributeListener}s are called whenever a session attribute is added, changed or removed.
+     */
+    @Test
+    public void testHttpSessionAttributeListenerOk() throws Exception
+    {
+        final CountDownLatch addedLatch = new CountDownLatch(1);
+        final CountDownLatch removedLatch = new CountDownLatch(1);
+        final CountDownLatch replacedLatch = new CountDownLatch(1);
+
+        HttpSessionAttributeListener listener = new HttpSessionAttributeListener()
+        {
+            @Override
+            public void attributeAdded(HttpSessionBindingEvent event)
+            {
+                addedLatch.countDown();
+            }
+
+            @Override
+            public void attributeRemoved(HttpSessionBindingEvent event)
+            {
+                removedLatch.countDown();
+            }
+
+            @Override
+            public void attributeReplaced(HttpSessionBindingEvent event)
+            {
+                replacedLatch.countDown();
+            }
+        };
+
+        ServiceRegistration<HttpSessionAttributeListener> reg = m_context.registerService(HttpSessionAttributeListener.class, listener, getListenerProps());
+
+        ServiceRegistration<Servlet> regS = m_context.registerService(Servlet.class,
+            new TestServlet()
+            {
+                @Override
+                protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+                {
+                    try
+                    {
+                        HttpSession session = req.getSession();
+
+                        session.setAttribute("foo", "bar");
+
+                        assertTrue(addedLatch.await(5, TimeUnit.SECONDS));
+
+                        session.setAttribute("foo", "qux");
+
+                        assertTrue(replacedLatch.await(5, TimeUnit.SECONDS));
+
+                        session.removeAttribute("foo");
+
+                        assertTrue(removedLatch.await(5, TimeUnit.SECONDS));
+
+                        resp.setStatus(SC_OK);
+                    }
+                    catch (AssertionError ae)
+                    {
+                        resp.sendError(SC_INTERNAL_SERVER_ERROR, ae.getMessage());
+                        throw ae;
+                    }
+                    catch (InterruptedException e)
+                    {
+                        resp.sendError(SC_SERVICE_UNAVAILABLE, e.getMessage());
+                    }
+                    finally
+                    {
+                        resp.flushBuffer();
+                    }
+                }
+            }, getServletProps("/session"));
+
+        try
+        {
+            assertContent(SC_OK, null, createURL("/session"));
+        }
+        finally
+        {
+            reg.unregister();
+        }
+    }
+
+    /**
+     * Tests {@link ServletContextListener}s
+     */
+    @Test
+    public void testServletContextListener() throws Exception
+    {
+        final CountDownLatch initLatch = new CountDownLatch(1);
+        final CountDownLatch destroyLatch = new CountDownLatch(1);
+
+        final ServletContextListener listener = new ServletContextListener()
+        {
+
+            @Override
+            public void contextInitialized(final ServletContextEvent sce)
+            {
+                initLatch.countDown();
+            }
+
+            @Override
+            public void contextDestroyed(final ServletContextEvent sce)
+            {
+                destroyLatch.countDown();
+            }
+        };
+
+        // register with default context
+        final ServiceRegistration<ServletContextListener> reg = m_context.registerService(ServletContextListener.class, listener, getListenerProps());
+
+        try
+        {
+            assertTrue(initLatch.await(5, TimeUnit.SECONDS));
         }
         finally
         {
             reg.unregister();
         }
+        assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
     }
 }