View Javadoc

1   package org.apache.myfaces.custom.date;
2   
3   import java.text.ParseException;
4   import java.text.SimpleDateFormat;
5   import java.util.Calendar;
6   import java.util.TimeZone;
7   import java.util.Date;
8   import java.util.Locale;
9   
10  import org.apache.myfaces.custom.date.AbstractHtmlInputDate.UserData;
11  
12  import junit.framework.TestCase;
13  
14  public class UserDataTest extends TestCase {
15      
16      private UserData userData;
17      
18      private String getDefaultTimeZoneId() {
19          return TimeZone.getDefault().getID();
20      }
21      
22      public void testConstructorDoesNotSetDateVariablesWhenTheDateIsNull() {
23          userData = new UserData(null, Locale.getDefault(), getDefaultTimeZoneId() , false, "date");
24          assertNull(userData.getYear());
25          assertNull(userData.getDay());
26          assertNull(userData.getHours());
27      }
28      
29      public void testParseReturnsNullWhenTypeIsDateAndComponentNotUsed() {
30          userData = new UserData(null, Locale.getDefault(), getDefaultTimeZoneId(), false, "date");
31          userData.setDay("");
32          userData.setMonth("-1");
33          userData.setYear("");
34          try {
35              Date date = userData.parse();
36              assertNull(date);
37          } catch (ParseException e) {
38              fail();
39          }
40      }
41      
42      public void testParseThrowsParseExceptionWhenInvalidDataIsEnteredForTypeDate() {
43          userData = new UserData(null, Locale.getDefault(), getDefaultTimeZoneId(), false, "date");
44          userData.setDay("40");
45          userData.setMonth("-1");
46          userData.setYear("2005");
47          try {
48              userData.parse();
49              fail();
50          } catch (ParseException e) {
51          }
52      }
53      
54      public void testParseGivesCorrectDateWhenValidDataIsEnteredForTypeDate() {
55          userData = new UserData(null, Locale.getDefault(), getDefaultTimeZoneId(), false, "date");
56          userData.setDay("20");
57          userData.setMonth("7");
58          userData.setYear("2006");
59          
60          Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
61          calendar.set(Calendar.DAY_OF_MONTH, 20);
62          calendar.set(Calendar.MONTH, 6);
63          calendar.set(Calendar.YEAR, 2006);
64          
65          //only day, month and year is considered for equality
66          SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");        
67          try {
68              Date date1 = userData.parse();
69              Date date2 = calendar.getTime();
70              assertEquals(formatter.format(date1), formatter.format(date2));
71          } catch (ParseException e) {
72              e.printStackTrace();
73          }
74      }
75      
76      public void testParseReturnsNullWhenTypeIsTimeAndComponentNotUsed() {
77          userData = new UserData(null, Locale.getDefault(), getDefaultTimeZoneId(), false, "time");
78          userData.setHours("");
79          userData.setMinutes("");
80          userData.setSeconds("");
81          
82          try {
83              assertNull(userData.parse());
84          } catch (ParseException e) {
85              fail();
86          }
87      }
88      
89      public void testParseThrowsParseExceptionWhenInvalidDataIsEnteredForTypeTime() {
90          userData = new UserData(null, Locale.getDefault(), getDefaultTimeZoneId(), false, "time");
91          userData.setHours("25");
92          userData.setMinutes("");
93          userData.setSeconds("10");
94          
95          try {
96              userData.parse();
97              fail();
98          } catch (ParseException e) {
99          }
100     }
101     
102     public void testParseGivesCorrectDateWhenValidDataIsEnteredForTypeTime() {
103         userData = new UserData(null, Locale.getDefault(), getDefaultTimeZoneId(), false, "time");
104         userData.setHours("10");
105         userData.setMinutes("50");
106         userData.setSeconds("30");
107         
108         Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
109         calendar.set(Calendar.HOUR_OF_DAY, 10);
110         calendar.set(Calendar.MINUTE, 50);
111         calendar.set(Calendar.SECOND, 30);
112         
113         //hour, minute, seconds is considered for equality
114         SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");        
115         try {
116             Date date1 = userData.parse();
117             Date date2 = calendar.getTime();
118             assertEquals(formatter.format(date1), formatter.format(date2));
119         } catch (ParseException e) {
120             e.printStackTrace();
121         }
122     }
123     
124     public void testParseReturnsNullWhenTypeIsFullAndComponentNotUsed() {
125         userData = new UserData(null, Locale.ENGLISH, "GMT+2:00", true, "full");
126         userData.setDay("");
127         userData.setMonth("-1");
128         userData.setYear("");
129         userData.setHours("");
130         userData.setMinutes("");
131         userData.setSeconds("");
132         userData.setAmpm("-1");
133         
134         try {
135             assertNull(userData.parse());
136         } catch (ParseException e) {
137             fail();
138         }
139     }
140     
141     public void testParseThrowsParseExceptionWhenInvalidDataIsEnteredForTypeFullCase1() {
142         userData = new UserData(null, Locale.getDefault(), getDefaultTimeZoneId(), true, "full");
143         //date is used, time is not
144         userData.setDay("40");
145         userData.setMonth("-1");
146         userData.setYear("");
147         userData.setHours("");
148         userData.setMinutes("");
149         userData.setSeconds("");
150         userData.setAmpm("-1");
151         
152         try {
153             userData.parse();
154             fail();
155         } catch (ParseException e) {
156         }
157     }
158     
159     public void testParseThrowsParseExceptionWhenInvalidDataIsEnteredForTypeFullCase2() {
160         userData = new UserData(null, Locale.getDefault(), getDefaultTimeZoneId(), true, "full");
161         //date is used time is not
162         userData.setDay("");
163         userData.setMonth("0");
164         userData.setYear("2005");
165         userData.setHours("");
166         userData.setMinutes("");
167         userData.setSeconds("");
168         userData.setAmpm("-1");
169         
170         try {
171             userData.parse();
172             fail();
173         } catch (ParseException e) {
174         }
175     }
176     
177     public void testParseThrowsParseExceptionWhenInvalidDataIsEnteredForTypeFullCase3() {
178         userData = new UserData(null, Locale.getDefault(), getDefaultTimeZoneId(), true, "full");
179         //date is used time is not
180         userData.setDay("5");
181         userData.setMonth("10");
182         userData.setYear("2005");
183         userData.setHours("");
184         userData.setMinutes("");
185         userData.setSeconds("");
186         userData.setAmpm("-1");
187         
188         try {
189             userData.parse();
190             fail();
191         } catch (ParseException e) {
192         }
193     }
194     
195     public void testParseThrowsParseExceptionWhenInvalidDataIsEnteredForTypeFullCase4() {
196         userData = new UserData(null, Locale.getDefault(), getDefaultTimeZoneId(), true, "full");
197         //date is not used but time is
198         userData.setDay("");
199         userData.setMonth("-1");
200         userData.setYear("");
201         userData.setHours("");
202         userData.setMinutes("30");
203         userData.setSeconds("");
204         userData.setAmpm("-1");
205         
206         try {
207             userData.parse();
208             fail();
209         } catch (ParseException e) {
210         }
211     }
212     
213     
214 
215 }