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    *      https://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.lang3.time;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  
23  import java.text.ParsePosition;
24  import java.util.Calendar;
25  import java.util.Date;
26  import java.util.Locale;
27  import java.util.TimeZone;
28  
29  import org.apache.commons.lang3.AbstractLangTest;
30  import org.junit.jupiter.api.Test;
31  import org.junitpioneer.jupiter.ReadsDefaultLocale;
32  import org.junitpioneer.jupiter.ReadsDefaultTimeZone;
33  
34  class FastDateParser_MoreOrLessTest extends AbstractLangTest {
35  
36      private static final TimeZone NEW_YORK = TimeZones.getTimeZone("America/New_York");
37  
38      @Test
39      @ReadsDefaultLocale
40      @ReadsDefaultTimeZone
41      void testInputHasLessCharacters() {
42          final FastDateParser parser = new FastDateParser("MM/dd/yyy", TimeZone.getDefault(), Locale.getDefault());
43          final ParsePosition parsePosition = new ParsePosition(0);
44          assertNull(parser.parse("03/23", parsePosition));
45          assertEquals(5, parsePosition.getErrorIndex());
46      }
47  
48      @Test
49      @ReadsDefaultLocale
50      @ReadsDefaultTimeZone
51      void testInputHasMoreCharacters() {
52          final FastDateParser parser = new FastDateParser("MM/dd", TimeZone.getDefault(), Locale.getDefault());
53          final ParsePosition parsePosition = new ParsePosition(0);
54          final Date date = parser.parse("3/23/61", parsePosition);
55          assertEquals(4, parsePosition.getIndex());
56  
57          final Calendar calendar = Calendar.getInstance();
58          calendar.setTime(date);
59          assertEquals(2, calendar.get(Calendar.MONTH));
60          assertEquals(23, calendar.get(Calendar.DATE));
61      }
62  
63      @Test
64      @ReadsDefaultLocale
65      @ReadsDefaultTimeZone
66      void testInputHasPrecedingCharacters() {
67          final FastDateParser parser = new FastDateParser("MM/dd", TimeZone.getDefault(), Locale.getDefault());
68          final ParsePosition parsePosition = new ParsePosition(0);
69          final Date date = parser.parse("A 3/23/61", parsePosition);
70          assertNull(date);
71          assertEquals(0, parsePosition.getIndex());
72          assertEquals(0, parsePosition.getErrorIndex());
73      }
74  
75      @Test
76      @ReadsDefaultLocale
77      @ReadsDefaultTimeZone
78      void testInputHasWhitespace() {
79          final FastDateParser parser = new FastDateParser("M/d/y", TimeZone.getDefault(), Locale.getDefault());
80          //SimpleDateFormat parser = new SimpleDateFormat("M/d/y");
81          final ParsePosition parsePosition = new ParsePosition(0);
82          final Date date = parser.parse(" 3/ 23/ 1961", parsePosition);
83          assertEquals(12, parsePosition.getIndex());
84  
85          final Calendar calendar = Calendar.getInstance();
86          calendar.setTime(date);
87          assertEquals(1961, calendar.get(Calendar.YEAR));
88          assertEquals(2, calendar.get(Calendar.MONTH));
89          assertEquals(23, calendar.get(Calendar.DATE));
90      }
91  
92      @Test
93      @ReadsDefaultLocale
94      @ReadsDefaultTimeZone
95      void testInputHasWrongCharacters() {
96          final FastDateParser parser = new FastDateParser("MM-dd-yyy", TimeZone.getDefault(), Locale.getDefault());
97          final ParsePosition parsePosition = new ParsePosition(0);
98          assertNull(parser.parse("03/23/1961", parsePosition));
99          assertEquals(2, parsePosition.getErrorIndex());
100     }
101 
102     @Test
103     void testInputHasWrongDay() {
104         final FastDateParser parser = new FastDateParser("EEEE, MM/dd/yyy", NEW_YORK, Locale.US);
105         final String input = "Thursday, 03/23/61";
106         final ParsePosition parsePosition = new ParsePosition(0);
107         assertNotNull(parser.parse(input, parsePosition));
108         assertEquals(input.length(), parsePosition.getIndex());
109 
110         parsePosition.setIndex(0);
111         assertNull(parser.parse("Thorsday, 03/23/61", parsePosition));
112         assertEquals(0, parsePosition.getErrorIndex());
113     }
114 
115     @Test
116     void testInputHasWrongTimeZone() {
117         final FastDateParser parser = new FastDateParser("mm:ss z", NEW_YORK, Locale.US);
118 
119         final String input = "11:23 Pacific Standard Time";
120         final ParsePosition parsePosition = new ParsePosition(0);
121         assertNotNull(parser.parse(input, parsePosition));
122         assertEquals(input.length(), parsePosition.getIndex());
123 
124         parsePosition.setIndex(0);
125         assertNull(parser.parse("11:23 Pacific Standard ", parsePosition));
126         assertEquals(6, parsePosition.getErrorIndex());
127     }
128 }