1 /*
   2  * Copyright (c) 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 package test.java.time.format;
  24 
  25 import java.time.*;
  26 import java.time.chrono.*;
  27 import java.time.format.*;
  28 import java.time.temporal.*;
  29 import java.util.Locale;
  30 
  31 import org.testng.annotations.BeforeMethod;
  32 import org.testng.annotations.DataProvider;
  33 import org.testng.annotations.Test;
  34 import static org.testng.Assert.assertEquals;
  35 
  36 /**
  37  * Test DateTimeFormatter with non-ISO chronology.
  38  *
  39  * Strings in test data are all dependent on CLDR data which may change
  40  * in future CLDR releases.
  41  */
  42 @Test(groups={"implementation"})
  43 public class TestNonIsoFormatter {
  44     private static final Chronology JAPANESE = JapaneseChronology.INSTANCE;
  45     private static final Chronology HIJRAH = HijrahChronology.INSTANCE;
  46     private static final Chronology MINGUO = MinguoChronology.INSTANCE;
  47     private static final Chronology BUDDHIST = ThaiBuddhistChronology.INSTANCE;
  48 
  49     private static final LocalDate IsoDate = LocalDate.of(2013, 2, 11);
  50 
  51     private static final Locale ARABIC = new Locale("ar");
  52     private static final Locale thTH = new Locale("th", "TH");
  53     private static final Locale thTHTH = new Locale("th", "TH", "TH");
  54 
  55     @BeforeMethod
  56     public void setUp() {
  57     }
  58 
  59     @DataProvider(name="format_data")
  60     Object[][] formatData() {
  61         return new Object[][] {
  62             // Chronology, Locale, ChronoLocalDate, expected string
  63             { JAPANESE, Locale.JAPANESE, JAPANESE.date(IsoDate),
  64               "\u5e73\u621025\u5e742\u670811\u65e5\u6708\u66dc\u65e5" }, // Japanese Heisei 25-02-11 (Mon)
  65             { HIJRAH, ARABIC, HIJRAH.date(IsoDate),
  66               "\u0627\u0644\u0627\u062b\u0646\u064a\u0646\u060c 30 \u0631\u0628\u064a\u0639 "
  67               + "\u0627\u0644\u0623\u0648\u0644 1434" }, // Hijrah AH 1434-03-30 (Mon)
  68             { MINGUO, Locale.TAIWAN, MINGUO.date(IsoDate),
  69               "\u6c11\u570b102\u5e742\u670811\u65e5\u661f\u671f\u4e00" }, // Minguo ROC 102-02-11 (Mon)
  70             { BUDDHIST, thTH, BUDDHIST.date(IsoDate),
  71               "\u0e27\u0e31\u0e19\u0e08\u0e31\u0e19\u0e17\u0e23\u0e4c\u0e17\u0e35\u0e48"
  72               + " 11 \u0e01\u0e38\u0e21\u0e20\u0e32\u0e1e\u0e31\u0e19\u0e18\u0e4c"
  73               + " \u0e1e.\u0e28. 2556" }, // ThaiBuddhist BE 2556-02-11
  74          // { BUDDHIST, thTHTH, BUDDHIST.date(IsoDate), "<TBS>" }, // doesn't work
  75         };
  76     }
  77 
  78     @DataProvider(name="invalid_text")
  79     Object[][] invalidText() {
  80         return new Object[][] {
  81             // TODO: currently fixed Chronology and Locale.
  82             { "\u662d\u548c64\u5e741\u67089\u65e5\u6708\u66dc\u65e5" }, // S64.01.09 (Mon)
  83             { "\u662d\u548c65\u5e741\u67081\u65e5\u6708\u66dc\u65e5" }, // S65.01.01 (Mon)
  84         };
  85     }
  86 
  87     @Test(dataProvider="format_data")
  88     public void test_formatLocalizedDate(Chronology chrono, Locale locale, ChronoLocalDate<?> date, String expected) {
  89         DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
  90             .withChronology(chrono).withLocale(locale);
  91         String text = dtf.format(date);
  92         assertEquals(text, expected);
  93     }
  94 
  95     @Test(dataProvider="format_data")
  96     public void test_parseLocalizedText(Chronology chrono, Locale locale, ChronoLocalDate<?> expected, String text) {
  97         DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
  98             .withChronology(chrono).withLocale(locale);
  99         TemporalAccessor temporal = dtf.parse(text);
 100         ChronoLocalDate<?> date = chrono.date(temporal);
 101         assertEquals(date, expected);
 102     }
 103 
 104     @Test(dataProvider="invalid_text", expectedExceptions=DateTimeParseException.class)
 105     public void test_parseInvalidText(String text) {
 106         DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
 107             .withChronology(JAPANESE).withLocale(Locale.JAPANESE);
 108         TemporalAccessor temporal = dtf.parse(text);
 109     }
 110 }