Add listener tests

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1687390 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java b/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java
index 9ee4d12..8426d66 100644
--- a/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java
+++ b/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java
@@ -313,15 +313,15 @@
             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),
diff --git a/http/itest/src/test/java/org/apache/felix/http/itest/EventListenerTest.java b/http/itest/src/test/java/org/apache/felix/http/itest/EventListenerTest.java
index 936bc9b..5f5d9aa 100644
--- a/http/itest/src/test/java/org/apache/felix/http/itest/EventListenerTest.java
+++ b/http/itest/src/test/java/org/apache/felix/http/itest/EventListenerTest.java
@@ -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.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,6 +58,161 @@
 @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 testHttpSessionListenerOldWhiteboardOk() 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, null);
+
+        register("/session", 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();
+            }
+        });
+
+        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();
+        }
+    }
+
+    /**
+     * Tests that {@link HttpSessionAttributeListener}s are called whenever a session attribute is added, changed or removed.
+     */
+    @Test
+    public void testHttpSessionAttributeListenerOldWhiteboardOk() 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, null);
+
+        register("/session", 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();
+                }
+            }
+        });
+
+        try
+        {
+            assertContent(SC_OK, null, createURL("/session"));
+        }
+        finally
+        {
+            reg.unregister();
+        }
+    }
+
     /**
      * Tests that {@link HttpSessionListener}s are called whenever a session is created or destroyed.
      */
@@ -76,29 +237,29 @@
             }
         };
 
-        ServiceRegistration reg = m_context.registerService(HttpSessionListener.class.getName(), listener, null);
-
-        register("/session", new TestServlet()
-        {
-            @Override
-            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+        ServiceRegistration<HttpSessionListener> reg = m_context.registerService(HttpSessionListener.class, listener, getListenerProps());
+        ServiceRegistration<Servlet> regS = m_context.registerService(Servlet.class,
+            new TestServlet()
             {
-                HttpSession session = req.getSession();
-                session.setMaxInactiveInterval(2);
+                @Override
+                protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+                {
+                    HttpSession session = req.getSession();
+                    session.setMaxInactiveInterval(2);
 
-                resp.setStatus(SC_OK);
-                resp.flushBuffer();
-            }
-        });
+                    resp.setStatus(SC_OK);
+                    resp.flushBuffer();
+                }
+            }, getServletProps("/session"));
 
         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));
@@ -106,6 +267,7 @@
         finally
         {
             reg.unregister();
+            regS.unregister();
         }
     }
 
@@ -140,49 +302,94 @@
             }
         };
 
-        ServiceRegistration reg = m_context.registerService(HttpSessionAttributeListener.class.getName(), listener, null);
+        ServiceRegistration<HttpSessionAttributeListener> reg = m_context.registerService(HttpSessionAttributeListener.class, listener, getListenerProps());
 
-        register("/session", new TestServlet()
-        {
-            @Override
-            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+        ServiceRegistration<Servlet> regS = m_context.registerService(Servlet.class,
+            new TestServlet()
             {
-                try
+                @Override
+                protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
                 {
-                    HttpSession session = req.getSession();
+                    try
+                    {
+                        HttpSession session = req.getSession();
 
-                    session.setAttribute("foo", "bar");
+                        session.setAttribute("foo", "bar");
 
-                    assertTrue(addedLatch.await(5, TimeUnit.SECONDS));
+                        assertTrue(addedLatch.await(5, TimeUnit.SECONDS));
 
-                    session.setAttribute("foo", "qux");
+                        session.setAttribute("foo", "qux");
 
-                    assertTrue(replacedLatch.await(5, TimeUnit.SECONDS));
+                        assertTrue(replacedLatch.await(5, TimeUnit.SECONDS));
 
-                    session.removeAttribute("foo");
+                        session.removeAttribute("foo");
 
-                    assertTrue(removedLatch.await(5, TimeUnit.SECONDS));
+                        assertTrue(removedLatch.await(5, TimeUnit.SECONDS));
 
-                    resp.setStatus(SC_OK);
+                        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();
+                    }
                 }
-                catch (InterruptedException e)
-                {
-                    resp.sendError(SC_SERVICE_UNAVAILABLE, e.getMessage());
-                }
-                finally
-                {
-                    resp.flushBuffer();
-                }
-            }
-        });
+            }, getServletProps("/session"));
 
         try
         {
-            assertContent(SC_OK, null, new URL("http://localhost:8080/session"));
+            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));
+    }
 }