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