1 package org.apache.fulcrum.testcontainer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
48
49
50
51
52
53
54
55
56
57
58
59
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
66 public static final String COMPONENT_APP_ROOT = Container.COMPONENT_APP_ROOT;
67
68
69 private String containerType = CONTAINER_YAAFI;
70
71
72 public static final int defaultLogLevel = ConsoleLogger.LEVEL_INFO;
73
74
75 private Container container;
76
77
78 private String configurationFileName = "src/test/TestComponentConfig.xml";
79
80
81 private String roleFileName = "src/test/TestRoleConfig.xml";
82
83
84 private String parameterFileName = null;
85
86
87 private int logLevel = defaultLogLevel;
88
89
90 public Map<String, Object> attributes = new HashMap<String, Object>();
91
92
93 public int maxInactiveInterval = 0;
94
95
96
97
98
99
100
101 protected void setConfigurationFileName(String configurationFileName)
102 {
103 this.configurationFileName = configurationFileName;
104 }
105
106
107
108
109
110
111
112 protected void setRoleFileName(String roleFileName)
113 {
114 this.roleFileName = roleFileName;
115 }
116
117
118
119
120
121
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
135
136 public BaseUnit5Test()
137 {
138 }
139
140
141
142
143 @AfterEach
144 public void tearDown()
145 {
146 if (container != null)
147 {
148 container.dispose();
149 }
150 container = null;
151 }
152
153
154
155
156
157
158 protected String getConfigurationFileName()
159 {
160 return configurationFileName;
161 }
162
163
164
165
166
167
168 protected String getRoleFileName()
169 {
170 return roleFileName;
171 }
172
173
174
175
176
177
178 protected String getParameterFileName()
179 {
180 return parameterFileName;
181 }
182
183
184
185
186
187
188
189
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
210
211
212
213 protected void release(Object component)
214 {
215 if (container != null)
216 {
217 container.release(component);
218 }
219 }
220
221
222
223
224
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
296
297 public String getContainerType()
298 {
299 return containerType;
300 }
301
302
303
304
305 public void setContainerType(String containerType)
306 {
307 this.containerType = containerType;
308 }
309 }