View Javadoc

    1   /*
    2    * $Id: PortletResultTest.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.result;
    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.PortletContext;
    30  import javax.portlet.PortletMode;
    31  import javax.portlet.PortletRequestDispatcher;
    32  import javax.portlet.RenderRequest;
    33  import javax.portlet.RenderResponse;
    34
    35  import junit.textui.TestRunner;
    36
    37  import org.apache.struts2.portlet.PortletActionConstants;
    38  import org.jmock.Mock;
    39  import org.jmock.cglib.MockObjectTestCase;
    40  import org.jmock.core.Constraint;
    41
    42  import com.opensymphony.xwork2.ActionContext;
    43  import com.opensymphony.xwork2.ActionInvocation;
    44
    45  /***
    46   * PortletResultTest. Insert description.
    47   *
    48   */
    49  public class PortletResultTest extends MockObjectTestCase {
    50
    51      Mock mockInvocation = null;
    52      Mock mockConfig = null;
    53      Mock mockCtx = null;
    54
    55      public void setUp() throws Exception {
    56          super.setUp();
    57          mockInvocation = mock(ActionInvocation.class);
    58          mockConfig = mock(PortletConfig.class);
    59          mockCtx = mock(PortletContext.class);
    60
    61          mockConfig.stubs().method(ANYTHING);
    62          mockConfig.stubs().method("getPortletContext").will(returnValue(mockCtx.proxy()));
    63
    64          Map paramMap = new HashMap();
    65          Map sessionMap = new HashMap();
    66
    67          Map context = new HashMap();
    68          context.put(ActionContext.SESSION, sessionMap);
    69          context.put(ActionContext.PARAMETERS, paramMap);
    70          context.put(PortletActionConstants.PORTLET_CONFIG, mockConfig.proxy());
    71
    72          ActionContext.setContext(new ActionContext(context));
    73
    74          mockInvocation.stubs().method("getInvocationContext").will(returnValue(ActionContext.getContext()));
    75
    76      }
    77
    78      public void testDoExecute_render() {
    79          Mock mockRequest = mock(RenderRequest.class);
    80          Mock mockResponse = mock(RenderResponse.class);
    81          Mock mockRd = mock(PortletRequestDispatcher.class);
    82          Mock mockPrep = mock(PortletRequestDispatcher.class);
    83
    84          RenderRequest req = (RenderRequest)mockRequest.proxy();
    85          RenderResponse res = (RenderResponse)mockResponse.proxy();
    86          PortletRequestDispatcher rd = (PortletRequestDispatcher)mockRd.proxy();
    87          PortletConfig cfg = (PortletConfig)mockConfig.proxy();
    88          PortletContext ctx = (PortletContext)mockCtx.proxy();
    89          ActionInvocation inv = (ActionInvocation)mockInvocation.proxy();
    90
    91          Constraint[] params = new Constraint[]{same(req), same(res)};
    92          mockRd.expects(once()).method("include").with(params);
    93          mockPrep.expects(once()).method("include").with(params);
    94          mockCtx.expects(once()).method("getRequestDispatcher").with(eq("/WEB-INF/pages/testPage.jsp")).will(returnValue(rd));
    95          mockCtx.expects(once()).method("getNamedDispatcher").with(eq("preparator")).will(returnValue(mockPrep.proxy()));
    96          mockResponse.expects(once()).method("setContentType").with(eq("text/html"));
    97          mockConfig.expects(once()).method("getPortletContext").will(returnValue(ctx));
    98
    99          mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
    100
    101         ActionContext ctxMap = ActionContext.getContext();
    102         ctxMap.put(PortletActionConstants.RESPONSE, res);
    103         ctxMap.put(PortletActionConstants.REQUEST, req);
    104         ctxMap.put(PortletActionConstants.PORTLET_CONFIG, cfg);
    105         ctxMap.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
    106
    107         PortletResult result = new PortletResult();
    108         try {
    109             result.doExecute("/WEB-INF/pages/testPage.jsp", inv);
    110         }
    111         catch(Exception e) {
    112             e.printStackTrace();
    113             fail("Error occured!");
    114         }
    115
    116     }
    117
    118     public void testDoExecute_event_locationIsAction() {
    119
    120         Mock mockRequest = mock(ActionRequest.class);
    121         Mock mockResponse = mock(ActionResponse.class);
    122
    123         Constraint[] params = new Constraint[]{eq(PortletActionConstants.ACTION_PARAM), eq("testView")};
    124         mockResponse.expects(once()).method("setRenderParameter").with(params);
    125         params = new Constraint[]{eq(PortletActionConstants.MODE_PARAM), eq(PortletMode.VIEW.toString())};
    126         mockResponse.expects(once()).method("setRenderParameter").with(params);
    127         mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
    128         ActionContext ctx = ActionContext.getContext();
    129
    130         ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
    131         ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
    132         ctx.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
    133
    134         PortletResult result = new PortletResult();
    135         try {
    136             result.doExecute("testView.action", (ActionInvocation)mockInvocation.proxy());
    137         }
    138         catch(Exception e) {
    139             e.printStackTrace();
    140             fail("Error occured!");
    141         }
    142
    143     }
    144
    145     public void testDoExecute_event_locationIsJsp() {
    146         Mock mockRequest = mock(ActionRequest.class);
    147         Mock mockResponse = mock(ActionResponse.class);
    148
    149         Constraint[] params = new Constraint[]{eq(PortletActionConstants.ACTION_PARAM), eq("renderDirect")};
    150         mockResponse.expects(once()).method("setRenderParameter").with(params);
    151         params = new Constraint[]{eq("location"), eq("/WEB-INF/pages/testJsp.jsp")};
    152         mockResponse.expects(once()).method("setRenderParameter").with(params);
    153         params = new Constraint[]{eq(PortletActionConstants.MODE_PARAM), eq(PortletMode.VIEW.toString())};
    154         mockResponse.expects(once()).method("setRenderParameter").with(params);
    155         mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
    156
    157         ActionContext ctx = ActionContext.getContext();
    158
    159         ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
    160         ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
    161         ctx.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
    162
    163         PortletResult result = new PortletResult();
    164         try {
    165             result.doExecute("/WEB-INF/pages/testJsp.jsp", (ActionInvocation)mockInvocation.proxy());
    166         }
    167         catch(Exception e) {
    168             e.printStackTrace();
    169             fail("Error occured!");
    170         }
    171     }
    172
    173     public void testDoExecute_event_locationHasQueryParams() {
    174         Mock mockRequest = mock(ActionRequest.class);
    175         Mock mockResponse = mock(ActionResponse.class);
    176
    177         Constraint[] params = new Constraint[]{eq(PortletActionConstants.ACTION_PARAM), eq("testView")};
    178         mockResponse.expects(once()).method("setRenderParameter").with(params);
    179         params = new Constraint[]{eq("testParam1"), eq("testValue1")};
    180         mockResponse.expects(once()).method("setRenderParameter").with(params);
    181         params = new Constraint[]{eq("testParam2"), eq("testValue2")};
    182         mockResponse.expects(once()).method("setRenderParameter").with(params);
    183         params = new Constraint[]{eq(PortletActionConstants.MODE_PARAM), eq(PortletMode.VIEW.toString())};
    184         mockResponse.expects(once()).method("setRenderParameter").with(params);
    185         mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
    186
    187         ActionContext ctx = ActionContext.getContext();
    188
    189         ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
    190         ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
    191         ctx.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
    192
    193         PortletResult result = new PortletResult();
    194         try {
    195             result.doExecute("testView.action?testParam1=testValue1&testParam2=testValue2", (ActionInvocation)mockInvocation.proxy());
    196         }
    197         catch(Exception e) {
    198             e.printStackTrace();
    199             fail("Error occured!");
    200         }
    201     }
    202
    203     public void testTitleAndContentType() throws Exception {
    204         Mock mockRequest = mock(RenderRequest.class);
    205         Mock mockResponse = mock(RenderResponse.class);
    206         Mock mockRd = mock(PortletRequestDispatcher.class);
    207         Mock mockPrep = mock(PortletRequestDispatcher.class);
    208
    209         RenderRequest req = (RenderRequest)mockRequest.proxy();
    210         RenderResponse res = (RenderResponse)mockResponse.proxy();
    211         PortletRequestDispatcher rd = (PortletRequestDispatcher)mockRd.proxy();
    212         PortletConfig cfg = (PortletConfig)mockConfig.proxy();
    213         PortletContext ctx = (PortletContext)mockCtx.proxy();
    214
    215         Constraint[] params = new Constraint[]{same(req), same(res)};
    216         mockRd.expects(once()).method("include").with(params);
    217         mockPrep.expects(once()).method("include").with(params);
    218         mockCtx.expects(once()).method("getRequestDispatcher").with(eq("/WEB-INF/pages/testPage.jsp")).will(returnValue(rd));
    219         mockCtx.expects(once()).method("getNamedDispatcher").with(eq("preparator")).will(returnValue(mockPrep.proxy()));
    220         mockConfig.expects(once()).method("getPortletContext").will(returnValue(ctx));
    221
    222         mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
    223
    224         ActionContext ctxMap = ActionContext.getContext();
    225         ctxMap.put(PortletActionConstants.RESPONSE, res);
    226         ctxMap.put(PortletActionConstants.REQUEST, req);
    227         ctxMap.put(PortletActionConstants.PORTLET_CONFIG, cfg);
    228         ctxMap.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
    229
    230         mockResponse.expects(atLeastOnce()).method("setTitle").with(eq("testTitle"));
    231         mockResponse.expects(atLeastOnce()).method("setContentType").with(eq("testContentType"));
    232
    233         PortletResult result = new PortletResult();
    234         result.setTitle("testTitle");
    235         result.setContentType("testContentType");
    236         result.doExecute("/WEB-INF/pages/testPage.jsp", (ActionInvocation)mockInvocation.proxy());
    237     }
    238
    239     public void tearDown() throws Exception {
    240         super.tearDown();
    241         ActionContext.setContext(null);
    242     }
    243
    244     public static void main(String[] args) {
    245         TestRunner.run(PortletResultTest.class);
    246     }
    247
    248 }