1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.view.facelets.tag.jsf.core;
21
22 import org.apache.myfaces.view.facelets.tag.jsf.core.reset.ResetValuesBean;
23 import java.util.Date;
24 import java.util.Locale;
25 import java.util.Map;
26 import java.util.TimeZone;
27
28 import javax.faces.component.UICommand;
29 import javax.faces.component.UIComponent;
30 import javax.faces.component.UIData;
31 import javax.faces.component.UIForm;
32 import javax.faces.component.UIGraphic;
33 import javax.faces.component.UIInput;
34 import javax.faces.component.UIOutput;
35 import javax.faces.component.UIPanel;
36 import javax.faces.component.UIViewRoot;
37 import javax.faces.component.html.HtmlCommandButton;
38 import javax.faces.component.html.HtmlCommandLink;
39 import javax.faces.component.html.HtmlDataTable;
40 import javax.faces.component.html.HtmlForm;
41 import javax.faces.component.html.HtmlGraphicImage;
42 import javax.faces.component.html.HtmlInputText;
43 import javax.faces.component.html.HtmlOutputText;
44 import javax.faces.convert.DateTimeConverter;
45 import javax.faces.convert.NumberConverter;
46 import javax.faces.event.ActionEvent;
47 import javax.faces.event.ActionListener;
48 import javax.faces.validator.DoubleRangeValidator;
49 import javax.faces.validator.LengthValidator;
50 import javax.faces.validator.LongRangeValidator;
51 import javax.faces.validator.Validator;
52 import org.apache.myfaces.renderkit.html.HtmlButtonRenderer;
53
54 import org.apache.myfaces.renderkit.html.HtmlFormRenderer;
55 import org.apache.myfaces.renderkit.html.HtmlImageRenderer;
56 import org.apache.myfaces.renderkit.html.HtmlLinkRenderer;
57 import org.apache.myfaces.renderkit.html.HtmlTableRenderer;
58 import org.apache.myfaces.renderkit.html.HtmlTextRenderer;
59 import org.apache.myfaces.view.facelets.FaceletTestCase;
60 import org.junit.Assert;
61 import org.junit.Test;
62
63 public class CoreTestCase extends FaceletTestCase
64 {
65
66 @Override
67 protected void setupComponents() throws Exception
68 {
69 application.addComponent(UIViewRoot.COMPONENT_TYPE, UIViewRoot.class
70 .getName());
71 application.addComponent(UIPanel.COMPONENT_TYPE,
72 UIPanel.class.getName());
73 application.addComponent(HtmlCommandLink.COMPONENT_TYPE,
74 HtmlCommandLink.class.getName());
75 application.addComponent(HtmlCommandButton.COMPONENT_TYPE,
76 HtmlCommandButton.class.getName());
77 application.addComponent(HtmlGraphicImage.COMPONENT_TYPE,
78 HtmlGraphicImage.class.getName());
79 application.addComponent(HtmlForm.COMPONENT_TYPE, HtmlForm.class
80 .getName());
81 application.addComponent(HtmlOutputText.COMPONENT_TYPE,
82 HtmlOutputText.class.getName());
83 application.addComponent(HtmlInputText.COMPONENT_TYPE,
84 HtmlInputText.class.getName());
85 application.addComponent(HtmlDataTable.COMPONENT_TYPE,
86 HtmlDataTable.class.getName());
87 }
88
89 @Override
90 protected void setupConvertersAndValidators() throws Exception
91 {
92 application.addConverter(DateTimeConverter.CONVERTER_ID,
93 DateTimeConverter.class.getName());
94 application.addConverter(NumberConverter.CONVERTER_ID,
95 NumberConverter.class.getName());
96 application.addValidator(LengthValidator.VALIDATOR_ID,
97 LengthValidator.class.getName());
98 application.addValidator(DoubleRangeValidator.VALIDATOR_ID,
99 DoubleRangeValidator.class.getName());
100 application.addValidator(LongRangeValidator.VALIDATOR_ID,
101 LongRangeValidator.class.getName());
102 }
103
104 @Override
105 protected void setupRenderers() throws Exception
106 {
107 renderKit.addRenderer(UIOutput.COMPONENT_FAMILY, "javax.faces.Text",
108 new HtmlTextRenderer());
109 renderKit.addRenderer(UIInput.COMPONENT_FAMILY, "javax.faces.Text",
110 new HtmlTextRenderer());
111 renderKit.addRenderer(UIGraphic.COMPONENT_FAMILY, "javax.faces.Image",
112 new HtmlImageRenderer());
113 renderKit.addRenderer(UICommand.COMPONENT_FAMILY, "javax.faces.Link",
114 new HtmlLinkRenderer());
115 renderKit.addRenderer(UIForm.COMPONENT_FAMILY, "javax.faces.Form",
116 new HtmlFormRenderer());
117 renderKit.addRenderer(UIData.COMPONENT_FAMILY, "javax.faces.Table",
118 new HtmlTableRenderer());
119 renderKit.addRenderer(UICommand.COMPONENT_FAMILY, "javax.faces.Button",
120 new HtmlButtonRenderer());
121
122 }
123
124 @Test
125 public void testActionListenerHandler() throws Exception
126 {
127 ActionListener listener = new ActionListenerImpl();
128
129 facesContext.getExternalContext().getRequestMap().put("actionListener",
130 listener);
131
132 UIViewRoot root = facesContext.getViewRoot();
133 vdl.buildView(facesContext, root, "actionListener.xml");
134
135 UICommand action1 = (UICommand) root.findComponent("action1");
136 UICommand action2 = (UICommand) root.findComponent("action2");
137
138 Assert.assertNotNull("action1", action1);
139 Assert.assertNotNull("action2", action2);
140
141 Assert.assertEquals("action1 listeners", 1,
142 action1.getActionListeners().length);
143 Assert.assertEquals("action2 listeners", 2,
144 action2.getActionListeners().length);
145
146
147
148 }
149
150 @Test
151 public void testAttributeHandler() throws Exception
152 {
153 String title = "Dog in a Funny Hat";
154 facesContext.getExternalContext().getRequestMap().put("title", title);
155
156 UIViewRoot root = facesContext.getViewRoot();
157 vdl.buildView(facesContext, root, "attribute.xml");
158
159 HtmlGraphicImage graphic1 = (HtmlGraphicImage) root
160 .findComponent("graphic1");
161 HtmlGraphicImage graphic2 = (HtmlGraphicImage) root
162 .findComponent("graphic2");
163
164 Assert.assertNotNull("graphic1", graphic1);
165 Assert.assertNotNull("graphic2", graphic2);
166
167 Assert.assertEquals("graphic1 title", "literal", graphic1.getTitle());
168 Assert.assertEquals("graphic2 title", title, graphic2.getTitle());
169 }
170
171 @Test
172 public void testConvertDateTimeHandler() throws Exception
173 {
174 Date now = new Date(1000 * 360 * 60 * 24 * 7);
175 facesContext.getExternalContext().getRequestMap().put("now", now);
176 UIViewRoot root = facesContext.getViewRoot();
177 root.setLocale(Locale.US);
178 vdl.buildView(facesContext, root, "convertDateTime.xml");
179
180 UIOutput out1 = (UIOutput) root.findComponent("form:out1");
181 UIOutput out2 = (UIOutput) root.findComponent("form:out2");
182 UIOutput out3 = (UIOutput) root.findComponent("form:out3");
183 UIOutput out4 = (UIOutput) root.findComponent("form:out4");
184 UIOutput out5 = (UIOutput) root.findComponent("form:out5");
185 UIOutput out6 = (UIOutput) root.findComponent("form:out6");
186
187 Assert.assertNotNull("out1", out1);
188 Assert.assertNotNull("out2", out2);
189 Assert.assertNotNull("out3", out3);
190 Assert.assertNotNull("out4", out4);
191 Assert.assertNotNull("out5", out5);
192 Assert.assertNotNull("out6", out6);
193
194 Assert.assertNotNull("out1 converter", out1.getConverter());
195 Assert.assertNotNull("out2 converter", out2.getConverter());
196 Assert.assertNotNull("out3 converter", out3.getConverter());
197 Assert.assertNotNull("out4 converter", out4.getConverter());
198 Assert.assertNotNull("out5 converter", out5.getConverter());
199 DateTimeConverter converter6 = (DateTimeConverter) out6.getConverter();
200
201 Assert.assertEquals("out1 value", "12/24/69", out1.getConverter().getAsString(
202 facesContext, out1, now));
203 Assert.assertEquals("out2 value", "12/24/69 6:57:12 AM", out2.getConverter()
204 .getAsString(facesContext, out2, now));
205 Assert.assertEquals("out3 value", "Dec 24, 1969", out3.getConverter()
206 .getAsString(facesContext, out3, now));
207 Assert.assertEquals("out4 value", "6:57:12 AM", out4.getConverter()
208 .getAsString(facesContext, out4, now));
209 Assert.assertEquals("out5 value", "0:57 AM, CST", out5.getConverter()
210 .getAsString(facesContext, out5, now));
211 Assert.assertEquals("Timezone should be GMT", TimeZone.getTimeZone("GMT"),
212 converter6.getTimeZone());
213 }
214
215 @Test
216 public void testConvertDelegateHandler() throws Exception
217 {
218 UIViewRoot root = facesContext.getViewRoot();
219 root.setLocale(Locale.US);
220 vdl.buildView(facesContext, root, "converter.xml");
221
222 UIOutput out1 = (UIOutput) root.findComponent("out1");
223
224 Assert.assertNotNull("out1", out1);
225
226 Assert.assertNotNull("out1 converter", out1.getConverter());
227
228 Assert.assertEquals("out1 value", new Double(42.5), out1.getConverter()
229 .getAsObject(facesContext, out1, out1.getLocalValue().toString()));
230 }
231
232 @Test
233 public void testConvertNumberHandler() throws Exception
234 {
235 UIViewRoot root = facesContext.getViewRoot();
236 root.setLocale(Locale.US);
237 vdl.buildView(facesContext, root, "convertNumber.xml");
238
239 UIOutput out1 = (UIOutput) root.findComponent("out1");
240 UIOutput out2 = (UIOutput) root.findComponent("out2");
241 UIOutput out3 = (UIOutput) root.findComponent("out3");
242 UIOutput out4 = (UIOutput) root.findComponent("out4");
243 UIOutput out5 = (UIOutput) root.findComponent("out5");
244
245 Assert.assertNotNull("out1", out1);
246 Assert.assertNotNull("out2", out2);
247 Assert.assertNotNull("out3", out3);
248 Assert.assertNotNull("out4", out4);
249 Assert.assertNotNull("out5", out5);
250
251 Assert.assertNotNull("out1 converter", out1.getConverter());
252 Assert.assertNotNull("out2 converter", out2.getConverter());
253 Assert.assertNotNull("out3 converter", out3.getConverter());
254 Assert.assertNotNull("out4 converter", out4.getConverter());
255 Assert.assertNotNull("out5 converter", out5.getConverter());
256
257 Assert.assertEquals("out1 value", "12", out1.getConverter().getAsString(facesContext,
258 out1, new Double(12.001)));
259 Assert.assertEquals("out2 value", "$12.00", out2.getConverter().getAsString(
260 facesContext, out2, new Double(12.00)));
261 Assert.assertEquals("out3 value", "00,032", out3.getConverter().getAsString(
262 facesContext, out3, new Double(32)));
263 Assert.assertEquals("out4 value", "0.67", out4.getConverter().getAsString(
264 facesContext, out4, new Double(2.0 / 3.0)));
265 Assert.assertEquals("out5 value", "67%", out5.getConverter().getAsString(
266 facesContext, out5, new Double(0.67)));
267 }
268
269 @Test
270 public void testFacetHandler() throws Exception
271 {
272 UIViewRoot root = facesContext.getViewRoot();
273 vdl.buildView(facesContext, root, "facet.xml");
274
275 UIData data = (UIData) root.findComponent("table");
276
277 Assert.assertNotNull("data", data);
278
279 UIComponent footer = data.getFooter();
280
281 Assert.assertNotNull("footer", footer);
282 }
283
284 @Test
285 public void testLoadBundleHandler() throws Exception
286 {
287 UIViewRoot root = facesContext.getViewRoot();
288 vdl.buildView(facesContext, root, "loadBundle.xml");
289
290 Object value = facesContext.getExternalContext().getRequestMap().get("foo");
291
292 Assert.assertNotNull("bundle loaded into request", value);
293 Assert.assertTrue(value instanceof Map);
294 String result = (String) ((Map) value).get("some.not.found.key");
295 Assert.assertTrue(result.contains("???"));
296 }
297
298 @Test
299 public void testValidateDelegateHandler() throws Exception
300 {
301 UIViewRoot root = facesContext.getViewRoot();
302 vdl.buildView(facesContext, root, "validator.xml");
303
304 UIInput input = (UIInput) root.findComponent("form:input");
305
306 Assert.assertNotNull("input", input);
307
308 Assert.assertEquals("input validator", 1, input.getValidators().length);
309
310 Validator v = input.getValidators()[0];
311
312 v.validate(facesContext, input, "4333");
313 }
314
315 @Test
316 public void testValidateDoubleRangeHandler() throws Exception
317 {
318 UIViewRoot root = facesContext.getViewRoot();
319 vdl.buildView(facesContext, root, "validateDoubleRange.xml");
320
321 UIInput input = (UIInput) root.findComponent("form:input");
322
323 Assert.assertNotNull("input", input);
324
325 Assert.assertEquals("input validator", 1, input.getValidators().length);
326
327 Validator v = input.getValidators()[0];
328
329 v.validate(facesContext, input, new Double(1.8));
330 }
331
332 @Test
333 public void testValidateLengthHandler() throws Exception
334 {
335 UIViewRoot root = facesContext.getViewRoot();
336 vdl.buildView(facesContext, root, "validateLength.xml");
337
338 UIInput input = (UIInput) root.findComponent("form:input");
339
340 Assert.assertNotNull("input", input);
341
342 Assert.assertEquals("input validator", 1, input.getValidators().length);
343
344 Validator v = input.getValidators()[0];
345
346 v.validate(facesContext, input, "beans");
347 }
348
349 @Test
350 public void testValidateLongRangeHandler() throws Exception
351 {
352 UIViewRoot root = facesContext.getViewRoot();
353 vdl.buildView(facesContext, root, "validateLongRange.xml");
354
355 UIInput input = (UIInput) root.findComponent("form:input");
356
357 Assert.assertNotNull("input", input);
358
359 Assert.assertEquals("input validator", 1, input.getValidators().length);
360
361 Validator v = input.getValidators()[0];
362
363 v.validate(facesContext, input, new Long(2000));
364 }
365
366 @Test
367 public void testValueChangeListenerHandler() throws Exception
368 {
369 UIViewRoot root = facesContext.getViewRoot();
370 vdl.buildView(facesContext, root, "valueChangeListener.xml");
371
372 UIInput input = (UIInput) root.findComponent("form:input");
373
374 Assert.assertNotNull("input", input);
375
376 Assert.assertEquals("input listener", 1,
377 input.getValueChangeListeners().length);
378 }
379
380 @Test
381 public void testViewHandler() throws Exception
382 {
383 UIViewRoot root = facesContext.getViewRoot();
384 vdl.buildView(facesContext, root, "view.xml");
385
386 Assert.assertEquals("german locale", Locale.GERMAN, root.getLocale());
387 }
388
389 @Test
390 public void testResetValuesActionListenerHandler() throws Exception
391 {
392 ResetValuesBean bean = new ResetValuesBean();
393
394 facesContext.getExternalContext().getRequestMap().put("bean", bean);
395 bean.setField1("Hello");
396 bean.setField2(2);
397
398 UIViewRoot root = facesContext.getViewRoot();
399 vdl.buildView(facesContext, root, "resetValuesActionListener_1.xhtml");
400
401 UICommand action1 = (UICommand) root.findComponent("mainForm:submit");
402
403 Assert.assertNotNull("mainForm:submit", action1);
404
405 Assert.assertEquals("mainForm:submit listeners", 1,
406 action1.getActionListeners().length);
407
408 UIInput field1 = (UIInput) root.findComponent("mainForm:field1");
409 field1.setValue("xxx");
410 Assert.assertEquals("xxx", field1.getValue());
411 Assert.assertTrue(field1.isLocalValueSet());
412
413 UIInput field2 = (UIInput) root.findComponent("mainForm:field2");
414 field2.setSubmittedValue("1");
415 field2.setValid(false);
416
417 action1.getActionListeners()[0].processAction(new ActionEvent(action1));
418
419
420 Assert.assertEquals("Hello",field1.getValue());
421 Assert.assertFalse(field1.isLocalValueSet());
422 Assert.assertNull(field2.getSubmittedValue());
423 Assert.assertTrue(field2.isValid());
424 }
425
426
427 }