View Javadoc

    1   /*
    2    * $Id: PortletSessionMapTest.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.portlet;
    22
    23  import java.util.Arrays;
    24  import java.util.Enumeration;
    25  import java.util.Iterator;
    26  import java.util.List;
    27  import java.util.Map;
    28  import java.util.Set;
    29
    30  import javax.portlet.PortletRequest;
    31  import javax.portlet.PortletSession;
    32
    33  import org.jmock.Mock;
    34  import org.jmock.MockObjectTestCase;
    35  import org.jmock.core.Constraint;
    36
    37
    38  /***
    39   * PortletSessionMapTest. Insert description.
    40   *
    41   */
    42  public class PortletSessionMapTest extends MockObjectTestCase {
    43
    44      public void testPut() {
    45
    46          Mock mockSession = mock(PortletSession.class);
    47          Mock mockRequest = mock(PortletRequest.class);
    48
    49          PortletRequest req = (PortletRequest)mockRequest.proxy();
    50          PortletSession session = (PortletSession)mockSession.proxy();
    51
    52          mockRequest.expects(once()).method("getPortletSession").will(returnValue(session));
    53          Constraint[] params = new Constraint[]{eq("testAttribute1"), eq("testValue1")};
    54          mockSession.expects(once()).method("setAttribute").with(params);
    55          mockSession.expects(once()).method("getAttribute").with(eq("testAttribute1")).will(returnValue("testValue1"));
    56          params = new Constraint[]{eq("testAttribute2"), eq("testValue2")};
    57          mockSession.expects(once()).method("setAttribute").with(params);
    58          mockSession.expects(once()).method("getAttribute").with(eq("testAttribute2")).will(returnValue("testValue2"));
    59
    60          PortletSessionMap map = new PortletSessionMap(req);
    61          map.put("testAttribute1", "testValue1");
    62          map.put("testAttribute2", "testValue2");
    63
    64      }
    65
    66      public void testGet() {
    67          Mock mockSession = mock(PortletSession.class);
    68          Mock mockRequest = mock(PortletRequest.class);
    69
    70          PortletRequest req = (PortletRequest)mockRequest.proxy();
    71          PortletSession session = (PortletSession)mockSession.proxy();
    72
    73          mockRequest.expects(once()).method("getPortletSession").will(returnValue(session));
    74          mockSession.expects(once()).method("getAttribute").with(eq("testAttribute1")).will(returnValue("testValue1"));
    75          mockSession.expects(once()).method("getAttribute").with(eq("testAttribute2")).will(returnValue("testValue2"));
    76
    77          PortletSessionMap map = new PortletSessionMap(req);
    78          Object val1 = map.get("testAttribute1");
    79          Object val2 = map.get("testAttribute2");
    80          assertEquals("testValue1", val1);
    81          assertEquals("testValue2", val2);
    82      }
    83
    84      public void testClear() {
    85          Mock mockSession = mock(PortletSession.class);
    86          Mock mockRequest = mock(PortletRequest.class);
    87
    88          PortletRequest req = (PortletRequest)mockRequest.proxy();
    89          PortletSession session = (PortletSession)mockSession.proxy();
    90
    91          mockRequest.expects(once()).method("getPortletSession").will(returnValue(session));
    92          mockSession.expects(once()).method("invalidate");
    93
    94          PortletSessionMap map = new PortletSessionMap(req);
    95          map.clear();
    96      }
    97
    98      public void testRemove() {
    99          Mock mockSession = mock(PortletSession.class);
    100         Mock mockRequest = mock(PortletRequest.class);
    101
    102         PortletRequest req = (PortletRequest)mockRequest.proxy();
    103         PortletSession session = (PortletSession)mockSession.proxy();
    104
    105
    106         mockRequest.expects(once()).method("getPortletSession").will(returnValue(session));
    107         mockSession.stubs().method("getAttribute").with(eq("dummyKey")).will(returnValue("dummyValue"));
    108         mockSession.expects(once()).method("removeAttribute").with(eq("dummyKey"));
    109
    110         PortletSessionMap map = new PortletSessionMap(req);
    111         Object ret = map.remove("dummyKey");
    112         assertEquals("dummyValue", ret);
    113     }
    114
    115     public void testEntrySet() {
    116         Mock mockSession = mock(PortletSession.class);
    117         Mock mockRequest = mock(PortletRequest.class);
    118
    119         PortletRequest req = (PortletRequest)mockRequest.proxy();
    120         PortletSession session = (PortletSession)mockSession.proxy();
    121
    122         Enumeration names = new Enumeration() {
    123
    124             List keys = Arrays.asList(new Object[]{"key1", "key2"});
    125             Iterator it = keys.iterator();
    126
    127             public boolean hasMoreElements() {
    128                 return it.hasNext();
    129             }
    130
    131             public Object nextElement() {
    132                 return it.next();
    133             }
    134
    135         };
    136
    137         mockSession.stubs().method("getAttributeNames").will(returnValue(names));
    138         mockSession.stubs().method("getAttribute").with(eq("key1")).will(returnValue("value1"));
    139         mockSession.stubs().method("getAttribute").with(eq("key2")).will(returnValue("value2"));
    140
    141         mockRequest.expects(once()).method("getPortletSession").will(returnValue(session));
    142
    143         PortletSessionMap map = new PortletSessionMap(req);
    144         Set entries = map.entrySet();
    145
    146         assertEquals(2, entries.size());
    147         Iterator it = entries.iterator();
    148         Map.Entry entry = (Map.Entry)it.next();
    149         assertEquals("key2", entry.getKey());
    150         assertEquals("value2", entry.getValue());
    151         entry = (Map.Entry)it.next();
    152         assertEquals("key1", entry.getKey());
    153         assertEquals("value1", entry.getValue());
    154
    155     }
    156
    157 }