1 /*
2 * Angkor Web Framework
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */
7
8 package com.tirsen.angkor.test.unit;
9
10 import com.tirsen.angkor.*;
11 import com.tirsen.angkor.test.unit.mock.MockRenderContext;
12 import com.tirsen.angkor.process.*;
13 import com.tirsen.angkor.event.ChangeEvent;
14 import com.tirsen.angkor.event.ChangeListener;
15 import com.tirsen.angkor.widget.*;
16
17 import javax.servlet.ServletException;
18 import java.io.IOException;
19
20 /***
21 * TODO document ProcessTest
22 *
23 * <!-- $Id: ProcessTest.java,v 1.2 2002/10/13 13:37:26 tirsen Exp $ -->
24 * <!-- $Author: tirsen $ -->
25 *
26 * @author Jon Tirs´n (tirsen@users.sourceforge.net)
27 * @version $Revision: 1.2 $
28 */
29 public class ProcessTest extends AbstractPipelineTest
30 {
31 private static final String firstModelValue = "firstvalue";
32 private static final String nextModelValue = "newvalue";
33
34 private TestController testController;
35 private Application application;
36 private MockRenderContext context;
37 private MockPipeline parsePipeline;
38 private MockPipeline eventPipeline;
39 private TestView testView;
40
41 public class TestController extends Controller implements ChangeListener
42 {
43 private ValueModel model = new BasicValueModel(firstModelValue);
44 private boolean eventReceived = false;
45 private boolean fatalError = false;
46 private boolean failDuringEventProcessing = false;
47
48 public TestController(Application application)
49 {
50 super(application);
51 }
52
53 public View createView()
54 {
55 TestView view = new TestView();
56
57 assertNotNull("model not created at this time", model);
58
59 view.setModel(model);
60
61 view.addChangeListener(this);
62
63 return view;
64 }
65
66 public void stateChanged(ChangeEvent evt)
67 {
68 eventReceived = true;
69 assertTrue("model value not changed, value: " + model.getValue(), nextModelValue.equals(model.getValue()));
70 try
71 {
72 if(failDuringEventProcessing) throwError("event processing");
73 }
74 catch(Exception e)
75 {
76 getApplication().handleError(e);
77 }
78 }
79
80 public boolean isEventReceived()
81 {
82 return eventReceived;
83 }
84
85 public void setEventReceived(boolean eventReceived)
86 {
87 this.eventReceived = eventReceived;
88 }
89
90 public void setFailDuringEventProcessing(boolean failDuringEventProcessing)
91 {
92 this.failDuringEventProcessing = failDuringEventProcessing;
93 }
94
95 public void setFatalError(boolean fatalError)
96 {
97 this.fatalError = fatalError;
98 }
99
100 protected void throwError(String phase) throws Exception
101 {
102 if(fatalError)
103 {
104 throw new Exception("Fatal error during " + phase + "!");
105 }
106 else
107 {
108 throw new NumberFormatException("Warning during " + phase + "!");
109 }
110 }
111 }
112
113 public class TestView extends TextInput
114 {
115 private boolean parsed = false;
116 private boolean rendered = false;
117
118 private boolean failDuringParse = false;
119 private boolean failDuringRender = false;
120 private boolean fatalError = false;
121
122 protected void throwError(String phase)
123 {
124 if(fatalError)
125 {
126 throw new SecurityException("Fatal error during " + phase + "!");
127 }
128 else
129 {
130 throw new NumberFormatException("Warning during " + phase + "!");
131 }
132 }
133
134 public void setFailDuringParse(boolean failDuringParse)
135 {
136 this.failDuringParse = failDuringParse;
137 }
138
139 public void setFailDuringRender(boolean failDuringRender)
140 {
141 this.failDuringRender = failDuringRender;
142 }
143
144 public void setFatalError(boolean fatalError)
145 {
146 this.fatalError = fatalError;
147 }
148
149 public void parse(RenderContext context)
150 {
151 if(failDuringParse) throwError("parse");
152
153 super.parse(context);
154 parsed = true;
155 }
156
157 public boolean isParsed()
158 {
159 return parsed;
160 }
161
162 public void render(RenderContext context) throws IOException
163 {
164 if(failDuringRender) throwError("render");
165
166 super.render(context);
167 rendered = true;
168 }
169
170 public boolean isRendered()
171 {
172 return rendered;
173 }
174
175 public void setRendered(boolean rendered)
176 {
177 this.rendered = rendered;
178 }
179 }
180
181 public void setUp() throws Exception, ServletException
182 {
183 super.setUp();
184 parsePipeline = new MockPipeline();
185 parsePipeline.addValve(new ErrorValve());
186 parsePipeline.addValve(new ParseValve());
187
188 eventPipeline = new MockPipeline();
189 eventPipeline.addValve(new ErrorValve());
190 eventPipeline.addValve(new EventValve());
191
192 //Pipeline renderPipeline = createRenderPipeline(testController);
193
194 context = new MockRenderContext();
195 context.ensureApplicationInit(MockApplication.class);
196 application = context.getApplication();
197
198 testController = new TestController(application);
199 application.registerComponent("current", testController);
200 testView = (TestView) testController.getView();
201 testView.getId();
202
203 application.init();
204 }
205
206 private Pipeline createRenderPipeline(ViewFactory component)
207 {
208 Pipeline renderPipeline = new DefaultPipeline();
209 renderPipeline.addValve(new ErrorValve());
210 renderPipeline.addValve(new SingleViewFactoryRenderValve(component));
211 return renderPipeline;
212 }
213
214 /***
215 * Tests the whole default processing. First tests each valve separately:
216 * <ol> {@link ParseValve}, checks parsing does <em>not</em> occur on first request.
217 * <ol> {@link EventValve}, checks events does <em>not</em> occur on first request.
218 * <ol> {@link RenderValve}, checks view rendered.
219 * <ol> {@link ParseValve}, checks parsing occured and model updated before events.
220 * <ol> {@link EventValve}, checks events was sent.
221 * After separate testing runs the processing as created by {@link Application}
222 * a couple of times checking the above things.
223 */
224 public void testProcessing() throws Exception
225 {
226 application.beforeRequest(context);
227
228 ValueModel model = testView.getModel();
229
230 // test parse valve
231 parsePipeline.execute(context);
232
233 assertTrue("test view was parsed before it was actually rendered", !testView.isParsed());
234
235 // send events
236 eventPipeline.execute(context);
237
238 assertTrue("change event was received before any change actually occured", !testController.isEventReceived());
239
240 // render valve (as a transition also prepares)
241 Pipeline renderPipeline = createRenderPipeline(testController);
242 renderPipeline.execute(context);
243
244 assertTrue("testView was not rendered", testView.isRendered());
245 testView.setRendered(false);
246
247 // this should reset all the controllers prepare-state
248 context.afterRequest();
249
250 assertTrue("model doesn't have correct value", firstModelValue.equals(model.getValue()));
251
252 // simulate we changed the value of the testView
253 prepareContextForChange(testView, false);
254
255 // parse again, this time components should have been registered and things will start happening
256 parsePipeline.execute(context);
257
258 assertTrue("test view was not parsed", testView.isParsed());
259
260 TestView view = testView;
261 ValueModel valueModel = view.getModel();
262 assertTrue("model have not been updated", nextModelValue.equals(valueModel.getValue()));
263 assertTrue("events was sent before models were updated (or at least before they were ordered to be sent)",
264 !testController.isEventReceived());
265
266 // 4b. send events
267 eventPipeline.execute(context);
268
269 assertTrue("change event was not received", testController.isEventReceived());
270 testController.setEventReceived(false);
271
272 valueModel.setValue(firstModelValue);
273 context.afterRequest();
274
275 // do the whole process in one pipeline
276
277 process();
278
279 for(int i = 0; i < 5; i++)
280 {
281 //Pipeline process = application.createRenderPipeline(testController);
282
283 prepareContextForChange(testView, false);
284
285 process();
286
287 assertTrue("testView was not rendered", testView.isRendered());
288 testView.setRendered(false);
289 assertTrue("change event was not sent", testController.isEventReceived());
290 testController.setEventReceived(false);
291
292 // clean up for next test method
293 context.afterRequest();
294 }
295 }
296
297 public void testAccessCurrent() throws Exception
298 {
299 Pipeline pipeline = new DefaultPipeline();
300 pipeline.addValve(new Valve() {
301 public void execute(ExecuteContext exec) throws Exception
302 {
303 assertSame("could not access application", application, Application.getApplication());
304 assertSame("could not access render context", context, RenderContext.getRenderContext());
305 }
306 });
307 pipeline.execute(context);
308 }
309
310 public void testError() throws Exception
311 {
312 final Exception exceptionToThrow = new Exception();
313 pipeline.addValve(new ErrorValve());
314 pipeline.addValve(new Valve()
315 {
316 public void execute(ExecuteContext exec) throws Exception
317 {
318 throw exceptionToThrow;
319 }
320 });
321 ExecuteContext executeContext = pipeline.createExecuteContext(context);
322 pipeline.execute(executeContext);
323 assertSame(exceptionToThrow, application.getError());
324 }
325
326 private void process() throws Exception
327 {
328 Pipeline process = application.createRenderPipeline(testController);
329 process.execute(context);
330 }
331
332 private void prepareContextForChange(View view, boolean register) throws Exception
333 {
334 parsePipeline.start(); // really just a QD to get uniqueId working...
335 context.setRequestParameter(view.uniqueId(context), nextModelValue);
336 if(register)
337 {
338 context.registerParsingComponent(view);
339 }
340 }
341 }
This page was automatically generated by Maven