1 package org.apache.turbine.modules;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
46
47
48
49
50
51
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
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
91
92 protected void tearDown() throws Exception {
93 super.tearDown();
94 if (tc != null) {
95 tc.dispose();
96 }
97 }
98
99
100
101
102 public ActionLoaderTest(String arg0) throws Exception {
103 super(arg0);
104 }
105
106
107
108
109
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
123 }
124
125 try {
126 ActionLoader.getInstance().exec(pipelineData, data.getAction());
127 fail("Should have thrown an exception");
128 } catch (Exception e) {
129
130 }
131 }
132
133
134
135
136
137
138 public void testActionEventBubblesException() throws Exception {
139
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
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
161 }
162 }
163
164
165
166
167
168
169
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
195
196
197
198
199
200 public void testActionEventDoesntBubbleException() throws Exception {
201
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
237 }
238 try {
239 ActionLoader.getInstance().exec(pipelineData, "boo");
240 fail("Should have thrown an exception");
241 } catch (Exception e) {
242
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 }