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