KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > valid > TestFieldLabel


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.valid;
16
17 import org.apache.hivemind.Location;
18 import org.apache.tapestry.BindingException;
19 import org.apache.tapestry.IBinding;
20 import org.apache.tapestry.IForm;
21 import org.apache.tapestry.IMarkupWriter;
22 import org.apache.tapestry.IPage;
23 import org.apache.tapestry.IRequestCycle;
24 import org.apache.tapestry.form.BaseFormComponentTest;
25 import org.apache.tapestry.form.IFormComponent;
26 import org.easymock.MockControl;
27
28 /**
29  * Tests for the {@link org.apache.tapestry.valid.FieldLabel} component.
30  *
31  * @author Howard M. Lewis Ship
32  * @since 4.0
33  */

34 public class TestFieldLabel extends BaseFormComponentTest
35 {
36     private IForm newForm(IValidationDelegate delegate)
37     {
38         MockControl control = newControl(IForm.class);
39         IForm form = (IForm) control.getMock();
40
41         form.getDelegate();
42         control.setReturnValue(delegate);
43
44         return form;
45     }
46
47     private IPage newFred()
48     {
49         MockControl control = newControl(IPage.class);
50         IPage page = (IPage) control.getMock();
51
52         page.getPageName();
53         control.setReturnValue("Fred");
54
55         page.getIdPath();
56         control.setReturnValue(null);
57
58         return page;
59     }
60
61     private IFormComponent newField(String JavaDoc displayName, String JavaDoc clientId)
62     {
63         MockControl control = newControl(IFormComponent.class);
64         IFormComponent field = (IFormComponent) control.getMock();
65
66         field.getDisplayName();
67         control.setReturnValue(displayName);
68
69         field.getClientId();
70         control.setReturnValue(clientId);
71
72         return field;
73     }
74
75     public void testRewinding()
76     {
77         MockControl cyclec = newControl(IRequestCycle.class);
78         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
79
80         cycle.isRewinding();
81         cyclec.setReturnValue(true);
82
83         replayControls();
84
85         FieldLabel fl = (FieldLabel) newInstance(FieldLabel.class);
86
87         fl.render(null, cycle);
88
89         verifyControls();
90     }
91
92     public void testNoField()
93     {
94         IValidationDelegate delegate = new MockDelegate();
95         IForm form = newForm(delegate);
96         IMarkupWriter writer = newBufferWriter();
97         MockControl cyclec = newControl(IRequestCycle.class);
98         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
99
100         trainIsRewinding(cyclec, cycle, false);
101         trainGetForm(cyclec, cycle, form);
102
103         replayControls();
104
105         FieldLabel fl = (FieldLabel) newInstance(FieldLabel.class, new Object JavaDoc[]
106         { "displayName", "FredFlintstone" });
107
108         fl.render(writer, cycle);
109
110         assertBuffer("{LABEL-PREFIX}<label>FredFlintstone</label>{LABEL-SUFFIX}");
111
112         verifyControls();
113     }
114
115     public void testNoFieldRaw()
116     {
117         IValidationDelegate delegate = new MockDelegate();
118         IForm form = newForm(delegate);
119         IMarkupWriter writer = newBufferWriter();
120         MockControl cyclec = newControl(IRequestCycle.class);
121         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
122
123         trainIsRewinding(cyclec, cycle, false);
124         trainGetForm(cyclec, cycle, form);
125
126         replayControls();
127
128         FieldLabel fl = (FieldLabel) newInstance(FieldLabel.class, new Object JavaDoc[]
129         { "displayName", "<b>FredFlintstone</b>", "raw", Boolean.TRUE });
130
131         fl.render(writer, cycle);
132
133         assertBuffer("{LABEL-PREFIX}<label><b>FredFlintstone</b></label>{LABEL-SUFFIX}");
134
135         verifyControls();
136     }
137
138     public void testNoFieldOrDisplayName()
139     {
140         IForm form = newForm();
141         IMarkupWriter writer = newBufferWriter();
142         MockControl cyclec = newControl(IRequestCycle.class);
143         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
144
145         Location l = newLocation();
146         IBinding binding = newBinding(l);
147         IPage page = newFred();
148
149         trainIsRewinding(cyclec, cycle, false);
150         trainGetForm(cyclec, cycle, form);
151
152         replayControls();
153
154         FieldLabel fl = (FieldLabel) newInstance(FieldLabel.class, new Object JavaDoc[]
155         { "id", "label", "page", page, "container", page });
156
157         fl.setBinding("field", binding);
158
159         try
160         {
161             fl.render(writer, cycle);
162             unreachable();
163         }
164         catch (BindingException ex)
165         {
166             assertEquals(
167                     "Value for parameter 'field' in component Fred/label is null, and a non-null value is required.",
168                     ex.getMessage());
169             assertSame(l, ex.getLocation());
170             assertSame(binding, ex.getBinding());
171         }
172
173         verifyControls();
174     }
175
176     public void testDisplayNameFromField()
177     {
178         IValidationDelegate delegate = new MockDelegate();
179
180         MockControl formc = newControl(IForm.class);
181         IForm form = (IForm) formc.getMock();
182
183         IMarkupWriter writer = newBufferWriter();
184         IFormComponent field = newField("MyLabel", null);
185         Location l = newLocation();
186
187         MockControl cyclec = newControl(IRequestCycle.class);
188         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
189
190         trainIsRewinding(cyclec, cycle, false);
191         trainGetForm(cyclec, cycle, form);
192
193         FieldLabel fl = (FieldLabel) newInstance(FieldLabel.class, new Object JavaDoc[]
194         { "location", l, "field", field });
195
196         form.prerenderField(writer, field, l);
197
198         form.getDelegate();
199         formc.setReturnValue(delegate);
200
201         replayControls();
202
203         fl.render(writer, cycle);
204
205         assertBuffer("{LABEL-PREFIX}<label>MyLabel</label>{LABEL-SUFFIX}");
206
207         verifyControls();
208     }
209
210     public void testFieldWithClientId()
211     {
212         IValidationDelegate delegate = new MockDelegate();
213
214         MockControl formc = newControl(IForm.class);
215         IForm form = (IForm) formc.getMock();
216
217         IMarkupWriter writer = newBufferWriter();
218         IFormComponent field = newField("MyLabel", "clientId");
219         Location l = newLocation();
220
221         MockControl cyclec = newControl(IRequestCycle.class);
222         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
223
224         trainIsRewinding(cyclec, cycle, false);
225         trainGetForm(cyclec, cycle, form);
226
227         FieldLabel fl = (FieldLabel) newInstance(FieldLabel.class, new Object JavaDoc[]
228         { "location", l, "field", field });
229
230         form.prerenderField(writer, field, l);
231
232         form.getDelegate();
233         formc.setReturnValue(delegate);
234
235         replayControls();
236
237         fl.render(writer, cycle);
238
239         assertBuffer("{LABEL-PREFIX}<label for=\"clientId\">MyLabel</label>{LABEL-SUFFIX}");
240
241         verifyControls();
242     }
243
244     public void testNoDisplayNameInField()
245     {
246         MockControl formc = newControl(IForm.class);
247         IForm form = (IForm) formc.getMock();
248
249         IMarkupWriter writer = newBufferWriter();
250
251         MockControl fieldc = newControl(IFormComponent.class);
252         IFormComponent field = (IFormComponent) fieldc.getMock();
253
254         MockControl cyclec = newControl(IRequestCycle.class);
255         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
256
257         trainIsRewinding(cyclec, cycle, false);
258         trainGetForm(cyclec, cycle, form);
259
260         Location l = newLocation();
261         IPage page = newFred();
262
263         FieldLabel fl = (FieldLabel) newInstance(FieldLabel.class, new Object JavaDoc[]
264         { "id", "label", "location", l, "field", field, "page", page, "container", page });
265
266         form.prerenderField(writer, field, l);
267
268         field.getDisplayName();
269         fieldc.setReturnValue(null);
270
271         field.getExtendedId();
272         fieldc.setReturnValue("Fred/field");
273
274         replayControls();
275
276         try
277         {
278             fl.render(writer, cycle);
279             unreachable();
280         }
281         catch (BindingException ex)
282         {
283             assertEquals(
284                     "Display name for Fred/label was not specified and was not provided by field Fred/field.",
285                     ex.getMessage());
286         }
287
288         verifyControls();
289     }
290 }
Popular Tags