View Javadoc

1   /*
2    * $Id: PortletStateInterceptorTest.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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 }