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 java.time.format.*;
  63 
  64 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
  65 import static java.time.temporal.ChronoField.DAY_OF_WEEK;
  66 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  67 import static org.testng.Assert.assertEquals;
  68 import static org.testng.Assert.assertTrue;
  69 
  70 import java.text.ParsePosition;
  71 import java.util.Locale;
  72 import java.time.format.DateTimeBuilder;
  73 import java.time.temporal.TemporalField;
  74 
  75 import org.testng.annotations.DataProvider;
  76 import org.testng.annotations.Test;
  77 
  78 /**
  79  * Test TextPrinterParser.
  80  */
  81 @Test(groups={"implementation"})
  82 public class TestTextParser extends AbstractTestPrinterParser {
  83 
  84     //-----------------------------------------------------------------------
  85     @DataProvider(name="error")
  86     Object[][] data_error() {
  87         return new Object[][] {
  88             {DAY_OF_WEEK, TextStyle.FULL, "Monday", -1, IndexOutOfBoundsException.class},
  89             {DAY_OF_WEEK, TextStyle.FULL, "Monday", 7, IndexOutOfBoundsException.class},
  90         };
  91     }
  92 
  93     @Test(dataProvider="error")
  94     public void test_parse_error(TemporalField field, TextStyle style, String text, int pos, Class<?> expected) {
  95         try {
  96             getFormatter(field, style).parseToBuilder(text, new ParsePosition(pos));
  97         } catch (RuntimeException ex) {
  98             assertTrue(expected.isInstance(ex));
  99         }
 100     }
 101 
 102     //-----------------------------------------------------------------------
 103     public void test_parse_midStr() throws Exception {
 104         ParsePosition pos = new ParsePosition(3);
 105         assertEquals(getFormatter(DAY_OF_WEEK, TextStyle.FULL)
 106                      .parseToBuilder("XxxMondayXxx", pos)
 107                      .getLong(DAY_OF_WEEK), 1L);
 108         assertEquals(pos.getIndex(), 9);
 109     }
 110 
 111     public void test_parse_remainderIgnored() throws Exception {
 112         ParsePosition pos = new ParsePosition(0);
 113         assertEquals(getFormatter(DAY_OF_WEEK, TextStyle.SHORT)
 114                      .parseToBuilder("Wednesday", pos)
 115                      .getLong(DAY_OF_WEEK), 3L);
 116         assertEquals(pos.getIndex(), 3);
 117     }
 118 
 119     //-----------------------------------------------------------------------
 120     public void test_parse_noMatch1() throws Exception {
 121         ParsePosition pos = new ParsePosition(0);
 122         DateTimeBuilder dtb =
 123             getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseToBuilder("Munday", pos);
 124         assertEquals(pos.getErrorIndex(), 0);
 125     }
 126 
 127     public void test_parse_noMatch2() throws Exception {
 128         ParsePosition pos = new ParsePosition(3);
 129         DateTimeBuilder dtb =
 130             getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseToBuilder("Monday", pos);
 131         assertEquals(pos.getErrorIndex(), 3);
 132     }
 133 
 134     public void test_parse_noMatch_atEnd() throws Exception {
 135         ParsePosition pos = new ParsePosition(6);
 136         DateTimeBuilder dtb =
 137             getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseToBuilder("Monday", pos);
 138         assertEquals(pos.getErrorIndex(), 6);
 139     }
 140 
 141     //-----------------------------------------------------------------------
 142     @DataProvider(name="parseText")
 143     Object[][] provider_text() {
 144         return new Object[][] {
 145             {DAY_OF_WEEK, TextStyle.FULL, 1, "Monday"},
 146             {DAY_OF_WEEK, TextStyle.FULL, 2, "Tuesday"},
 147             {DAY_OF_WEEK, TextStyle.FULL, 3, "Wednesday"},
 148             {DAY_OF_WEEK, TextStyle.FULL, 4, "Thursday"},
 149             {DAY_OF_WEEK, TextStyle.FULL, 5, "Friday"},
 150             {DAY_OF_WEEK, TextStyle.FULL, 6, "Saturday"},
 151             {DAY_OF_WEEK, TextStyle.FULL, 7, "Sunday"},
 152 
 153             {DAY_OF_WEEK, TextStyle.SHORT, 1, "Mon"},
 154             {DAY_OF_WEEK, TextStyle.SHORT, 2, "Tue"},
 155             {DAY_OF_WEEK, TextStyle.SHORT, 3, "Wed"},
 156             {DAY_OF_WEEK, TextStyle.SHORT, 4, "Thu"},
 157             {DAY_OF_WEEK, TextStyle.SHORT, 5, "Fri"},
 158             {DAY_OF_WEEK, TextStyle.SHORT, 6, "Sat"},
 159             {DAY_OF_WEEK, TextStyle.SHORT, 7, "Sun"},
 160 
 161             {MONTH_OF_YEAR, TextStyle.FULL, 1, "January"},
 162             {MONTH_OF_YEAR, TextStyle.FULL, 12, "December"},
 163 
 164             {MONTH_OF_YEAR, TextStyle.SHORT, 1, "Jan"},
 165             {MONTH_OF_YEAR, TextStyle.SHORT, 12, "Dec"},
 166        };
 167     }
 168 
 169     @DataProvider(name="parseNumber")
 170     Object[][] provider_number() {
 171         return new Object[][] {
 172             {DAY_OF_MONTH, TextStyle.FULL, 1, "1"},
 173             {DAY_OF_MONTH, TextStyle.FULL, 2, "2"},
 174             {DAY_OF_MONTH, TextStyle.FULL, 30, "30"},
 175             {DAY_OF_MONTH, TextStyle.FULL, 31, "31"},
 176 
 177             {DAY_OF_MONTH, TextStyle.SHORT, 1, "1"},
 178             {DAY_OF_MONTH, TextStyle.SHORT, 2, "2"},
 179             {DAY_OF_MONTH, TextStyle.SHORT, 30, "30"},
 180             {DAY_OF_MONTH, TextStyle.SHORT, 31, "31"},
 181        };
 182     }
 183 
 184     @Test(dataProvider="parseText")
 185     public void test_parseText(TemporalField field, TextStyle style, int value, String input) throws Exception {
 186         ParsePosition pos = new ParsePosition(0);
 187         assertEquals(getFormatter(field, style).parseToBuilder(input, pos).getLong(field), (long) value);
 188         assertEquals(pos.getIndex(), input.length());
 189     }
 190 
 191     @Test(dataProvider="parseNumber")
 192     public void test_parseNumber(TemporalField field, TextStyle style, int value, String input) throws Exception {
 193         ParsePosition pos = new ParsePosition(0);
 194         assertEquals(getFormatter(field, style).parseToBuilder(input, pos).getLong(field), (long) value);
 195         assertEquals(pos.getIndex(), input.length());
 196     }
 197 
 198     //-----------------------------------------------------------------------
 199     @Test(dataProvider="parseText")
 200     public void test_parse_strict_caseSensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception {
 201         setCaseSensitive(true);
 202         ParsePosition pos = new ParsePosition(0);
 203         getFormatter(field, style).parseToBuilder(input.toUpperCase(), pos);
 204         assertEquals(pos.getErrorIndex(), 0);
 205     }
 206 
 207     @Test(dataProvider="parseText")
 208     public void test_parse_strict_caseInsensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception {
 209         setCaseSensitive(false);
 210         ParsePosition pos = new ParsePosition(0);
 211         assertEquals(getFormatter(field, style).parseToBuilder(input.toUpperCase(), pos).getLong(field), (long) value);
 212         assertEquals(pos.getIndex(), input.length());
 213     }
 214 
 215     //-----------------------------------------------------------------------
 216     @Test(dataProvider="parseText")
 217     public void test_parse_strict_caseSensitive_parseLower(TemporalField field, TextStyle style, int value, String input) throws Exception {
 218         setCaseSensitive(true);
 219         ParsePosition pos = new ParsePosition(0);
 220         getFormatter(field, style).parseToBuilder(input.toLowerCase(), pos);
 221         assertEquals(pos.getErrorIndex(), 0);
 222     }
 223 
 224     @Test(dataProvider="parseText")
 225     public void test_parse_strict_caseInsensitive_parseLower(TemporalField field, TextStyle style, int value, String input) throws Exception {
 226         setCaseSensitive(false);
 227         ParsePosition pos = new ParsePosition(0);
 228         assertEquals(getFormatter(field, style).parseToBuilder(input.toLowerCase(), pos).getLong(field), (long) value);
 229         assertEquals(pos.getIndex(), input.length());
 230     }
 231 
 232     //-----------------------------------------------------------------------
 233     //-----------------------------------------------------------------------
 234     //-----------------------------------------------------------------------
 235     public void test_parse_full_strict_full_match() throws Exception {
 236         setStrict(true);
 237         ParsePosition pos = new ParsePosition(0);
 238         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseToBuilder("January", pos).getLong(MONTH_OF_YEAR), 1L);
 239         assertEquals(pos.getIndex(), 7);
 240     }
 241 
 242     public void test_parse_full_strict_short_noMatch() throws Exception {
 243         setStrict(true);
 244         ParsePosition pos = new ParsePosition(0);
 245         getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseToBuilder("Janua", pos);
 246         assertEquals(pos.getErrorIndex(), 0);
 247     }
 248 
 249     public void test_parse_full_strict_number_noMatch() throws Exception {
 250         setStrict(true);
 251         ParsePosition pos = new ParsePosition(0);
 252         getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseToBuilder("1", pos);
 253         assertEquals(pos.getErrorIndex(), 0);
 254     }
 255 
 256     //-----------------------------------------------------------------------
 257     public void test_parse_short_strict_full_match() throws Exception {
 258         setStrict(true);
 259         ParsePosition pos = new ParsePosition(0);
 260         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseToBuilder("January", pos).getLong(MONTH_OF_YEAR), 1L);
 261         assertEquals(pos.getIndex(), 3);
 262     }
 263 
 264     public void test_parse_short_strict_short_match() throws Exception {
 265         setStrict(true);
 266         ParsePosition pos = new ParsePosition(0);
 267         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseToBuilder("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
 268         assertEquals(pos.getIndex(), 3);
 269     }
 270 
 271     public void test_parse_short_strict_number_noMatch() throws Exception {
 272         setStrict(true);
 273         ParsePosition pos = new ParsePosition(0);
 274         getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseToBuilder("1", pos);
 275         assertEquals(pos.getErrorIndex(), 0);
 276     }
 277 
 278     //-----------------------------------------------------------------------
 279     public void test_parse_french_short_strict_full_noMatch() throws Exception {
 280         setStrict(true);
 281         ParsePosition pos = new ParsePosition(0);
 282         getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).withLocale(Locale.FRENCH)
 283                                                     .parseToBuilder("janvier", pos);
 284         assertEquals(pos.getErrorIndex(), 0);
 285     }
 286 
 287     public void test_parse_french_short_strict_short_match() throws Exception {
 288         setStrict(true);
 289         ParsePosition pos = new ParsePosition(0);
 290         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).withLocale(Locale.FRENCH)
 291                                                                  .parseToBuilder("janv.", pos)
 292                                                                  .getLong(MONTH_OF_YEAR),
 293                      1L);
 294         assertEquals(pos.getIndex(), 5);
 295     }
 296 
 297     //-----------------------------------------------------------------------
 298     public void test_parse_full_lenient_full_match() throws Exception {
 299         setStrict(false);
 300         ParsePosition pos = new ParsePosition(0);
 301         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseToBuilder("January.", pos).getLong(MONTH_OF_YEAR), 1L);
 302         assertEquals(pos.getIndex(), 7);
 303     }
 304 
 305     public void test_parse_full_lenient_short_match() throws Exception {
 306         setStrict(false);
 307         ParsePosition pos = new ParsePosition(0);
 308         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseToBuilder("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
 309         assertEquals(pos.getIndex(), 3);
 310     }
 311 
 312     public void test_parse_full_lenient_number_match() throws Exception {
 313         setStrict(false);
 314         ParsePosition pos = new ParsePosition(0);
 315         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseToBuilder("1", pos).getLong(MONTH_OF_YEAR), 1L);
 316         assertEquals(pos.getIndex(), 1);
 317     }
 318 
 319     //-----------------------------------------------------------------------
 320     public void test_parse_short_lenient_full_match() throws Exception {
 321         setStrict(false);
 322         ParsePosition pos = new ParsePosition(0);
 323         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseToBuilder("January", pos).getLong(MONTH_OF_YEAR), 1L);
 324         assertEquals(pos.getIndex(), 7);
 325     }
 326 
 327     public void test_parse_short_lenient_short_match() throws Exception {
 328         setStrict(false);
 329         ParsePosition pos = new ParsePosition(0);
 330         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseToBuilder("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
 331         assertEquals(pos.getIndex(), 3);
 332     }
 333 
 334     public void test_parse_short_lenient_number_match() throws Exception {
 335         setStrict(false);
 336         ParsePosition pos = new ParsePosition(0);
 337         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseToBuilder("1", pos).getLong(MONTH_OF_YEAR), 1L);
 338         assertEquals(pos.getIndex(), 1);
 339     }
 340 
 341 }