Added itests showing that HttpSession(Attribute)Listener work as expected.



git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1586722 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 99aec42..14840a2 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
@@ -85,6 +85,11 @@
         private final CountDownLatch m_initLatch;
         private final CountDownLatch m_destroyLatch;
 
+        public TestFilter()
+        {
+            this(null, null);
+        }
+        
         public TestFilter(CountDownLatch initLatch, CountDownLatch destroyLatch)
         {
             m_initLatch = initLatch;
@@ -125,6 +130,11 @@
         private final CountDownLatch m_initLatch;
         private final CountDownLatch m_destroyLatch;
 
+        public TestServlet()
+        {
+            this(null, null);
+        }
+        
         public TestServlet(CountDownLatch initLatch, CountDownLatch destroyLatch)
         {
             m_initLatch = initLatch;
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
new file mode 100644
index 0000000..6fa9d37
--- /dev/null
+++ b/http/itest/src/test/java/org/apache/felix/http/itest/EventListenerTest.java
@@ -0,0 +1,184 @@
+/*
+ * 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.felix.http.itest;
+
+import static javax.servlet.http.HttpServletResponse.*;
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpSessionAttributeListener;
+import javax.servlet.http.HttpSessionBindingEvent;
+import javax.servlet.http.HttpSessionEvent;
+import javax.servlet.http.HttpSessionListener;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * Test cases for all supported event listeners.
+ * 
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+@RunWith(JUnit4TestRunner.class)
+public class EventListenerTest extends BaseIntegrationTest
+{
+    /**
+     * 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 reg = m_context.registerService(HttpSessionListener.class.getName(), 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, new URL("http://localhost:8080/session"));
+
+            // Session should been created...
+            assertTrue(createdLatch.await(50, TimeUnit.SECONDS));
+
+            assertContent(SC_OK, null, new URL("http://localhost:8080/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 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 reg = m_context.registerService(HttpSessionAttributeListener.class.getName(), 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 (InterruptedException e)
+                {
+                    resp.sendError(SC_SERVICE_UNAVAILABLE, e.getMessage());
+                }
+                finally
+                {
+                    resp.flushBuffer();
+                }
+            }
+        });
+
+        try
+        {
+            assertContent(SC_OK, null, new URL("http://localhost:8080/session"));
+        }
+        finally
+        {
+            reg.unregister();
+        }
+    }
+}