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