001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.cli2.validation;
018
019import java.text.DateFormat;
020import java.text.DateFormatSymbols;
021import java.text.SimpleDateFormat;
022import java.util.Arrays;
023import java.util.Calendar;
024import java.util.Date;
025import java.util.Iterator;
026import java.util.List;
027
028import junit.framework.Test;
029import junit.framework.TestCase;
030import junit.framework.TestSuite;
031
032import org.apache.commons.cli2.resource.ResourceConstants;
033import org.apache.commons.cli2.resource.ResourceHelper;
034
035/**
036 * JUnit test case for DateValidator.
037 *
038 * @author Rob Oxspring
039 * @author John Keyes
040 */
041public class DateValidatorTest
042    extends TestCase {
043    private static final ResourceHelper resources = ResourceHelper.getResourceHelper();
044    public static final DateFormat D_M_YY = new SimpleDateFormat("d/M/yy");
045    public static final DateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
046    private List formats = Arrays.asList(new Object[] { D_M_YY, YYYY_MM_DD });
047
048    public void testSingleFormatValidate()
049        throws InvalidArgumentException {
050        final Object[] array = new Object[] { "23/12/03" };
051        final List list = Arrays.asList(array);
052        final Validator validator = new DateValidator(D_M_YY);
053
054        validator.validate(list);
055
056        final Iterator i = list.iterator();
057        assertEquals("2003-12-23", YYYY_MM_DD.format((Date) i.next()));
058        assertFalse(i.hasNext());
059    }
060
061    public void testDefaultDateFormatValidate()
062        throws InvalidArgumentException {
063        DateFormatSymbols symbols =  new DateFormatSymbols();
064        final Object[] array = new Object[] { "23-" + symbols.getShortMonths()[11] + "-2003" };
065        final List list = Arrays.asList(array);
066        final Validator validator = new DateValidator( new SimpleDateFormat("dd-MMM-yyyy") );
067
068        validator.validate(list);
069
070        final Iterator i = list.iterator();
071        // CLI-40: For some reason, the YYYY_MM_DD object gets quite
072        // confused here and returns 2003-12-22. If we make a new one
073        // there is no problem.
074        assertEquals("2003-12-23", new SimpleDateFormat("yyyy-MM-dd").format((Date) i.next()));
075        assertFalse(i.hasNext());
076    }
077
078    public void testDefaultTimeFormatValidate()
079        throws InvalidArgumentException {
080        final Object[] array = new Object[] { "18:00:00" };
081        final List list = Arrays.asList(array);
082        final Validator validator = new DateValidator( new SimpleDateFormat("HH:mm:ss") );
083
084        validator.validate(list);
085
086        final Iterator i = list.iterator();
087        final DateFormat df = new SimpleDateFormat("HH:mm:ss");
088        assertEquals("18:00:00", df.format((Date) i.next()));
089        assertFalse(i.hasNext());
090    }
091
092    public void testDefaultDateTimeFormatValidate()
093        throws InvalidArgumentException {
094        DateFormatSymbols symbols =  new DateFormatSymbols();
095        final Object[] array = new Object[] { "23-" + symbols.getShortMonths()[0] + "-2003 18:00:00" };
096        final List list = Arrays.asList(array);
097        final Validator validator = new DateValidator( new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss") );
098
099        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}