1 /*
   2  * Copyright (c) 2014, 2017, 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 import java.text.SimpleDateFormat;
  25 import java.time.chrono.JapaneseChronology;
  26 import java.time.chrono.JapaneseDate;
  27 import java.time.chrono.JapaneseEra;
  28 import java.time.format.DateTimeFormatter;
  29 import java.time.format.TextStyle;
  30 import java.util.Calendar;
  31 import java.util.Date;
  32 import java.util.GregorianCalendar;
  33 import static java.util.GregorianCalendar.*;
  34 import java.util.Locale;
  35 import java.util.TimeZone;
  36 
  37 /*
  38  * Usage:
  39  *   java SupplementalJapaneseEraTest <flag>
  40  *    <flag>
  41  *      -s   prints start time for a test era
  42  *      -e   prints the English name of the last predefined era
  43  *
  44  *   java -Djdk.calendar.japanese.supplemental.era=... SupplementalJapaneseEraTest <flag>
  45  *      -t   executes tests with a valid property value
  46  *      -b <eraname>
  47  *           executes tests with an invalid property value
  48  *           <eraname> must be the output with -e
  49  */
  50 
  51 public class SupplementalJapaneseEraTest {
  52     private static final Locale WAREKI_LOCALE = Locale.forLanguageTag("ja-JP-u-ca-japanese");
  53     private static final String NEW_ERA_NAME = "NewEra";
  54     private static final String NEW_ERA_ABBR = "N.E.";
  55     private static int errors = 0;
  56 
  57     public static void main(String[] args) {
  58         // args[0] is a flag.
  59         switch (args[0]) {
  60         case "-s":
  61             // print the start time of the new era for testing
  62             Calendar cal = new Calendar.Builder()
  63                 .setCalendarType("japanese")
  64                 .setTimeZone(TimeZone.getTimeZone("GMT"))
  65                 .setDate(200, FEBRUARY, 11)
  66                 .build();
  67             System.out.println(cal.getTimeInMillis());
  68             break;
  69 
  70         case "-e":
  71             // print the current era name in English
  72             Calendar jcal = new Calendar.Builder()
  73                 .setCalendarType("japanese")
  74                 .setFields(YEAR, 1, DAY_OF_YEAR, 1)
  75                 .build();
  76             System.out.println(jcal.getDisplayName(ERA, LONG, Locale.US));
  77             break;
  78 
  79         case "-t":
  80             // test with a valid property value
  81             testProperty();
  82             break;
  83 
  84         case "-b":
  85             // test with an invalid property value
  86             // args[1] is the current era name given by -e.
  87             testValidation(args[1].replace("\r", "")); // remove any CR for Cygwin
  88             break;
  89         }
  90         if (errors != 0) {
  91             throw new RuntimeException("test failed");
  92         }
  93     }
  94 
  95     private static void testProperty() {
  96         Calendar jcal = new Calendar.Builder()
  97             .setCalendarType("japanese")
  98             .setFields(YEAR, 1, DAY_OF_YEAR, 1)
  99             .build();
 100         Date firstDayOfEra = jcal.getTime();
 101 
 102         jcal.set(ERA, jcal.get(ERA) - 1); // previous era
 103         jcal.set(YEAR, 1);
 104         jcal.set(DAY_OF_YEAR, 1);
 105         Calendar cal = new GregorianCalendar();
 106         cal.setTimeInMillis(jcal.getTimeInMillis());
 107         cal.add(YEAR, 199);
 108         int year = cal.get(YEAR);
 109 
 110         SimpleDateFormat sdf;
 111         String expected, got;
 112 
 113         // test long era name
 114         sdf = new SimpleDateFormat("GGGG y-MM-dd", WAREKI_LOCALE);
 115         got = sdf.format(firstDayOfEra);
 116         expected = NEW_ERA_NAME + " 1-02-11";
 117         if (!expected.equals(got)) {
 118             System.err.printf("GGGG y-MM-dd: got=\"%s\", expected=\"%s\"%n", got, expected);
 119             errors++;
 120         }
 121 
 122         // test era abbreviation
 123         sdf = new SimpleDateFormat("G y-MM-dd", WAREKI_LOCALE);
 124         got = sdf.format(firstDayOfEra);
 125         expected = NEW_ERA_ABBR+" 1-02-11";
 126         if (!expected.equals(got)) {
 127             System.err.printf("G y-MM-dd: got=\"%s\", expected=\"%s\"%n", got, expected);
 128             errors++;
 129         }
 130 
 131         // confirm the gregorian year
 132         sdf = new SimpleDateFormat("y", Locale.US);
 133         int y = Integer.parseInt(sdf.format(firstDayOfEra));
 134         if (y != year) {
 135             System.err.printf("Gregorian year: got=%d, expected=%d%n", y, year);
 136             errors++;
 137         }
 138 
 139         // test java.time.chrono.JapaneseEra
 140         JapaneseDate jdate = JapaneseDate.of(year, 2, 11);
 141         got = jdate.toString();
 142         expected = "Japanese " + NEW_ERA_NAME + " 1-02-11";
 143         if (!expected.equals(got)) {
 144             System.err.printf("JapaneseDate: got=\"%s\", expected=\"%s\"%n", got, expected);
 145             errors++;
 146         }
 147         JapaneseEra jera = jdate.getEra();
 148         got = jera.getDisplayName(TextStyle.FULL, Locale.US);
 149         if (!NEW_ERA_NAME.equals(got)) {
 150             System.err.printf("JapaneseEra (FULL): got=\"%s\", expected=\"%s\"%n", got, NEW_ERA_NAME);
 151             errors++;
 152         }
 153         got = jera.getDisplayName(TextStyle.SHORT, Locale.US);
 154         if (!NEW_ERA_NAME.equals(got)) {
 155             System.err.printf("JapaneseEra (SHORT): got=\"%s\", expected=\"%s\"%n", got, NEW_ERA_NAME);
 156             errors++;
 157         }
 158         got = jera.getDisplayName(TextStyle.NARROW, Locale.US);
 159         if (!NEW_ERA_ABBR.equals(got)) {
 160             System.err.printf("JapaneseEra (NARROW): got=\"%s\", expected=\"%s\"%n", got, NEW_ERA_ABBR);
 161             errors++;
 162         }
 163         got = jera.getDisplayName(TextStyle.NARROW_STANDALONE, Locale.US);
 164         if (!NEW_ERA_ABBR.equals(got)) {
 165             System.err.printf("JapaneseEra (NARROW_STANDALONE): got=\"%s\", expected=\"%s\"%n", got, NEW_ERA_ABBR);
 166             errors++;
 167         }
 168 
 169         // test full/short/narrow names with java.time.format
 170         got = DateTimeFormatter
 171             .ofPattern("GGGG G GGGGG")
 172             .withLocale(Locale.US)
 173             .withChronology(JapaneseChronology.INSTANCE)
 174             .format(jdate);
 175         expected = NEW_ERA_NAME + " " + NEW_ERA_NAME + " " + NEW_ERA_ABBR;
 176         if (!expected.equals(got)) {
 177             System.err.printf("java.time formatter full/short/narrow names: got=\"%s\", expected=\"%s\"%n", got, expected);
 178             errors++;
 179         }
 180     }
 181 
 182     private static void testValidation(String eraName) {
 183         Calendar jcal = new Calendar.Builder()
 184             .setCalendarType("japanese")
 185             .setFields(YEAR, 1, DAY_OF_YEAR, 1)
 186             .build();
 187         if (!jcal.getDisplayName(ERA, LONG, Locale.US).equals(eraName)) {
 188             errors++;
 189             String prop = System.getProperty("jdk.calendar.japanese.supplemental.era");
 190             System.err.println("Era changed with invalid property: " + prop);
 191         }
 192     }
 193 }