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