You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by de...@apache.org on 2018/05/03 18:26:18 UTC

[myfaces-test] 05/08: MYFACESTEST-5 Implementation of MockHttpSession.invalidate() (Thanks to Christoph Göldner for this patch) + test case

This is an automated email from the ASF dual-hosted git repository.

deki pushed a commit to branch 1_0_0_beta_2
in repository https://gitbox.apache.org/repos/asf/myfaces-test.git

commit 706198feaf34edaf0619c4896d1f74c0d02b6521
Author: Jakob Korherr <ja...@apache.org>
AuthorDate: Mon Mar 15 17:17:03 2010 +0000

    MYFACESTEST-5 Implementation of MockHttpSession.invalidate() (Thanks to Christoph Göldner for this patch) + test case
---
 .../apache/myfaces/test/mock/MockHttpSession.java  |  26 ++++-
 .../myfaces/test/mock/MockHttpSessionTest.java     | 124 +++++++++++++++++++++
 2 files changed, 148 insertions(+), 2 deletions(-)

diff --git a/test12/src/main/java/org/apache/myfaces/test/mock/MockHttpSession.java b/test12/src/main/java/org/apache/myfaces/test/mock/MockHttpSession.java
index 80fb5cb..62686de 100644
--- a/test12/src/main/java/org/apache/myfaces/test/mock/MockHttpSession.java
+++ b/test12/src/main/java/org/apache/myfaces/test/mock/MockHttpSession.java
@@ -98,6 +98,7 @@ public class MockHttpSession implements HttpSession {
     private HashMap attributes = new HashMap();
     private String id = "123";
     private ServletContext servletContext = null;
+    private boolean invalid = false;
 
 
     // ---------------------------------------------------------- Public Methods
@@ -119,6 +120,8 @@ public class MockHttpSession implements HttpSession {
     /** {@inheritDoc} */
     public Object getAttribute(String name) {
 
+        assertValidity();
+
         return attributes.get(name);
 
     }
@@ -127,6 +130,8 @@ public class MockHttpSession implements HttpSession {
     /** {@inheritDoc} */
     public Enumeration getAttributeNames() {
 
+        assertValidity();
+
         return new MockEnumeration(attributes.keySet().iterator());
 
     }
@@ -198,9 +203,11 @@ public class MockHttpSession implements HttpSession {
 
     /** {@inheritDoc} */
     public void invalidate() {
+        
+        assertValidity();
 
-        throw new UnsupportedOperationException();
-
+        attributes.clear();
+        invalid = true;
     }
 
 
@@ -223,6 +230,8 @@ public class MockHttpSession implements HttpSession {
     /** {@inheritDoc} */
     public void removeAttribute(String name) {
 
+        assertValidity();
+
         if (attributes.containsKey(name)) {
             Object value = attributes.remove(name);
             fireAttributeRemoved(name, value);
@@ -242,6 +251,8 @@ public class MockHttpSession implements HttpSession {
     /** {@inheritDoc} */
     public void setAttribute(String name, Object value) {
 
+        assertValidity();
+
         if (name == null) {
             throw new IllegalArgumentException("Attribute name cannot be null");
         }
@@ -335,4 +346,15 @@ public class MockHttpSession implements HttpSession {
     }
 
 
+    /**
+     * <p>Throws an {@link IllegalStateException} if this session is invalid.</p>
+     */
+    private void assertValidity() 
+    {
+        if (invalid) 
+        {
+            throw new IllegalStateException("Session is invalid.");
+        }
+    }
+
 }
diff --git a/test12/src/test/java/org/apache/myfaces/test/mock/MockHttpSessionTest.java b/test12/src/test/java/org/apache/myfaces/test/mock/MockHttpSessionTest.java
new file mode 100644
index 0000000..014f100
--- /dev/null
+++ b/test12/src/test/java/org/apache/myfaces/test/mock/MockHttpSessionTest.java
@@ -0,0 +1,124 @@
+/*
+ * 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.myfaces.test.mock;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.myfaces.test.base.AbstractJsfTestCase;
+
+/**
+ * Test class for MockHttpSession.
+ * 
+ * @author Jakob Korherr (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class MockHttpSessionTest extends AbstractJsfTestCase
+{
+
+    public static Test suite() 
+    {
+        return (new TestSuite(MockHttpSessionTest.class));
+    }
+    
+    private MockHttpSession session;
+    
+    public MockHttpSessionTest(String name)
+    {
+        super(name);
+    }
+    
+    protected void setUp() throws Exception 
+    {
+        super.setUp();
+        
+        session = new MockHttpSession();
+    }
+
+    protected void tearDown() throws Exception 
+    {
+        super.tearDown();
+        
+        session = null;
+    }
+    
+    /**
+     * Tests if the session is correctly invalidated.
+     */
+    public void testSessionInvalidate()
+    {
+        // first store a value in the session and retrieve it again
+        session.setAttribute("someAttribute", "someValue");
+        assertEquals("someValue", session.getAttribute("someAttribute"));
+        
+        // invalidate the session
+        session.invalidate();
+        
+        try
+        {
+            session.getAttribute("someAttribute");
+            fail("Session was already invalidated, getAttribute() has to throw an IllegalStateException.");
+        }
+        catch(IllegalStateException e)
+        {
+            // expected Exception
+        }
+        
+        try
+        {
+            session.setAttribute("someAttribute", "anotherValue");
+            fail("Session was already invalidated, setAttribute() has to throw an IllegalStateException.");
+        }
+        catch(IllegalStateException e)
+        {
+            // expected Exception
+        }
+        
+        try
+        {
+            session.removeAttribute("someAttribute");
+            fail("Session was already invalidated, removeAttribute() has to throw an IllegalStateException.");
+        }
+        catch(IllegalStateException e)
+        {
+            // expected Exception
+        }
+        
+        try
+        {
+            session.invalidate();
+            fail("Session was already invalidated, invalidate() has to throw an IllegalStateException.");
+        }
+        catch(IllegalStateException e)
+        {
+            // expected Exception
+        }
+        
+        try
+        {
+            session.getAttributeNames();
+            fail("Session was already invalidated, getAttributeNames() has to throw an IllegalStateException.");
+        }
+        catch(IllegalStateException e)
+        {
+            // expected Exception
+        }
+    }
+    
+}

-- 
To stop receiving notification emails like this one, please contact
deki@apache.org.