001: /*
002: * $Id: PortletActionContextTest.java 495502 2007-01-12 07:15:43Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.portlet.context;
022:
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import javax.portlet.ActionRequest;
027: import javax.portlet.ActionResponse;
028: import javax.portlet.PortletConfig;
029: import javax.portlet.RenderRequest;
030: import javax.portlet.RenderResponse;
031:
032: import junit.textui.TestRunner;
033:
034: import org.apache.struts2.dispatcher.mapper.ActionMapping;
035: import org.apache.struts2.portlet.PortletActionConstants;
036: import org.jmock.Mock;
037: import org.jmock.MockObjectTestCase;
038:
039: import com.opensymphony.xwork2.ActionContext;
040:
041: /**
042: */
043: public class PortletActionContextTest extends MockObjectTestCase {
044:
045: Mock mockRenderRequest;
046: Mock mockRenderResponse;
047: Mock mockPortletConfig;
048: Mock mockActionRequest;
049: Mock mockActionResponse;
050:
051: RenderRequest renderRequest;
052: RenderResponse renderResponse;
053:
054: ActionRequest actionRequest;
055: ActionResponse actionResponse;
056:
057: PortletConfig portletConfig;
058:
059: Map context = new HashMap();
060:
061: public void setUp() throws Exception {
062: super .setUp();
063: mockRenderRequest = mock(RenderRequest.class);
064: mockRenderResponse = mock(RenderResponse.class);
065: mockActionRequest = mock(ActionRequest.class);
066: mockActionResponse = mock(ActionResponse.class);
067: mockPortletConfig = mock(PortletConfig.class);
068:
069: renderRequest = (RenderRequest) mockRenderRequest.proxy();
070: renderResponse = (RenderResponse) mockRenderResponse.proxy();
071: actionRequest = (ActionRequest) mockActionRequest.proxy();
072: actionResponse = (ActionResponse) mockActionResponse.proxy();
073: portletConfig = (PortletConfig) mockPortletConfig.proxy();
074:
075: ActionContext.setContext(new ActionContext(context));
076: }
077:
078: public void testGetPhase() {
079: context.put(PortletActionConstants.PHASE,
080: PortletActionConstants.RENDER_PHASE);
081:
082: assertEquals(PortletActionConstants.RENDER_PHASE,
083: PortletActionContext.getPhase());
084: }
085:
086: public void testIsRender() {
087: context.put(PortletActionConstants.PHASE,
088: PortletActionConstants.RENDER_PHASE);
089:
090: assertTrue(PortletActionContext.isRender());
091: assertFalse(PortletActionContext.isEvent());
092: }
093:
094: public void testIsEvent() {
095: context.put(PortletActionConstants.PHASE,
096: PortletActionConstants.EVENT_PHASE);
097:
098: assertTrue(PortletActionContext.isEvent());
099: assertFalse(PortletActionContext.isRender());
100: }
101:
102: public void testGetPortletConfig() {
103: context.put(PortletActionConstants.PORTLET_CONFIG,
104: portletConfig);
105: assertSame(portletConfig, PortletActionContext
106: .getPortletConfig());
107: }
108:
109: public void testGetRenderRequestAndResponse() {
110: context.put(PortletActionConstants.REQUEST, renderRequest);
111: context.put(PortletActionConstants.RESPONSE, renderResponse);
112: context.put(PortletActionConstants.PHASE,
113: PortletActionConstants.RENDER_PHASE);
114: assertSame(renderRequest, PortletActionContext
115: .getRenderRequest());
116: assertSame(renderResponse, PortletActionContext
117: .getRenderResponse());
118: assertSame(renderRequest, PortletActionContext.getRequest());
119: assertSame(renderResponse, PortletActionContext.getResponse());
120: }
121:
122: public void testGetRenderRequestAndResponseInEventPhase() {
123: context.put(PortletActionConstants.REQUEST, renderRequest);
124: context.put(PortletActionConstants.RESPONSE, renderResponse);
125: context.put(PortletActionConstants.PHASE,
126: PortletActionConstants.EVENT_PHASE);
127: try {
128: PortletActionContext.getRenderRequest();
129: fail("Should throw IllegalStateException!");
130: } catch (IllegalStateException e) {
131: assertTrue(true);
132: }
133: try {
134: PortletActionContext.getRenderResponse();
135: fail("Should throw IllegalStateException!");
136: } catch (IllegalStateException e) {
137: assertTrue(true);
138: }
139: }
140:
141: public void testGetActionRequestAndResponse() {
142: context.put(PortletActionConstants.REQUEST, actionRequest);
143: context.put(PortletActionConstants.RESPONSE, actionResponse);
144: context.put(PortletActionConstants.PHASE,
145: PortletActionConstants.EVENT_PHASE);
146: assertSame(actionRequest, PortletActionContext
147: .getActionRequest());
148: assertSame(actionResponse, PortletActionContext
149: .getActionResponse());
150: assertSame(actionRequest, PortletActionContext.getRequest());
151: assertSame(actionResponse, PortletActionContext.getResponse());
152: }
153:
154: public void testGetActionRequestAndResponseInRenderPhase() {
155: context.put(PortletActionConstants.REQUEST, actionRequest);
156: context.put(PortletActionConstants.RESPONSE, actionResponse);
157: context.put(PortletActionConstants.PHASE,
158: PortletActionConstants.RENDER_PHASE);
159: try {
160: PortletActionContext.getActionRequest();
161: fail("Should throw IllegalStateException!");
162: } catch (IllegalStateException e) {
163: assertTrue(true);
164: }
165: try {
166: PortletActionContext.getActionResponse();
167: fail("Should throw IllegalStateException!");
168: } catch (IllegalStateException e) {
169: assertTrue(true);
170: }
171: }
172:
173: public void testGetNamespace() {
174: context.put(PortletActionConstants.PORTLET_NAMESPACE,
175: "testNamespace");
176: assertEquals("testNamespace", PortletActionContext
177: .getPortletNamespace());
178: }
179:
180: public void testGetDefaultActionForMode() {
181: ActionMapping mapping = new ActionMapping();
182: context.put(PortletActionConstants.DEFAULT_ACTION_FOR_MODE,
183: mapping);
184: assertEquals(mapping, PortletActionContext
185: .getDefaultActionForMode());
186: }
187:
188: public void tearDown() throws Exception {
189: ActionContext.setContext(null);
190: super .tearDown();
191: }
192:
193: public static void main(String[] args) {
194: TestRunner.run(PortletActionContextTest.class);
195: }
196: }
|