View Javadoc

    1   /*
    2    * $Id: PortletActionContextTest.java 495502 2007-01-12 07:15:43Z mrdon $
    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.context;
    22
    23  import java.util.HashMap;
    24  import java.util.Map;
    25
    26  import javax.portlet.ActionRequest;
    27  import javax.portlet.ActionResponse;
    28  import javax.portlet.PortletConfig;
    29  import javax.portlet.RenderRequest;
    30  import javax.portlet.RenderResponse;
    31
    32  import junit.textui.TestRunner;
    33
    34  import org.apache.struts2.dispatcher.mapper.ActionMapping;
    35  import org.apache.struts2.portlet.PortletActionConstants;
    36  import org.jmock.Mock;
    37  import org.jmock.MockObjectTestCase;
    38
    39  import com.opensymphony.xwork2.ActionContext;
    40
    41  /***
    42   */
    43  public class PortletActionContextTest extends MockObjectTestCase {
    44
    45      Mock mockRenderRequest;
    46      Mock mockRenderResponse;
    47      Mock mockPortletConfig;
    48      Mock mockActionRequest;
    49      Mock mockActionResponse;
    50
    51      RenderRequest renderRequest;
    52      RenderResponse renderResponse;
    53
    54      ActionRequest actionRequest;
    55      ActionResponse actionResponse;
    56
    57      PortletConfig portletConfig;
    58
    59      Map context = new HashMap();
    60
    61      public void setUp() throws Exception {
    62          super.setUp();
    63          mockRenderRequest = mock(RenderRequest.class);
    64          mockRenderResponse = mock(RenderResponse.class);
    65          mockActionRequest = mock(ActionRequest.class);
    66          mockActionResponse = mock(ActionResponse.class);
    67          mockPortletConfig = mock(PortletConfig.class);
    68
    69          renderRequest = (RenderRequest)mockRenderRequest.proxy();
    70          renderResponse = (RenderResponse)mockRenderResponse.proxy();
    71          actionRequest = (ActionRequest)mockActionRequest.proxy();
    72          actionResponse = (ActionResponse)mockActionResponse.proxy();
    73          portletConfig = (PortletConfig)mockPortletConfig.proxy();
    74
    75
    76          ActionContext.setContext(new ActionContext(context));
    77      }
    78
    79      public void testGetPhase() {
    80          context.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
    81
    82          assertEquals(PortletActionConstants.RENDER_PHASE, PortletActionContext.getPhase());
    83      }
    84
    85      public void testIsRender() {
    86          context.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
    87
    88          assertTrue(PortletActionContext.isRender());
    89          assertFalse(PortletActionContext.isEvent());
    90      }
    91
    92      public void testIsEvent() {
    93          context.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
    94
    95          assertTrue(PortletActionContext.isEvent());
    96          assertFalse(PortletActionContext.isRender());
    97      }
    98
    99      public void testGetPortletConfig() {
    100         context.put(PortletActionConstants.PORTLET_CONFIG, portletConfig);
    101         assertSame(portletConfig, PortletActionContext.getPortletConfig());
    102     }
    103
    104     public void testGetRenderRequestAndResponse() {
    105         context.put(PortletActionConstants.REQUEST, renderRequest);
    106         context.put(PortletActionConstants.RESPONSE, renderResponse);
    107         context.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
    108         assertSame(renderRequest, PortletActionContext.getRenderRequest());
    109         assertSame(renderResponse, PortletActionContext.getRenderResponse());
    110         assertSame(renderRequest, PortletActionContext.getRequest());
    111         assertSame(renderResponse, PortletActionContext.getResponse());
    112     }
    113
    114     public void testGetRenderRequestAndResponseInEventPhase() {
    115         context.put(PortletActionConstants.REQUEST, renderRequest);
    116         context.put(PortletActionConstants.RESPONSE, renderResponse);
    117         context.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
    118         try {
    119             PortletActionContext.getRenderRequest();
    120             fail("Should throw IllegalStateException!");
    121         }
    122         catch(IllegalStateException e) {
    123             assertTrue(true);
    124         }
    125         try {
    126             PortletActionContext.getRenderResponse();
    127             fail("Should throw IllegalStateException!");
    128         }
    129         catch(IllegalStateException e) {
    130             assertTrue(true);
    131         }
    132     }
    133
    134     public void testGetActionRequestAndResponse() {
    135         context.put(PortletActionConstants.REQUEST, actionRequest);
    136         context.put(PortletActionConstants.RESPONSE, actionResponse);
    137         context.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
    138         assertSame(actionRequest, PortletActionContext.getActionRequest());
    139         assertSame(actionResponse, PortletActionContext.getActionResponse());
    140         assertSame(actionRequest, PortletActionContext.getRequest());
    141         assertSame(actionResponse, PortletActionContext.getResponse());
    142     }
    143
    144     public void testGetActionRequestAndResponseInRenderPhase() {
    145         context.put(PortletActionConstants.REQUEST, actionRequest);
    146         context.put(PortletActionConstants.RESPONSE, actionResponse);
    147         context.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
    148         try {
    149             PortletActionContext.getActionRequest();
    150             fail("Should throw IllegalStateException!");
    151         }
    152         catch(IllegalStateException e) {
    153             assertTrue(true);
    154         }
    155         try {
    156             PortletActionContext.getActionResponse();
    157             fail("Should throw IllegalStateException!");
    158         }
    159         catch(IllegalStateException e) {
    160             assertTrue(true);
    161         }
    162     }
    163
    164     public void testGetNamespace() {
    165         context.put(PortletActionConstants.PORTLET_NAMESPACE, "testNamespace");
    166         assertEquals("testNamespace", PortletActionContext.getPortletNamespace());
    167     }
    168
    169     public void testGetDefaultActionForMode() {
    170         ActionMapping mapping = new ActionMapping();
    171         context.put(PortletActionConstants.DEFAULT_ACTION_FOR_MODE, mapping);
    172         assertEquals(mapping, PortletActionContext.getDefaultActionForMode());
    173     }
    174
    175     public void tearDown() throws Exception {
    176         ActionContext.setContext(null);
    177         super.tearDown();
    178     }
    179
    180     public static void main(String[] args) {
    181         TestRunner.run(PortletActionContextTest.class);
    182     }
    183 }