1 /*
   2  * Copyright (c) 2012, 2018, 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) 2009-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 
  61 /*
  62  * @test
  63  * @modules jdk.localedata
  64  */
  65 package test.java.time.format;
  66 
  67 import java.time.chrono.ChronoLocalDate;
  68 import java.time.chrono.Chronology;
  69 import java.time.chrono.IsoChronology;
  70 import java.time.chrono.JapaneseChronology;
  71 import java.time.chrono.JapaneseEra;
  72 import java.time.chrono.MinguoChronology;
  73 import java.time.chrono.ThaiBuddhistChronology;
  74 import java.time.format.DateTimeFormatter;
  75 import java.time.format.DateTimeFormatterBuilder;
  76 import java.time.format.FormatStyle;
  77 import java.time.format.ResolverStyle;
  78 import java.time.LocalDate;
  79 import java.time.temporal.ChronoField;
  80 import java.time.temporal.Temporal;
  81 
  82 import java.util.HashMap;
  83 import java.util.Locale;
  84 import java.util.Map;
  85 
  86 import static org.testng.Assert.assertEquals;
  87 
  88 import org.testng.annotations.BeforeMethod;
  89 import org.testng.annotations.DataProvider;
  90 import org.testng.annotations.Test;
  91 
  92 /**
  93  * Test DateTimeFormatterBuilder.
  94  */
  95 @Test
  96 public class TestDateTimeFormatterBuilderWithLocale {
  97 
  98     private DateTimeFormatterBuilder builder;
  99 
 100     @BeforeMethod
 101     public void setUp() {
 102         builder = new DateTimeFormatterBuilder();
 103     }
 104 
 105     //-----------------------------------------------------------------------
 106     @DataProvider(name="patternPrint")
 107     Object[][] data_patternPrint() {
 108         return new Object[][] {
 109             {"Q", date(2012, 2, 10), "1"},
 110             {"QQ", date(2012, 2, 10), "01"},
 111             {"QQQ", date(2012, 2, 10), "Q1"},
 112             {"QQQQ", date(2012, 2, 10), "1st quarter"},
 113             {"QQQQQ", date(2012, 2, 10), "1"},
 114         };
 115     }
 116 
 117     @Test(dataProvider="patternPrint")
 118     public void test_appendPattern_patternPrint(String input, Temporal temporal, String expected) throws Exception {
 119         DateTimeFormatter f = builder.appendPattern(input).toFormatter(Locale.UK);
 120         String test = f.format(temporal);
 121         assertEquals(test, expected);
 122     }
 123 
 124     //-----------------------------------------------------------------------
 125     @DataProvider(name="mapTextLookup")
 126     Object[][] data_mapTextLookup() {
 127         return new Object[][] {
 128             {IsoChronology.INSTANCE.date(1, 1, 1), Locale.ENGLISH},
 129             {JapaneseChronology.INSTANCE.date(JapaneseEra.HEISEI, 1, 1, 8), Locale.ENGLISH},
 130             {MinguoChronology.INSTANCE.date(1, 1, 1), Locale.ENGLISH},
 131             {ThaiBuddhistChronology.INSTANCE.date(1, 1, 1), Locale.ENGLISH},
 132         };
 133     }
 134 
 135     @Test(dataProvider="mapTextLookup")
 136     public void test_appendText_mapTextLookup(ChronoLocalDate date, Locale locale) {
 137         final String new1st = "1st";
 138         final long firstYear = 1L;
 139         Map<Long, String> yearMap = new HashMap<>();
 140         yearMap.put(firstYear, new1st);
 141         builder.appendText(ChronoField.YEAR_OF_ERA, yearMap);
 142         DateTimeFormatter formatter = builder
 143             .toFormatter(locale)
 144             .withResolverStyle(ResolverStyle.STRICT);
 145 
 146         assertEquals(date.format(formatter), new1st);
 147         assertEquals(formatter.parse(new1st).getLong(ChronoField.YEAR_OF_ERA), firstYear);
 148     }
 149 
 150 
 151     //-----------------------------------------------------------------------
 152     @DataProvider(name="localePatterns")
 153     Object[][] localizedDateTimePatterns() {
 154         return new Object[][] {
 155             // French Locale and ISO Chronology
 156             {FormatStyle.FULL, FormatStyle.FULL, IsoChronology.INSTANCE, Locale.FRENCH, "EEEE d MMMM y '\u00e0' HH:mm:ss zzzz"},
 157             {FormatStyle.LONG, FormatStyle.LONG, IsoChronology.INSTANCE, Locale.FRENCH, "d MMMM y '\u00e0' HH:mm:ss z"},
 158             {FormatStyle.MEDIUM, FormatStyle.MEDIUM, IsoChronology.INSTANCE, Locale.FRENCH, "d MMM y '\u00e0' HH:mm:ss"},
 159             {FormatStyle.SHORT, FormatStyle.SHORT, IsoChronology.INSTANCE, Locale.FRENCH, "dd/MM/y HH:mm"},
 160             {FormatStyle.FULL, null, IsoChronology.INSTANCE, Locale.FRENCH, "EEEE d MMMM y"},
 161             {FormatStyle.LONG, null, IsoChronology.INSTANCE, Locale.FRENCH, "d MMMM y"},
 162             {FormatStyle.MEDIUM, null, IsoChronology.INSTANCE, Locale.FRENCH, "d MMM y"},
 163             {FormatStyle.SHORT, null, IsoChronology.INSTANCE, Locale.FRENCH, "dd/MM/y"},
 164             {null, FormatStyle.FULL, IsoChronology.INSTANCE, Locale.FRENCH, "HH:mm:ss zzzz"},
 165             {null, FormatStyle.LONG, IsoChronology.INSTANCE, Locale.FRENCH, "HH:mm:ss z"},
 166             {null, FormatStyle.MEDIUM, IsoChronology.INSTANCE, Locale.FRENCH, "HH:mm:ss"},
 167             {null, FormatStyle.SHORT, IsoChronology.INSTANCE, Locale.FRENCH, "HH:mm"},
 168 
 169             // Japanese Locale and JapaneseChronology
 170             {FormatStyle.FULL, FormatStyle.FULL, JapaneseChronology.INSTANCE, Locale.JAPANESE, "Gy\u5e74M\u6708d\u65e5EEEE H\u6642mm\u5206ss\u79d2 zzzz"},
 171             {FormatStyle.LONG, FormatStyle.LONG, JapaneseChronology.INSTANCE, Locale.JAPANESE, "Gy\u5e74M\u6708d\u65e5 H:mm:ss z"},
 172             {FormatStyle.MEDIUM, FormatStyle.MEDIUM, JapaneseChronology.INSTANCE, Locale.JAPANESE, "Gy\u5e74M\u6708d\u65e5 H:mm:ss"},
 173             {FormatStyle.SHORT, FormatStyle.SHORT, JapaneseChronology.INSTANCE, Locale.JAPANESE, "GGGGGy/M/d H:mm"},
 174             {FormatStyle.FULL, null, JapaneseChronology.INSTANCE, Locale.JAPANESE, "Gy\u5e74M\u6708d\u65e5EEEE"},
 175             {FormatStyle.LONG, null, JapaneseChronology.INSTANCE, Locale.JAPANESE, "Gy\u5e74M\u6708d\u65e5"},
 176             {FormatStyle.MEDIUM, null, JapaneseChronology.INSTANCE, Locale.JAPANESE, "Gy\u5e74M\u6708d\u65e5"},
 177             {FormatStyle.SHORT, null, JapaneseChronology.INSTANCE, Locale.JAPANESE, "GGGGGy/M/d"},
 178             {null, FormatStyle.FULL, JapaneseChronology.INSTANCE, Locale.JAPANESE, "H\u6642mm\u5206ss\u79d2 zzzz"},
 179             {null, FormatStyle.LONG, JapaneseChronology.INSTANCE, Locale.JAPANESE, "H:mm:ss z"},
 180             {null, FormatStyle.MEDIUM, JapaneseChronology.INSTANCE, Locale.JAPANESE, "H:mm:ss"},
 181             {null, FormatStyle.SHORT, JapaneseChronology.INSTANCE, Locale.JAPANESE, "H:mm"},
 182 
 183             // Chinese Local and Chronology
 184             {FormatStyle.FULL, FormatStyle.FULL, MinguoChronology.INSTANCE, Locale.CHINESE, "Gy\u5e74M\u6708d\u65e5EEEE zzzz ah:mm:ss"},
 185             {FormatStyle.LONG, FormatStyle.LONG, MinguoChronology.INSTANCE, Locale.CHINESE, "Gy\u5e74M\u6708d\u65e5 z ah:mm:ss"},
 186             {FormatStyle.MEDIUM, FormatStyle.MEDIUM, MinguoChronology.INSTANCE, Locale.CHINESE, "Gy\u5e74M\u6708d\u65e5 ah:mm:ss"},
 187             {FormatStyle.SHORT, FormatStyle.SHORT, MinguoChronology.INSTANCE, Locale.CHINESE, "Gyy/M/d ah:mm"},
 188             {FormatStyle.FULL, null, MinguoChronology.INSTANCE, Locale.CHINESE, "Gy\u5e74M\u6708d\u65e5EEEE"},
 189             {FormatStyle.LONG, null, MinguoChronology.INSTANCE, Locale.CHINESE, "Gy\u5e74M\u6708d\u65e5"},
 190             {FormatStyle.MEDIUM, null, MinguoChronology.INSTANCE, Locale.CHINESE, "Gy\u5e74M\u6708d\u65e5"},
 191             {FormatStyle.SHORT, null, MinguoChronology.INSTANCE, Locale.CHINESE, "Gyy/M/d"},
 192             {null, FormatStyle.FULL, MinguoChronology.INSTANCE, Locale.CHINESE, "zzzz ah:mm:ss"},
 193             {null, FormatStyle.LONG, MinguoChronology.INSTANCE, Locale.CHINESE, "z ah:mm:ss"},
 194             {null, FormatStyle.MEDIUM, MinguoChronology.INSTANCE, Locale.CHINESE, "ah:mm:ss"},
 195             {null, FormatStyle.SHORT, MinguoChronology.INSTANCE, Locale.CHINESE, "ah:mm"},
 196         };
 197     }
 198 
 199     @Test(dataProvider="localePatterns")
 200     public void test_getLocalizedDateTimePattern(FormatStyle dateStyle, FormatStyle timeStyle,
 201             Chronology chrono, Locale locale, String expected) {
 202         String actual = DateTimeFormatterBuilder.getLocalizedDateTimePattern(dateStyle, timeStyle, chrono, locale);
 203         assertEquals(actual, expected, "Pattern " + convertNonAscii(actual));
 204     }
 205 
 206     /**
 207      * Returns a string that includes non-ascii characters after expanding
 208      * the non-ascii characters to their Java language \\uxxxx form.
 209      * @param input an input string
 210      * @return the encoded string.
 211      */
 212     private String convertNonAscii(String input) {
 213         StringBuilder sb = new StringBuilder(input.length() * 6);
 214         for (int i = 0; i < input.length(); i++) {
 215             char ch = input.charAt(i);
 216             if (ch < 255) {
 217                 sb.append(ch);
 218             } else {
 219                 sb.append("\\u");
 220                 sb.append(Integer.toHexString(ch));
 221             }
 222         }
 223         return sb.toString();
 224     }
 225 
 226     private static Temporal date(int y, int m, int d) {
 227         return LocalDate.of(y, m, d);
 228     }
 229 }