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