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