1 package org.apache.turbine.modules;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Vector;
30
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.apache.turbine.Turbine;
34 import org.apache.turbine.modules.actions.VelocityActionDoesNothing;
35 import org.apache.turbine.om.security.User;
36 import org.apache.turbine.pipeline.DefaultPipelineData;
37 import org.apache.turbine.pipeline.PipelineData;
38 import org.apache.turbine.test.BaseTestCase;
39 import org.apache.turbine.test.EnhancedMockHttpServletRequest;
40 import org.apache.turbine.test.EnhancedMockHttpSession;
41 import org.apache.turbine.util.RunData;
42 import org.apache.turbine.util.TurbineConfig;
43 import org.junit.AfterClass;
44 import org.junit.Before;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47
48 import com.mockobjects.servlet.MockHttpServletResponse;
49 import com.mockobjects.servlet.MockServletConfig;
50
51
52
53
54
55
56
57
58
59
60
61
62 public class ActionLoaderTest extends BaseTestCase
63 {
64 private static TurbineConfig tc = null;
65 private MockServletConfig config = null;
66 private EnhancedMockHttpServletRequest request = null;
67 private EnhancedMockHttpSession session = null;
68 private HttpServletResponse response = null;
69
70
71
72
73
74 @BeforeClass
75 public static void init() {
76 tc = new TurbineConfig(".", "/conf/test/CompleteTurbineResources.properties");
77 tc.initialize();
78 }
79
80 @Before
81 public void setUpBefore() throws Exception
82 {
83 config = new MockServletConfig();
84 config.setupNoParameters();
85 request = new EnhancedMockHttpServletRequest();
86 request.setupServerName("bob");
87 request.setupGetProtocol("http");
88 request.setupScheme("scheme");
89 request.setupPathInfo("damn");
90 request.setupGetServletPath("damn2");
91 request.setupGetContextPath("wow");
92 request.setupGetContentType("html/text");
93 request.setupAddHeader("Content-type", "html/text");
94 request.setupAddHeader("Accept-Language", "en-US");
95 Vector<String> v = new Vector<String>();
96 request.setupGetParameterNames(v.elements());
97 session = new EnhancedMockHttpSession();
98 response = new MockHttpServletResponse();
99 session.setupGetAttribute(User.SESSION_KEY, null);
100 request.setSession(session);
101 }
102
103
104
105
106 @AfterClass
107 public static void tearDown() throws Exception
108 {
109 if (tc != null)
110 {
111 tc.dispose();
112 }
113 }
114
115
116
117
118
119
120
121
122
123 @Test
124 public void testDoPerformBubblesException() throws Exception
125 {
126 System.out.println("tcturbine:"+ tc.getTurbine());
127
128 }
129
130
131
132
133
134
135
136
137
138 @Test
139 public void testActionEventBubblesException() throws Exception
140 {
141
142
143 request.setupAddParameter("eventSubmit_doCauseexception", "foo");
144 RunData data = getRunData(request, response, config);
145 PipelineData pipelineData = new DefaultPipelineData();
146 Map<Class<?>, Object> runDataMap = new HashMap<Class<?>, Object>();
147 runDataMap.put(RunData.class, data);
148 pipelineData.put(RunData.class, runDataMap);
149 data.setAction("VelocityActionThrowsException");
150 data.getParameters().add("eventSubmit_doCauseexception", "foo");
151 assertTrue(data.getParameters().containsKey("eventSubmit_doCauseexception"));
152 try
153 {
154 ActionLoader.getInstance().exec(data, data.getAction());
155 fail("Should have bubbled out an exception thrown by the action.");
156 }
157 catch (Exception e)
158 {
159
160 }
161 try
162 {
163 ActionLoader.getInstance().exec(pipelineData, data.getAction());
164 fail("Should have bubbled out an exception thrown by the action.");
165 }
166 catch (Exception e)
167 {
168
169 }
170 }
171
172
173
174
175
176
177
178
179
180 @Test
181 public void testDoPerformDoesntBubbleException() throws Exception
182 {
183 Turbine.getConfiguration().setProperty("action.event.bubbleexception", Boolean.FALSE);
184 assertFalse(Turbine.getConfiguration().getBoolean("action.event.bubbleexception"));
185 RunData data = getRunData(request, response, config);
186 PipelineData pipelineData = new DefaultPipelineData();
187 Map<Class<?>, Object> runDataMap = new HashMap<Class<?>, Object>();
188 runDataMap.put(RunData.class, data);
189 pipelineData.put(RunData.class, runDataMap);
190 data.setAction("VelocityActionThrowsException");
191 try
192 {
193 ActionLoader.getInstance().exec(data, data.getAction());
194 }
195 catch (Exception e)
196 {
197 fail("Should NOT have thrown an exception:" + e.getMessage());
198 }
199 try
200 {
201 ActionLoader.getInstance().exec(pipelineData, data.getAction());
202 }
203 catch (Exception e)
204 {
205 fail("Should NOT have thrown an exception:" + e.getMessage());
206 }
207 }
208
209
210
211
212
213
214
215
216
217 @Test
218 public void testActionEventDoesntBubbleException() throws Exception
219 {
220
221
222 Turbine.getConfiguration().setProperty("action.event.bubbleexception", Boolean.FALSE);
223 request.setupAddParameter("eventSubmit_doCauseexception", "foo");
224 RunData data = getRunData(request, response, config);
225 PipelineData pipelineData = new DefaultPipelineData();
226 Map<Class<?>, Object> runDataMap = new HashMap<Class<?>, Object>();
227 runDataMap.put(RunData.class, data);
228 pipelineData.put(RunData.class, runDataMap);
229 data.setAction("VelocityActionThrowsException");
230 data.getParameters().add("eventSubmit_doCauseexception", "foo");
231 assertTrue(data.getParameters().containsKey("eventSubmit_doCauseexception"));
232
233 try
234 {
235 ActionLoader.getInstance().exec(data, data.getAction());
236 }
237 catch (Exception e)
238 {
239 fail("Should NOT have thrown an exception:" + e.getMessage());
240 }
241 try
242 {
243 ActionLoader.getInstance().exec(pipelineData, data.getAction());
244 }
245 catch (Exception e)
246 {
247 fail("Should NOT have thrown an exception:" + e.getMessage());
248 }
249 }
250
251
252
253
254
255
256
257
258 @Test
259 public void testActionEventAnnotation() throws Exception
260 {
261
262
263 request.setupAddParameter("eventSubmit_annotatedEvent", "foo");
264 RunData data = getRunData(request, response, config);
265 PipelineData pipelineData = data;
266 data.setAction("VelocityActionDoesNothing");
267 data.getParameters().add("eventSubmit_annotatedEvent", "foo");
268
269 int numberOfCalls = VelocityActionDoesNothing.numberOfCalls;
270 int pipelineDataCalls = VelocityActionDoesNothing.pipelineDataCalls;
271 int actionEventCalls = VelocityActionDoesNothing.actionEventCalls;
272 try
273 {
274 ActionLoader.getInstance().exec(pipelineData, data.getAction());
275 }
276 catch (Exception e)
277 {
278 e.printStackTrace();
279 fail("Should not have thrown an exception.");
280 }
281 assertEquals(numberOfCalls + 1, VelocityActionDoesNothing.numberOfCalls);
282 assertEquals(pipelineDataCalls, VelocityActionDoesNothing.pipelineDataCalls);
283 assertEquals(actionEventCalls + 1, VelocityActionDoesNothing.actionEventCalls);
284 }
285
286 @Test
287 public void testNonexistentActionCausesError() throws Exception
288 {
289 RunData data = getRunData(request, response, config);
290 PipelineData pipelineData = new DefaultPipelineData();
291 Map<Class<?>, Object> runDataMap = new HashMap<Class<?>, Object>();
292 runDataMap.put(RunData.class, data);
293 pipelineData.put(RunData.class, runDataMap);
294 data.setAction("ImaginaryAction");
295 try
296 {
297 ActionLoader.getInstance().exec(data, "boo");
298 fail("Should have thrown an exception");
299 }
300 catch (Exception e)
301 {
302
303 }
304 try
305 {
306 ActionLoader.getInstance().exec(pipelineData, "boo");
307 fail("Should have thrown an exception");
308 }
309 catch (Exception e)
310 {
311
312 }
313 }
314
315 @Test
316 public void testDoPerformWithPipelineData() throws Exception
317 {
318 RunData data = getRunData(request, response, config);
319 PipelineData pipelineData = data;
320 data.setAction("VelocityActionDoesNothing");
321 int numberOfCalls = VelocityActionDoesNothing.numberOfCalls;
322 int pipelineDataCalls = VelocityActionDoesNothing.pipelineDataCalls;
323 try
324 {
325 ActionLoader.getInstance().exec(pipelineData, data.getAction());
326 }
327 catch (Exception e)
328 {
329 e.printStackTrace();
330 fail("Should not have thrown an exception.");
331 }
332 assertEquals(numberOfCalls + 1, VelocityActionDoesNothing.numberOfCalls);
333 assertEquals(pipelineDataCalls + 1, VelocityActionDoesNothing.pipelineDataCalls);
334 }
335
336 @Test
337 public void testDoPerformWithServiceInjection() throws Exception
338 {
339 RunData data = getRunData(request, response, config);
340 PipelineData pipelineData = data;
341 data.setAction("VelocityActionWithServiceInjection");
342
343 try
344 {
345 ActionLoader.getInstance().exec(pipelineData, data.getAction());
346 }
347 catch (Exception e)
348 {
349 e.printStackTrace();
350 fail("Should not have thrown an exception.");
351 }
352 }
353 }