1 /*
2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * This file is available under and governed by the GNU General Public
26  * License version 2 only, as published by the Free Software Foundation.
27  * However, the following notice accompanied the original version of this
28  * file:
29  *
30  * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
31  *
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions are met:
36  *
37  *  * Redistributions of source code must retain the above copyright notice,
38  *    this list of conditions and the following disclaimer.
39  *
40  *  * Redistributions in binary form must reproduce the above copyright notice,
41  *    this list of conditions and the following disclaimer in the documentation
42  *    and/or other materials provided with the distribution.
43  *
44  *  * Neither the name of JSR-310 nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
52  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
56  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
57  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  */
60 package test.java.time.format;
61 
62 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
63 import static java.time.temporal.ChronoField.DAY_OF_WEEK;
64 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
65 import static java.time.temporal.IsoFields.QUARTER_OF_YEAR;
66 import static org.testng.Assert.assertEquals;
67 import static org.testng.Assert.assertTrue;
68 
69 import java.text.ParsePosition;
70 import java.time.DayOfWeek;
71 import java.time.format.DateTimeFormatter;
72 import java.time.format.DateTimeFormatterBuilder;
73 import java.time.format.TextStyle;
74 import java.time.temporal.TemporalAccessor;
75 import java.time.temporal.TemporalField;
76 import java.time.temporal.TemporalQueries;
77 import java.time.temporal.ChronoField;
78 import java.util.Locale;
79 
80 import org.testng.annotations.DataProvider;
81 import org.testng.annotations.Test;
82 
83 /**
84  * Test TextPrinterParser.
85  */
86 @Test
87 public class TestTextParser extends AbstractTestPrinterParser {
88 
89     //-----------------------------------------------------------------------
90     @DataProvider(name="error")
data_error()91     Object[][] data_error() {
92         return new Object[][] {
93             {DAY_OF_WEEK, TextStyle.FULL, "Monday", -1, IndexOutOfBoundsException.class},
94             {DAY_OF_WEEK, TextStyle.FULL, "Monday", 7, IndexOutOfBoundsException.class},
95         };
96     }
97 
98     @Test(dataProvider="error")
test_parse_error(TemporalField field, TextStyle style, String text, int pos, Class<?> expected)99     public void test_parse_error(TemporalField field, TextStyle style, String text, int pos, Class<?> expected) {
100         try {
101             getFormatter(field, style).parseUnresolved(text, new ParsePosition(pos));
102         } catch (RuntimeException ex) {
103             assertTrue(expected.isInstance(ex));
104         }
105     }
106 
107     //-----------------------------------------------------------------------
test_parse_midStr()108     public void test_parse_midStr() throws Exception {
109         ParsePosition pos = new ParsePosition(3);
110         assertEquals(getFormatter(DAY_OF_WEEK, TextStyle.FULL)
111                      .parseUnresolved("XxxMondayXxx", pos)
112                      .getLong(DAY_OF_WEEK), 1L);
113         assertEquals(pos.getIndex(), 9);
114     }
115 
test_parse_remainderIgnored()116     public void test_parse_remainderIgnored() throws Exception {
117         ParsePosition pos = new ParsePosition(0);
118         assertEquals(getFormatter(DAY_OF_WEEK, TextStyle.SHORT)
119                      .parseUnresolved("Wednesday", pos)
120                      .getLong(DAY_OF_WEEK), 3L);
121         assertEquals(pos.getIndex(), 3);
122     }
123 
124     //-----------------------------------------------------------------------
test_parse_noMatch1()125     public void test_parse_noMatch1() throws Exception {
126         ParsePosition pos = new ParsePosition(0);
127         TemporalAccessor parsed =
128             getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseUnresolved("Munday", pos);
129         assertEquals(pos.getErrorIndex(), 0);
130         assertEquals(parsed, null);
131     }
132 
test_parse_noMatch2()133     public void test_parse_noMatch2() throws Exception {
134         ParsePosition pos = new ParsePosition(3);
135         TemporalAccessor parsed =
136             getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseUnresolved("Monday", pos);
137         assertEquals(pos.getErrorIndex(), 3);
138         assertEquals(parsed, null);
139     }
140 
test_parse_noMatch_atEnd()141     public void test_parse_noMatch_atEnd() throws Exception {
142         ParsePosition pos = new ParsePosition(6);
143         TemporalAccessor parsed =
144             getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseUnresolved("Monday", pos);
145         assertEquals(pos.getErrorIndex(), 6);
146         assertEquals(parsed, null);
147     }
148 
149     //-----------------------------------------------------------------------
150     @DataProvider(name="parseText")
provider_text()151     Object[][] provider_text() {
152         return new Object[][] {
153             {DAY_OF_WEEK, TextStyle.FULL, 1, "Monday"},
154             {DAY_OF_WEEK, TextStyle.FULL, 2, "Tuesday"},
155             {DAY_OF_WEEK, TextStyle.FULL, 3, "Wednesday"},
156             {DAY_OF_WEEK, TextStyle.FULL, 4, "Thursday"},
157             {DAY_OF_WEEK, TextStyle.FULL, 5, "Friday"},
158             {DAY_OF_WEEK, TextStyle.FULL, 6, "Saturday"},
159             {DAY_OF_WEEK, TextStyle.FULL, 7, "Sunday"},
160 
161             {DAY_OF_WEEK, TextStyle.SHORT, 1, "Mon"},
162             {DAY_OF_WEEK, TextStyle.SHORT, 2, "Tue"},
163             {DAY_OF_WEEK, TextStyle.SHORT, 3, "Wed"},
164             {DAY_OF_WEEK, TextStyle.SHORT, 4, "Thu"},
165             {DAY_OF_WEEK, TextStyle.SHORT, 5, "Fri"},
166             {DAY_OF_WEEK, TextStyle.SHORT, 6, "Sat"},
167             {DAY_OF_WEEK, TextStyle.SHORT, 7, "Sun"},
168 
169             {MONTH_OF_YEAR, TextStyle.FULL, 1, "January"},
170             {MONTH_OF_YEAR, TextStyle.FULL, 12, "December"},
171 
172             {MONTH_OF_YEAR, TextStyle.SHORT, 1, "Jan"},
173             {MONTH_OF_YEAR, TextStyle.SHORT, 12, "Dec"},
174 
175             {QUARTER_OF_YEAR, TextStyle.FULL, 1, "1st quarter"},
176             {QUARTER_OF_YEAR, TextStyle.FULL, 2, "2nd quarter"},
177             {QUARTER_OF_YEAR, TextStyle.FULL, 3, "3rd quarter"},
178             {QUARTER_OF_YEAR, TextStyle.FULL, 4, "4th quarter"},
179 
180             {QUARTER_OF_YEAR, TextStyle.SHORT, 1, "Q1"},
181             {QUARTER_OF_YEAR, TextStyle.SHORT, 2, "Q2"},
182             {QUARTER_OF_YEAR, TextStyle.SHORT, 3, "Q3"},
183             {QUARTER_OF_YEAR, TextStyle.SHORT, 4, "Q4"},
184 
185             {QUARTER_OF_YEAR, TextStyle.NARROW, 1, "1"},
186             {QUARTER_OF_YEAR, TextStyle.NARROW, 2, "2"},
187             {QUARTER_OF_YEAR, TextStyle.NARROW, 3, "3"},
188             {QUARTER_OF_YEAR, TextStyle.NARROW, 4, "4"},
189        };
190     }
191 
192     @DataProvider(name="parseNumber")
provider_number()193     Object[][] provider_number() {
194         return new Object[][] {
195             {DAY_OF_MONTH, TextStyle.FULL, 1, "1"},
196             {DAY_OF_MONTH, TextStyle.FULL, 2, "2"},
197             {DAY_OF_MONTH, TextStyle.FULL, 30, "30"},
198             {DAY_OF_MONTH, TextStyle.FULL, 31, "31"},
199 
200             {DAY_OF_MONTH, TextStyle.SHORT, 1, "1"},
201             {DAY_OF_MONTH, TextStyle.SHORT, 2, "2"},
202             {DAY_OF_MONTH, TextStyle.SHORT, 30, "30"},
203             {DAY_OF_MONTH, TextStyle.SHORT, 31, "31"},
204        };
205     }
206 
207     @DataProvider(name="parseDayOfWeekText")
providerDayOfWeekData()208     Object[][] providerDayOfWeekData() {
209         return new Object[][] {
210             // Locale, pattern, input text, expected DayOfWeek
211             {Locale.US, "e",  "1",  DayOfWeek.SUNDAY},
212             {Locale.US, "ee", "01", DayOfWeek.SUNDAY},
213             {Locale.US, "c",  "1",  DayOfWeek.SUNDAY},
214         };
215     }
216 
217 
218     @Test(dataProvider="parseText")
test_parseText(TemporalField field, TextStyle style, int value, String input)219     public void test_parseText(TemporalField field, TextStyle style, int value, String input) throws Exception {
220         ParsePosition pos = new ParsePosition(0);
221         assertEquals(getFormatter(field, style).parseUnresolved(input, pos).getLong(field), (long) value);
222         assertEquals(pos.getIndex(), input.length());
223     }
224 
225     @Test(dataProvider="parseNumber")
test_parseNumber(TemporalField field, TextStyle style, int value, String input)226     public void test_parseNumber(TemporalField field, TextStyle style, int value, String input) throws Exception {
227         ParsePosition pos = new ParsePosition(0);
228         assertEquals(getFormatter(field, style).parseUnresolved(input, pos).getLong(field), (long) value);
229         assertEquals(pos.getIndex(), input.length());
230     }
231 
232     @Test(dataProvider="parseDayOfWeekText")
test_parseDayOfWeekText(Locale locale, String pattern, String input, DayOfWeek expected)233     public void test_parseDayOfWeekText(Locale locale, String pattern, String input, DayOfWeek expected) {
234         DateTimeFormatter formatter = getPatternFormatter(pattern).withLocale(locale);
235         ParsePosition pos = new ParsePosition(0);
236         assertEquals(DayOfWeek.from(formatter.parse(input, pos)), expected);
237         assertEquals(pos.getIndex(), input.length());
238     }
239 
240     //-----------------------------------------------------------------------
241     @Test(dataProvider="parseText")
test_parse_strict_caseSensitive_parseUpper(TemporalField field, TextStyle style, int value, String input)242     public void test_parse_strict_caseSensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception {
243         if (input.equals(input.toUpperCase(Locale.ROOT))) {
244             // Skip if the given input is all upper case (e.g., "Q1")
245             return;
246         }
247         setCaseSensitive(true);
248         ParsePosition pos = new ParsePosition(0);
249         getFormatter(field, style).parseUnresolved(input.toUpperCase(Locale.ROOT), pos);
250         assertEquals(pos.getErrorIndex(), 0);
251     }
252 
253     @Test(dataProvider="parseText")
test_parse_strict_caseInsensitive_parseUpper(TemporalField field, TextStyle style, int value, String input)254     public void test_parse_strict_caseInsensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception {
255         setCaseSensitive(false);
256         ParsePosition pos = new ParsePosition(0);
257         assertEquals(getFormatter(field, style).parseUnresolved(input.toUpperCase(Locale.ROOT), pos).getLong(field), (long) value);
258         assertEquals(pos.getIndex(), input.length());
259     }
260 
261     //-----------------------------------------------------------------------
262     @Test(dataProvider="parseText")
test_parse_strict_caseSensitive_parseLower(TemporalField field, TextStyle style, int value, String input)263     public void test_parse_strict_caseSensitive_parseLower(TemporalField field, TextStyle style, int value, String input) throws Exception {
264         if (input.equals(input.toLowerCase(Locale.ROOT))) {
265             // Skip if the given input is all lower case (e.g., "1st quarter")
266             return;
267         }
268         setCaseSensitive(true);
269         ParsePosition pos = new ParsePosition(0);
270         getFormatter(field, style).parseUnresolved(input.toLowerCase(Locale.ROOT), pos);
271         assertEquals(pos.getErrorIndex(), 0);
272     }
273 
274     @Test(dataProvider="parseText")
test_parse_strict_caseInsensitive_parseLower(TemporalField field, TextStyle style, int value, String input)275     public void test_parse_strict_caseInsensitive_parseLower(TemporalField field, TextStyle style, int value, String input) throws Exception {
276         setCaseSensitive(false);
277         ParsePosition pos = new ParsePosition(0);
278         assertEquals(getFormatter(field, style).parseUnresolved(input.toLowerCase(Locale.ROOT), pos).getLong(field), (long) value);
279         assertEquals(pos.getIndex(), input.length());
280     }
281 
282     //-----------------------------------------------------------------------
283     //-----------------------------------------------------------------------
284     //-----------------------------------------------------------------------
test_parse_full_strict_full_match()285     public void test_parse_full_strict_full_match() throws Exception {
286         setStrict(true);
287         ParsePosition pos = new ParsePosition(0);
288         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("January", pos).getLong(MONTH_OF_YEAR), 1L);
289         assertEquals(pos.getIndex(), 7);
290     }
291 
test_parse_full_strict_short_noMatch()292     public void test_parse_full_strict_short_noMatch() throws Exception {
293         setStrict(true);
294         ParsePosition pos = new ParsePosition(0);
295         getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("Janua", pos);
296         assertEquals(pos.getErrorIndex(), 0);
297     }
298 
test_parse_full_strict_number_noMatch()299     public void test_parse_full_strict_number_noMatch() throws Exception {
300         setStrict(true);
301         ParsePosition pos = new ParsePosition(0);
302         getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("1", pos);
303         assertEquals(pos.getErrorIndex(), 0);
304     }
305 
306     //-----------------------------------------------------------------------
test_parse_short_strict_full_match()307     public void test_parse_short_strict_full_match() throws Exception {
308         setStrict(true);
309         ParsePosition pos = new ParsePosition(0);
310         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("January", pos).getLong(MONTH_OF_YEAR), 1L);
311         assertEquals(pos.getIndex(), 3);
312     }
313 
test_parse_short_strict_short_match()314     public void test_parse_short_strict_short_match() throws Exception {
315         setStrict(true);
316         ParsePosition pos = new ParsePosition(0);
317         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
318         assertEquals(pos.getIndex(), 3);
319     }
320 
test_parse_short_strict_number_noMatch()321     public void test_parse_short_strict_number_noMatch() throws Exception {
322         setStrict(true);
323         ParsePosition pos = new ParsePosition(0);
324         getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("1", pos);
325         assertEquals(pos.getErrorIndex(), 0);
326     }
327 
328     //-----------------------------------------------------------------------
test_parse_full_lenient_full_match()329     public void test_parse_full_lenient_full_match() throws Exception {
330         setStrict(false);
331         ParsePosition pos = new ParsePosition(0);
332         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("January.", pos).getLong(MONTH_OF_YEAR), 1L);
333         assertEquals(pos.getIndex(), 7);
334     }
335 
test_parse_full_lenient_short_match()336     public void test_parse_full_lenient_short_match() throws Exception {
337         setStrict(false);
338         ParsePosition pos = new ParsePosition(0);
339         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
340         assertEquals(pos.getIndex(), 3);
341     }
342 
test_parse_full_lenient_number_match()343     public void test_parse_full_lenient_number_match() throws Exception {
344         setStrict(false);
345         ParsePosition pos = new ParsePosition(0);
346         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("1", pos).getLong(MONTH_OF_YEAR), 1L);
347         assertEquals(pos.getIndex(), 1);
348     }
349 
350     //-----------------------------------------------------------------------
test_parse_short_lenient_full_match()351     public void test_parse_short_lenient_full_match() throws Exception {
352         setStrict(false);
353         ParsePosition pos = new ParsePosition(0);
354         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("January", pos).getLong(MONTH_OF_YEAR), 1L);
355         assertEquals(pos.getIndex(), 7);
356     }
357 
test_parse_short_lenient_short_match()358     public void test_parse_short_lenient_short_match() throws Exception {
359         setStrict(false);
360         ParsePosition pos = new ParsePosition(0);
361         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
362         assertEquals(pos.getIndex(), 3);
363     }
364 
test_parse_short_lenient_number_match()365     public void test_parse_short_lenient_number_match() throws Exception {
366         setStrict(false);
367         ParsePosition pos = new ParsePosition(0);
368         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("1", pos).getLong(MONTH_OF_YEAR), 1L);
369         assertEquals(pos.getIndex(), 1);
370     }
371 
372 }
373