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