View Javadoc

    1   /*
    2    * $Id: PortletUrlHelperTest.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.util;
    22
    23  import java.util.HashMap;
    24  import java.util.Map;
    25
    26  import javax.portlet.PortletMode;
    27  import javax.portlet.PortletURL;
    28  import javax.portlet.RenderRequest;
    29  import javax.portlet.RenderResponse;
    30  import javax.portlet.WindowState;
    31
    32  import junit.framework.TestCase;
    33
    34  import org.apache.struts2.portlet.context.PortletActionContext;
    35  import org.easymock.MockControl;
    36
    37  import com.opensymphony.xwork2.ActionContext;
    38
    39  /***
    40   */
    41  public class PortletUrlHelperTest extends TestCase {
    42
    43      RenderResponse renderResponse;
    44
    45      RenderRequest renderRequest;
    46
    47      PortletURL url;
    48
    49      MockControl renderResponseControl;
    50
    51      MockControl renderRequestControl;
    52
    53      MockControl portletUrlControl;
    54
    55      public void setUp() throws Exception {
    56          super.setUp();
    57
    58          renderRequestControl = MockControl.createControl(RenderRequest.class);
    59          renderResponseControl = MockControl.createControl(RenderResponse.class);
    60          portletUrlControl = MockControl.createControl(PortletURL.class);
    61
    62          renderRequest = (RenderRequest) renderRequestControl.getMock();
    63          renderResponse = (RenderResponse) renderResponseControl.getMock();
    64          url = (PortletURL) portletUrlControl.getMock();
    65
    66          renderRequestControl.expectAndDefaultReturn(renderRequest
    67                  .getPortletMode(), PortletMode.VIEW);
    68          renderRequestControl.expectAndDefaultReturn(renderRequest
    69                  .getWindowState(), WindowState.NORMAL);
    70
    71          Map modeNamespaceMap = new HashMap();
    72          modeNamespaceMap.put("view", "/view");
    73          modeNamespaceMap.put("edit", "/edit");
    74          modeNamespaceMap.put("help", "/help");
    75
    76          Map context = new HashMap();
    77          context.put(PortletActionContext.REQUEST, renderRequest);
    78          context.put(PortletActionContext.RESPONSE, renderResponse);
    79          context.put(PortletActionContext.PHASE,
    80                  PortletActionContext.RENDER_PHASE);
    81          context.put(PortletActionContext.MODE_NAMESPACE_MAP, modeNamespaceMap);
    82
    83          ActionContext.setContext(new ActionContext(context));
    84
    85      }
    86
    87      public void testCreateRenderUrlWithNoModeOrState() throws Exception {
    88          renderResponseControl.expectAndReturn(renderResponse.createRenderURL(),
    89                  url);
    90
    91          url.setPortletMode(PortletMode.VIEW);
    92          url.setWindowState(WindowState.NORMAL);
    93          url.setParameters(null);
    94          portletUrlControl.setMatcher(MockControl.ALWAYS_MATCHER);
    95          renderRequestControl.replay();
    96          renderResponseControl.replay();
    97          portletUrlControl.replay();
    98          PortletUrlHelper.buildUrl("testAction", null,
    99                  new HashMap(), null, null, null);
    100         portletUrlControl.verify();
    101         renderRequestControl.verify();
    102         renderResponseControl.verify();
    103     }
    104
    105     public void testCreateRenderUrlWithDifferentPortletMode() throws Exception {
    106         renderResponseControl.expectAndReturn(renderResponse.createRenderURL(),
    107                 url);
    108
    109         url.setPortletMode(PortletMode.EDIT);
    110         url.setWindowState(WindowState.NORMAL);
    111         url.setParameters(null);
    112         portletUrlControl.setMatcher(MockControl.ALWAYS_MATCHER);
    113         renderRequestControl.replay();
    114         renderResponseControl.replay();
    115         portletUrlControl.replay();
    116         PortletUrlHelper.buildUrl("testAction", null,
    117                 new HashMap(), null, "edit", null);
    118         portletUrlControl.verify();
    119         renderRequestControl.verify();
    120         renderResponseControl.verify();
    121     }
    122
    123     public void testCreateRenderUrlWithDifferentWindowState() throws Exception {
    124         renderResponseControl.expectAndReturn(renderResponse.createRenderURL(),
    125                 url);
    126
    127         url.setPortletMode(PortletMode.VIEW);
    128         url.setWindowState(WindowState.MAXIMIZED);
    129         url.setParameters(null);
    130         portletUrlControl.setMatcher(MockControl.ALWAYS_MATCHER);
    131         renderRequestControl.replay();
    132         renderResponseControl.replay();
    133         portletUrlControl.replay();
    134         PortletUrlHelper.buildUrl("testAction", null,
    135                 new HashMap(), null, null, "maximized");
    136         portletUrlControl.verify();
    137         renderRequestControl.verify();
    138         renderResponseControl.verify();
    139     }
    140
    141     public void testCreateActionUrl() throws Exception {
    142         renderResponseControl.expectAndReturn(renderResponse.createActionURL(),
    143                 url);
    144
    145         url.setPortletMode(PortletMode.VIEW);
    146         url.setWindowState(WindowState.NORMAL);
    147         url.setParameters(null);
    148         portletUrlControl.setMatcher(MockControl.ALWAYS_MATCHER);
    149         renderRequestControl.replay();
    150         renderResponseControl.replay();
    151         portletUrlControl.replay();
    152         PortletUrlHelper.buildUrl("testAction", null,
    153                 new HashMap(), "action", null, null);
    154         portletUrlControl.verify();
    155         renderRequestControl.verify();
    156         renderResponseControl.verify();
    157     }
    158
    159 }