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.chrono.ChronoLocalDate;
  72 import java.time.chrono.JapaneseChronology;
  73 import java.time.chrono.HijrahDate;
  74 import java.time.chrono.JapaneseDate;
  75 import java.time.chrono.MinguoDate;
  76 import java.time.chrono.ThaiBuddhistDate;
  77 import java.time.format.DateTimeFormatter;
  78 import java.time.format.DateTimeFormatterBuilder;
  79 import java.time.format.TextStyle;
  80 import java.time.format.SignStyle;
  81 import java.time.temporal.TemporalAccessor;
  82 import java.time.temporal.TemporalField;
  83 import java.time.temporal.TemporalQueries;
  84 import java.time.temporal.ChronoField;
  85 import java.util.Locale;
  86 
  87 import org.testng.annotations.DataProvider;
  88 import org.testng.annotations.Test;
  89 
  90 /**
  91  * Test TextPrinterParser.
  92  */
  93 @Test
  94 public class TestTextParser extends AbstractTestPrinterParser {
  95     static final Locale RUSSIAN = new Locale("ru");
  96     static final Locale FINNISH = new Locale("fi");
  97 
  98     //-----------------------------------------------------------------------
  99     @DataProvider(name="error")
 100     Object[][] data_error() {
 101         return new Object[][] {
 102             {DAY_OF_WEEK, TextStyle.FULL, "Monday", -1, IndexOutOfBoundsException.class},
 103             {DAY_OF_WEEK, TextStyle.FULL, "Monday", 7, IndexOutOfBoundsException.class},
 104         };
 105     }
 106 
 107     @Test(dataProvider="error")
 108     public void test_parse_error(TemporalField field, TextStyle style, String text, int pos, Class<?> expected) {
 109         try {
 110             getFormatter(field, style).parseUnresolved(text, new ParsePosition(pos));
 111         } catch (RuntimeException ex) {
 112             assertTrue(expected.isInstance(ex));
 113         }
 114     }
 115 
 116     //-----------------------------------------------------------------------
 117     public void test_parse_midStr() throws Exception {
 118         ParsePosition pos = new ParsePosition(3);
 119         assertEquals(getFormatter(DAY_OF_WEEK, TextStyle.FULL)
 120                      .parseUnresolved("XxxMondayXxx", pos)
 121                      .getLong(DAY_OF_WEEK), 1L);
 122         assertEquals(pos.getIndex(), 9);
 123     }
 124 
 125     public void test_parse_remainderIgnored() throws Exception {
 126         ParsePosition pos = new ParsePosition(0);
 127         assertEquals(getFormatter(DAY_OF_WEEK, TextStyle.SHORT)
 128                      .parseUnresolved("Wednesday", pos)
 129                      .getLong(DAY_OF_WEEK), 3L);
 130         assertEquals(pos.getIndex(), 3);
 131     }
 132 
 133     //-----------------------------------------------------------------------
 134     public void test_parse_noMatch1() throws Exception {
 135         ParsePosition pos = new ParsePosition(0);
 136         TemporalAccessor parsed =
 137             getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseUnresolved("Munday", pos);
 138         assertEquals(pos.getErrorIndex(), 0);
 139         assertEquals(parsed, null);
 140     }
 141 
 142     public void test_parse_noMatch2() throws Exception {
 143         ParsePosition pos = new ParsePosition(3);
 144         TemporalAccessor parsed =
 145             getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseUnresolved("Monday", pos);
 146         assertEquals(pos.getErrorIndex(), 3);
 147         assertEquals(parsed, null);
 148     }
 149 
 150     public void test_parse_noMatch_atEnd() throws Exception {
 151         ParsePosition pos = new ParsePosition(6);
 152         TemporalAccessor parsed =
 153             getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseUnresolved("Monday", pos);
 154         assertEquals(pos.getErrorIndex(), 6);
 155         assertEquals(parsed, null);
 156     }
 157 
 158     //-----------------------------------------------------------------------
 159     @DataProvider(name="parseText")
 160     Object[][] provider_text() {
 161         return new Object[][] {
 162             {DAY_OF_WEEK, TextStyle.FULL, 1, "Monday"},
 163             {DAY_OF_WEEK, TextStyle.FULL, 2, "Tuesday"},
 164             {DAY_OF_WEEK, TextStyle.FULL, 3, "Wednesday"},
 165             {DAY_OF_WEEK, TextStyle.FULL, 4, "Thursday"},
 166             {DAY_OF_WEEK, TextStyle.FULL, 5, "Friday"},
 167             {DAY_OF_WEEK, TextStyle.FULL, 6, "Saturday"},
 168             {DAY_OF_WEEK, TextStyle.FULL, 7, "Sunday"},
 169 
 170             {DAY_OF_WEEK, TextStyle.SHORT, 1, "Mon"},
 171             {DAY_OF_WEEK, TextStyle.SHORT, 2, "Tue"},
 172             {DAY_OF_WEEK, TextStyle.SHORT, 3, "Wed"},
 173             {DAY_OF_WEEK, TextStyle.SHORT, 4, "Thu"},
 174             {DAY_OF_WEEK, TextStyle.SHORT, 5, "Fri"},
 175             {DAY_OF_WEEK, TextStyle.SHORT, 6, "Sat"},
 176             {DAY_OF_WEEK, TextStyle.SHORT, 7, "Sun"},
 177 
 178             {MONTH_OF_YEAR, TextStyle.FULL, 1, "January"},
 179             {MONTH_OF_YEAR, TextStyle.FULL, 12, "December"},
 180 
 181             {MONTH_OF_YEAR, TextStyle.SHORT, 1, "Jan"},
 182             {MONTH_OF_YEAR, TextStyle.SHORT, 12, "Dec"},
 183 
 184             {QUARTER_OF_YEAR, TextStyle.FULL, 1, "1st quarter"},
 185             {QUARTER_OF_YEAR, TextStyle.FULL, 2, "2nd quarter"},
 186             {QUARTER_OF_YEAR, TextStyle.FULL, 3, "3rd quarter"},
 187             {QUARTER_OF_YEAR, TextStyle.FULL, 4, "4th quarter"},
 188 
 189             {QUARTER_OF_YEAR, TextStyle.SHORT, 1, "Q1"},
 190             {QUARTER_OF_YEAR, TextStyle.SHORT, 2, "Q2"},
 191             {QUARTER_OF_YEAR, TextStyle.SHORT, 3, "Q3"},
 192             {QUARTER_OF_YEAR, TextStyle.SHORT, 4, "Q4"},
 193 
 194             {QUARTER_OF_YEAR, TextStyle.NARROW, 1, "1"},
 195             {QUARTER_OF_YEAR, TextStyle.NARROW, 2, "2"},
 196             {QUARTER_OF_YEAR, TextStyle.NARROW, 3, "3"},
 197             {QUARTER_OF_YEAR, TextStyle.NARROW, 4, "4"},
 198        };
 199     }
 200 
 201     @DataProvider(name="parseNumber")
 202     Object[][] provider_number() {
 203         return new Object[][] {
 204             {DAY_OF_MONTH, TextStyle.FULL, 1, "1"},
 205             {DAY_OF_MONTH, TextStyle.FULL, 2, "2"},
 206             {DAY_OF_MONTH, TextStyle.FULL, 30, "30"},
 207             {DAY_OF_MONTH, TextStyle.FULL, 31, "31"},
 208 
 209             {DAY_OF_MONTH, TextStyle.SHORT, 1, "1"},
 210             {DAY_OF_MONTH, TextStyle.SHORT, 2, "2"},
 211             {DAY_OF_MONTH, TextStyle.SHORT, 30, "30"},
 212             {DAY_OF_MONTH, TextStyle.SHORT, 31, "31"},
 213        };
 214     }
 215 
 216     @DataProvider(name="parseDayOfWeekText")
 217     Object[][] providerDayOfWeekData() {
 218         return new Object[][] {
 219             // Locale, pattern, input text, expected DayOfWeek
 220             {Locale.US, "e",  "1",  DayOfWeek.SUNDAY},
 221             {Locale.US, "ee", "01", DayOfWeek.SUNDAY},
 222             {Locale.US, "c",  "1",  DayOfWeek.SUNDAY},
 223         };
 224     }
 225 
 226 
 227     @Test(dataProvider="parseText")
 228     public void test_parseText(TemporalField field, TextStyle style, int value, String input) throws Exception {
 229         ParsePosition pos = new ParsePosition(0);
 230         assertEquals(getFormatter(field, style).parseUnresolved(input, pos).getLong(field), (long) value);
 231         assertEquals(pos.getIndex(), input.length());
 232     }
 233 
 234     @Test(dataProvider="parseNumber")
 235     public void test_parseNumber(TemporalField field, TextStyle style, int value, String input) throws Exception {
 236         ParsePosition pos = new ParsePosition(0);
 237         assertEquals(getFormatter(field, style).parseUnresolved(input, pos).getLong(field), (long) value);
 238         assertEquals(pos.getIndex(), input.length());
 239     }
 240 
 241     @Test(dataProvider="parseDayOfWeekText")
 242     public void test_parseDayOfWeekText(Locale locale, String pattern, String input, DayOfWeek expected) {
 243         DateTimeFormatter formatter = getPatternFormatter(pattern).withLocale(locale);
 244         ParsePosition pos = new ParsePosition(0);
 245         assertEquals(DayOfWeek.from(formatter.parse(input, pos)), expected);
 246         assertEquals(pos.getIndex(), input.length());
 247     }
 248 
 249     //-----------------------------------------------------------------------
 250     @Test(dataProvider="parseText")
 251     public void test_parse_strict_caseSensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception {
 252         if (input.equals(input.toUpperCase(Locale.ROOT))) {
 253             // Skip if the given input is all upper case (e.g., "Q1")
 254             return;
 255         }
 256         setCaseSensitive(true);
 257         ParsePosition pos = new ParsePosition(0);
 258         getFormatter(field, style).parseUnresolved(input.toUpperCase(Locale.ROOT), pos);
 259         assertEquals(pos.getErrorIndex(), 0);
 260     }
 261 
 262     @Test(dataProvider="parseText")
 263     public void test_parse_strict_caseInsensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception {
 264         setCaseSensitive(false);
 265         ParsePosition pos = new ParsePosition(0);
 266         assertEquals(getFormatter(field, style).parseUnresolved(input.toUpperCase(Locale.ROOT), pos).getLong(field), (long) value);
 267         assertEquals(pos.getIndex(), input.length());
 268     }
 269 
 270     //-----------------------------------------------------------------------
 271     @Test(dataProvider="parseText")
 272     public void test_parse_strict_caseSensitive_parseLower(TemporalField field, TextStyle style, int value, String input) throws Exception {
 273         if (input.equals(input.toLowerCase(Locale.ROOT))) {
 274             // Skip if the given input is all lower case (e.g., "1st quarter")
 275             return;
 276         }
 277         setCaseSensitive(true);
 278         ParsePosition pos = new ParsePosition(0);
 279         getFormatter(field, style).parseUnresolved(input.toLowerCase(Locale.ROOT), pos);
 280         assertEquals(pos.getErrorIndex(), 0);
 281     }
 282 
 283     @Test(dataProvider="parseText")
 284     public void test_parse_strict_caseInsensitive_parseLower(TemporalField field, TextStyle style, int value, String input) throws Exception {
 285         setCaseSensitive(false);
 286         ParsePosition pos = new ParsePosition(0);
 287         assertEquals(getFormatter(field, style).parseUnresolved(input.toLowerCase(Locale.ROOT), pos).getLong(field), (long) value);
 288         assertEquals(pos.getIndex(), input.length());
 289     }
 290 
 291     //-----------------------------------------------------------------------
 292     //-----------------------------------------------------------------------
 293     //-----------------------------------------------------------------------
 294     public void test_parse_full_strict_full_match() throws Exception {
 295         setStrict(true);
 296         ParsePosition pos = new ParsePosition(0);
 297         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("January", pos).getLong(MONTH_OF_YEAR), 1L);
 298         assertEquals(pos.getIndex(), 7);
 299     }
 300 
 301     public void test_parse_full_strict_short_noMatch() throws Exception {
 302         setStrict(true);
 303         ParsePosition pos = new ParsePosition(0);
 304         getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("Janua", pos);
 305         assertEquals(pos.getErrorIndex(), 0);
 306     }
 307 
 308     public void test_parse_full_strict_number_noMatch() throws Exception {
 309         setStrict(true);
 310         ParsePosition pos = new ParsePosition(0);
 311         getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("1", pos);
 312         assertEquals(pos.getErrorIndex(), 0);
 313     }
 314 
 315     //-----------------------------------------------------------------------
 316     public void test_parse_short_strict_full_match() throws Exception {
 317         setStrict(true);
 318         ParsePosition pos = new ParsePosition(0);
 319         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("January", pos).getLong(MONTH_OF_YEAR), 1L);
 320         assertEquals(pos.getIndex(), 3);
 321     }
 322 
 323     public void test_parse_short_strict_short_match() throws Exception {
 324         setStrict(true);
 325         ParsePosition pos = new ParsePosition(0);
 326         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
 327         assertEquals(pos.getIndex(), 3);
 328     }
 329 
 330     public void test_parse_short_strict_number_noMatch() throws Exception {
 331         setStrict(true);
 332         ParsePosition pos = new ParsePosition(0);
 333         getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("1", pos);
 334         assertEquals(pos.getErrorIndex(), 0);
 335     }
 336 
 337     //-----------------------------------------------------------------------
 338     public void test_parse_full_lenient_full_match() throws Exception {
 339         setStrict(false);
 340         ParsePosition pos = new ParsePosition(0);
 341         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("January.", pos).getLong(MONTH_OF_YEAR), 1L);
 342         assertEquals(pos.getIndex(), 7);
 343     }
 344 
 345     public void test_parse_full_lenient_short_match() throws Exception {
 346         setStrict(false);
 347         ParsePosition pos = new ParsePosition(0);
 348         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
 349         assertEquals(pos.getIndex(), 3);
 350     }
 351 
 352     public void test_parse_full_lenient_number_match() throws Exception {
 353         setStrict(false);
 354         ParsePosition pos = new ParsePosition(0);
 355         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("1", pos).getLong(MONTH_OF_YEAR), 1L);
 356         assertEquals(pos.getIndex(), 1);
 357     }
 358 
 359     //-----------------------------------------------------------------------
 360     public void test_parse_short_lenient_full_match() throws Exception {
 361         setStrict(false);
 362         ParsePosition pos = new ParsePosition(0);
 363         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("January", pos).getLong(MONTH_OF_YEAR), 1L);
 364         assertEquals(pos.getIndex(), 7);
 365     }
 366 
 367     public void test_parse_short_lenient_short_match() throws Exception {
 368         setStrict(false);
 369         ParsePosition pos = new ParsePosition(0);
 370         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
 371         assertEquals(pos.getIndex(), 3);
 372     }
 373 
 374     public void test_parse_short_lenient_number_match() throws Exception {
 375         setStrict(false);
 376         ParsePosition pos = new ParsePosition(0);
 377         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("1", pos).getLong(MONTH_OF_YEAR), 1L);
 378         assertEquals(pos.getIndex(), 1);
 379     }
 380 
 381     //-----------------------------------------------------------------------
 382     @DataProvider(name="parseChronoLocalDate")
 383     Object[][] provider_chronoLocalDate() {
 384         return new Object[][] {
 385             { HijrahDate.now() },
 386             { JapaneseDate.now() },
 387             { MinguoDate.now() },
 388             { ThaiBuddhistDate.now() }};
 389     }
 390 
 391     private static final DateTimeFormatter fmt_chrono =
 392         new DateTimeFormatterBuilder()
 393             .optionalStart()
 394             .appendChronologyId()
 395             .appendLiteral(' ')
 396             .optionalEnd()
 397             .optionalStart()
 398             .appendText(ChronoField.ERA, TextStyle.SHORT)
 399             .appendLiteral(' ')
 400             .optionalEnd()
 401             .appendValue(ChronoField.YEAR_OF_ERA, 1, 9, SignStyle.NORMAL)
 402             .appendLiteral('-')
 403             .appendValue(ChronoField.MONTH_OF_YEAR, 1, 2, SignStyle.NEVER)
 404             .appendLiteral('-')
 405             .appendValue(ChronoField.DAY_OF_MONTH, 1, 2, SignStyle.NEVER)
 406             .toFormatter();
 407 
 408     @Test(dataProvider="parseChronoLocalDate")
 409     public void test_chronoLocalDate(ChronoLocalDate date) throws Exception {
 410         System.out.printf(" %s, [fmt=%s]%n", date, fmt_chrono.format(date));
 411         assertEquals(date, fmt_chrono.parse(fmt_chrono.format(date), ChronoLocalDate::from));
 412 
 413         DateTimeFormatter fmt = DateTimeFormatter.ofPattern("[GGG ]yyy-MM-dd")
 414                                                  .withChronology(date.getChronology());
 415         System.out.printf(" %s, [fmt=%s]%n", date.toString(), fmt.format(date));
 416         assertEquals(date, fmt.parse(fmt.format(date), ChronoLocalDate::from));
 417     }
 418 
 419 }