View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.cli2.validation;
18  
19  import java.text.DateFormat;
20  import java.text.DateFormatSymbols;
21  import java.text.SimpleDateFormat;
22  import java.util.Arrays;
23  import java.util.Calendar;
24  import java.util.Date;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import junit.framework.Test;
29  import junit.framework.TestCase;
30  import junit.framework.TestSuite;
31  
32  import org.apache.commons.cli2.resource.ResourceConstants;
33  import org.apache.commons.cli2.resource.ResourceHelper;
34  
35  /**
36   * JUnit test case for DateValidator.
37   *
38   * @author Rob Oxspring
39   * @author John Keyes
40   */
41  public class DateValidatorTest
42      extends TestCase {
43      private static final ResourceHelper resources = ResourceHelper.getResourceHelper();
44      public static final DateFormat D_M_YY = new SimpleDateFormat("d/M/yy");
45      public static final DateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
46      private List formats = Arrays.asList(new Object[] { D_M_YY, YYYY_MM_DD });
47  
48      public void testSingleFormatValidate()
49          throws InvalidArgumentException {
50          final Object[] array = new Object[] { "23/12/03" };
51          final List list = Arrays.asList(array);
52          final Validator validator = new DateValidator(D_M_YY);
53  
54          validator.validate(list);
55  
56          final Iterator i = list.iterator();
57          assertEquals("2003-12-23", YYYY_MM_DD.format((Date) i.next()));
58          assertFalse(i.hasNext());
59      }
60  
61      public void testDefaultDateFormatValidate()
62          throws InvalidArgumentException {
63          DateFormatSymbols symbols =  new DateFormatSymbols();
64          final Object[] array = new Object[] { "23-" + symbols.getShortMonths()[11] + "-2003" };
65          final List list = Arrays.asList(array);
66          final Validator validator = new DateValidator( new SimpleDateFormat("dd-MMM-yyyy") );
67  
68          validator.validate(list);
69  
70          final Iterator i = list.iterator();
71          // CLI-40: For some reason, the YYYY_MM_DD object gets quite
72          // confused here and returns 2003-12-22. If we make a new one
73          // there is no problem.
74          assertEquals("2003-12-23", new SimpleDateFormat("yyyy-MM-dd").format((Date) i.next()));
75          assertFalse(i.hasNext());
76      }
77  
78      public void testDefaultTimeFormatValidate()
79          throws InvalidArgumentException {
80          final Object[] array = new Object[] { "18:00:00" };
81          final List list = Arrays.asList(array);
82          final Validator validator = new DateValidator( new SimpleDateFormat("HH:mm:ss") );
83  
84          validator.validate(list);
85  
86          final Iterator i = list.iterator();
87          final DateFormat df = new SimpleDateFormat("HH:mm:ss");
88          assertEquals("18:00:00", df.format((Date) i.next()));
89          assertFalse(i.hasNext());
90      }
91  
92      public void testDefaultDateTimeFormatValidate()
93          throws InvalidArgumentException {
94          DateFormatSymbols symbols =  new DateFormatSymbols();
95          final Object[] array = new Object[] { "23-" + symbols.getShortMonths()[0] + "-2003 18:00:00" };
96          final List list = Arrays.asList(array);
97          final Validator validator = new DateValidator( new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss") );
98  
99          validator.validate(list);
100 
101         final Iterator i = list.iterator();
102         final DateFormat df = new SimpleDateFormat("yyyy/M/dd HH:mm:ss");
103         assertEquals("2003/1/23 18:00:00", df.format((Date) i.next()));
104         assertFalse(i.hasNext());
105     }
106 
107     public void testDefaultValidator()
108         throws InvalidArgumentException {
109         final Object[] array = new Object[] { "23/01/03 18:00" };
110         final List list = Arrays.asList(array);
111         final Validator validator = new DateValidator(new SimpleDateFormat("dd/MM/yy HH:mm"));
112 
113         validator.validate(list);
114 
115         final Iterator i = list.iterator();
116         final DateFormat df = new SimpleDateFormat("yyyy/M/dd HH:mm:ss");
117         assertEquals("2003/1/23 18:00:00", df.format((Date) i.next()));
118         assertFalse(i.hasNext());
119     }
120 
121     public void testValidate()
122         throws InvalidArgumentException {
123         final Object[] array = new Object[] { "23/12/03", "2002-10-12" };
124         final List list = Arrays.asList(array);
125         final Validator validator = new DateValidator(formats);
126 
127         validator.validate(list);
128 
129         final Iterator i = list.iterator();
130         assertEquals("2003-12-23", YYYY_MM_DD.format((Date) i.next()));
131         assertEquals("2002-10-12", YYYY_MM_DD.format((Date) i.next()));
132         assertFalse(i.hasNext());
133     }
134 
135     public void testMinimumBounds()
136         throws InvalidArgumentException {
137         final DateValidator validator = new DateValidator(formats);
138         final Calendar cal = Calendar.getInstance();
139 
140         {
141             final Object[] array = new Object[] { "23/12/03", "2002-10-12" };
142             final List list = Arrays.asList(array);
143             cal.set(2002, 1, 12);
144 
145             final Date min = cal.getTime();
146             validator.setMinimum(min);
147             assertTrue("maximum bound is set", validator.getMaximum() == null);
148             assertEquals("minimum bound is incorrect", min, validator.getMinimum());
149             validator.validate(list);
150         }
151 
152         {
153             final Object[] array = new Object[] { "23/12/03", "2002-10-12" };
154             final List list = Arrays.asList(array);
155             cal.set(2003, 1, 12);
156 
157             final Date min = cal.getTime();
158             validator.setMinimum(min);
159 
160             try {
161                 validator.validate(list);
162                 fail("minimum out of bounds exception not caught");
163             } catch (final InvalidArgumentException exp) {
164                 assertEquals(resources.getMessage(ResourceConstants.DATEVALIDATOR_DATE_OUTOFRANGE,
165                                                   new Object[] { "2002-10-12" }), exp.getMessage());
166             }
167         }
168     }
169 
170     public void testFormats()
171         throws InvalidArgumentException {
172         final DateValidator validator = new DateValidator(formats);
173         assertEquals("date format is incorrect", ((SimpleDateFormat) formats.get(0)).toPattern(),
174                      ((SimpleDateFormat) validator.getFormats()[0]).toPattern());
175         assertEquals("date format is incorrect", ((SimpleDateFormat) formats.get(1)).toPattern(),
176                      ((SimpleDateFormat) validator.getFormats()[1]).toPattern());
177     }
178 
179     public void testMaximumBounds()
180         throws InvalidArgumentException {
181         final DateValidator validator = new DateValidator(formats);
182         final Calendar cal = Calendar.getInstance();
183 
184         {
185             final Object[] array = new Object[] { "23/12/03", "2002-10-12" };
186             final List list = Arrays.asList(array);
187             cal.set(2004, 1, 12);
188 
189             final Date max = cal.getTime();
190             validator.setMaximum(max);
191             assertTrue("minimum bound is set", validator.getMinimum() == null);
192             assertEquals("maximum bound is incorrect", max, validator.getMaximum());
193             validator.validate(list);
194         }
195 
196         {
197             final Object[] array = new Object[] { "23/12/03", "2004-10-12" };
198             final List list = Arrays.asList(array);
199             cal.set(2004, 1, 12);
200 
201             final Date max = cal.getTime();
202             validator.setMaximum(max);
203 
204             try {
205                 validator.validate(list);
206                 fail("maximum out of bounds exception not caught");
207             } catch (final InvalidArgumentException exp) {
208                 assertEquals(resources.getMessage(ResourceConstants.DATEVALIDATOR_DATE_OUTOFRANGE,
209                                                   new Object[] { "2004-10-12" }), exp.getMessage());
210             }
211         }
212     }
213 
214     public static Test suite() {
215         Test result = new TestSuite(DateValidatorTest.class); // default behavior
216         result = new TimeZoneTestSuite("EST", result); // ensure it runs in EST timezone
217 
218         return result;
219     }
220 }