View Javadoc

    1   /*
    2    * $Id: SessionMapTest.java 471756 2006-11-06 15:01:43Z husted $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.dispatcher;
    22
    23  import java.util.ArrayList;
    24  import java.util.Collections;
    25  import java.util.Enumeration;
    26  import java.util.HashMap;
    27  import java.util.List;
    28  import java.util.Map;
    29
    30  import javax.servlet.http.HttpServletRequest;
    31  import javax.servlet.http.HttpSession;
    32
    33  import junit.framework.TestCase;
    34
    35  import com.mockobjects.constraint.Constraint;
    36  import com.mockobjects.constraint.IsAnything;
    37  import com.mockobjects.constraint.IsEqual;
    38  import com.mockobjects.dynamic.Mock;
    39
    40
    41  /***
    42   */
    43  public class SessionMapTest extends TestCase {
    44
    45      private Mock requestMock;
    46      private Mock sessionMock;
    47
    48
    49      public void testClearInvalidatesTheSession() throws Exception {
    50          List<String> attributeNames = new ArrayList<String>();
    51          attributeNames.add("test");
    52          attributeNames.add("anotherTest");
    53          Enumeration attributeNamesEnum = Collections.enumeration(attributeNames);
    54
    55          MockSessionMap sessionMap = new MockSessionMap((HttpServletRequest) requestMock.proxy());
    56          sessionMock.expect("setAttribute",
    57                  new Constraint[] {
    58                      new IsEqual("test"), new IsEqual("test value")
    59                  });
    60          sessionMock.expect("setAttribute",
    61                  new Constraint[] {
    62                      new IsEqual("anotherTest"), new IsEqual("another test value")
    63                  });
    64          sessionMock.expectAndReturn("getAttributeNames", attributeNamesEnum);
    65          sessionMock.expect("removeAttribute",
    66                  new Constraint[]{
    67                      new IsEqual("test")
    68                  });
    69          sessionMock.expect("removeAttribute",
    70                  new Constraint[]{
    71                      new IsEqual("anotherTest")
    72                  });
    73          sessionMap.put("test", "test value");
    74          sessionMap.put("anotherTest", "another test value");
    75          sessionMap.clear();
    76          assertNull(sessionMap.get("test"));
    77          assertNull(sessionMap.get("anotherTest"));
    78          sessionMock.verify();
    79      }
    80
    81      public void testGetOnSessionMapUsesWrappedSessionsGetAttribute() throws Exception {
    82          Object value = new Object();
    83          sessionMock.expectAndReturn("getAttribute", new Constraint[]{
    84                  new IsEqual("KEY")
    85          }, value);
    86
    87          SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
    88          assertEquals("Expected the get using KEY to return the value object setup in the mockSession", value, sessionMap.get("KEY"));
    89          sessionMock.verify();
    90      }
    91
    92      public void testPutOnSessionMapUsesWrappedSessionsSetsAttribute() throws Exception {
    93          Object value = new Object();
    94          sessionMock.expect("getAttribute", new Constraint[]{new IsAnything()});
    95          sessionMock.expect("setAttribute", new Constraint[]{
    96                  new IsEqual("KEY"), new IsEqual(value)
    97          });
    98
    99          SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
    100         sessionMap.put("KEY", value);
    101         sessionMock.verify();
    102     }
    103
    104     public void testPuttingObjectInMapReturnsNullForPreviouslyUnusedKey() throws Exception {
    105         Object value = new Object();
    106         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
    107                 new IsEqual("KEY")
    108         }, null);
    109         sessionMock.expect("setAttribute", new Constraint[]{
    110                 new IsEqual("KEY"), new IsEqual(value)
    111         });
    112
    113         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
    114         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));
    115         sessionMock.verify();
    116     }
    117
    118     public void testPuttingObjectInMapReturnsPreviousValueForKey() throws Exception {
    119         Object originalValue = new Object();
    120         Object value = new Object();
    121         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
    122                 new IsEqual("KEY")
    123         }, null);
    124         sessionMock.expect("setAttribute", new Constraint[]{
    125                 new IsEqual("KEY"), new IsEqual(originalValue)
    126         });
    127         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
    128                 new IsEqual("KEY")
    129         }, originalValue);
    130         sessionMock.expect("setAttribute", new Constraint[]{
    131                 new IsEqual("KEY"), new IsEqual(value)
    132         });
    133
    134         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
    135         sessionMap.put("KEY", originalValue);
    136         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));
    137         sessionMock.verify();
    138     }
    139
    140     public void testRemovePassThroughCallToRemoveAttribute() throws Exception {
    141         Object value = new Object();
    142         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
    143                 new IsEqual("KEY")
    144         }, value);
    145         sessionMock.expect("removeAttribute", new Constraint[]{
    146                 new IsEqual("KEY")
    147         });
    148
    149         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
    150         assertEquals(value, sessionMap.remove("KEY"));
    151         sessionMock.verify();
    152     }
    153
    154     protected void setUp() throws Exception {
    155         sessionMock = new Mock(HttpSession.class);
    156         requestMock = new Mock(HttpServletRequest.class);
    157         requestMock.matchAndReturn("getSession", new Constraint[]{new IsEqual(Boolean.FALSE)}, sessionMock.proxy());
    158     }
    159
    160
    161     /***
    162      * class that extends session map, making the values available in a local map -- useful
    163      * for confirming put and get calls in the superclass. ie useful for testing that the get is done before
    164      * putting new data into the map.
    165      */
    166     private class MockSessionMap extends SessionMap {
    167
    168         private static final long serialVersionUID = 8783604360786273764L;
    169
    170         private Map map = new HashMap();
    171
    172         public MockSessionMap(HttpServletRequest request) {
    173             super(request);
    174         }
    175
    176         public Object get(Object key) {
    177             return map.get(key);
    178         }
    179
    180         public Object put(Object key, Object value) {
    181             Object originalValue = super.put(key, value);
    182             map.put(key, value); //put the value into our map after putting it in the superclass map to avoid polluting the get call.
    183
    184             return originalValue;
    185         }
    186
    187         public void clear() {
    188             super.clear();
    189             map.clear();
    190         }
    191     }
    192 }