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

svn commit: r923343 - in /myfaces/test/trunk/test12/src: main/java/org/apache/myfaces/test/mock/MockHttpSession.java test/java/org/apache/myfaces/test/mock/MockHttpSessionTest.java

Author: jakobk
Date: Mon Mar 15 17:17:03 2010
New Revision: 923343

URL: http://svn.apache.org/viewvc?rev=923343&view=rev
Log:
MYFACESTEST-5 Implementation of MockHttpSession.invalidate() (Thanks to Christoph Göldner for this patch) + test case

Added:
    myfaces/test/trunk/test12/src/test/java/org/apache/myfaces/test/mock/MockHttpSessionTest.java   (with props)
Modified:
    myfaces/test/trunk/test12/src/main/java/org/apache/myfaces/test/mock/MockHttpSession.java

Modified: myfaces/test/trunk/test12/src/main/java/org/apache/myfaces/test/mock/MockHttpSession.java
URL: http://svn.apache.org/viewvc/myfaces/test/trunk/test12/src/main/java/org/apache/myfaces/test/mock/MockHttpSession.java?rev=923343&r1=923342&r2=923343&view=diff
==============================================================================
--- myfaces/test/trunk/test12/src/main/java/org/apache/myfaces/test/mock/MockHttpSession.java (original)
+++ myfaces/test/trunk/test12/src/main/java/org/apache/myfaces/test/mock/MockHttpSession.java Mon Mar 15 17:17:03 2010
@@ -98,6 +98,7 @@ public class MockHttpSession implements 
     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 
     /** {@inheritDoc} */
     public Object getAttribute(String name) {
 
+        assertValidity();
+
         return attributes.get(name);
 
     }
@@ -127,6 +130,8 @@ public class MockHttpSession implements 
     /** {@inheritDoc} */
     public Enumeration getAttributeNames() {
 
+        assertValidity();
+
         return new MockEnumeration(attributes.keySet().iterator());
 
     }
@@ -198,9 +203,11 @@ public class MockHttpSession implements 
 
     /** {@inheritDoc} */
     public void invalidate() {
+        
+        assertValidity();
 
-        throw new UnsupportedOperationException();
-
+        attributes.clear();
+        invalid = true;
     }
 
 
@@ -223,6 +230,8 @@ public class MockHttpSession implements 
     /** {@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 
     /** {@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 
     }
 
 
+    /**
+     * <p>Throws an {@link IllegalStateException} if this session is invalid.</p>
+     */
+    private void assertValidity() 
+    {
+        if (invalid) 
+        {
+            throw new IllegalStateException("Session is invalid.");
+        }
+    }
+
 }

Added: myfaces/test/trunk/test12/src/test/java/org/apache/myfaces/test/mock/MockHttpSessionTest.java
URL: http://svn.apache.org/viewvc/myfaces/test/trunk/test12/src/test/java/org/apache/myfaces/test/mock/MockHttpSessionTest.java?rev=923343&view=auto
==============================================================================
--- myfaces/test/trunk/test12/src/test/java/org/apache/myfaces/test/mock/MockHttpSessionTest.java (added)
+++ myfaces/test/trunk/test12/src/test/java/org/apache/myfaces/test/mock/MockHttpSessionTest.java Mon Mar 15 17:17:03 2010
@@ -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
+        }
+    }
+    
+}

Propchange: myfaces/test/trunk/test12/src/test/java/org/apache/myfaces/test/mock/MockHttpSessionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/test/trunk/test12/src/test/java/org/apache/myfaces/test/mock/MockHttpSessionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: myfaces/test/trunk/test12/src/test/java/org/apache/myfaces/test/mock/MockHttpSessionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain