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) 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.YEAR;
     63 import static org.testng.Assert.assertEquals;
     64 import static org.testng.Assert.fail;
     65 
     66 import java.time.DateTimeException;
     67 import java.time.LocalDate;
     68 import java.time.chrono.MinguoDate;
     69 import java.time.format.DateTimeFormatter;
     70 import java.time.format.DateTimeFormatterBuilder;
     71 import java.time.temporal.TemporalField;
     72 
     73 import org.testng.annotations.DataProvider;
     74 import org.testng.annotations.Test;
     75 import test.java.time.temporal.MockFieldValue;
     76 
     77 /**
     78  * Test ReducedPrinterParser.
     79  */
     80 @Test
     81 public class TestReducedPrinter extends AbstractTestPrinterParser {
     82 
     83     private DateTimeFormatter getFormatter0(TemporalField field, int width, int baseValue) {
     84         return builder.appendValueReduced(field, width, width, baseValue).toFormatter(locale).withDecimalStyle(decimalStyle);
     85     }
     86 
     87     private DateTimeFormatter getFormatter0(TemporalField field, int minWidth, int maxWidth, int baseValue) {
     88         return builder.appendValueReduced(field, minWidth, maxWidth, baseValue).toFormatter(locale).withDecimalStyle(decimalStyle);
     89     }
     90 
     91     private DateTimeFormatter getFormatterBaseDate(TemporalField field, int minWidth, int maxWidth, int baseValue) {
     92         return builder.appendValueReduced(field, minWidth, maxWidth, LocalDate.of(baseValue, 1, 1)).toFormatter(locale).withDecimalStyle(decimalStyle);
     93     }
     94 
     95     //-----------------------------------------------------------------------
     96     @Test(expectedExceptions=DateTimeException.class)
     97     public void test_print_emptyCalendrical() throws Exception {
     98         getFormatter0(YEAR, 2, 2010).formatTo(EMPTY_DTA, buf);
     99     }
    100 
    101     //-----------------------------------------------------------------------
    102     public void test_print_append() throws Exception {
    103         buf.append("EXISTING");
    104         getFormatter0(YEAR, 2, 2010).formatTo(LocalDate.of(2012, 1, 1), buf);
    105         assertEquals(buf.toString(), "EXISTING12");
    106     }
    107 
    108     //-----------------------------------------------------------------------
    109     @DataProvider(name="Pivot")
    110     Object[][] provider_pivot() {
    111         return new Object[][] {
    112             {1, 1, 2010, 2010, "0"},
    113             {1, 1, 2010, 2011, "1"},
    114             {1, 1, 2010, 2012, "2"},
    115             {1, 1, 2010, 2013, "3"},
    116             {1, 1, 2010, 2014, "4"},
    117             {1, 1, 2010, 2015, "5"},
    118             {1, 1, 2010, 2016, "6"},
    119             {1, 1, 2010, 2017, "7"},
    120             {1, 1, 2010, 2018, "8"},
    121             {1, 1, 2010, 2019, "9"},
    122             {1, 1, 2010, 2009, "9"},
    123             {1, 1, 2010, 2020, "0"},
    124 
    125             {2, 2, 2010, 2010, "10"},
    126             {2, 2, 2010, 2011, "11"},
    127             {2, 2, 2010, 2021, "21"},
    128             {2, 2, 2010, 2099, "99"},
    129             {2, 2, 2010, 2100, "00"},
    130             {2, 2, 2010, 2109, "09"},
    131             {2, 2, 2010, 2009, "09"},
    132             {2, 2, 2010, 2110, "10"},
    133 
    134             {2, 2, 2005, 2005, "05"},
    135             {2, 2, 2005, 2099, "99"},
    136             {2, 2, 2005, 2100, "00"},
    137             {2, 2, 2005, 2104, "04"},
    138             {2, 2, 2005, 2004, "04"},
    139             {2, 2, 2005, 2105, "05"},
    140 
    141             {3, 3, 2005, 2005, "005"},
    142             {3, 3, 2005, 2099, "099"},
    143             {3, 3, 2005, 2100, "100"},
    144             {3, 3, 2005, 2999, "999"},
    145             {3, 3, 2005, 3000, "000"},
    146             {3, 3, 2005, 3004, "004"},
    147             {3, 3, 2005, 2004, "004"},
    148             {3, 3, 2005, 3005, "005"},
    149 
    150             {9, 9, 2005, 2005, "000002005"},
    151             {9, 9, 2005, 2099, "000002099"},
    152             {9, 9, 2005, 2100, "000002100"},
    153             {9, 9, 2005, 999999999, "999999999"},
    154             {9, 9, 2005, 1000000000, "000000000"},
    155             {9, 9, 2005, 1000002004, "000002004"},
    156             {9, 9, 2005, 2004, "000002004"},
    157             {9, 9, 2005, 1000002005, "000002005"},
    158 
    159             {2, 2, -2005, -2005, "05"},
    160             {2, 2, -2005, -2000, "00"},
    161             {2, 2, -2005, -1999, "99"},
    162             {2, 2, -2005, -1904, "04"},
    163             {2, 2, -2005, -2006, "06"},
    164             {2, 2, -2005, -1905, "05"},
    165 
    166             {2, 4, 2005, 2099, "99"},
    167             {2, 4, 2005, 2100, "00"},
    168             {2, 4, 2005, 9999, "9999"},
    169             {2, 4, 2005, 1000000000, "00"},
    170             {2, 4, 2005, 1000002004, "2004"},
    171             {2, 4, 2005, 2004, "2004"},
    172             {2, 4, 2005, 2005, "05"},
    173             {2, 4, 2005, 2104, "04"},
    174             {2, 4, 2005, 2105, "2105"},
    175         };
    176     }
    177 
    178     @Test(dataProvider="Pivot")
    179     public void test_pivot(int minWidth, int maxWidth, int baseValue, int value, String result) throws Exception {
    180         try {
    181             getFormatter0(YEAR, minWidth, maxWidth, baseValue).formatTo(new MockFieldValue(YEAR, value), buf);
    182             if (result == null) {
    183                 fail("Expected exception");
    184             }
    185             assertEquals(buf.toString(), result);
    186         } catch (DateTimeException ex) {
    187             if (result == null || value < 0) {
    188                 assertEquals(ex.getMessage().contains(YEAR.toString()), true);
    189             } else {
    190                 throw ex;
    191             }
    192         }
    193     }
    194 
    195     @Test(dataProvider="Pivot")
    196     public void test_pivot_baseDate(int minWidth, int maxWidth, int baseValue, int value, String result) throws Exception {
    197         try {
    198             getFormatterBaseDate(YEAR, minWidth, maxWidth, baseValue).formatTo(new MockFieldValue(YEAR, value), buf);
    199             if (result == null) {
    200                 fail("Expected exception");
    201             }
    202             assertEquals(buf.toString(), result);
    203         } catch (DateTimeException ex) {
    204             if (result == null || value < 0) {
    205                 assertEquals(ex.getMessage().contains(YEAR.toString()), true);
    206             } else {
    207                 throw ex;
    208             }
    209         }
    210     }
    211 
    212     //-----------------------------------------------------------------------
    213     public void test_minguoChrono_fixedWidth() throws Exception {
    214         // ISO 2021 is Minguo 110
    215         DateTimeFormatter f = getFormatterBaseDate(YEAR, 2, 2, 2021);
    216         MinguoDate date = MinguoDate.of(109, 6, 30);
    217         assertEquals(f.format(date), "09");
    218         date = MinguoDate.of(110, 6, 30);
    219         assertEquals(f.format(date), "10");
    220         date = MinguoDate.of(199, 6, 30);
    221         assertEquals(f.format(date), "99");
    222         date = MinguoDate.of(200, 6, 30);
    223         assertEquals(f.format(date), "00");
    224         date = MinguoDate.of(209, 6, 30);
    225         assertEquals(f.format(date), "09");
    226         date = MinguoDate.of(210, 6, 30);
    227         assertEquals(f.format(date), "10");
    228     }
    229 
    230     public void test_minguoChrono_extendedWidth() throws Exception {
    231         // ISO 2021 is Minguo 110
    232         DateTimeFormatter f = getFormatterBaseDate(YEAR, 2, 4, 2021);
    233         MinguoDate date = MinguoDate.of(109, 6, 30);
    234         assertEquals(f.format(date), "109");
    235         date = MinguoDate.of(110, 6, 30);
    236         assertEquals(f.format(date), "10");
    237         date = MinguoDate.of(199, 6, 30);
    238         assertEquals(f.format(date), "99");
    239         date = MinguoDate.of(200, 6, 30);
    240         assertEquals(f.format(date), "00");
    241         date = MinguoDate.of(209, 6, 30);
    242         assertEquals(f.format(date), "09");
    243         date = MinguoDate.of(210, 6, 30);
    244         assertEquals(f.format(date), "210");
    245     }
    246 
    247     //-----------------------------------------------------------------------
    248     public void test_toString() throws Exception {
    249         assertEquals(getFormatter0(YEAR, 2, 2, 2005).toString(), "ReducedValue(Year,2,2,2005)");
    250     }
    251 
    252     //-----------------------------------------------------------------------
    253     // Cases and values in adjacent parsing mode
    254     //-----------------------------------------------------------------------
    255     @DataProvider(name="PrintAdjacent")
    256     Object[][] provider_printAdjacent() {
    257         return new Object[][] {
    258             // general
    259             {"yyMMdd", "990703",   1999, 7, 3},
    260             {"yyMMdd", "990703",   2099, 7, 3},
    261             {"yyMMdd", "200703",   2020, 7, 3},
    262             {"ddMMyy", "030714",   2014, 7, 3},
    263             {"ddMMyy", "030720",   2020, 7, 3},
    264             {"ddMMy",  "03072001", 2001, 7, 3},
    265         };
    266     }
    267 
    268     @Test(dataProvider="PrintAdjacent")
    269     public void test_printAdjacent(String pattern, String text, int year, int month, int day) {
    270         builder = new DateTimeFormatterBuilder();
    271         builder.appendPattern(pattern);
    272         DateTimeFormatter dtf = builder.toFormatter();
    273 
    274         LocalDate ld = LocalDate.of(year, month, day);
    275         String actual = dtf.format(ld);
    276         assertEquals(actual, text, "formatter output: " + dtf);
    277     }
    278 
    279 }
    280