1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.portlet.interceptor;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import javax.portlet.ActionResponse;
28 import javax.portlet.RenderRequest;
29
30 import junit.framework.TestCase;
31
32 import org.apache.struts2.StrutsTestCase;
33 import org.apache.struts2.dispatcher.DefaultActionSupport;
34 import org.apache.struts2.portlet.PortletActionConstants;
35 import org.apache.struts2.portlet.dispatcher.DirectRenderFromEventAction;
36 import org.easymock.EasyMock;
37
38 import com.opensymphony.xwork2.ActionContext;
39 import com.opensymphony.xwork2.ActionInvocation;
40 import com.opensymphony.xwork2.util.ValueStack;
41 import com.opensymphony.xwork2.util.ValueStackFactory;
42
43 public class PortletStateInterceptorTest extends StrutsTestCase implements PortletActionConstants {
44
45 private PortletStateInterceptor interceptor;
46
47 public void setUp() throws Exception {
48 super.setUp();
49 interceptor = new PortletStateInterceptor();
50 }
51
52 public void testCopyValueStackFromEventToRenderPhase() throws Exception {
53 ActionResponse actionResponse = EasyMock.createNiceMock(ActionResponse.class);
54 ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class);
55
56 Map<String, Object> ctxMap = new HashMap<String, Object>();
57 ctxMap.put(PHASE, EVENT_PHASE);
58 ctxMap.put(RESPONSE, actionResponse);
59 Map<String, Object> session = new HashMap<String, Object>();
60
61 ActionContext ctx = new ActionContext(ctxMap);
62 ctx.setSession(session);
63 EasyMock.expect(invocation.getInvocationContext()).andStubReturn(ctx);
64 actionResponse.setRenderParameter(EVENT_ACTION, "true");
65
66 ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
67 EasyMock.expect(invocation.getStack()).andStubReturn(stack);
68
69 EasyMock.replay(actionResponse);
70 EasyMock.replay(invocation);
71
72 interceptor.intercept(invocation);
73
74 EasyMock.verify(actionResponse);
75 EasyMock.verify(invocation);
76
77 assertSame(stack, session.get(STACK_FROM_EVENT_PHASE));
78
79 }
80
81 public void testDoNotRestoreValueStackInRenderPhaseWhenProperPrg() throws Exception {
82 RenderRequest renderRequest = EasyMock.createNiceMock(RenderRequest.class);
83 ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class);
84
85
86 ValueStack eventPhaseStack = container.getInstance(ValueStackFactory.class).createValueStack();
87 eventPhaseStack.set("testKey", "testValue");
88
89 ValueStack currentStack = container.getInstance(ValueStackFactory.class).createValueStack();
90 currentStack.set("anotherTestKey", "anotherTestValue");
91
92 Map<String, Object> ctxMap = new HashMap<String, Object>();
93 Map<String, Object> session = new HashMap<String, Object>();
94
95 session.put(STACK_FROM_EVENT_PHASE, eventPhaseStack);
96
97 ctxMap.put(PHASE, RENDER_PHASE);
98 ctxMap.put(REQUEST, renderRequest);
99
100 ActionContext ctx = new ActionContext(ctxMap);
101 ctx.setSession(session);
102
103 EasyMock.expect(invocation.getInvocationContext()).andStubReturn(ctx);
104 EasyMock.expect(invocation.getStack()).andStubReturn(currentStack);
105 EasyMock.expect(invocation.getAction()).andStubReturn(new DefaultActionSupport());
106 EasyMock.expect(renderRequest.getParameter(EVENT_ACTION)).andStubReturn("true");
107
108 EasyMock.replay(renderRequest);
109 EasyMock.replay(invocation);
110
111 interceptor.intercept(invocation);
112
113 ValueStack resultingStack = invocation.getStack();
114
115 assertNull(resultingStack.findValue("testKey"));
116 assertEquals("anotherTestValue", resultingStack.findValue("anotherTestKey"));
117
118
119 }
120
121 public void testRestoreValueStackInRenderPhaseWhenNotProperPrg() throws Exception {
122 RenderRequest renderRequest = EasyMock.createNiceMock(RenderRequest.class);
123 ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class);
124
125 ValueStack eventPhaseStack = container.getInstance(ValueStackFactory.class).createValueStack();
126 eventPhaseStack.set("testKey", "testValue");
127
128 ValueStack currentStack = container.getInstance(ValueStackFactory.class).createValueStack();
129 currentStack.set("anotherTestKey", "anotherTestValue");
130
131 EasyMock.expect(invocation.getStack()).andStubReturn(currentStack);
132
133 Map<String, Object> ctxMap = new HashMap<String, Object>();
134 Map<String, Object> session = new HashMap<String, Object>();
135
136 session.put(STACK_FROM_EVENT_PHASE, eventPhaseStack);
137
138 ctxMap.put(PHASE, RENDER_PHASE);
139 ctxMap.put(REQUEST, renderRequest);
140
141 ActionContext ctx = new ActionContext(ctxMap);
142 ctx.setSession(session);
143
144 EasyMock.expect(invocation.getInvocationContext()).andStubReturn(ctx);
145 EasyMock.expect(invocation.getStack()).andStubReturn(currentStack);
146 EasyMock.expect(invocation.getAction()).andStubReturn(new DirectRenderFromEventAction());
147 EasyMock.expect(renderRequest.getParameter(EVENT_ACTION)).andStubReturn("true");
148
149 EasyMock.replay(renderRequest);
150 EasyMock.replay(invocation);
151
152 interceptor.intercept(invocation);
153
154 ValueStack resultingStack = invocation.getStack();
155 assertEquals("testValue", resultingStack.findValue("testKey"));
156 assertEquals("anotherTestValue", resultingStack.findValue("anotherTestKey"));
157
158
159 }
160 }