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) 2010-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_YEAR;
  65 import static java.time.temporal.ChronoField.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.format.DateTimeBuilder;
  71 import java.time.temporal.TemporalField;
  72 
  73 import org.testng.annotations.DataProvider;
  74 import org.testng.annotations.Test;
  75 
  76 /**
  77  * Test ReducedPrinterParser.
  78  */
  79 @Test(groups={"implementation"})
  80 public class TestReducedParser extends AbstractTestPrinterParser {
  81 
  82     private DateTimeFormatter getFormatter0(TemporalField field, int width, int baseValue) {
  83         return builder.appendValueReduced(field, width, baseValue).toFormatter(locale).withSymbols(symbols);
  84     }
  85 
  86     //-----------------------------------------------------------------------
  87     @DataProvider(name="error")
  88     Object[][] data_error() {
  89         return new Object[][] {
  90             {YEAR, 2, 2010, "12", -1, IndexOutOfBoundsException.class},
  91             {YEAR, 2, 2010, "12", 3, IndexOutOfBoundsException.class},
  92         };
  93     }
  94 
  95     @Test(dataProvider="error")
  96     public void test_parse_error(TemporalField field, int width, int baseValue, String text, int pos, Class<?> expected) {
  97         try {
  98             getFormatter0(field, width, baseValue).parseToBuilder(text, new ParsePosition(pos));
  99         } catch (RuntimeException ex) {
 100             assertTrue(expected.isInstance(ex));
 101         }
 102     }
 103 
 104     //-----------------------------------------------------------------------
 105     public void test_parse_fieldRangeIgnored() throws Exception {
 106         ParsePosition pos = new ParsePosition(0);
 107         DateTimeBuilder dtb = getFormatter0(DAY_OF_YEAR, 3, 10).parseToBuilder("456", pos);
 108         assertEquals(pos.getIndex(), 3);
 109         assertParsed(dtb, DAY_OF_YEAR, 456L);  // parsed dayOfYear=456
 110     }
 111 
 112     //-----------------------------------------------------------------------
 113     @DataProvider(name="Parse")
 114     Object[][] provider_parse() {
 115         return new Object[][] {
 116              // negative zero
 117             {YEAR, 1, 2010, "-0", 0, 0, null},
 118 
 119             // general
 120             {YEAR, 2, 2010, "Xxx12Xxx", 3, 5, 2012},
 121             {YEAR, 2, 2010, "12345", 0, 2, 2012},
 122             {YEAR, 2, 2010, "12-45", 0, 2, 2012},
 123 
 124             // insufficient digits
 125             {YEAR, 2, 2010, "0", 0, 0, null},
 126             {YEAR, 2, 2010, "1", 0, 0, null},
 127             {YEAR, 2, 2010, "1", 1, 1, null},
 128             {YEAR, 2, 2010, "1-2", 0, 0, null},
 129             {YEAR, 2, 2010, "9", 0, 0, null},
 130 
 131             // other junk
 132             {YEAR, 2, 2010, "A0", 0, 0, null},
 133             {YEAR, 2, 2010, "0A", 0, 0, null},
 134             {YEAR, 2, 2010, "  1", 0, 0, null},
 135             {YEAR, 2, 2010, "-1", 0, 0, null},
 136             {YEAR, 2, 2010, "-10", 0, 0, null},
 137 
 138             // parse OK 1
 139             {YEAR, 1, 2010, "0", 0, 1, 2010},
 140             {YEAR, 1, 2010, "9", 0, 1, 2019},
 141             {YEAR, 1, 2010, "10", 0, 1, 2011},
 142 
 143             {YEAR, 1, 2005, "0", 0, 1, 2010},
 144             {YEAR, 1, 2005, "4", 0, 1, 2014},
 145             {YEAR, 1, 2005, "5", 0, 1, 2005},
 146             {YEAR, 1, 2005, "9", 0, 1, 2009},
 147             {YEAR, 1, 2005, "10", 0, 1, 2011},
 148 
 149             // parse OK 2
 150             {YEAR, 2, 2010, "00", 0, 2, 2100},
 151             {YEAR, 2, 2010, "09", 0, 2, 2109},
 152             {YEAR, 2, 2010, "10", 0, 2, 2010},
 153             {YEAR, 2, 2010, "99", 0, 2, 2099},
 154             {YEAR, 2, 2010, "100", 0, 2, 2010},
 155 
 156             // parse OK 2
 157             {YEAR, 2, -2005, "05", 0, 2, -2005},
 158             {YEAR, 2, -2005, "00", 0, 2, -2000},
 159             {YEAR, 2, -2005, "99", 0, 2, -1999},
 160             {YEAR, 2, -2005, "06", 0, 2, -1906},
 161             {YEAR, 2, -2005, "100", 0, 2, -1910},
 162        };
 163     }
 164 
 165     @Test(dataProvider="Parse")
 166     public void test_parse(TemporalField field, int width, int baseValue, String input, int pos, int parseLen, Integer parseVal) {
 167         ParsePosition ppos = new ParsePosition(pos);
 168         DateTimeBuilder dtb = getFormatter0(field, width, baseValue).parseToBuilder(input, ppos);
 169         if (ppos.getErrorIndex() != -1) {
 170             assertEquals(ppos.getErrorIndex(), parseLen);
 171         } else {
 172             assertEquals(ppos.getIndex(), parseLen);
 173             assertParsed(dtb, YEAR, parseVal != null ? (long) parseVal : null);
 174         }
 175     }
 176 
 177     @Test(dataProvider="Parse")
 178     public void test_parseLenient(TemporalField field, int width, int baseValue, String input, int pos, int parseLen, Integer parseVal) {
 179         setStrict(false);
 180         ParsePosition ppos = new ParsePosition(pos);
 181         DateTimeBuilder dtb = getFormatter0(field, width, baseValue).parseToBuilder(input, ppos);
 182         if (ppos.getErrorIndex() != -1) {
 183             assertEquals(ppos.getErrorIndex(), parseLen);
 184         } else {
 185             assertEquals(ppos.getIndex(), parseLen);
 186             assertParsed(dtb, YEAR, parseVal != null ? (long) parseVal : null);
 187         }
 188     }
 189 
 190     private void assertParsed(DateTimeBuilder dtb, TemporalField field, Long value) {
 191         if (value == null) {
 192             assertEquals(dtb, null);
 193         } else {
 194             assertEquals(dtb.getLong(field), (long)value);
 195         }
 196     }
 197 
 198 }