001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.valid;
016    
017    import java.text.DateFormat;
018    import java.util.Calendar;
019    import java.util.Date;
020    import java.util.GregorianCalendar;
021    import java.util.Locale;
022    
023    import org.apache.tapestry.form.IFormComponent;
024    import org.testng.annotations.AfterMethod;
025    import org.testng.annotations.Test;
026    
027    /**
028     * Tests the {@link DateValidator}class.
029     * 
030     * @author Howard Lewis Ship
031     * @since 1.0.8
032     */
033    @Test(sequential=true)
034    public class TestDateValidator extends BaseValidatorTestCase
035    {
036        private Calendar calendar = new GregorianCalendar();
037    
038        private DateValidator v = new DateValidator();
039    
040        @AfterMethod
041        public void reset()
042        {
043            v.setRequired(false);
044            v.setRequiredMessage(null);
045            v.setDateTooEarlyMessage(null);
046            v.setDateTooLateMessage(null);
047            v.setDisplayFormat(DateValidator.DEFAULT_DISPLAY_FORMAT);
048            v.setInvalidDateFormatMessage(null);
049            v.setMaximum(null);
050            v.setMinimum(null);
051            v.setFormat(DateValidator.DEFAULT_DATE_FORMAT);
052        }
053        
054        private Date buildDate(int month, int day, int year)
055        {
056            calendar.clear();
057    
058            calendar.set(Calendar.MONTH, month);
059            calendar.set(Calendar.DAY_OF_MONTH, day);
060            calendar.set(Calendar.YEAR, year);
061    
062            return calendar.getTime();
063        }
064    
065        public void testToStringNull()
066        {
067            String out = v.toString(null, null);
068    
069            assertNull(out);
070        }
071    
072        public void testToStringValid()
073        {
074            String out = v.toString(null, buildDate(Calendar.DECEMBER, 8, 2001));
075    
076            assertEquals("12/08/2001", out);
077        }
078    
079        public void testToStringFormat()
080        {
081            if (IS_JDK13)
082                return;
083    
084            DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
085    
086            v.setFormat(format);
087    
088            String out = v.toString(null, buildDate(Calendar.DECEMBER, 8, 2001));
089    
090            assertEquals("08.12.01", out);
091        }
092    
093        public void testToObjectNull() throws ValidatorException
094        {
095            IFormComponent field = newField();
096    
097            replay();
098    
099            Object out = v.toObject(field, null);
100    
101            assertNull(out);
102    
103            verify();
104        }
105    
106        public void testToObjectEmpty() throws ValidatorException
107        {
108            IFormComponent field = newField();
109    
110            replay();
111    
112            Object out = v.toObject(field, "");
113    
114            assertNull(out);
115    
116            verify();
117        }
118    
119        public void testToObjectInvalid()
120        {
121            IFormComponent field = newField("badDatesIndy");
122    
123            replay();
124    
125            try
126            {
127                v.toObject(field, "frankenhooker");
128    
129                unreachable();
130            }
131            catch (ValidatorException ex)
132            {
133                assertEquals("Invalid date format for badDatesIndy.  Format is MM/DD/YYYY.".toLowerCase(), ex
134                        .getMessage().toLowerCase());
135                assertEquals(ValidationConstraint.DATE_FORMAT, ex.getConstraint());
136            }
137    
138            verify();
139        }
140    
141        public void testOverrideInvalidDateFormatMessage()
142        {
143    
144            IFormComponent field = newField("badDatesIndy");
145    
146            replay();
147    
148            v.setInvalidDateFormatMessage("Enter a valid date for {0}.");
149    
150            try
151            {
152                v.toObject(field, "frankenhooker");
153    
154                unreachable();
155            }
156            catch (ValidatorException ex)
157            {
158                assertEquals("Enter a valid date for badDatesIndy.", ex.getMessage());
159    
160            }
161    
162            verify();
163        }
164    
165        public void testToObjectFormat() throws ValidatorException
166        {
167            if (IS_JDK13)
168                return;
169    
170            DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
171    
172            v.setFormat(format);
173    
174            // Again, adjust for missing German localization in JDK 1.3
175    
176            Object out = v.toObject(null, "08.12.01");
177    
178            assertEquals(buildDate(Calendar.DECEMBER, 8, 2001), out, "Result.");
179        }
180    
181        public void testToObjectMinimum()
182        {
183            IFormComponent field = newField("toObjectMinimum", Locale.ENGLISH);
184    
185            replay();
186    
187            v.setMinimum(buildDate(Calendar.DECEMBER, 24, 2001));
188    
189            try
190            {
191                v.toObject(field, "12/8/2001");
192                unreachable();
193            }
194            catch (ValidatorException ex)
195            {
196                assertEquals(ValidationConstraint.TOO_SMALL, ex.getConstraint());
197            }
198    
199            verify();
200        }
201    
202        public void testOverrideDateTooEarlyMessage()
203        {
204            IFormComponent field = newField("inputDate", Locale.ENGLISH);
205    
206            replay();
207    
208            v.setMinimum(buildDate(Calendar.DECEMBER, 24, 2001));
209            v.setDateTooEarlyMessage("Provide a date for {0} after Dec 24 2001.");
210    
211            try
212            {
213                v.toObject(field, "12/8/2001");
214                unreachable();
215            }
216            catch (ValidatorException ex)
217            {
218                assertEquals("Provide a date for inputDate after Dec 24 2001.", ex.getMessage());
219                assertEquals(ValidationConstraint.TOO_SMALL, ex.getConstraint());
220            }
221    
222            verify();
223        }
224    
225        public void testToObjectMinimumNull() throws ValidatorException
226        {
227            v.setMinimum(buildDate(Calendar.DECEMBER, 24, 2001));
228    
229            Object out = v.toObject(null, null);
230    
231            assertNull(out);
232        }
233    
234        public void testToObjectMaximum()
235        {
236            IFormComponent field = newField("toObjectMaximum");
237    
238            replay();
239    
240            v.setMaximum(buildDate(Calendar.DECEMBER, 24, 2001));
241    
242            try
243            {
244                v.toObject(field, "12/8/2002");
245                unreachable();
246            }
247            catch (ValidatorException ex)
248            {
249                assertEquals("toObjectMaximum must be on or before 12/24/2001.", ex.getMessage());
250                assertEquals(ValidationConstraint.TOO_LARGE, ex.getConstraint());
251            }
252    
253            verify();
254        }
255    
256        public void testOverrideDateTooLateMessage()
257        {
258            IFormComponent field = newField("toObjectMaximum");
259    
260            replay();
261    
262            v.setMaximum(buildDate(Calendar.DECEMBER, 24, 2001));
263            v.setDateTooLateMessage("Try again with a date before Dec 24 2001 in {0}.");
264    
265            try
266            {
267                v.toObject(field, "12/8/2002");
268                unreachable();
269            }
270            catch (ValidatorException ex)
271            {
272                assertEquals("Try again with a date before Dec 24 2001 in toObjectMaximum.", ex
273                        .getMessage());
274            }
275    
276            verify();
277        }
278    
279        public void testToObjectMaximumNull() throws ValidatorException
280        {
281            v.setMaximum(buildDate(Calendar.DECEMBER, 24, 2001));
282    
283            Object out = v.toObject(null, null);
284    
285            assertNull(out);
286        }
287    }