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  
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Vector;
26  
27  import javax.servlet.http.HttpServletResponse;
28  
29  import junit.framework.Assert;
30  
31  import org.apache.turbine.Turbine;
32  import org.apache.turbine.modules.actions.VelocityActionDoesNothing;
33  import org.apache.turbine.om.security.User;
34  import org.apache.turbine.pipeline.DefaultPipelineData;
35  import org.apache.turbine.pipeline.PipelineData;
36  import org.apache.turbine.test.BaseTestCase;
37  import org.apache.turbine.test.EnhancedMockHttpServletRequest;
38  import org.apache.turbine.test.EnhancedMockHttpSession;
39  import org.apache.turbine.util.RunData;
40  import org.apache.turbine.util.TurbineConfig;
41  
42  import com.mockobjects.servlet.MockHttpServletResponse;
43  import com.mockobjects.servlet.MockServletConfig;
44  /**
45   * This test case is to verify whether exceptions in Velocity actions are
46   * properly bubbled up when action.event.bubbleexception=true.  Or, if
47   * action.event.bubbleexception=false, then the exceptions should be
48   * logged and sunk.
49   *
50   * @author     <a href="mailto:epugh@upstate.com">Eric Pugh</a>
51   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
52   */
53  public class ActionLoaderTest extends BaseTestCase {
54  	private static TurbineConfig tc = null;
55  	private MockServletConfig config = null;
56  	private EnhancedMockHttpServletRequest request = null;
57  	private EnhancedMockHttpSession session = null;
58  	private HttpServletResponse response = null;
59  
60      /*
61  	 * @see TestCase#setUp()
62  	 */
63  	protected void setUp() throws Exception {
64  		super.setUp();
65  		config = new MockServletConfig();
66  		config.setupNoParameters();
67  		request = new EnhancedMockHttpServletRequest();
68  		request.setupServerName("bob");
69  		request.setupGetProtocol("http");
70  		request.setupScheme("scheme");
71  		request.setupPathInfo("damn");
72  		request.setupGetServletPath("damn2");
73  		request.setupGetContextPath("wow");
74  		request.setupGetContentType("html/text");
75  		request.setupAddHeader("Content-type", "html/text");
76  		request.setupAddHeader("Accept-Language", "en-US");
77  		Vector v = new Vector();
78  		request.setupGetParameterNames(v.elements());
79  		session = new EnhancedMockHttpSession();
80  		response = new MockHttpServletResponse();
81  		session.setupGetAttribute(User.SESSION_KEY, null);
82  		request.setSession(session);
83  		tc =
84  			new TurbineConfig(
85  				".",
86  				"/conf/test/CompleteTurbineResources.properties");
87  		tc.initialize();
88  	}
89  	/*
90  	 * @see TestCase#tearDown()
91  	 */
92  	protected void tearDown() throws Exception {
93  		super.tearDown();
94  		if (tc != null) {
95  			tc.dispose();
96  		}
97  	}
98  	/**
99  	 * 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 }