View Javadoc

1   /*
2    * $Id: $
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  package org.apache.struts2.views.freemarker;
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.PortletContext;
29  import javax.portlet.PortletMode;
30  
31  import org.apache.struts2.StrutsStatics;
32  import org.apache.struts2.portlet.PortletActionConstants;
33  import org.jmock.Mock;
34  import org.jmock.cglib.MockObjectTestCase;
35  import org.jmock.core.Constraint;
36  
37  import com.opensymphony.xwork2.ActionContext;
38  import com.opensymphony.xwork2.ActionInvocation;
39  import com.opensymphony.xwork2.ActionProxy;
40  
41  public class PortletFreemarkerResultTest extends MockObjectTestCase implements PortletActionConstants {
42  
43      Mock mockInvocation = null;
44      Mock mockConfig = null;
45      Mock mockCtx = null;
46  
47      public void setUp() throws Exception {
48          super.setUp();
49          mockInvocation = mock(ActionInvocation.class);
50          mockCtx = mock(PortletContext.class);
51  
52          Map paramMap = new HashMap();
53          Map sessionMap = new HashMap();
54  
55          Map context = new HashMap();
56          context.put(ActionContext.SESSION, sessionMap);
57          context.put(ActionContext.PARAMETERS, paramMap);
58          context.put(StrutsStatics.STRUTS_PORTLET_CONTEXT, mockCtx.proxy());
59  
60          ActionContext.setContext(new ActionContext(context));
61  
62          mockInvocation.stubs().method("getInvocationContext").will(returnValue(ActionContext.getContext()));
63  
64      }
65  
66      public void testDoExecute_event_locationIsJsp() {
67          Mock mockRequest = mock(ActionRequest.class);
68          Mock mockResponse = mock(ActionResponse.class);
69          Mock mockProxy = mock(ActionProxy.class);
70  
71          Constraint[] params = new Constraint[]{eq(PortletActionConstants.ACTION_PARAM), eq("freemarkerDirect")};
72          mockResponse.expects(once()).method("setRenderParameter").with(params);
73          params = new Constraint[]{eq(PortletActionConstants.MODE_PARAM), eq(PortletMode.VIEW.toString())};
74          mockResponse.expects(once()).method("setRenderParameter").with(params);
75          mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
76          mockProxy.stubs().method("getNamespace").will(returnValue(""));
77  
78          mockInvocation.stubs().method("getProxy").will(returnValue(mockProxy.proxy()));
79  
80          ActionContext ctx = ActionContext.getContext();
81  
82          Map session = new HashMap();
83          
84          ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
85          ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
86          ctx.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
87          ctx.put(ActionContext.SESSION, session);
88  
89          PortletFreemarkerResult result = new PortletFreemarkerResult();
90          try {
91              result.doExecute("/WEB-INF/pages/testJsp.ftl", (ActionInvocation)mockInvocation.proxy());
92          }
93          catch(Exception e) {
94              e.printStackTrace();
95              fail("Error occured!");
96          }
97          assertEquals("/WEB-INF/pages/testJsp.ftl", session.get(RENDER_DIRECT_LOCATION));
98      }
99  
100 
101     public void tearDown() throws Exception {
102         super.tearDown();
103         ActionContext.setContext(null);
104     }
105 }