1 5 package com.opensymphony.webwork.dispatcher; 6 7 import com.mockobjects.constraint.Constraint; 8 import com.mockobjects.constraint.IsAnything; 9 import com.mockobjects.constraint.IsEqual; 10 import com.mockobjects.dynamic.Mock; 11 import junit.framework.TestCase; 12 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpSession ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 18 19 22 public class SessionMapTest extends TestCase { 23 25 private Mock requestMock; 26 private Mock sessionMock; 27 28 30 public void testClearInvalidatesTheSession() throws Exception { 31 MockSessionMap sessionMap = new MockSessionMap((HttpServletRequest ) requestMock.proxy()); 32 sessionMap.clear(); 33 sessionMock.verify(); 34 } 35 36 public void testGetOnSessionMapUsesWrappedSessionsGetAttribute() throws Exception { 37 Object value = new Object (); 38 sessionMock.expectAndReturn("getAttribute", new Constraint[]{ 39 new IsEqual("KEY") 40 }, value); 41 42 SessionMap sessionMap = new SessionMap((HttpServletRequest ) requestMock.proxy()); 43 assertEquals("Expected the get using KEY to return the value object setup in the mockSession", value, sessionMap.get("KEY")); 44 sessionMock.verify(); 45 } 46 47 public void testPutOnSessionMapUsesWrappedSessionsSetsAttribute() throws Exception { 48 Object value = new Object (); 49 sessionMock.expect("getAttribute", new Constraint[]{new IsAnything()}); 50 sessionMock.expect("setAttribute", new Constraint[]{ 51 new IsEqual("KEY"), new IsEqual(value) 52 }); 53 54 SessionMap sessionMap = new SessionMap((HttpServletRequest ) requestMock.proxy()); 55 sessionMap.put("KEY", value); 56 sessionMock.verify(); 57 } 58 59 public void testPuttingObjectInMapReturnsNullForPreviouslyUnusedKey() throws Exception { 60 Object value = new Object (); 61 sessionMock.expectAndReturn("getAttribute", new Constraint[]{ 62 new IsEqual("KEY") 63 }, null); 64 sessionMock.expect("setAttribute", new Constraint[]{ 65 new IsEqual("KEY"), new IsEqual(value) 66 }); 67 68 SessionMap sessionMap = new SessionMap((HttpServletRequest ) requestMock.proxy()); 69 assertNull("should be null, as the contract for Map says that put returns the previous value in the map for the key", sessionMap.put("KEY", value)); 70 sessionMock.verify(); 71 } 72 73 public void testPuttingObjectInMapReturnsPreviousValueForKey() throws Exception { 74 Object originalValue = new Object (); 75 Object value = new Object (); 76 sessionMock.expectAndReturn("getAttribute", new Constraint[]{ 77 new IsEqual("KEY") 78 }, null); 79 sessionMock.expect("setAttribute", new Constraint[]{ 80 new IsEqual("KEY"), new IsEqual(originalValue) 81 }); 82 sessionMock.expectAndReturn("getAttribute", new Constraint[]{ 83 new IsEqual("KEY") 84 }, originalValue); 85 sessionMock.expect("setAttribute", new Constraint[]{ 86 new IsEqual("KEY"), new IsEqual(value) 87 }); 88 89 SessionMap sessionMap = new SessionMap((HttpServletRequest ) requestMock.proxy()); 90 sessionMap.put("KEY", originalValue); 91 assertEquals("should be the OriginalValue, as the contract for Map says that put returns the previous value in the map for the key", originalValue, sessionMap.put("KEY", value)); 92 sessionMock.verify(); 93 } 94 95 public void testRemovePassThroughCallToRemoveAttribute() throws Exception { 96 Object value = new Object (); 97 sessionMock.expectAndReturn("getAttribute", new Constraint[]{ 98 new IsEqual("KEY") 99 }, value); 100 sessionMock.expect("removeAttribute", new Constraint[]{ 101 new IsEqual("KEY") 102 }); 103 104 SessionMap sessionMap = new SessionMap((HttpServletRequest ) requestMock.proxy()); 105 assertEquals(value, sessionMap.remove("KEY")); 106 sessionMock.verify(); 107 } 108 109 protected void setUp() throws Exception { 110 sessionMock = new Mock(HttpSession .class); 111 requestMock = new Mock(HttpServletRequest .class); 112 requestMock.matchAndReturn("getSession", new Constraint[] { new IsEqual(Boolean.FALSE)}, sessionMock.proxy()); 113 } 114 115 117 122 private class MockSessionMap extends SessionMap { 123 private Map map = new HashMap (); 124 125 public MockSessionMap(HttpServletRequest request) { 126 super(request); 127 } 128 129 public Object get(Object key) { 130 return map.get(key); 131 } 132 133 public Object put(Object key, Object value) { 134 Object originalValue = super.put(key, value); 135 map.put(key, value); 137 return originalValue; 138 } 139 } 140 } 141 | Popular Tags |