View Javadoc

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