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
    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   * This test case is to verify whether exceptions in Velocity actions are
    53   * properly bubbled up when action.event.bubbleexception=true. Or, if
    54   * action.event.bubbleexception=false, then the exceptions should be logged and
    55   * sunk.
    56   *
    57   * Changes 2014/Jun/26 (gk): removed Constructor with String parameter as no Test VelocityErrorScreenTest is found and JUnit does not allow it.
    58   *
    59   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
    60   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    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       * @see TestCase#setUp()
    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      * @see TestCase#tearDown()
    105      */
    106     @AfterClass
    107     public static void tearDown() throws Exception
    108     {
    109         if (tc != null)
    110         {
    111             tc.dispose();
    112         }
    113     }
    114
    115     /**
    116      * This unit test verifies that if your standard doPerform is called, and it
    117      * throws an Exception, the exception is bubbled up out of the
    118      * ActionLoader...
    119      *
    120      * @throws Exception
    121      *             If something goes wrong with the unit test
    122      */
    123     @Test
    124     public void testDoPerformBubblesException() throws Exception
    125     {
    126         System.out.println("tcturbine:"+ tc.getTurbine());
    127
    128     }
    129
    130     /**
    131      * This unit test verifies that if an Action Event doEventSubmit_ is called,
    132      * and it throws an Exception, the exception is bubbled up out of the
    133      * ActionLoader...
    134      *
    135      * @throws Exception
    136      *             If something goes wrong with the unit test
    137      */
    138     @Test
    139     public void testActionEventBubblesException() throws Exception
    140     {
    141         // can't seem to figure out how to setup the Mock Request with the right
    142         // parameters...
    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             // good
    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             // good
    169         }
    170     }
    171
    172     /**
    173      * This unit test verifies that if your standard doPerform is called, and it
    174      * throws an Exception, if the action.event.bubbleexception property is set
    175      * to false then the exception is NOT bubbled up
    176      *
    177      * @throws Exception
    178      *             If something goes wrong with the unit test
    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      * This unit test verifies that if an Action Event doEventSubmit_ is called,
    211      * and it throws an Exception, if the action.event.bubbleexception property
    212      * is set to false then the exception is NOT bubbled up
    213      *
    214      * @throws Exception
    215      *             If something goes wrong with the unit test
    216      */
    217     @Test
    218     public void testActionEventDoesntBubbleException() throws Exception
    219     {
    220         // can't seem to figure out how to setup the Mock Request with the right
    221         // parameters...
    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      * This unit test verifies that if an Action Event doEventSubmit_ is called,
    253      * a properly annotated method is being called
    254      *
    255      * @throws Exception
    256      *             If something goes wrong with the unit test
    257      */
    258     @Test
    259     public void testActionEventAnnotation() throws Exception
    260     {
    261         // can't seem to figure out how to setup the Mock Request with the right
    262         // parameters...
    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             // good
    303         }
    304         try
    305         {
    306             ActionLoader.getInstance().exec(pipelineData, "boo");
    307             fail("Should have thrown an exception");
    308         }
    309         catch (Exception e)
    310         {
    311             // good
    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 }