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 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import javax.servlet.ServletConfig;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36 import org.apache.turbine.Turbine;
37 import org.apache.turbine.modules.actions.VelocityActionDoesNothing;
38 import org.apache.turbine.pipeline.DefaultPipelineData;
39 import org.apache.turbine.pipeline.PipelineData;
40 import org.apache.turbine.test.BaseTestCase;
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
49
50
51
52
53
54
55
56
57
58
59 public class ActionLoaderTest extends BaseTestCase
60 {
61 private static TurbineConfig tc = null;
62 private ServletConfig config = null;
63 private HttpServletRequest request = null;
64 private HttpServletResponse response = null;
65
66
67
68
69
70 @BeforeClass
71 public static void init()
72 {
73 tc = new TurbineConfig(".", "/conf/test/CompleteTurbineResources.properties");
74 tc.initialize();
75 }
76
77 @Before
78 public void setUpBefore() throws Exception
79 {
80 config = mock(ServletConfig.class);
81 request = getMockRequest();
82 response = mock(HttpServletResponse.class);
83 }
84
85
86
87
88 @AfterClass
89 public static void tearDown() throws Exception
90 {
91 if (tc != null)
92 {
93 tc.dispose();
94 }
95 }
96
97
98
99
100
101
102
103
104
105 @Test
106 public void testDoPerformBubblesException() throws Exception
107 {
108 System.out.println("tcturbine:"+ tc.getTurbine());
109 }
110
111
112
113
114
115
116
117
118
119 @Test
120 public void testActionEventBubblesException() throws Exception
121 {
122 when(request.getParameterValues("eventSubmit_doCauseexception")).thenReturn(new String[] { "foo" });
123 RunData data = getRunData(request, response, config);
124 PipelineData pipelineData = new DefaultPipelineData();
125 Map<Class<?>, Object> runDataMap = new HashMap<Class<?>, Object>();
126 runDataMap.put(RunData.class, data);
127 pipelineData.put(RunData.class, runDataMap);
128 data.setAction("VelocityActionThrowsException");
129 data.getParameters().add("eventSubmit_doCauseexception", "foo");
130 assertTrue(data.getParameters().containsKey("eventSubmit_doCauseexception"));
131 try
132 {
133 ActionLoader.getInstance().exec(data, data.getAction());
134 fail("Should have bubbled out an exception thrown by the action.");
135 }
136 catch (Exception e)
137 {
138
139 }
140 try
141 {
142 ActionLoader.getInstance().exec(pipelineData, data.getAction());
143 fail("Should have bubbled out an exception thrown by the action.");
144 }
145 catch (Exception e)
146 {
147
148 }
149 }
150
151
152
153
154
155
156
157
158
159 @Test
160 public void testDoPerformDoesntBubbleException() throws Exception
161 {
162 Turbine.getConfiguration().setProperty("action.event.bubbleexception", Boolean.FALSE);
163 assertFalse(Turbine.getConfiguration().getBoolean("action.event.bubbleexception"));
164 RunData data = getRunData(request, response, config);
165 PipelineData pipelineData = new DefaultPipelineData();
166 Map<Class<?>, Object> runDataMap = new HashMap<Class<?>, Object>();
167 runDataMap.put(RunData.class, data);
168 pipelineData.put(RunData.class, runDataMap);
169 data.setAction("VelocityActionThrowsException");
170 try
171 {
172 ActionLoader.getInstance().exec(data, data.getAction());
173 }
174 catch (Exception e)
175 {
176 fail("Should NOT have thrown an exception:" + e.getMessage());
177 }
178 try
179 {
180 ActionLoader.getInstance().exec(pipelineData, data.getAction());
181 }
182 catch (Exception e)
183 {
184 fail("Should NOT have thrown an exception:" + e.getMessage());
185 }
186 }
187
188
189
190
191
192
193
194
195
196 @Test
197 public void testActionEventDoesntBubbleException() throws Exception
198 {
199
200
201 Turbine.getConfiguration().setProperty("action.event.bubbleexception", Boolean.FALSE);
202 when(request.getParameterValues("eventSubmit_doCauseexception")).thenReturn(new String[] { "foo" });
203 RunData data = getRunData(request, response, config);
204 PipelineData pipelineData = new DefaultPipelineData();
205 Map<Class<?>, Object> runDataMap = new HashMap<Class<?>, Object>();
206 runDataMap.put(RunData.class, data);
207 pipelineData.put(RunData.class, runDataMap);
208 data.setAction("VelocityActionThrowsException");
209 data.getParameters().add("eventSubmit_doCauseexception", "foo");
210 assertTrue(data.getParameters().containsKey("eventSubmit_doCauseexception"));
211
212 try
213 {
214 ActionLoader.getInstance().exec(data, data.getAction());
215 }
216 catch (Exception e)
217 {
218 fail("Should NOT have thrown an exception:" + e.getMessage());
219 }
220 try
221 {
222 ActionLoader.getInstance().exec(pipelineData, data.getAction());
223 }
224 catch (Exception e)
225 {
226 fail("Should NOT have thrown an exception:" + e.getMessage());
227 }
228 }
229
230
231
232
233
234
235
236
237 @Test
238 public void testActionEventAnnotation() throws Exception
239 {
240 when(request.getParameterValues("eventSubmit_annotatedEvent")).thenReturn(new String[] { "foo" });
241 RunData data = getRunData(request, response, config);
242 PipelineData pipelineData = data;
243 data.setAction("VelocityActionDoesNothing");
244 data.getParameters().add("eventSubmit_annotatedEvent", "foo");
245
246 int numberOfCalls = VelocityActionDoesNothing.numberOfCalls;
247 int pipelineDataCalls = VelocityActionDoesNothing.pipelineDataCalls;
248 int actionEventCalls = VelocityActionDoesNothing.actionEventCalls;
249 try
250 {
251 ActionLoader.getInstance().exec(pipelineData, data.getAction());
252 }
253 catch (Exception e)
254 {
255 e.printStackTrace();
256 fail("Should not have thrown an exception.");
257 }
258 assertEquals(numberOfCalls + 1, VelocityActionDoesNothing.numberOfCalls);
259 assertEquals(pipelineDataCalls, VelocityActionDoesNothing.pipelineDataCalls);
260 assertEquals(actionEventCalls + 1, VelocityActionDoesNothing.actionEventCalls);
261 }
262
263 @Test
264 public void testNonexistentActionCausesError() throws Exception
265 {
266 RunData data = getRunData(request, response, config);
267 PipelineData pipelineData = new DefaultPipelineData();
268 Map<Class<?>, Object> runDataMap = new HashMap<Class<?>, Object>();
269 runDataMap.put(RunData.class, data);
270 pipelineData.put(RunData.class, runDataMap);
271 data.setAction("ImaginaryAction");
272 try
273 {
274 ActionLoader.getInstance().exec(data, "boo");
275 fail("Should have thrown an exception");
276 }
277 catch (Exception e)
278 {
279
280 }
281 try
282 {
283 ActionLoader.getInstance().exec(pipelineData, "boo");
284 fail("Should have thrown an exception");
285 }
286 catch (Exception e)
287 {
288
289 }
290 }
291
292 @Test
293 public void testDoPerformWithPipelineData() throws Exception
294 {
295 RunData data = getRunData(request, response, config);
296 PipelineData pipelineData = data;
297 data.setAction("VelocityActionDoesNothing");
298 int numberOfCalls = VelocityActionDoesNothing.numberOfCalls;
299 int pipelineDataCalls = VelocityActionDoesNothing.pipelineDataCalls;
300 try
301 {
302 ActionLoader.getInstance().exec(pipelineData, data.getAction());
303 }
304 catch (Exception e)
305 {
306 e.printStackTrace();
307 fail("Should not have thrown an exception.");
308 }
309 assertEquals(numberOfCalls + 1, VelocityActionDoesNothing.numberOfCalls);
310 assertEquals(pipelineDataCalls + 1, VelocityActionDoesNothing.pipelineDataCalls);
311 }
312
313 @Test
314 public void testDoPerformWithServiceInjection() throws Exception
315 {
316 RunData data = getRunData(request, response, config);
317 PipelineData pipelineData = data;
318 data.setAction("VelocityActionWithServiceInjection");
319
320 try
321 {
322 ActionLoader.getInstance().exec(pipelineData, data.getAction());
323 }
324 catch (Exception e)
325 {
326 e.printStackTrace();
327 fail("Should not have thrown an exception.");
328 }
329 }
330 }