View Javadoc

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