You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by ni...@apache.org on 2008/12/09 20:19:19 UTC

svn commit: r724839 - in /struts/struts2/trunk/core/src: main/java/org/apache/struts2/dispatcher/SessionMap.java test/java/org/apache/struts2/dispatcher/SessionMapTest.java

Author: nilsga
Date: Tue Dec  9 11:19:19 2008
New Revision: 724839

URL: http://svn.apache.org/viewvc?rev=724839&view=rev
Log:
WW-2751 Fixed containsKey for SessionMap. Patch by Ian Robinson.

Modified:
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/SessionMap.java
    struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/SessionMapTest.java

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/SessionMap.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/SessionMap.java?rev=724839&r1=724838&r2=724839&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/SessionMap.java (original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/SessionMap.java Tue Dec  9 11:19:19 2008
@@ -205,4 +205,21 @@
             return value;
         }
     }
+
+    
+    /**
+     * Checks if the specified session attribute with the given key exists.
+     *
+     * @param key the name of the session attribute.
+     * @return <tt>true</tt> if the session attribute exits or <tt>false</tt> if it doesn't exist.
+     */
+    public boolean containsKey(Object key) {
+        if (session == null) {
+            return false;
+        }
+
+        synchronized (session) {
+            return (session.getAttribute(key.toString()) != null);
+        }
+    }
 }

Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/SessionMapTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/SessionMapTest.java?rev=724839&r1=724838&r2=724839&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/SessionMapTest.java (original)
+++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/SessionMapTest.java Tue Dec  9 11:19:19 2008
@@ -31,6 +31,8 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 
+import org.springframework.mock.web.MockHttpServletRequest;
+
 import junit.framework.TestCase;
 
 import com.mockobjects.constraint.Constraint;
@@ -102,6 +104,57 @@
         sessionMock.verify();
     }
 
+    public void testGetObjectOnSessionMapUsesWrappedSessionsGetAttributeWithStringValue() throws Exception {
+        Object key = new Object();
+        Object value = new Object();
+        sessionMock.expectAndReturn("getAttribute", new Constraint[]{
+                new IsEqual(key.toString())
+        }, value);
+
+        SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
+        assertEquals("Expected the get using KEY to return the value object setup in the mockSession", value, sessionMap.get(key));
+        sessionMock.verify();
+    }
+
+    public void testPutObjectOnSessionMapUsesWrappedSessionsSetsAttributeWithStringValue() throws Exception {
+        Object key = new Object();
+        Object value = new Object();
+        sessionMock.expect("getAttribute", new Constraint[]{new IsAnything()});
+        sessionMock.expect("setAttribute", new Constraint[]{
+                new IsEqual(key.toString()), new IsEqual(value)
+        });
+
+        SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
+        sessionMap.put(key, value);
+        sessionMock.verify();
+    }
+    
+    public void testContainsKeyWillFindAnObjectPutOnSessionMap() throws Exception {
+    	
+    	MockHttpServletRequest request = new MockHttpServletRequest();
+    	
+        Object key = new Object();
+        Object value = new Object();
+        
+        SessionMap<Object, Object> sessionMap = new SessionMap<Object, Object>(request);
+        sessionMap.put(key, value);
+        assertTrue(sessionMap.containsKey(key));
+    }
+
+    public void testContainsKeyWillReturnFalseIfObjectNotFoundOnSessionMap() throws Exception {
+    	
+    	MockHttpServletRequest request = new MockHttpServletRequest();
+    	
+        Object key = new Object();
+        Object someOtherKey = new Object();
+        Object value = new Object();
+        
+        SessionMap<Object, Object> sessionMap = new SessionMap<Object, Object>(request);
+        sessionMap.put(key, value);
+        
+        assertFalse(sessionMap.containsKey(someOtherKey));
+    }
+
     public void testPuttingObjectInMapReturnsNullForPreviouslyUnusedKey() throws Exception {
         Object value = new Object();
         sessionMock.expectAndReturn("getAttribute", new Constraint[]{