KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright 2004, 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 java.text.DateFormat JavaDoc;
18 import java.util.Calendar JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.GregorianCalendar JavaDoc;
21 import java.util.Locale JavaDoc;
22
23 import org.apache.tapestry.form.IFormComponent;
24 import org.apache.tapestry.valid.DateValidator;
25 import org.apache.tapestry.valid.ValidationConstraint;
26 import org.apache.tapestry.valid.ValidatorException;
27
28 /**
29  * Tests the {@link DateValidator}class.
30  *
31  * @author Howard Lewis Ship
32  * @since 1.0.8
33  */

34
35 public class TestDateValidator extends BaseValidatorTestCase
36 {
37     private Calendar JavaDoc calendar = new GregorianCalendar JavaDoc();
38
39     private DateValidator v = new DateValidator();
40
41     private Date JavaDoc buildDate(int month, int day, int year)
42     {
43         calendar.clear();
44
45         calendar.set(Calendar.MONTH, month);
46         calendar.set(Calendar.DAY_OF_MONTH, day);
47         calendar.set(Calendar.YEAR, year);
48
49         return calendar.getTime();
50     }
51
52     public void testToStringNull()
53     {
54         String JavaDoc out = v.toString(null, null);
55
56         assertNull(out);
57     }
58
59     public void testToStringValid()
60     {
61         String JavaDoc out = v.toString(null, buildDate(Calendar.DECEMBER, 8, 2001));
62
63         assertEquals("Result.", "12/08/2001", out);
64     }
65
66     public void testToStringFormat()
67     {
68         if (IS_JDK13)
69             return;
70
71         DateFormat JavaDoc format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
72
73         v.setFormat(format);
74
75         String JavaDoc out = v.toString(null, buildDate(Calendar.DECEMBER, 8, 2001));
76
77         assertEquals("Result.", "08.12.01", out);
78     }
79
80     public void testToObjectNull() throws ValidatorException
81     {
82         IFormComponent field = newField();
83
84         replayControls();
85
86         Object JavaDoc out = v.toObject(field, null);
87
88         assertNull(out);
89
90         verifyControls();
91     }
92
93     public void testToObjectEmpty() throws ValidatorException
94     {
95         IFormComponent field = newField();
96
97         replayControls();
98
99         Object JavaDoc out = v.toObject(field, "");
100
101         assertNull(out);
102
103         verifyControls();
104     }
105
106     public void testToObjectInvalid()
107     {
108         IFormComponent field = newField("badDatesIndy");
109
110         replayControls();
111
112         try
113         {
114             v.toObject(field, "frankenhooker");
115
116             unreachable();
117         }
118         catch (ValidatorException ex)
119         {
120             assertEquals("Invalid date format for badDatesIndy. Format is MM/dd/yyyy.", ex
121                     .getMessage());
122             assertEquals(ValidationConstraint.DATE_FORMAT, ex.getConstraint());
123         }
124
125         verifyControls();
126     }
127
128     public void testOverrideInvalidDateFormatMessage()
129     {
130
131         IFormComponent field = newField("badDatesIndy");
132
133         replayControls();
134
135         v.setInvalidDateFormatMessage("Enter a valid date for {0}.");
136
137         try
138         {
139             v.toObject(field, "frankenhooker");
140
141             unreachable();
142         }
143         catch (ValidatorException ex)
144         {
145             assertEquals("Enter a valid date for badDatesIndy.", ex.getMessage());
146
147         }
148
149         verifyControls();
150     }
151
152     public void testToObjectFormat() throws ValidatorException
153     {
154         if (IS_JDK13)
155             return;
156
157         DateFormat JavaDoc format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
158
159         v.setFormat(format);
160
161         // Again, adjust for missing German localization in JDK 1.3
162

163         Object JavaDoc out = v.toObject(null, "08.12.01");
164
165         assertEquals("Result.", buildDate(Calendar.DECEMBER, 8, 2001), out);
166     }
167
168     public void testToObjectMinimum()
169     {
170         IFormComponent field = newField("toObjectMinimum", Locale.ENGLISH);
171
172         replayControls();
173
174         v.setMinimum(buildDate(Calendar.DECEMBER, 24, 2001));
175
176         try
177         {
178             v.toObject(field, "12/8/2001");
179             unreachable();
180         }
181         catch (ValidatorException ex)
182         {
183             assertEquals(ValidationConstraint.TOO_SMALL, ex.getConstraint());
184         }
185
186         verifyControls();
187     }
188
189     public void testOverrideDateTooEarlyMessage()
190     {
191         IFormComponent field = newField("inputDate", Locale.ENGLISH);
192
193         replayControls();
194
195         v.setMinimum(buildDate(Calendar.DECEMBER, 24, 2001));
196         v.setDateTooEarlyMessage("Provide a date for {0} after Dec 24 2001.");
197
198         try
199         {
200             v.toObject(field, "12/8/2001");
201             unreachable();
202         }
203         catch (ValidatorException ex)
204         {
205             assertEquals("Provide a date for inputDate after Dec 24 2001.", ex.getMessage());
206             assertEquals(ValidationConstraint.TOO_SMALL, ex.getConstraint());
207         }
208
209         verifyControls();
210     }
211
212     public void testToObjectMinimumNull() throws ValidatorException
213     {
214         v.setMinimum(buildDate(Calendar.DECEMBER, 24, 2001));
215
216         Object JavaDoc out = v.toObject(null, null);
217
218         assertNull(out);
219     }
220
221     public void testToObjectMaximum()
222     {
223         IFormComponent field = newField("toObjectMaximum");
224
225         replayControls();
226
227         v.setMaximum(buildDate(Calendar.DECEMBER, 24, 2001));
228
229         try
230         {
231             v.toObject(field, "12/8/2002");
232             unreachable();
233         }
234         catch (ValidatorException ex)
235         {
236             assertEquals("toObjectMaximum must be on or before 12/24/2001.", ex.getMessage());
237             assertEquals(ValidationConstraint.TOO_LARGE, ex.getConstraint());
238         }
239
240         verifyControls();
241     }
242
243     public void testOverrideDateTooLateMessage()
244     {
245         IFormComponent field = newField("toObjectMaximum");
246
247         replayControls();
248
249         v.setMaximum(buildDate(Calendar.DECEMBER, 24, 2001));
250         v.setDateTooLateMessage("Try again with a date before Dec 24 2001 in {0}.");
251
252         try
253         {
254             v.toObject(field, "12/8/2002");
255             unreachable();
256         }
257         catch (ValidatorException ex)
258         {
259             assertEquals("Try again with a date before Dec 24 2001 in toObjectMaximum.", ex
260                     .getMessage());
261         }
262
263         verifyControls();
264     }
265
266     public void testToObjectMaximumNull() throws ValidatorException
267     {
268         v.setMaximum(buildDate(Calendar.DECEMBER, 24, 2001));
269
270         Object JavaDoc out = v.toObject(null, null);
271
272         assertNull(out);
273     }
274 }
Popular Tags