1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package javax.faces.convert;
21
22 import org.apache.shale.test.base.AbstractJsfTestCase;
23
24 import javax.faces.component.UIInput;
25 import javax.faces.context.FacesContext;
26 import java.util.Date;
27 import java.util.Locale;
28 import java.util.TimeZone;
29 import java.text.SimpleDateFormat;
30
31 import junit.framework.Test;
32
33 public class DateTimeConverterTest extends AbstractJsfTestCase
34 {
35 private DateTimeConverter mock;
36
37 public static void main(String[] args)
38 {
39 junit.textui.TestRunner.run(DateTimeConverterTest.class);
40 }
41
42 public DateTimeConverterTest(String name)
43 {
44 super(name);
45 }
46
47 protected void setUp() throws Exception
48 {
49 super.setUp();
50
51 mock = new DateTimeConverter();
52 mock.setTimeZone(TimeZone.getDefault());
53 FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.GERMANY);
54
55 }
56
57 protected void tearDown() throws Exception
58 {
59 super.tearDown();
60
61 mock = null;
62 }
63
64
65
66
67 public void testGetAsObject()
68 {
69
70 UIInput input = new UIInput();
71
72 mock.setPattern("MM/dd/yyyy");
73
74
75 try
76 {
77 mock.getAsObject(FacesContext.getCurrentInstance(),input,"15/15/15");
78
79 assertTrue("this date should not be parsable - and it is, so this is wrong.",false);
80 }
81 catch (ConverterException e)
82 {
83
84 }
85
86
87 try
88 {
89 Date date = (Date) mock.getAsObject(FacesContext.getCurrentInstance(),input,"12/01/01");
90
91 SimpleDateFormat format = new SimpleDateFormat("MM/dd/yy");
92 format.setTimeZone(TimeZone.getDefault());
93
94 String str = format.format(date);
95
96 assertEquals("12/01/01",str);
97
98 format = new SimpleDateFormat("MM/dd/yyyy");
99 format.setTimeZone(TimeZone.getDefault());
100
101 str = format.format(date);
102
103 assertEquals("12/01/0001",str);
104 }
105 catch (ConverterException e)
106 {
107 assertTrue("this date should not be parsable - and it is, so this is wrong.",false);
108 }
109 }
110 }