001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.portlet.context;
006:
007: import java.util.HashMap;
008: import java.util.Map;
009:
010: import javax.portlet.ActionRequest;
011: import javax.portlet.ActionResponse;
012: import javax.portlet.PortletConfig;
013: import javax.portlet.RenderRequest;
014: import javax.portlet.RenderResponse;
015:
016: import junit.textui.TestRunner;
017:
018: import org.jmock.Mock;
019: import org.jmock.MockObjectTestCase;
020:
021: import com.opensymphony.webwork.portlet.PortletActionConstants;
022: import com.opensymphony.xwork.ActionContext;
023:
024: /**
025: * @author Nils-Helge Garli
026: *
027: * TODO To change the template for this generated type comment go to
028: * Window - Preferences - Java - Code Style - Code Templates
029: */
030: public class PortletActionContextTest extends MockObjectTestCase {
031:
032: Mock mockRenderRequest;
033: Mock mockRenderResponse;
034: Mock mockPortletConfig;
035: Mock mockActionRequest;
036: Mock mockActionResponse;
037:
038: RenderRequest renderRequest;
039: RenderResponse renderResponse;
040:
041: ActionRequest actionRequest;
042: ActionResponse actionResponse;
043:
044: PortletConfig portletConfig;
045:
046: Map context = new HashMap();
047:
048: public void setUp() throws Exception {
049: super .setUp();
050: mockRenderRequest = mock(RenderRequest.class);
051: mockRenderResponse = mock(RenderResponse.class);
052: mockActionRequest = mock(ActionRequest.class);
053: mockActionResponse = mock(ActionResponse.class);
054: mockPortletConfig = mock(PortletConfig.class);
055:
056: renderRequest = (RenderRequest) mockRenderRequest.proxy();
057: renderResponse = (RenderResponse) mockRenderResponse.proxy();
058: actionRequest = (ActionRequest) mockActionRequest.proxy();
059: actionResponse = (ActionResponse) mockActionResponse.proxy();
060: portletConfig = (PortletConfig) mockPortletConfig.proxy();
061:
062: ActionContext.setContext(new ActionContext(context));
063: }
064:
065: public void testGetPhase() {
066: context.put(PortletActionConstants.PHASE,
067: PortletActionConstants.RENDER_PHASE);
068:
069: assertEquals(PortletActionConstants.RENDER_PHASE,
070: PortletActionContext.getPhase());
071: }
072:
073: public void testIsRender() {
074: context.put(PortletActionConstants.PHASE,
075: PortletActionConstants.RENDER_PHASE);
076:
077: assertTrue(PortletActionContext.isRender());
078: assertFalse(PortletActionContext.isEvent());
079: }
080:
081: public void testIsEvent() {
082: context.put(PortletActionConstants.PHASE,
083: PortletActionConstants.EVENT_PHASE);
084:
085: assertTrue(PortletActionContext.isEvent());
086: assertFalse(PortletActionContext.isRender());
087: }
088:
089: public void testGetPortletConfig() {
090: context.put(PortletActionConstants.PORTLET_CONFIG,
091: portletConfig);
092: assertSame(portletConfig, PortletActionContext
093: .getPortletConfig());
094: }
095:
096: public void testGetRenderRequestAndResponse() {
097: context.put(PortletActionConstants.REQUEST, renderRequest);
098: context.put(PortletActionConstants.RESPONSE, renderResponse);
099: context.put(PortletActionConstants.PHASE,
100: PortletActionConstants.RENDER_PHASE);
101: assertSame(renderRequest, PortletActionContext
102: .getRenderRequest());
103: assertSame(renderResponse, PortletActionContext
104: .getRenderResponse());
105: assertSame(renderRequest, PortletActionContext.getRequest());
106: assertSame(renderResponse, PortletActionContext.getResponse());
107: }
108:
109: public void testGetRenderRequestAndResponseInEventPhase() {
110: context.put(PortletActionConstants.REQUEST, renderRequest);
111: context.put(PortletActionConstants.RESPONSE, renderResponse);
112: context.put(PortletActionConstants.PHASE,
113: PortletActionConstants.EVENT_PHASE);
114: try {
115: PortletActionContext.getRenderRequest();
116: fail("Should throw IllegalStateException!");
117: } catch (IllegalStateException e) {
118: assertTrue(true);
119: }
120: try {
121: PortletActionContext.getRenderResponse();
122: fail("Should throw IllegalStateException!");
123: } catch (IllegalStateException e) {
124: assertTrue(true);
125: }
126: }
127:
128: public void testGetActionRequestAndResponse() {
129: context.put(PortletActionConstants.REQUEST, actionRequest);
130: context.put(PortletActionConstants.RESPONSE, actionResponse);
131: context.put(PortletActionConstants.PHASE,
132: PortletActionConstants.EVENT_PHASE);
133: assertSame(actionRequest, PortletActionContext
134: .getActionRequest());
135: assertSame(actionResponse, PortletActionContext
136: .getActionResponse());
137: assertSame(actionRequest, PortletActionContext.getRequest());
138: assertSame(actionResponse, PortletActionContext.getResponse());
139: }
140:
141: public void testGetActionRequestAndResponseInRenderPhase() {
142: context.put(PortletActionConstants.REQUEST, actionRequest);
143: context.put(PortletActionConstants.RESPONSE, actionResponse);
144: context.put(PortletActionConstants.PHASE,
145: PortletActionConstants.RENDER_PHASE);
146: try {
147: PortletActionContext.getActionRequest();
148: fail("Should throw IllegalStateException!");
149: } catch (IllegalStateException e) {
150: assertTrue(true);
151: }
152: try {
153: PortletActionContext.getActionResponse();
154: fail("Should throw IllegalStateException!");
155: } catch (IllegalStateException e) {
156: assertTrue(true);
157: }
158: }
159:
160: public void testGetNamespace() {
161: context.put(PortletActionConstants.PORTLET_NAMESPACE,
162: "testNamespace");
163: assertEquals("testNamespace", PortletActionContext
164: .getPortletNamespace());
165: }
166:
167: public void testGetDefaultActionForMode() {
168: context.put(PortletActionConstants.DEFAULT_ACTION_FOR_MODE,
169: "testAction");
170: assertEquals("testAction", PortletActionContext
171: .getDefaultActionForMode());
172: }
173:
174: public void tearDown() throws Exception {
175: ActionContext.setContext(null);
176: super .tearDown();
177: }
178:
179: public static void main(String[] args) {
180: TestRunner.run(PortletActionContextTest.class);
181: }
182: }
|