< prev index next >

test/java/time/test/java/time/format/TestTextParser.java

Print this page


   1 /*
   2  * Copyright (c) 2012, 2013, 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  */


 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     // Test data is dependent on localized resources.
 217     @DataProvider(name="parseStandaloneText")
 218     Object[][] providerStandaloneText() {
 219         // Locale, TemporalField, TextStyle, expected value, input text
 220         return new Object[][] {
 221             {RUSSIAN, MONTH_OF_YEAR, TextStyle.FULL_STANDALONE,   1, "\u044f\u043d\u0432\u0430\u0440\u044c"},
 222             {RUSSIAN, MONTH_OF_YEAR, TextStyle.FULL_STANDALONE,  12, "\u0434\u0435\u043a\u0430\u0431\u0440\u044c"},
 223             {RUSSIAN, MONTH_OF_YEAR, TextStyle.SHORT_STANDALONE,  1, "\u044f\u043d\u0432."},
 224             {RUSSIAN, MONTH_OF_YEAR, TextStyle.SHORT_STANDALONE, 12, "\u0434\u0435\u043a."},
 225             {FINNISH, DAY_OF_WEEK,   TextStyle.FULL_STANDALONE,   2, "tiistai"},
 226             {FINNISH, DAY_OF_WEEK,   TextStyle.SHORT_STANDALONE,  2, "ti"},
 227         };
 228     }
 229 
 230     @DataProvider(name="parseDayOfWeekText")
 231     Object[][] providerDayOfWeekData() {
 232         return new Object[][] {
 233             // Locale, pattern, input text, expected DayOfWeek
 234             {Locale.US, "e",  "1",  DayOfWeek.SUNDAY},
 235             {Locale.US, "ee", "01", DayOfWeek.SUNDAY},
 236             {Locale.US, "c",  "1",  DayOfWeek.SUNDAY},
 237 
 238             {Locale.UK, "e",  "1",  DayOfWeek.MONDAY},
 239             {Locale.UK, "ee", "01", DayOfWeek.MONDAY},
 240             {Locale.UK, "c",  "1",  DayOfWeek.MONDAY},
 241         };
 242     }
 243 
 244     // Test data is dependent on localized resources.
 245     @DataProvider(name="parseLenientText")
 246     Object[][] providerLenientText() {
 247         // Locale, TemporalField, expected value, input text
 248         return new Object[][] {
 249             {RUSSIAN, MONTH_OF_YEAR, 1, "\u044f\u043d\u0432\u0430\u0440\u044f"}, // full format
 250             {RUSSIAN, MONTH_OF_YEAR, 1, "\u044f\u043d\u0432\u0430\u0440\u044c"}, // full standalone
 251             {RUSSIAN, MONTH_OF_YEAR, 1, "\u044f\u043d\u0432."}, // short format
 252             {RUSSIAN, MONTH_OF_YEAR, 1, "\u044f\u043d\u0432."}, // short standalone
 253         };
 254     }
 255 
 256 
 257 
 258     @Test(dataProvider="parseText")
 259     public void test_parseText(TemporalField field, TextStyle style, int value, String input) throws Exception {
 260         ParsePosition pos = new ParsePosition(0);
 261         assertEquals(getFormatter(field, style).parseUnresolved(input, pos).getLong(field), (long) value);
 262         assertEquals(pos.getIndex(), input.length());
 263     }
 264 
 265     @Test(dataProvider="parseNumber")
 266     public void test_parseNumber(TemporalField field, TextStyle style, int value, String input) throws Exception {
 267         ParsePosition pos = new ParsePosition(0);
 268         assertEquals(getFormatter(field, style).parseUnresolved(input, pos).getLong(field), (long) value);
 269         assertEquals(pos.getIndex(), input.length());
 270     }
 271 
 272     @Test(dataProvider="parseStandaloneText")
 273     public void test_parseStandaloneText(Locale locale, TemporalField field, TextStyle style, int expectedValue, String input) {
 274         DateTimeFormatter formatter = getFormatter(field, style).withLocale(locale);
 275         ParsePosition pos = new ParsePosition(0);
 276         assertEquals(formatter.parseUnresolved(input, pos).getLong(field), (long) expectedValue);
 277         assertEquals(pos.getIndex(), input.length());
 278     }
 279 
 280     @Test(dataProvider="parseDayOfWeekText")
 281     public void test_parseDayOfWeekText(Locale locale, String pattern, String input, DayOfWeek expected) {
 282         DateTimeFormatter formatter = getPatternFormatter(pattern).withLocale(locale);
 283         ParsePosition pos = new ParsePosition(0);
 284         assertEquals(DayOfWeek.from(formatter.parse(input, pos)), expected);
 285         assertEquals(pos.getIndex(), input.length());
 286     }
 287 
 288     //-----------------------------------------------------------------------
 289     @Test(dataProvider="parseText")
 290     public void test_parse_strict_caseSensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception {
 291         if (input.equals(input.toUpperCase(Locale.ROOT))) {
 292             // Skip if the given input is all upper case (e.g., "Q1")
 293             return;
 294         }
 295         setCaseSensitive(true);
 296         ParsePosition pos = new ParsePosition(0);
 297         getFormatter(field, style).parseUnresolved(input.toUpperCase(Locale.ROOT), pos);
 298         assertEquals(pos.getErrorIndex(), 0);
 299     }


 357         ParsePosition pos = new ParsePosition(0);
 358         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("January", pos).getLong(MONTH_OF_YEAR), 1L);
 359         assertEquals(pos.getIndex(), 3);
 360     }
 361 
 362     public void test_parse_short_strict_short_match() throws Exception {
 363         setStrict(true);
 364         ParsePosition pos = new ParsePosition(0);
 365         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
 366         assertEquals(pos.getIndex(), 3);
 367     }
 368 
 369     public void test_parse_short_strict_number_noMatch() throws Exception {
 370         setStrict(true);
 371         ParsePosition pos = new ParsePosition(0);
 372         getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("1", pos);
 373         assertEquals(pos.getErrorIndex(), 0);
 374     }
 375 
 376     //-----------------------------------------------------------------------
 377     public void test_parse_french_short_strict_full_noMatch() throws Exception {
 378         setStrict(true);
 379         ParsePosition pos = new ParsePosition(0);
 380         getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).withLocale(Locale.FRENCH)
 381                                                     .parseUnresolved("janvier", pos);
 382         assertEquals(pos.getErrorIndex(), 0);
 383     }
 384 
 385     public void test_parse_french_short_strict_short_match() throws Exception {
 386         setStrict(true);
 387         ParsePosition pos = new ParsePosition(0);
 388         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).withLocale(Locale.FRENCH)
 389                                                                  .parseUnresolved("janv.", pos)
 390                                                                  .getLong(MONTH_OF_YEAR),
 391                      1L);
 392         assertEquals(pos.getIndex(), 5);
 393     }
 394 
 395     //-----------------------------------------------------------------------
 396     public void test_parse_full_lenient_full_match() throws Exception {
 397         setStrict(false);
 398         ParsePosition pos = new ParsePosition(0);
 399         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("January.", pos).getLong(MONTH_OF_YEAR), 1L);
 400         assertEquals(pos.getIndex(), 7);
 401     }
 402 
 403     public void test_parse_full_lenient_short_match() throws Exception {
 404         setStrict(false);
 405         ParsePosition pos = new ParsePosition(0);
 406         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
 407         assertEquals(pos.getIndex(), 3);
 408     }
 409 
 410     public void test_parse_full_lenient_number_match() throws Exception {
 411         setStrict(false);
 412         ParsePosition pos = new ParsePosition(0);
 413         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("1", pos).getLong(MONTH_OF_YEAR), 1L);
 414         assertEquals(pos.getIndex(), 1);
 415     }


 417     //-----------------------------------------------------------------------
 418     public void test_parse_short_lenient_full_match() throws Exception {
 419         setStrict(false);
 420         ParsePosition pos = new ParsePosition(0);
 421         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("January", pos).getLong(MONTH_OF_YEAR), 1L);
 422         assertEquals(pos.getIndex(), 7);
 423     }
 424 
 425     public void test_parse_short_lenient_short_match() throws Exception {
 426         setStrict(false);
 427         ParsePosition pos = new ParsePosition(0);
 428         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
 429         assertEquals(pos.getIndex(), 3);
 430     }
 431 
 432     public void test_parse_short_lenient_number_match() throws Exception {
 433         setStrict(false);
 434         ParsePosition pos = new ParsePosition(0);
 435         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("1", pos).getLong(MONTH_OF_YEAR), 1L);
 436         assertEquals(pos.getIndex(), 1);
 437     }
 438 
 439     @Test(dataProvider="parseLenientText")
 440     public void test_parseLenientText(Locale locale, TemporalField field, int expectedValue, String input) {
 441         setStrict(false);
 442         ParsePosition pos = new ParsePosition(0);
 443         DateTimeFormatter formatter = getFormatter(field).withLocale(locale);
 444         assertEquals(formatter.parseUnresolved(input, pos).getLong(field), (long) expectedValue);
 445         assertEquals(pos.getIndex(), input.length());
 446     }
 447 
 448     //-----------------------------------------------------------------------
 449     @DataProvider(name="parseChronoLocalDate")
 450     Object[][] provider_chronoLocalDate() {
 451         return new Object[][] {
 452             { HijrahDate.now() },
 453             { JapaneseDate.now() },
 454             { MinguoDate.now() },
 455             { ThaiBuddhistDate.now() }};
 456     }
 457 
 458     private static final DateTimeFormatter fmt_chrono =
 459         new DateTimeFormatterBuilder()
 460             .optionalStart()
 461             .appendChronologyId()
 462             .appendLiteral(' ')
 463             .optionalEnd()
 464             .optionalStart()
 465             .appendText(ChronoField.ERA, TextStyle.SHORT)


   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  */


 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     }


 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     }


 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)


< prev index next >