Path: blob/master/test/jdk/java/util/Calendar/SupplementalJapaneseEraTest.java
41149 views
/*1* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.text.SimpleDateFormat;24import java.time.chrono.JapaneseChronology;25import java.time.chrono.JapaneseDate;26import java.time.chrono.JapaneseEra;27import java.time.format.DateTimeFormatter;28import java.time.format.TextStyle;29import java.util.Calendar;30import java.util.Date;31import java.util.GregorianCalendar;32import static java.util.GregorianCalendar.*;33import java.util.Locale;3435/*36* Usage:37* java -Djdk.calendar.japanese.supplemental.era=... SupplementalJapaneseEraTest <flag>38* -t executes tests with a valid property value39* -b <eraname>40* executes tests with an invalid property value41*/4243public class SupplementalJapaneseEraTest {44private static final Locale WAREKI_LOCALE = Locale.forLanguageTag("ja-JP-u-ca-japanese");45private static final String SUP_ERA_NAME = "SupEra";46private static final String SUP_ERA_ABBR = "S.E.";47private static int errors = 0;4849public static void main(String[] args) {50// args[0] is a flag.51switch (args[0]) {52case "-t":53// test with a valid property value54testProperty();55break;5657case "-b":58// test with an invalid property value59// args[1] is the current era name.60testValidation(args[1].replace("\r", "")); // remove any CR for Cygwin61break;62}63if (errors != 0) {64throw new RuntimeException("test failed");65}66}6768private static void testProperty() {69Calendar jcal = new Calendar.Builder()70.setCalendarType("japanese")71.setFields(ERA, 6, YEAR, 1, DAY_OF_YEAR, 1)72.build();73Date firstDayOfEra = jcal.getTime();74jcal.set(ERA, jcal.get(ERA) - 1); // previous era75jcal.set(YEAR, 1);76jcal.set(DAY_OF_YEAR, 1);77Calendar cal = new GregorianCalendar();78cal.setTimeInMillis(jcal.getTimeInMillis());79cal.add(YEAR, 199);80int year = cal.get(YEAR);8182SimpleDateFormat sdf;83String expected, got;8485// test long era name86sdf = new SimpleDateFormat("GGGG y-MM-dd", WAREKI_LOCALE);87got = sdf.format(firstDayOfEra);88expected = SUP_ERA_NAME + " 1-02-11";89if (!expected.equals(got)) {90System.err.printf("GGGG y-MM-dd: got=\"%s\", expected=\"%s\"%n", got, expected);91errors++;92}9394// test era abbreviation95sdf = new SimpleDateFormat("G y-MM-dd", WAREKI_LOCALE);96got = sdf.format(firstDayOfEra);97expected = SUP_ERA_ABBR + " 1-02-11";98if (!expected.equals(got)) {99System.err.printf("G y-MM-dd: got=\"%s\", expected=\"%s\"%n", got, expected);100errors++;101}102103// confirm the gregorian year104sdf = new SimpleDateFormat("y", Locale.US);105int y = Integer.parseInt(sdf.format(firstDayOfEra));106if (y != year) {107System.err.printf("Gregorian year: got=%d, expected=%d%n", y, year);108errors++;109}110111// test java.time.chrono.JapaneseEra112JapaneseDate jdate = JapaneseDate.of(year, 2, 11);113got = jdate.toString();114expected = "Japanese " + SUP_ERA_NAME + " 1-02-11";115if (!expected.equals(got)) {116System.err.printf("JapaneseDate: got=\"%s\", expected=\"%s\"%n", got, expected);117errors++;118}119JapaneseEra jera = jdate.getEra();120got = jera.getDisplayName(TextStyle.FULL, Locale.US);121if (!SUP_ERA_NAME.equals(got)) {122System.err.printf("JapaneseEra (FULL): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_NAME);123errors++;124}125got = jera.getDisplayName(TextStyle.SHORT, Locale.US);126if (!SUP_ERA_NAME.equals(got)) {127System.err.printf("JapaneseEra (SHORT): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_NAME);128errors++;129}130got = jera.getDisplayName(TextStyle.NARROW, Locale.US);131if (!SUP_ERA_ABBR.equals(got)) {132System.err.printf("JapaneseEra (NARROW): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_ABBR);133errors++;134}135got = jera.getDisplayName(TextStyle.NARROW_STANDALONE, Locale.US);136if (!SUP_ERA_ABBR.equals(got)) {137System.err.printf("JapaneseEra (NARROW_STANDALONE): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_ABBR);138errors++;139}140141// test full/short/narrow names with java.time.format142got = DateTimeFormatter143.ofPattern("GGGG G GGGGG")144.withLocale(Locale.US)145.withChronology(JapaneseChronology.INSTANCE)146.format(jdate);147expected = SUP_ERA_NAME + " " + SUP_ERA_NAME + " " + SUP_ERA_ABBR;148if (!expected.equals(got)) {149System.err.printf("java.time formatter full/short/narrow names: got=\"%s\", expected=\"%s\"%n", got, expected);150errors++;151}152}153154private static void testValidation(String eraName) {155Calendar jcal = new Calendar.Builder()156.setCalendarType("japanese")157.setFields(YEAR, 1, DAY_OF_YEAR, 1)158.build();159if (!jcal.getDisplayName(ERA, LONG, Locale.US).equals(eraName)) {160errors++;161String prop = System.getProperty("jdk.calendar.japanese.supplemental.era");162System.err.println("Era changed with invalid property: " + prop);163}164}165}166167168