View Javadoc
1   package org.apache.turbine.modules;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * This test case is to verify whether exceptions in Velocity actions are
52   * properly bubbled up when action.event.bubbleexception=true. Or, if
53   * action.event.bubbleexception=false, then the exceptions should be logged and
54   * sunk.
55   *
56   * Changes 2014/Jun/26 (gk): removed Constructor with String parameter as no Test VelocityErrorScreenTest is found and JUnit does not allow it.
57   *
58   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
59   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
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       * @see TestCase#setUp()
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       * @see TestCase#tearDown()
89       */
90      @AfterClass
91      public static void tearDown() throws Exception
92      {
93          if (tc != null)
94          {
95              tc.dispose();
96          }
97      }
98  
99      /**
100      * This unit test verifies that if your standard doPerform is called, and it
101      * throws an Exception, the exception is bubbled up out of the
102      * ActionLoader...
103      *
104      * @throws Exception
105      *             If something goes wrong with the unit test
106      */
107     @Test
108     public void testDoPerformBubblesException() throws Exception
109     {
110         System.out.println("tcturbine:"+ tc.getTurbine());
111     }
112 
113     /**
114      * This unit test verifies that if an Action Event doEventSubmit_ is called,
115      * and it throws an Exception, the exception is bubbled up out of the
116      * ActionLoader...
117      *
118      * @throws Exception
119      *             If something goes wrong with the unit test
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             // good
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             // good
150         }
151     }
152 
153     /**
154      * This unit test verifies that if your standard doPerform is called, and it
155      * throws an Exception, if the action.event.bubbleexception property is set
156      * to false then the exception is NOT bubbled up
157      *
158      * @throws Exception
159      *             If something goes wrong with the unit test
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      * This unit test verifies that if an Action Event doEventSubmit_ is called,
192      * and it throws an Exception, if the action.event.bubbleexception property
193      * is set to false then the exception is NOT bubbled up
194      *
195      * @throws Exception
196      *             If something goes wrong with the unit test
197      */
198     @Test
199     public void testActionEventDoesntBubbleException() throws Exception
200     {
201         // can't seem to figure out how to setup the Mock Request with the right
202         // parameters...
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      * This unit test verifies that if an Action Event doEventSubmit_ is called,
234      * a properly annotated method is being called
235      *
236      * @throws Exception
237      *             If something goes wrong with the unit test
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             // good
282         }
283         try
284         {
285             ActionLoader.getInstance().exec(pipelineData, "boo");
286             fail("Should have thrown an exception");
287         }
288         catch (Exception e)
289         {
290             // good
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 }