1   package org.apache.turbine.modules;
2   
3   /*
4    * Copyright 2001-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Vector;
23  
24  import javax.servlet.ServletConfig;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import junit.framework.Assert;
28  
29  import org.apache.turbine.Turbine;
30  import org.apache.turbine.modules.actions.VelocityActionDoesNothing;
31  import org.apache.turbine.om.security.User;
32  import org.apache.turbine.pipeline.DefaultPipelineData;
33  import org.apache.turbine.pipeline.PipelineData;
34  import org.apache.turbine.services.template.TemplateService;
35  import org.apache.turbine.test.BaseTestCase;
36  import org.apache.turbine.test.EnhancedMockHttpServletRequest;
37  import org.apache.turbine.test.EnhancedMockHttpSession;
38  import org.apache.turbine.util.RunData;
39  import org.apache.turbine.util.TurbineConfig;
40  
41  import com.mockobjects.servlet.MockHttpServletResponse;
42  import com.mockobjects.servlet.MockServletConfig;
43  /***
44   * This test case is to verify whether exceptions in Velocity actions are 
45   * properly bubbled up when action.event.bubbleexception=true.  Or, if
46   * action.event.bubbleexception=false, then the exceptions should be
47   * logged and sunk.
48   * 
49   * @author     <a href="mailto:epugh@upstate.com">Eric Pugh</a>
50   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
51   */
52  public class ActionLoaderTest extends BaseTestCase {
53  	private static TurbineConfig tc = null;
54  	private static TemplateService ts = null;
55  	private MockServletConfig config = null;
56  	private EnhancedMockHttpServletRequest request = null;
57  	private EnhancedMockHttpSession session = null;
58  	private HttpServletResponse response = null;
59  	private static ServletConfig sc = null;
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  		sc = config;
84  		tc =
85  			new TurbineConfig(
86  				".",
87  				"/conf/test/CompleteTurbineResources.properties");
88  		tc.initialize();
89  	}
90  	/*
91  	 * @see TestCase#tearDown()
92  	 */
93  	protected void tearDown() throws Exception {
94  		super.tearDown();
95  		if (tc != null) {
96  			tc.dispose();
97  		}
98  	}
99  	/***
100 	 * Constructor for VelocityErrorScreenTest.
101 	 * @param arg0
102 	 */
103 	public ActionLoaderTest(String arg0) throws Exception {
104 		super(arg0);
105 	}
106 	/***
107 	 * This unit test verifies that if your standard doPerform is called, 
108 	 * and it throws an Exception, the exception is bubbled up out of the ActionLoader...
109 	 * 
110 	 * @throws Exception If something goes wrong with the unit test
111 	 */
112 	public void testDoPerformBubblesException() throws Exception {
113 		RunData data = getRunData(request,response,config);
114 		PipelineData pipelineData = new DefaultPipelineData();
115 		Map runDataMap = new HashMap();
116 		runDataMap.put(RunData.class, data);
117 		pipelineData.put(RunData.class, runDataMap);
118 		data.setAction("VelocityActionThrowsException");
119 		try {
120 			ActionLoader.getInstance().exec(data, data.getAction());
121 			fail("Should have thrown an exception");
122 		} catch (Exception e) {
123 			//good
124 		}
125 		
126 		try {
127 			ActionLoader.getInstance().exec(pipelineData, data.getAction());
128 			fail("Should have thrown an exception");
129 		} catch (Exception e) {
130 			//good
131 		}
132 	}
133 	/***
134 	   * This unit test verifies that if an Action Event doEventSubmit_ is called, and it throws an Exception, the
135 	   * exception is bubbled up out of the ActionLoader...
136 	   * 
137 	   * @throws Exception If something goes wrong with the unit test
138 	   */
139 	public void testActionEventBubblesException() throws Exception {
140 		// can't seem to figure out how to setup the Mock Request with the right parameters...
141 		request.setupAddParameter("eventSubmit_doCauseexception", "foo");
142 		RunData data = getRunData(request,response,config);
143 		PipelineData pipelineData = new DefaultPipelineData();
144 		Map runDataMap = new HashMap();
145 		runDataMap.put(RunData.class, data);
146 		pipelineData.put(RunData.class, runDataMap);
147 		data.setAction("VelocityActionThrowsException");
148 		data.getParameters().add("eventSubmit_doCauseexception", "foo");
149 		assertTrue(
150 			data.getParameters().containsKey("eventSubmit_doCauseexception"));
151 		try {
152 			ActionLoader.getInstance().exec(data, data.getAction());
153 			fail("Should have bubbled out an exception thrown by the action.");
154 		} catch (Exception e) {
155 			//good
156 		}
157 		try {
158 			ActionLoader.getInstance().exec(pipelineData, data.getAction());
159 			fail("Should have bubbled out an exception thrown by the action.");
160 		} catch (Exception e) {
161 			//good
162 		}
163 	}
164 
165 	/***
166 	 * This unit test verifies that if your standard doPerform is called, 
167 	 * and it throws an Exception, if the action.event.bubbleexception
168      * property is set to false then the exception is NOT bubbled up
169 	 * 
170 	 * @throws Exception If something goes wrong with the unit test
171 	 */
172 	public void testDoPerformDoesntBubbleException() throws Exception {
173 		Turbine.getConfiguration().setProperty("action.event.bubbleexception",Boolean.FALSE);
174 		assertFalse(Turbine.getConfiguration().getBoolean("action.event.bubbleexception"));
175 		RunData data = getRunData(request,response,config);
176 		PipelineData pipelineData = new DefaultPipelineData();
177 		Map runDataMap = new HashMap();
178 		runDataMap.put(RunData.class, data);
179 		pipelineData.put(RunData.class, runDataMap);
180 		data.setAction("VelocityActionThrowsException");
181 		try {
182 			ActionLoader.getInstance().exec(data, data.getAction());
183 		
184 		} catch (Exception e) {
185 			fail("Should NOT have thrown an exception:" + e.getMessage());
186 		}	
187 		try {
188 			ActionLoader.getInstance().exec(pipelineData, data.getAction());
189 		
190 		} catch (Exception e) {
191 			fail("Should NOT have thrown an exception:" + e.getMessage());
192 		}
193 	}
194 	/***
195      * This unit test verifies that if an Action Event doEventSubmit_ is called, 
196      * and it throws an Exception, if the action.event.bubbleexception
197      * property is set to false then the exception is NOT bubbled up
198      * 
199      * @throws Exception If something goes wrong with the unit test
200      */
201 	public void testActionEventDoesntBubbleException() throws Exception {
202 		// can't seem to figure out how to setup the Mock Request with the right parameters...
203 		Turbine.getConfiguration().setProperty("action.event.bubbleexception",Boolean.FALSE);
204 		request.setupAddParameter("eventSubmit_doCauseexception", "foo");
205 		RunData data = getRunData(request,response,config);
206 		PipelineData pipelineData = new DefaultPipelineData();
207 		Map 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(
213 			data.getParameters().containsKey("eventSubmit_doCauseexception"));
214 		
215 		try {
216 			ActionLoader.getInstance().exec(data, data.getAction());			
217 		} catch (Exception e) {
218 			fail("Should NOT have thrown an exception:" + e.getMessage());
219 		}
220 		try {
221 			ActionLoader.getInstance().exec(pipelineData, data.getAction());			
222 		} catch (Exception e) {
223 			fail("Should NOT have thrown an exception:" + e.getMessage());
224 		}
225 	}
226 	public void testNonexistentActionCausesError() throws Exception {
227 	    RunData data = getRunData(request,response,config);
228 		PipelineData pipelineData = new DefaultPipelineData();
229 		Map runDataMap = new HashMap();
230 		runDataMap.put(RunData.class, data);
231 		pipelineData.put(RunData.class, runDataMap);
232 		data.setAction("ImaginaryAction");
233 		try {
234 			ActionLoader.getInstance().exec(data, "boo");
235 			fail("Should have thrown an exception");
236 		} catch (Exception e) {
237 			//good
238 		}
239 		try {
240 			ActionLoader.getInstance().exec(pipelineData, "boo");
241 			fail("Should have thrown an exception");
242 		} catch (Exception e) {
243 			//good
244 		}
245 	}
246 	
247 	public void testDoPerformWithRunData() throws Exception
248 	{
249 	    RunData data = getRunData(request,response,config);
250 		data.setAction("VelocityActionDoesNothing");
251 		try {
252 			ActionLoader.getInstance().exec(data, data.getAction());
253 		} catch (Exception e) {
254 		    e.printStackTrace();
255 		    Assert.fail("Should not have thrown an exception.");
256 		}
257 	    
258 	}
259 	
260 	public void testDoPerformWithPipelineData() throws Exception
261 	{
262 	    RunData data = getRunData(request,response,config);
263         PipelineData pipelineData = data;
264 		data.setAction("VelocityActionDoesNothing");
265 		int numberOfCalls = VelocityActionDoesNothing.numberOfCalls;
266 		int pipelineDataCalls = VelocityActionDoesNothing.pipelineDataCalls;
267 		int runDataCalls = VelocityActionDoesNothing.runDataCalls;
268 		try {
269 			ActionLoader.getInstance().exec(pipelineData, data.getAction());
270 		} catch (Exception e) {
271 		    e.printStackTrace();
272 		    Assert.fail("Should not have thrown an exception.");
273 		}
274 		assertEquals(numberOfCalls+1,VelocityActionDoesNothing.numberOfCalls);
275 		assertEquals(runDataCalls,VelocityActionDoesNothing.runDataCalls);
276 		assertEquals(pipelineDataCalls+1,VelocityActionDoesNothing.pipelineDataCalls);
277 	    
278 	}
279 
280 }