View Javadoc

1   /*
2    * $Id: PortletActionContextTest.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.portlet.context;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.portlet.ActionRequest;
24  import javax.portlet.ActionResponse;
25  import javax.portlet.PortletConfig;
26  import javax.portlet.RenderRequest;
27  import javax.portlet.RenderResponse;
28  
29  import junit.textui.TestRunner;
30  
31  import org.apache.struts2.portlet.PortletActionConstants;
32  import org.jmock.Mock;
33  import org.jmock.MockObjectTestCase;
34  
35  import com.opensymphony.xwork2.ActionContext;
36  
37  /***
38   */
39  public class PortletActionContextTest extends MockObjectTestCase {
40      
41      Mock mockRenderRequest;
42      Mock mockRenderResponse;
43      Mock mockPortletConfig;
44      Mock mockActionRequest;
45      Mock mockActionResponse;
46      
47      RenderRequest renderRequest;
48      RenderResponse renderResponse;
49      
50      ActionRequest actionRequest;
51      ActionResponse actionResponse;
52      
53      PortletConfig portletConfig;
54      
55      Map context = new HashMap();
56      
57      public void setUp() throws Exception {
58          super.setUp();
59          mockRenderRequest = mock(RenderRequest.class);
60          mockRenderResponse = mock(RenderResponse.class);
61          mockActionRequest = mock(ActionRequest.class);
62          mockActionResponse = mock(ActionResponse.class);
63          mockPortletConfig = mock(PortletConfig.class);
64          
65          renderRequest = (RenderRequest)mockRenderRequest.proxy();
66          renderResponse = (RenderResponse)mockRenderResponse.proxy();
67          actionRequest = (ActionRequest)mockActionRequest.proxy();
68          actionResponse = (ActionResponse)mockActionResponse.proxy();
69          portletConfig = (PortletConfig)mockPortletConfig.proxy();
70          
71          
72          ActionContext.setContext(new ActionContext(context));
73      }
74      
75      public void testGetPhase() {
76          context.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
77          
78          assertEquals(PortletActionConstants.RENDER_PHASE, PortletActionContext.getPhase());
79      }
80      
81      public void testIsRender() {
82          context.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
83          
84          assertTrue(PortletActionContext.isRender());
85          assertFalse(PortletActionContext.isEvent());
86      }
87      
88      public void testIsEvent() {
89          context.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
90          
91          assertTrue(PortletActionContext.isEvent());
92          assertFalse(PortletActionContext.isRender());
93      }
94      
95      public void testGetPortletConfig() {
96          context.put(PortletActionConstants.PORTLET_CONFIG, portletConfig);
97          assertSame(portletConfig, PortletActionContext.getPortletConfig());
98      }
99      
100     public void testGetRenderRequestAndResponse() {
101         context.put(PortletActionConstants.REQUEST, renderRequest);
102         context.put(PortletActionConstants.RESPONSE, renderResponse);
103         context.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
104         assertSame(renderRequest, PortletActionContext.getRenderRequest());
105         assertSame(renderResponse, PortletActionContext.getRenderResponse());
106         assertSame(renderRequest, PortletActionContext.getRequest());
107         assertSame(renderResponse, PortletActionContext.getResponse());
108     }
109     
110     public void testGetRenderRequestAndResponseInEventPhase() {
111         context.put(PortletActionConstants.REQUEST, renderRequest);
112         context.put(PortletActionConstants.RESPONSE, renderResponse);
113         context.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
114         try {
115             PortletActionContext.getRenderRequest();
116             fail("Should throw IllegalStateException!");
117         }
118         catch(IllegalStateException e) {
119             assertTrue(true);
120         }
121         try {
122             PortletActionContext.getRenderResponse();
123             fail("Should throw IllegalStateException!");
124         }
125         catch(IllegalStateException e) {
126             assertTrue(true);
127         }
128     }
129     
130     public void testGetActionRequestAndResponse() {
131         context.put(PortletActionConstants.REQUEST, actionRequest);
132         context.put(PortletActionConstants.RESPONSE, actionResponse);
133         context.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
134         assertSame(actionRequest, PortletActionContext.getActionRequest());
135         assertSame(actionResponse, PortletActionContext.getActionResponse());
136         assertSame(actionRequest, PortletActionContext.getRequest());
137         assertSame(actionResponse, PortletActionContext.getResponse());
138     }
139     
140     public void testGetActionRequestAndResponseInRenderPhase() {
141         context.put(PortletActionConstants.REQUEST, actionRequest);
142         context.put(PortletActionConstants.RESPONSE, actionResponse);
143         context.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
144         try {
145             PortletActionContext.getActionRequest();
146             fail("Should throw IllegalStateException!");
147         }
148         catch(IllegalStateException e) {
149             assertTrue(true);
150         }
151         try {
152             PortletActionContext.getActionResponse();
153             fail("Should throw IllegalStateException!");
154         }
155         catch(IllegalStateException e) {
156             assertTrue(true);
157         }
158     }
159     
160     public void testGetNamespace() {
161         context.put(PortletActionConstants.PORTLET_NAMESPACE, "testNamespace");
162         assertEquals("testNamespace", PortletActionContext.getPortletNamespace());
163     }
164     
165     public void testGetDefaultActionForMode() {
166         context.put(PortletActionConstants.DEFAULT_ACTION_FOR_MODE, "testAction");
167         assertEquals("testAction", PortletActionContext.getDefaultActionForMode());
168     }
169     
170     public void tearDown() throws Exception {
171         ActionContext.setContext(null);
172         super.tearDown();
173     }
174     
175     public static void main(String[] args) {
176         TestRunner.run(PortletActionContextTest.class);
177     }
178 }