KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > TestSubmit


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.form;
16
17 import java.util.Collection JavaDoc;
18 import java.util.LinkedList JavaDoc;
19
20 import org.apache.hivemind.util.PropertyUtils;
21 import org.apache.tapestry.IActionListener;
22 import org.apache.tapestry.IBinding;
23 import org.apache.tapestry.IForm;
24 import org.apache.tapestry.IMarkupWriter;
25 import org.apache.tapestry.IRequestCycle;
26 import org.apache.tapestry.listener.ListenerInvokerTerminator;
27 import org.apache.tapestry.test.Creator;
28 import org.apache.tapestry.valid.IValidationDelegate;
29 import org.easymock.MockControl;
30
31 /**
32  * Tests for {@link org.apache.tapestry.form.Submit}.
33  *
34  * @author Howard M. Lewis Ship
35  * @since 4.0
36  */

37 public class TestSubmit extends BaseFormComponentTest
38 {
39     public void testPrerendered()
40     {
41         Creator creator = new Creator();
42         Submit submit = (Submit) creator.newInstance(Submit.class);
43
44         MockControl formc = newControl(IForm.class);
45         IForm form = (IForm) formc.getMock();
46         MockControl cyclec = newControl(IRequestCycle.class);
47         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
48         IMarkupWriter writer = newWriter();
49
50         trainGetForm(cyclec, cycle, form);
51
52         trainWasPrerendered(formc, form, writer, submit, true);
53
54         replayControls();
55
56         submit.renderComponent(writer, cycle);
57
58         verifyControls();
59     }
60
61     public void testRender()
62     {
63         Creator creator = new Creator();
64         Submit submit = (Submit) creator.newInstance(Submit.class);
65
66         MockControl delegatec = newControl(IValidationDelegate.class);
67         IValidationDelegate delegate = (IValidationDelegate) delegatec.getMock();
68         MockControl formc = newControl(IForm.class);
69         IForm form = (IForm) formc.getMock();
70         MockControl cyclec = newControl(IRequestCycle.class);
71         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
72         IMarkupWriter writer = newWriter();
73
74         trainGetForm(cyclec, cycle, form);
75
76         trainWasPrerendered(formc, form, writer, submit, false);
77
78         trainGetDelegate(formc, form, delegate);
79
80         delegate.setFormComponent(submit);
81         delegatec.setVoidCallable();
82
83         trainGetElementId(formc, form, submit, "fred");
84
85         trainIsRewinding(formc, form, false);
86
87         trainIsRewinding(cyclec, cycle, false);
88         
89         writer.beginEmpty("input");
90         writer.attribute("type", "submit");
91         writer.attribute("name", "fred");
92         writer.closeTag();
93
94         trainIsInError(delegatec, delegate, false);
95         
96         replayControls();
97
98         submit.renderComponent(writer, cycle);
99
100         verifyControls();
101     }
102
103     public void testRenderDisabled()
104     {
105         Creator creator = new Creator();
106         Submit submit = (Submit) creator.newInstance(Submit.class, new Object JavaDoc[]
107         { "disabled", Boolean.TRUE });
108
109         MockControl delegatec = newControl(IValidationDelegate.class);
110         IValidationDelegate delegate = (IValidationDelegate) delegatec.getMock();
111         MockControl formc = newControl(IForm.class);
112         IForm form = (IForm) formc.getMock();
113         MockControl cyclec = newControl(IRequestCycle.class);
114         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
115         IMarkupWriter writer = newWriter();
116
117         trainGetForm(cyclec, cycle, form);
118
119         trainWasPrerendered(formc, form, writer, submit, false);
120
121         trainGetDelegate(formc, form, delegate);
122
123         delegate.setFormComponent(submit);
124         delegatec.setVoidCallable();
125
126         trainGetElementId(formc, form, submit, "fred");
127
128         trainIsRewinding(formc, form, false);
129
130         trainIsRewinding(cyclec, cycle, false);
131         
132         writer.beginEmpty("input");
133         writer.attribute("type", "submit");
134         writer.attribute("name", "fred");
135         writer.attribute("disabled", "disabled");
136         writer.closeTag();
137
138         trainIsInError(delegatec, delegate, false);
139         
140         replayControls();
141
142         submit.renderComponent(writer, cycle);
143
144         verifyControls();
145     }
146
147     public void testRenderWithLabel()
148     {
149         Creator creator = new Creator();
150         Submit submit = (Submit) creator.newInstance(Submit.class, new Object JavaDoc[]
151         { "label", "flintstone" });
152
153         MockControl delegatec = newControl(IValidationDelegate.class);
154         IValidationDelegate delegate = (IValidationDelegate) delegatec.getMock();
155         MockControl formc = newControl(IForm.class);
156         IForm form = (IForm) formc.getMock();
157         MockControl cyclec = newControl(IRequestCycle.class);
158         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
159         IMarkupWriter writer = newWriter();
160
161         trainGetForm(cyclec, cycle, form);
162
163         trainWasPrerendered(formc, form, writer, submit, false);
164
165         trainGetDelegate(formc, form, delegate);
166
167         delegate.setFormComponent(submit);
168         delegatec.setVoidCallable();
169
170         trainGetElementId(formc, form, submit, "fred");
171
172         trainIsRewinding(formc, form, false);
173
174         trainIsRewinding(cyclec, cycle, false);
175         
176         writer.beginEmpty("input");
177         writer.attribute("type", "submit");
178         writer.attribute("name", "fred");
179         writer.attribute("value", "flintstone");
180         writer.closeTag();
181
182         trainIsInError(delegatec, delegate, false);
183         
184         replayControls();
185
186         submit.renderComponent(writer, cycle);
187
188         verifyControls();
189     }
190
191     public void testRewindingDisabled()
192     {
193         Creator creator = new Creator();
194         Submit submit = (Submit) creator.newInstance(Submit.class, new Object JavaDoc[]
195         { "disabled", Boolean.TRUE });
196
197         IValidationDelegate delegate = newDelegate();
198         MockControl formc = newControl(IForm.class);
199         IForm form = (IForm) formc.getMock();
200         MockControl cyclec = newControl(IRequestCycle.class);
201         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
202         IMarkupWriter writer = newWriter();
203
204         trainGetForm(cyclec, cycle, form);
205
206         trainWasPrerendered(formc, form, writer, submit, false);
207
208         form.getDelegate();
209         formc.setReturnValue(delegate);
210
211         delegate.setFormComponent(submit);
212
213         trainGetElementId(formc, form, submit, "fred");
214
215         trainIsRewinding(formc, form, true);
216
217         replayControls();
218
219         submit.renderComponent(writer, cycle);
220
221         verifyControls();
222     }
223
224     public void testRewindNotForm()
225     {
226         Creator creator = new Creator();
227         Submit submit = (Submit) creator.newInstance(Submit.class);
228
229         IValidationDelegate delegate = newDelegate();
230         MockControl formc = newControl(IForm.class);
231         IForm form = (IForm) formc.getMock();
232         MockControl cyclec = newControl(IRequestCycle.class);
233         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
234         IMarkupWriter writer = newWriter();
235
236         trainGetForm(cyclec, cycle, form);
237
238         trainWasPrerendered(formc, form, writer, submit, false);
239
240         form.getDelegate();
241         formc.setReturnValue(delegate);
242
243         delegate.setFormComponent(submit);
244
245         trainGetElementId(formc, form, submit, "fred");
246
247         trainIsRewinding(formc, form, false);
248         trainIsRewinding(cyclec, cycle, true);
249
250         replayControls();
251
252         submit.renderComponent(writer, cycle);
253
254         verifyControls();
255     }
256
257     public void testRewindNotTrigger()
258     {
259         Creator creator = new Creator();
260         Submit submit = (Submit) creator.newInstance(Submit.class);
261
262         IValidationDelegate delegate = newDelegate();
263         MockControl formc = newControl(IForm.class);
264         IForm form = (IForm) formc.getMock();
265         MockControl cyclec = newControl(IRequestCycle.class);
266         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
267         IMarkupWriter writer = newWriter();
268
269         trainGetForm(cyclec, cycle, form);
270
271         trainWasPrerendered(formc, form, writer, submit, false);
272
273         form.getDelegate();
274         formc.setReturnValue(delegate);
275
276         delegate.setFormComponent(submit);
277
278         trainGetElementId(formc, form, submit, "fred");
279
280         trainIsRewinding(formc, form, true);
281
282         trainGetParameter(cyclec, cycle, "fred", null);
283
284         replayControls();
285
286         submit.renderComponent(writer, cycle);
287
288         verifyControls();
289     }
290
291     public void testRewindTriggered()
292     {
293         Creator creator = new Creator();
294         Submit submit = (Submit) creator.newInstance(Submit.class, new Object JavaDoc[]
295         { "tag", "clicked" });
296
297         IBinding binding = newBinding();
298         submit.setBinding("selected", binding);
299
300         IValidationDelegate delegate = newDelegate();
301         MockControl formc = newControl(IForm.class);
302         IForm form = (IForm) formc.getMock();
303         MockControl cyclec = newControl(IRequestCycle.class);
304         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
305         IMarkupWriter writer = newWriter();
306
307         trainGetForm(cyclec, cycle, form);
308
309         trainWasPrerendered(formc, form, writer, submit, false);
310
311         form.getDelegate();
312         formc.setReturnValue(delegate);
313
314         delegate.setFormComponent(submit);
315
316         trainGetElementId(formc, form, submit, "fred");
317
318         trainIsRewinding(formc, form, true);
319
320         trainGetParameter(cyclec, cycle, "fred", "flintstone");
321
322         replayControls();
323
324         submit.renderComponent(writer, cycle);
325
326         assertEquals("clicked", PropertyUtils.read(submit, "selected"));
327
328         verifyControls();
329     }
330
331     public void testTriggerWithListener()
332     {
333         IActionListener listener = newListener();
334         IForm form = newForm();
335         IRequestCycle cycle = newCycle();
336
337         Creator creator = new Creator();
338         Submit submit = (Submit) creator.newInstance(Submit.class, new Object JavaDoc[]
339         { "listener", listener, "listenerInvoker", new ListenerInvokerTerminator() });
340
341         listener.actionTriggered(submit, cycle);
342
343         replayControls();
344
345         submit.handleClick(cycle, form);
346
347         verifyControls();
348     }
349
350     public void testTriggerWithDeferredListener()
351     {
352         IActionListener listener = newListener();
353         MockForm form = new MockForm();
354         IRequestCycle cycle = newCycle();
355
356         Creator creator = new Creator();
357         Submit submit = (Submit) creator.newInstance(Submit.class, new Object JavaDoc[]
358         { "listener", listener, "defer", Boolean.TRUE, "listenerInvoker",
359                 new ListenerInvokerTerminator() });
360
361         replayControls();
362
363         submit.handleClick(cycle, form);
364
365         verifyControls();
366
367         listener.actionTriggered(submit, cycle);
368
369         replayControls();
370
371         form.runDeferred();
372
373         verifyControls();
374     }
375
376     public void testTriggerWithDeferredListenerAndSingleParameter()
377     {
378         IActionListener listener = newListener();
379         MockForm form = new MockForm();
380         MockControl cycleControl = newControl(IRequestCycle.class);
381         IRequestCycle cycle = (IRequestCycle) cycleControl.getMock();
382
383         Object JavaDoc parameter = new Object JavaDoc();
384         Creator creator = new Creator();
385         Submit submit = (Submit) creator.newInstance(Submit.class, new Object JavaDoc[]
386         { "listener", listener, "defer", Boolean.TRUE, "parameters", parameter, "listenerInvoker",
387                 new ListenerInvokerTerminator() });
388
389         cycle.setListenerParameters(new Object JavaDoc[]
390         { parameter });
391         cycleControl.setMatcher(MockControl.ARRAY_MATCHER);
392
393         replayControls();
394
395         submit.handleClick(cycle, form);
396
397         verifyControls();
398
399         listener.actionTriggered(submit, cycle);
400
401         replayControls();
402
403         form.runDeferred();
404
405         verifyControls();
406     }
407
408     public void testTriggerWithDeferredListenerAndMultipleParameters()
409     {
410         IActionListener listener = newListener();
411         MockForm form = new MockForm();
412         MockControl cycleControl = newControl(IRequestCycle.class);
413         IRequestCycle cycle = (IRequestCycle) cycleControl.getMock();
414
415         Collection JavaDoc parameters = new LinkedList JavaDoc();
416         parameters.add("p1");
417         parameters.add("p2");
418
419         Creator creator = new Creator();
420         Submit submit = (Submit) creator.newInstance(Submit.class, new Object JavaDoc[]
421         { "listener", listener, "defer", Boolean.TRUE, "parameters", parameters, "listenerInvoker",
422                 new ListenerInvokerTerminator() });
423
424         cycle.setListenerParameters(new Object JavaDoc[]
425         { "p1", "p2" });
426         cycleControl.setMatcher(MockControl.ARRAY_MATCHER);
427
428         replayControls();
429
430         submit.handleClick(cycle, form);
431
432         verifyControls();
433
434         listener.actionTriggered(submit, cycle);
435
436         replayControls();
437
438         form.runDeferred();
439
440         verifyControls();
441     }
442 }
443
Popular Tags