View Javadoc
1   package org.apache.fulcrum.testcontainer;
2   
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  import static org.mockito.ArgumentMatchers.any;
23  import static org.mockito.ArgumentMatchers.anyInt;
24  import static org.mockito.ArgumentMatchers.anyString;
25  import static org.mockito.Mockito.doAnswer;
26  import static org.mockito.Mockito.mock;
27  import static org.mockito.Mockito.when;
28  
29  import java.util.HashMap;
30  import java.util.Locale;
31  import java.util.Map;
32  import java.util.Objects;
33  import java.util.Vector;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpSession;
37  
38  import org.apache.avalon.framework.component.ComponentException;
39  import org.apache.avalon.framework.logger.AbstractLogEnabled;
40  import org.apache.avalon.framework.logger.ConsoleLogger;
41  import org.apache.avalon.framework.logger.Logger;
42  import org.junit.jupiter.api.AfterEach;
43  import org.mockito.invocation.InvocationOnMock;
44  import org.mockito.stubbing.Answer;
45  
46  /**
47   * Alternative Base class to {@link BaseUnit4Test} for component tests.
48   * 
49   * This version doesn't load the container until the first request for a
50   * component. This allows the tester to populate the configurationFileName and
51   * roleFileName, possible one per test.
52   * 
53   * JUnit 5 Version of BaseUnitTest class.
54   * 
55   * @see BaseUnit4Test
56   *
57   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
58   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
59   * @version $Id$
60   */
61  public class BaseUnit5Test {
62  	public static final String CONTAINER_ECM = "CONTAINER_ECM";
63  	public static final String CONTAINER_YAAFI = "CONTAINER_YAAFI";
64  
65  	/** Key used in the context for defining the application root */
66  	public static final String COMPONENT_APP_ROOT = Container.COMPONENT_APP_ROOT;
67  
68  	/** Pick the default container to be YAAFI, running in **/
69  	private String containerType = CONTAINER_YAAFI;
70  
71  	/** Use INFO for ConsoleLogger */
72  	public static final int defaultLogLevel = ConsoleLogger.LEVEL_INFO;
73  
74  	/** Container for the components */
75  	private Container container;
76  
77  	/** Setup our default configurationFileName */
78  	private String configurationFileName = "src/test/TestComponentConfig.xml";
79  
80  	/** Setup our default roleFileName */
81  	private String roleFileName = "src/test/TestRoleConfig.xml";
82  
83  	/** Setup our default parameterFileName */
84  	private String parameterFileName = null;
85  
86  	/** Set the log level (only works for YAAFI container) */
87  	private int logLevel = defaultLogLevel;
88  
89  	/** Hash map to store attributes for the test **/
90  	public Map<String, Object> attributes = new HashMap<String, Object>();
91  
92  	/** set the Max inactive interval **/
93  	public int maxInactiveInterval = 0;
94  
95  	/**
96  	 * Gets the configuration file name for the container should use for this test.
97  	 * By default it is src/test/TestComponentConfig.
98  	 * 
99  	 * @param configurationFileName the location of the config file
100 	 */
101 	protected void setConfigurationFileName(String configurationFileName) 
102 	{
103 		this.configurationFileName = configurationFileName;
104 	}
105 
106 	/**
107 	 * Override the role file name for the container should use for this test. By
108 	 * default it is src/test/TestRoleConfig.
109 	 * 
110 	 * @param roleFileName location of the role file
111 	 */
112 	protected void setRoleFileName(String roleFileName) 
113 	{
114 		this.roleFileName = roleFileName;
115 	}
116 
117 	/**
118 	 * Set the console logger level
119 	 * 
120 	 * @see org.apache.avalon.framework.logger.ConsoleLogger for debugging levels
121 	 * @param logLevel set valid logging level
122 	 */
123 	protected void setLogLevel(int logLevel) 
124 	{
125 		this.logLevel = logLevel;
126 	}
127 	
128     public int getLogLevel()
129     {
130         return logLevel;
131     }
132 
133 	/**
134 	 * Constructor for test.
135 	 */
136 	public BaseUnit5Test() 
137 	{
138 	}
139 
140 	/**
141 	 * Clean up after each test is run.
142 	 */
143 	@AfterEach
144 	public void tearDown() 
145 	{
146 		if (container != null) 
147 		{
148 			container.dispose();
149 		}
150 		container = null;
151 	}
152 
153 	/**
154 	 * Gets the configuration file name for the container should use for this test.
155 	 *
156 	 * @return The filename of the configuration file
157 	 */
158 	protected String getConfigurationFileName() 
159 	{
160 		return configurationFileName;
161 	}
162 
163 	/**
164 	 * Gets the role file name for the container should use for this test.
165 	 *
166 	 * @return The filename of the role configuration file
167 	 */
168 	protected String getRoleFileName() 
169 	{
170 		return roleFileName;
171 	}
172 
173 	/**
174 	 * Gets the parameter file name for the container should use for this test.
175 	 *
176 	 * @return The filename of the role configuration file
177 	 */
178 	protected String getParameterFileName() 
179 	{
180 		return parameterFileName;
181 	}
182 
183 	/**
184 	 * Returns an instance of the named component. This method will also start the
185 	 * container if it has not been started already
186 	 *
187 	 * @param roleName Name of the role the component fills.
188 	 * @return instance of the component
189 	 * @throws ComponentException generic exception
190 	 */
191 	protected Object lookup(String roleName) throws ComponentException 
192 	{
193 		if (container == null) 
194 		{
195 			if (containerType.equals(CONTAINER_ECM)) 
196 			{
197 				container = new ECMContainer();
198 			} 
199 			else 
200 			{
201 				container = new YAAFIContainer(logLevel);
202 			}
203 			container.startup(getConfigurationFileName(), getRoleFileName(), getParameterFileName());
204 		}
205 		return container.lookup(roleName);
206 	}
207 
208 	/**
209 	 * Releases the component.
210 	 *
211 	 * @param component component to be released
212 	 */
213 	protected void release(Object component) 
214 	{
215 		if (container != null) 
216 		{
217 			container.release(component);
218 		}
219 	}
220 
221 	/**
222 	 * Get a mock requestion
223 	 *
224 	 * @return HttpServletRequest a mock servlet request
225 	 */
226 	protected HttpServletRequest getMockRequest() 
227 	{
228 		HttpServletRequest request = mock(HttpServletRequest.class);
229 		HttpSession session = mock(HttpSession.class);
230 
231 		doAnswer(new Answer<Object>() 
232 		{
233 			@Override
234 			public Object answer(InvocationOnMock invocation) throws Throwable {
235 				String key = (String) invocation.getArguments()[0];
236 				return attributes.get(key);
237 			}
238 		}).when(session).getAttribute(anyString());
239 
240 		doAnswer(new Answer<Object>() 
241 		{
242 			@Override
243 			public Object answer(InvocationOnMock invocation) throws Throwable {
244 				String key = (String) invocation.getArguments()[0];
245 				Object value = invocation.getArguments()[1];
246 				attributes.put(key, value);
247 				return null;
248 			}
249 		}).when(session).setAttribute(anyString(), any());
250 
251 		when(session.getMaxInactiveInterval()).thenReturn(maxInactiveInterval);
252 
253 		doAnswer(new Answer<Integer>() 
254 		{
255 			@Override
256 			public Integer answer(InvocationOnMock invocation) throws Throwable {
257 				return Integer.valueOf(maxInactiveInterval);
258 			}
259 		}).when(session).getMaxInactiveInterval();
260 
261 		doAnswer(new Answer<Object>() 
262 		{
263 			@Override
264 			public Object answer(InvocationOnMock invocation) throws Throwable {
265 				Integer value = (Integer) invocation.getArguments()[0];
266 				maxInactiveInterval = value.intValue();
267 				return null;
268 			}
269 		}).when(session).setMaxInactiveInterval(anyInt());
270 
271 		when(session.isNew()).thenReturn(true);
272 		when(request.getSession()).thenReturn(session);
273 
274 		when(request.getServerName()).thenReturn("bob");
275 		when(request.getProtocol()).thenReturn("http");
276 		when(request.getScheme()).thenReturn("scheme");
277 		when(request.getPathInfo()).thenReturn("damn");
278 		when(request.getServletPath()).thenReturn("damn2");
279 		when(request.getContextPath()).thenReturn("wow");
280 		when(request.getContentType()).thenReturn("text/html");
281 
282 		when(request.getCharacterEncoding()).thenReturn("US-ASCII");
283 		when(request.getServerPort()).thenReturn(8080);
284 		when(request.getLocale()).thenReturn(Locale.US);
285 
286 		when(request.getHeader("Content-type")).thenReturn("text/html");
287 		when(request.getHeader("Accept-Language")).thenReturn("en-US");
288 
289 		Vector<String> v = new Vector<String>();
290 		when(request.getParameterNames()).thenReturn(v.elements());
291 		return request;
292 	}
293 
294 	/**
295 	 * @return the container type
296 	 */
297 	public String getContainerType() 
298 	{
299 		return containerType;
300 	}
301 
302 	/**
303 	 * @param containerType container type to set
304 	 */
305 	public void setContainerType(String containerType) 
306 	{
307 		this.containerType = containerType;
308 	}
309 }