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