View Javadoc

1   /*
2    * $Id: PortletAwareInterceptorTest.java 759073 2009-03-27 09:40:17Z nilsga $
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.PortletConfig;
28  import javax.portlet.PortletRequest;
29  
30  import junit.framework.TestCase;
31  
32  import org.apache.struts2.portlet.PortletActionConstants;
33  import org.easymock.EasyMock;
34  
35  import com.opensymphony.xwork2.ActionContext;
36  import com.opensymphony.xwork2.ActionInvocation;
37  
38  public class PortletAwareInterceptorTest extends TestCase implements PortletActionConstants {
39  
40  	private PortletAwareInterceptor interceptor;
41  	private TestAction action;
42  	private PortletRequest portletRequest;
43  	private PortletConfig portletConfig;
44  	private Map<String, Object> contextMap;
45  	private ActionInvocation invocation;
46  	
47  	protected void setUp() throws Exception {
48  		super.setUp();
49  		interceptor = new PortletAwareInterceptor();
50  		action = new TestAction();
51  		portletRequest = EasyMock.createNiceMock(PortletRequest.class);
52  		portletConfig = EasyMock.createNiceMock(PortletConfig.class);
53  		contextMap = new HashMap<String, Object>();
54  		invocation = EasyMock.createNiceMock(ActionInvocation.class);
55  		EasyMock.expect(invocation.getAction()).andReturn(action);
56  		EasyMock.expect(invocation.getInvocationContext()).andReturn(new ActionContext(contextMap));
57  	}
58  	
59  	protected void tearDown() throws Exception {
60  		super.tearDown();
61  	}
62  	
63  	public void testPortletRequestIsSet() throws Exception {
64  		contextMap.put(REQUEST, portletRequest);
65  		EasyMock.replay(invocation);
66  		interceptor.intercept(invocation);
67  		assertEquals(portletRequest, action.getPortletRequest());
68  	}
69  	
70  	public void testPortletConfigIsSet() throws Exception {
71  		contextMap.put(PORTLET_CONFIG, portletConfig);
72  		EasyMock.replay(invocation);
73  		interceptor.intercept(invocation);
74  		assertEquals(portletConfig, action.getPortletConfig());
75  	}
76  	
77  	public static class TestAction implements PortletRequestAware, PortletConfigAware {
78  
79  		private PortletRequest request;
80  		private PortletConfig config;
81  
82  		public void setPortletRequest(PortletRequest request) {
83  			this.request = request;
84  		}
85  
86  		public void setPortletConfig(PortletConfig portletConfig) {
87  			this.config = portletConfig;
88  		}
89  
90  		public PortletConfig getPortletConfig() {
91  			return config;
92  		}
93  
94  		public PortletRequest getPortletRequest() {
95  			return request;
96  		}
97  		
98  	}
99  }