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.Vector;
33
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpSession;
36
37 import org.apache.avalon.framework.component.ComponentException;
38 import org.apache.avalon.framework.logger.ConsoleLogger;
39 import org.junit.After;
40 import org.mockito.invocation.InvocationOnMock;
41 import org.mockito.stubbing.Answer;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class BaseUnit4Test {
60 public static final String CONTAINER_ECM = "CONTAINER_ECM";
61 public static final String CONTAINER_YAAFI = "CONTAINER_YAAFI";
62
63
64 public static final String COMPONENT_APP_ROOT = Container.COMPONENT_APP_ROOT;
65
66
67 private String containerType = CONTAINER_YAAFI;
68
69
70 public static final int defaultLogLevel = ConsoleLogger.LEVEL_INFO;
71
72
73 private Container container;
74
75
76 private String configurationFileName = "src/test/TestComponentConfig.xml";
77
78
79 private String roleFileName = "src/test/TestRoleConfig.xml";
80
81
82 private String parameterFileName = null;
83
84
85 private int logLevel = defaultLogLevel;
86
87
88 public Map<String, Object> attributes = new HashMap<String, Object>();
89
90
91 public int maxInactiveInterval = 0;
92
93
94
95
96
97
98
99 protected void setConfigurationFileName(String configurationFileName)
100 {
101 this.configurationFileName = configurationFileName;
102 }
103
104
105
106
107
108
109
110 protected void setRoleFileName(String roleFileName)
111 {
112 this.roleFileName = roleFileName;
113 }
114
115
116
117
118
119
120
121 protected void setLogLevel(int logLevel)
122 {
123 this.logLevel = logLevel;
124 }
125
126
127
128
129 public BaseUnit4Test()
130 {
131 }
132
133
134
135
136 @After
137 public void tearDown()
138 {
139 if (container != null) {
140 container.dispose();
141 }
142 container = null;
143 }
144
145
146
147
148
149
150 protected String getConfigurationFileName()
151 {
152 return configurationFileName;
153 }
154
155
156
157
158
159
160 protected String getRoleFileName()
161 {
162 return roleFileName;
163 }
164
165
166
167
168
169
170 protected String getParameterFileName()
171 {
172 return parameterFileName;
173 }
174
175
176
177
178
179
180
181
182
183 protected Object lookup(String roleName) throws ComponentException
184 {
185 if (container == null)
186 {
187 if (containerType.equals(CONTAINER_ECM))
188 {
189 container = new ECMContainer();
190 }
191 else
192 {
193 container = new YAAFIContainer(logLevel);
194 }
195 container.startup(getConfigurationFileName(), getRoleFileName(), getParameterFileName());
196 }
197 return container.lookup(roleName);
198 }
199
200
201
202
203
204
205 protected void release(Object component)
206 {
207 if (container != null)
208 {
209 container.release(component);
210 }
211 }
212
213
214
215
216
217
218 protected HttpServletRequest getMockRequest()
219 {
220 HttpServletRequest request = mock(HttpServletRequest.class);
221 HttpSession session = mock(HttpSession.class);
222
223 doAnswer(new Answer<Object>()
224 {
225 @Override
226 public Object answer(InvocationOnMock invocation) throws Throwable
227 {
228 String key = (String) invocation.getArguments()[0];
229 return attributes.get(key);
230 }
231 }).when(session).getAttribute(anyString());
232
233 doAnswer(new Answer<Object>()
234 {
235 @Override
236 public Object answer(InvocationOnMock invocation) throws Throwable
237 {
238 String key = (String) invocation.getArguments()[0];
239 Object value = invocation.getArguments()[1];
240 attributes.put(key, value);
241 return null;
242 }
243 }).when(session).setAttribute(anyString(), any());
244
245 when(session.getMaxInactiveInterval()).thenReturn(maxInactiveInterval);
246
247 doAnswer(new Answer<Integer>()
248 {
249 @Override
250 public Integer answer(InvocationOnMock invocation) throws Throwable {
251 return Integer.valueOf(maxInactiveInterval);
252 }
253 }).when(session).getMaxInactiveInterval();
254
255 doAnswer(new Answer<Object>()
256 {
257 @Override
258 public Object answer(InvocationOnMock invocation) throws Throwable
259 {
260 Integer value = (Integer) invocation.getArguments()[0];
261 maxInactiveInterval = value.intValue();
262 return null;
263 }
264 }).when(session).setMaxInactiveInterval(anyInt());
265
266 when(session.isNew()).thenReturn(true);
267 when(request.getSession()).thenReturn(session);
268
269 when(request.getServerName()).thenReturn("bob");
270 when(request.getProtocol()).thenReturn("http");
271 when(request.getScheme()).thenReturn("scheme");
272 when(request.getPathInfo()).thenReturn("damn");
273 when(request.getServletPath()).thenReturn("damn2");
274 when(request.getContextPath()).thenReturn("wow");
275 when(request.getContentType()).thenReturn("text/html");
276
277 when(request.getCharacterEncoding()).thenReturn("US-ASCII");
278 when(request.getServerPort()).thenReturn(8080);
279 when(request.getLocale()).thenReturn(Locale.US);
280
281 when(request.getHeader("Content-type")).thenReturn("text/html");
282 when(request.getHeader("Accept-Language")).thenReturn("en-US");
283
284 Vector<String> v = new Vector<String>();
285 when(request.getParameterNames()).thenReturn(v.elements());
286 return request;
287 }
288
289 public String getContainerType()
290 {
291 return containerType;
292 }
293
294 public void setContainerType(String containerType)
295 {
296 this.containerType = containerType;
297 }
298 }