KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Point JavaDoc;
18
19 import org.apache.hivemind.util.PropertyUtils;
20 import org.apache.tapestry.IAsset;
21 import org.apache.tapestry.IBinding;
22 import org.apache.tapestry.IForm;
23 import org.apache.tapestry.IMarkupWriter;
24 import org.apache.tapestry.IRequestCycle;
25 import org.apache.tapestry.test.Creator;
26 import org.apache.tapestry.valid.IValidationDelegate;
27 import org.easymock.MockControl;
28
29 /**
30  * Tests for {@link org.apache.tapestry.form.ImageSubmit}.
31  *
32  * @author Howard M. Lewis Ship
33  * @since 4.0
34  */

35 public class TestImageSubmit extends BaseFormComponentTest
36 {
37     protected IAsset newAsset(IRequestCycle cycle, String JavaDoc imageURL)
38     {
39         MockControl control = newControl(IAsset.class);
40         IAsset asset = (IAsset) control.getMock();
41
42         asset.buildURL(cycle);
43         control.setReturnValue(imageURL);
44
45         return asset;
46     }
47
48     public void testPrerendered()
49     {
50         Creator creator = new Creator();
51         ImageSubmit submit = (ImageSubmit) creator.newInstance(ImageSubmit.class);
52
53         MockControl formc = newControl(IForm.class);
54         IForm form = (IForm) formc.getMock();
55         MockControl cyclec = newControl(IRequestCycle.class);
56         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
57         IMarkupWriter writer = newWriter();
58
59         trainGetForm(cyclec, cycle, form);
60
61         trainWasPrerendered(formc, form, writer, submit, true);
62
63         replayControls();
64
65         submit.renderComponent(writer, cycle);
66
67         verifyControls();
68     }
69
70     public void testRender()
71     {
72         MockControl delegatec = newControl(IValidationDelegate.class);
73         IValidationDelegate delegate = (IValidationDelegate) delegatec.getMock();
74         MockControl formc = newControl(IForm.class);
75         IForm form = (IForm) formc.getMock();
76         MockControl cyclec = newControl(IRequestCycle.class);
77         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
78         IMarkupWriter writer = newWriter();
79         IAsset image = newAsset(cycle, "image-url");
80
81         Creator creator = new Creator();
82         ImageSubmit submit = (ImageSubmit) creator.newInstance(ImageSubmit.class, new Object JavaDoc[]
83         { "image", image });
84
85         trainGetForm(cyclec, cycle, form);
86
87         trainWasPrerendered(formc, form, writer, submit, false);
88
89         trainGetDelegate(formc, form, delegate);
90
91         delegate.setFormComponent(submit);
92         delegatec.setVoidCallable();
93
94         trainGetElementId(formc, form, submit, "fred");
95
96         trainIsRewinding(formc, form, false);
97
98         trainIsRewinding(cyclec, cycle, false);
99
100         writer.beginEmpty("input");
101         writer.attribute("type", "image");
102         writer.attribute("name", "fred");
103         writer.attribute("border", 0);
104         writer.attribute("src", "image-url");
105         writer.closeTag();
106
107         trainIsInError(delegatec, delegate, false);
108         
109         replayControls();
110
111         submit.renderComponent(writer, cycle);
112
113         verifyControls();
114     }
115
116     public void testRenderDisabled()
117     {
118         MockControl delegatec = newControl(IValidationDelegate.class);
119         IValidationDelegate delegate = (IValidationDelegate) delegatec.getMock();
120         MockControl formc = newControl(IForm.class);
121         IForm form = (IForm) formc.getMock();
122         MockControl cyclec = newControl(IRequestCycle.class);
123         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
124         IMarkupWriter writer = newWriter();
125         IAsset image = newAsset(cycle, "disabled-image-url");
126
127         Creator creator = new Creator();
128         ImageSubmit submit = (ImageSubmit) creator.newInstance(ImageSubmit.class, new Object JavaDoc[]
129         { "disabledImage", image, "disabled", Boolean.TRUE });
130
131         trainGetForm(cyclec, cycle, form);
132
133         trainWasPrerendered(formc, form, writer, submit, false);
134
135         form.getDelegate();
136         formc.setReturnValue(delegate);
137
138         delegate.setFormComponent(submit);
139
140         trainGetElementId(formc, form, submit, "fred");
141
142         trainIsRewinding(formc, form, false);
143
144         trainIsRewinding(cyclec, cycle, false);
145
146         writer.beginEmpty("input");
147         writer.attribute("type", "image");
148         writer.attribute("name", "fred");
149         writer.attribute("disabled", "disabled");
150         writer.attribute("border", 0);
151         writer.attribute("src", "disabled-image-url");
152         writer.closeTag();
153
154         trainIsInError(delegatec, delegate, false);
155
156         replayControls();
157
158         submit.renderComponent(writer, cycle);
159
160         verifyControls();
161     }
162
163     public void testRenderDisabledNoDisabledImage()
164     {
165         MockControl delegatec = newControl(IValidationDelegate.class);
166         IValidationDelegate delegate = (IValidationDelegate) delegatec.getMock();
167         MockControl formc = newControl(IForm.class);
168         IForm form = (IForm) formc.getMock();
169         MockControl cyclec = newControl(IRequestCycle.class);
170         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
171         IMarkupWriter writer = newWriter();
172         IAsset image = newAsset(cycle, "image-url");
173
174         Creator creator = new Creator();
175         ImageSubmit submit = (ImageSubmit) creator.newInstance(ImageSubmit.class, new Object JavaDoc[]
176         { "image", image, "disabled", Boolean.TRUE });
177
178         trainGetForm(cyclec, cycle, form);
179
180         trainWasPrerendered(formc, form, writer, submit, false);
181
182         form.getDelegate();
183         formc.setReturnValue(delegate);
184
185         delegate.setFormComponent(submit);
186
187         trainGetElementId(formc, form, submit, "fred");
188
189         trainIsRewinding(formc, form, false);
190
191         trainIsRewinding(cyclec, cycle, false);
192
193         writer.beginEmpty("input");
194         writer.attribute("type", "image");
195         writer.attribute("name", "fred");
196         writer.attribute("disabled", "disabled");
197         writer.attribute("border", 0);
198         writer.attribute("src", "image-url");
199         writer.closeTag();
200
201         trainIsInError(delegatec, delegate, false);
202         
203         replayControls();
204
205         submit.renderComponent(writer, cycle);
206
207         verifyControls();
208     }
209
210     public void testRenderWithNameOverride()
211     {
212         MockControl delegatec = newControl(IValidationDelegate.class);
213         IValidationDelegate delegate = (IValidationDelegate) delegatec.getMock();
214         MockControl formc = newControl(IForm.class);
215         IForm form = (IForm) formc.getMock();
216         MockControl cyclec = newControl(IRequestCycle.class);
217         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
218         IMarkupWriter writer = newWriter();
219         IAsset image = newAsset(cycle, "image-url");
220
221         Creator creator = new Creator();
222         ImageSubmit submit = (ImageSubmit) creator.newInstance(ImageSubmit.class, new Object JavaDoc[]
223         { "image", image, "nameOverride", "barney" });
224
225         trainGetForm(cyclec, cycle, form);
226
227         trainWasPrerendered(formc, form, writer, submit, false);
228
229         form.getDelegate();
230         formc.setReturnValue(delegate);
231
232         delegate.setFormComponent(submit);
233
234         form.getElementId(submit, "barney");
235         formc.setReturnValue("barney$0");
236
237         trainIsRewinding(formc, form, false);
238
239         trainIsRewinding(cyclec, cycle, false);
240         
241         writer.beginEmpty("input");
242         writer.attribute("type", "image");
243         writer.attribute("name", "barney$0");
244         writer.attribute("border", 0);
245         writer.attribute("src", "image-url");
246         writer.closeTag();
247
248         trainIsInError(delegatec, delegate, false);
249         
250         replayControls();
251
252         submit.renderComponent(writer, cycle);
253
254         verifyControls();
255     }
256
257     public void testRewindingDisabled()
258     {
259         Creator creator = new Creator();
260         ImageSubmit submit = (ImageSubmit) creator.newInstance(ImageSubmit.class, new Object JavaDoc[]
261         { "disabled", Boolean.TRUE });
262
263         IValidationDelegate delegate = newDelegate();
264         MockControl formc = newControl(IForm.class);
265         IForm form = (IForm) formc.getMock();
266         MockControl cyclec = newControl(IRequestCycle.class);
267         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
268         IMarkupWriter writer = newWriter();
269
270         trainGetForm(cyclec, cycle, form);
271
272         trainWasPrerendered(formc, form, writer, submit, false);
273
274         form.getDelegate();
275         formc.setReturnValue(delegate);
276
277         delegate.setFormComponent(submit);
278
279         trainGetElementId(formc, form, submit, "fred");
280
281         trainIsRewinding(formc, form, true);
282
283         replayControls();
284
285         submit.renderComponent(writer, cycle);
286
287         verifyControls();
288     }
289
290     public void testRewindNotTrigger()
291     {
292         Creator creator = new Creator();
293         ImageSubmit submit = (ImageSubmit) creator.newInstance(ImageSubmit.class);
294
295         IValidationDelegate delegate = newDelegate();
296         MockControl formc = newControl(IForm.class);
297         IForm form = (IForm) formc.getMock();
298         MockControl cyclec = newControl(IRequestCycle.class);
299         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
300         IMarkupWriter writer = newWriter();
301
302         trainGetForm(cyclec, cycle, form);
303
304         trainWasPrerendered(formc, form, writer, submit, false);
305
306         form.getDelegate();
307         formc.setReturnValue(delegate);
308
309         delegate.setFormComponent(submit);
310
311         trainGetElementId(formc, form, submit, "fred");
312
313         trainIsRewinding(formc, form, true);
314
315         trainGetParameter(cyclec, cycle, "fred.x", null);
316
317         replayControls();
318
319         submit.renderComponent(writer, cycle);
320
321         verifyControls();
322     }
323
324     public void testRewindTrigger()
325     {
326         Creator creator = new Creator();
327         ImageSubmit submit = (ImageSubmit) creator.newInstance(ImageSubmit.class, new Object JavaDoc[]
328         { "tag", "clicked" });
329
330         IBinding binding = newBinding();
331         submit.setBinding("selected", binding);
332
333         IValidationDelegate delegate = newDelegate();
334         MockControl formc = newControl(IForm.class);
335         IForm form = (IForm) formc.getMock();
336         MockControl cyclec = newControl(IRequestCycle.class);
337         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
338         IMarkupWriter writer = newWriter();
339
340         trainGetForm(cyclec, cycle, form);
341
342         trainWasPrerendered(formc, form, writer, submit, false);
343
344         form.getDelegate();
345         formc.setReturnValue(delegate);
346
347         delegate.setFormComponent(submit);
348
349         trainGetElementId(formc, form, submit, "fred");
350
351         trainIsRewinding(formc, form, true);
352
353         trainGetParameter(cyclec, cycle, "fred.x", "33");
354
355         replayControls();
356
357         submit.renderComponent(writer, cycle);
358
359         assertEquals("clicked", PropertyUtils.read(submit, "selected"));
360
361         // Note: we rely on the fact that ImageSubmit subclasses
362
// from Submit to test some of the extra logic about
363
// notifying listeners (deferred or not).
364
// This test "proves" that Submit.handleClick() is invoked.
365

366         verifyControls();
367     }
368
369     public void testRewindTriggeredWithPointBound()
370     {
371         // Note: because there isn't a real Form to set the name, we'll do it here.
372

373         Creator creator = new Creator();
374         ImageSubmit submit = (ImageSubmit) creator.newInstance(ImageSubmit.class, new Object JavaDoc[]
375         { "tag", "clicked", "name", "fred" });
376
377         IBinding binding = newBinding();
378         submit.setBinding("selected", binding);
379         submit.setBinding("point", binding);
380
381         IValidationDelegate delegate = newDelegate();
382         MockControl formc = newControl(IForm.class);
383         IForm form = (IForm) formc.getMock();
384         MockControl cyclec = newControl(IRequestCycle.class);
385         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
386         IMarkupWriter writer = newWriter();
387
388         trainGetForm(cyclec, cycle, form);
389
390         trainWasPrerendered(formc, form, writer, submit, false);
391
392         form.getDelegate();
393         formc.setReturnValue(delegate);
394
395         delegate.setFormComponent(submit);
396
397         trainGetElementId(formc, form, submit, "fred");
398
399         trainIsRewinding(formc, form, true);
400
401         trainGetParameter(cyclec, cycle, "fred.x", "33");
402         trainGetParameter(cyclec, cycle, "fred.x", "33");
403         trainGetParameter(cyclec, cycle, "fred.y", "19");
404
405         replayControls();
406
407         submit.renderComponent(writer, cycle);
408
409         assertEquals("clicked", PropertyUtils.read(submit, "selected"));
410         assertEquals(new Point JavaDoc(33, 19), PropertyUtils.read(submit, "point"));
411
412         verifyControls();
413     }
414 }
415
Popular Tags