001    package org.apache.turbine.modules;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    import java.util.Vector;
026    
027    import javax.servlet.http.HttpServletResponse;
028    
029    import junit.framework.Assert;
030    
031    import org.apache.turbine.Turbine;
032    import org.apache.turbine.modules.actions.VelocityActionDoesNothing;
033    import org.apache.turbine.om.security.User;
034    import org.apache.turbine.pipeline.DefaultPipelineData;
035    import org.apache.turbine.pipeline.PipelineData;
036    import org.apache.turbine.test.BaseTestCase;
037    import org.apache.turbine.test.EnhancedMockHttpServletRequest;
038    import org.apache.turbine.test.EnhancedMockHttpSession;
039    import org.apache.turbine.util.RunData;
040    import org.apache.turbine.util.TurbineConfig;
041    
042    import com.mockobjects.servlet.MockHttpServletResponse;
043    import com.mockobjects.servlet.MockServletConfig;
044    /**
045     * This test case is to verify whether exceptions in Velocity actions are
046     * properly bubbled up when action.event.bubbleexception=true.  Or, if
047     * action.event.bubbleexception=false, then the exceptions should be
048     * logged and sunk.
049     *
050     * @author     <a href="mailto:epugh@upstate.com">Eric Pugh</a>
051     * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
052     */
053    public class ActionLoaderTest extends BaseTestCase {
054            private static TurbineConfig tc = null;
055            private MockServletConfig config = null;
056            private EnhancedMockHttpServletRequest request = null;
057            private EnhancedMockHttpSession session = null;
058            private HttpServletResponse response = null;
059    
060        /*
061             * @see TestCase#setUp()
062             */
063            protected void setUp() throws Exception {
064                    super.setUp();
065                    config = new MockServletConfig();
066                    config.setupNoParameters();
067                    request = new EnhancedMockHttpServletRequest();
068                    request.setupServerName("bob");
069                    request.setupGetProtocol("http");
070                    request.setupScheme("scheme");
071                    request.setupPathInfo("damn");
072                    request.setupGetServletPath("damn2");
073                    request.setupGetContextPath("wow");
074                    request.setupGetContentType("html/text");
075                    request.setupAddHeader("Content-type", "html/text");
076                    request.setupAddHeader("Accept-Language", "en-US");
077                    Vector v = new Vector();
078                    request.setupGetParameterNames(v.elements());
079                    session = new EnhancedMockHttpSession();
080                    response = new MockHttpServletResponse();
081                    session.setupGetAttribute(User.SESSION_KEY, null);
082                    request.setSession(session);
083                    tc =
084                            new TurbineConfig(
085                                    ".",
086                                    "/conf/test/CompleteTurbineResources.properties");
087                    tc.initialize();
088            }
089            /*
090             * @see TestCase#tearDown()
091             */
092            protected void tearDown() throws Exception {
093                    super.tearDown();
094                    if (tc != null) {
095                            tc.dispose();
096                    }
097            }
098            /**
099             * Constructor for VelocityErrorScreenTest.
100             * @param arg0
101             */
102            public ActionLoaderTest(String arg0) throws Exception {
103                    super(arg0);
104            }
105            /**
106             * This unit test verifies that if your standard doPerform is called,
107             * and it throws an Exception, the exception is bubbled up out of the ActionLoader...
108             *
109             * @throws Exception If something goes wrong with the unit test
110             */
111            public void testDoPerformBubblesException() throws Exception {
112                    RunData data = getRunData(request,response,config);
113                    PipelineData pipelineData = new DefaultPipelineData();
114                    Map runDataMap = new HashMap();
115                    runDataMap.put(RunData.class, data);
116                    pipelineData.put(RunData.class, runDataMap);
117                    data.setAction("VelocityActionThrowsException");
118                    try {
119                            ActionLoader.getInstance().exec(data, data.getAction());
120                            fail("Should have thrown an exception");
121                    } catch (Exception e) {
122                            //good
123                    }
124    
125                    try {
126                            ActionLoader.getInstance().exec(pipelineData, data.getAction());
127                            fail("Should have thrown an exception");
128                    } catch (Exception e) {
129                            //good
130                    }
131            }
132            /**
133               * This unit test verifies that if an Action Event doEventSubmit_ is called, and it throws an Exception, the
134               * exception is bubbled up out of the ActionLoader...
135               *
136               * @throws Exception If something goes wrong with the unit test
137               */
138            public void testActionEventBubblesException() throws Exception {
139                    // can't seem to figure out how to setup the Mock Request with the right parameters...
140                    request.setupAddParameter("eventSubmit_doCauseexception", "foo");
141                    RunData data = getRunData(request,response,config);
142                    PipelineData pipelineData = new DefaultPipelineData();
143                    Map runDataMap = new HashMap();
144                    runDataMap.put(RunData.class, data);
145                    pipelineData.put(RunData.class, runDataMap);
146                    data.setAction("VelocityActionThrowsException");
147                    data.getParameters().add("eventSubmit_doCauseexception", "foo");
148                    assertTrue(
149                            data.getParameters().containsKey("eventSubmit_doCauseexception"));
150                    try {
151                            ActionLoader.getInstance().exec(data, data.getAction());
152                            fail("Should have bubbled out an exception thrown by the action.");
153                    } catch (Exception e) {
154                            //good
155                    }
156                    try {
157                            ActionLoader.getInstance().exec(pipelineData, data.getAction());
158                            fail("Should have bubbled out an exception thrown by the action.");
159                    } catch (Exception e) {
160                            //good
161                    }
162            }
163    
164            /**
165             * This unit test verifies that if your standard doPerform is called,
166             * and it throws an Exception, if the action.event.bubbleexception
167         * property is set to false then the exception is NOT bubbled up
168             *
169             * @throws Exception If something goes wrong with the unit test
170             */
171            public void testDoPerformDoesntBubbleException() throws Exception {
172                    Turbine.getConfiguration().setProperty("action.event.bubbleexception",Boolean.FALSE);
173                    assertFalse(Turbine.getConfiguration().getBoolean("action.event.bubbleexception"));
174                    RunData data = getRunData(request,response,config);
175                    PipelineData pipelineData = new DefaultPipelineData();
176                    Map runDataMap = new HashMap();
177                    runDataMap.put(RunData.class, data);
178                    pipelineData.put(RunData.class, runDataMap);
179                    data.setAction("VelocityActionThrowsException");
180                    try {
181                            ActionLoader.getInstance().exec(data, data.getAction());
182    
183                    } catch (Exception e) {
184                            fail("Should NOT have thrown an exception:" + e.getMessage());
185                    }
186                    try {
187                            ActionLoader.getInstance().exec(pipelineData, data.getAction());
188    
189                    } catch (Exception e) {
190                            fail("Should NOT have thrown an exception:" + e.getMessage());
191                    }
192            }
193            /**
194         * This unit test verifies that if an Action Event doEventSubmit_ is called,
195         * and it throws an Exception, if the action.event.bubbleexception
196         * property is set to false then the exception is NOT bubbled up
197         *
198         * @throws Exception If something goes wrong with the unit test
199         */
200            public void testActionEventDoesntBubbleException() throws Exception {
201                    // can't seem to figure out how to setup the Mock Request with the right parameters...
202                    Turbine.getConfiguration().setProperty("action.event.bubbleexception",Boolean.FALSE);
203                    request.setupAddParameter("eventSubmit_doCauseexception", "foo");
204                    RunData data = getRunData(request,response,config);
205                    PipelineData pipelineData = new DefaultPipelineData();
206                    Map runDataMap = new HashMap();
207                    runDataMap.put(RunData.class, data);
208                    pipelineData.put(RunData.class, runDataMap);
209                    data.setAction("VelocityActionThrowsException");
210                    data.getParameters().add("eventSubmit_doCauseexception", "foo");
211                    assertTrue(
212                            data.getParameters().containsKey("eventSubmit_doCauseexception"));
213    
214                    try {
215                            ActionLoader.getInstance().exec(data, data.getAction());
216                    } catch (Exception e) {
217                            fail("Should NOT have thrown an exception:" + e.getMessage());
218                    }
219                    try {
220                            ActionLoader.getInstance().exec(pipelineData, data.getAction());
221                    } catch (Exception e) {
222                            fail("Should NOT have thrown an exception:" + e.getMessage());
223                    }
224            }
225            public void testNonexistentActionCausesError() throws Exception {
226                RunData data = getRunData(request,response,config);
227                    PipelineData pipelineData = new DefaultPipelineData();
228                    Map runDataMap = new HashMap();
229                    runDataMap.put(RunData.class, data);
230                    pipelineData.put(RunData.class, runDataMap);
231                    data.setAction("ImaginaryAction");
232                    try {
233                            ActionLoader.getInstance().exec(data, "boo");
234                            fail("Should have thrown an exception");
235                    } catch (Exception e) {
236                            //good
237                    }
238                    try {
239                            ActionLoader.getInstance().exec(pipelineData, "boo");
240                            fail("Should have thrown an exception");
241                    } catch (Exception e) {
242                            //good
243                    }
244            }
245    
246            public void testDoPerformWithRunData() throws Exception
247            {
248                RunData data = getRunData(request,response,config);
249                    data.setAction("VelocityActionDoesNothing");
250                    try {
251                            ActionLoader.getInstance().exec(data, data.getAction());
252                    } catch (Exception e) {
253                        e.printStackTrace();
254                        Assert.fail("Should not have thrown an exception.");
255                    }
256    
257            }
258    
259            public void testDoPerformWithPipelineData() throws Exception
260            {
261                RunData data = getRunData(request,response,config);
262            PipelineData pipelineData = data;
263                    data.setAction("VelocityActionDoesNothing");
264                    int numberOfCalls = VelocityActionDoesNothing.numberOfCalls;
265                    int pipelineDataCalls = VelocityActionDoesNothing.pipelineDataCalls;
266                    int runDataCalls = VelocityActionDoesNothing.runDataCalls;
267                    try {
268                            ActionLoader.getInstance().exec(pipelineData, data.getAction());
269                    } catch (Exception e) {
270                        e.printStackTrace();
271                        Assert.fail("Should not have thrown an exception.");
272                    }
273                    assertEquals(numberOfCalls+1,VelocityActionDoesNothing.numberOfCalls);
274                    assertEquals(runDataCalls,VelocityActionDoesNothing.runDataCalls);
275                    assertEquals(pipelineDataCalls+1,VelocityActionDoesNothing.pipelineDataCalls);
276    
277            }
278    
279    }