Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.java
47031 views
/*1* Copyright (c) 2017, 2019, 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*/2223/**24* @test25* @bug 8054214 8173423 817777626* @summary Create an equivalent test case for JDK9's SupplementalJapaneseEraTest27* @library /lib/testlibrary28* @run testng SupplementalJapaneseEraTest29*/3031import java.io.BufferedReader;32import java.io.BufferedWriter;33import java.io.IOException;34import java.nio.file.FileVisitResult;35import java.nio.file.Files;36import java.nio.file.Path;37import java.nio.file.Paths;38import java.nio.file.SimpleFileVisitor;39import java.nio.file.StandardCopyOption;40import java.nio.file.attribute.BasicFileAttributes;41import java.text.SimpleDateFormat;42import java.time.chrono.JapaneseChronology;43import java.time.chrono.JapaneseDate;44import java.time.chrono.JapaneseEra;45import java.time.format.DateTimeFormatterBuilder;46import java.time.format.TextStyle;47import java.util.Calendar;48import java.util.Date;49import java.util.GregorianCalendar;50import static java.util.GregorianCalendar.*;51import java.util.Locale;52import java.util.Properties;53import java.util.TimeZone;5455import jdk.testlibrary.ProcessTools;56import org.testng.annotations.BeforeTest;57import org.testng.annotations.Test;58import static org.testng.Assert.*;5960public class SupplementalJapaneseEraTest {61private static final Locale WAREKI_LOCALE = Locale.forLanguageTag("ja-JP-u-ca-japanese");62private static final String SUP_ERA_NAME = "SupEra";63private static final String SUP_ERA_ABBR = "S.E.";64private static final int NEW_ERA_YEAR = 200;65private static final int NEW_ERA_MONTH = FEBRUARY;66private static final int NEW_ERA_DAY = 11;67private static int errors = 0;6869@BeforeTest70public void prepareTestJDK() throws IOException {71Path src = Paths.get(System.getProperty("test.jdk")).toAbsolutePath();72Path dst = Paths.get("testjava").toAbsolutePath();73Files.walkFileTree(src, new CopyFileVisitor(src, dst));74}7576@Test77public void invokeTestJDK() throws Throwable {78assertTrue(ProcessTools.executeCommand(79Paths.get("testjava", "bin", "java").toAbsolutePath().toString(),80"-cp",81System.getProperty("test.classes"),82"SupplementalJapaneseEraTest")83.getExitValue() == 0);84}8586public static void main(String[] args) {87testProperty();8889if (errors != 0) {90throw new RuntimeException("test failed");91}92}9394// copied from JDK9's test95private static void testProperty() {96Calendar jcal = new Calendar.Builder()97.setCalendarType("japanese")98.setFields(ERA, 6, YEAR, 1, DAY_OF_YEAR, 1)99.build();100Date firstDayOfEra = jcal.getTime();101102jcal.set(ERA, jcal.get(ERA) - 1); // previous era103jcal.set(YEAR, 1);104jcal.set(DAY_OF_YEAR, 1);105Calendar cal = new GregorianCalendar();106cal.setTimeInMillis(jcal.getTimeInMillis());107cal.add(YEAR, 199);108int year = cal.get(YEAR);109110SimpleDateFormat sdf;111String expected, got;112113// test long era name114sdf = new SimpleDateFormat("GGGG y-MM-dd", WAREKI_LOCALE);115got = sdf.format(firstDayOfEra);116expected = SUP_ERA_NAME + " 1-02-11";117if (!expected.equals(got)) {118System.err.printf("GGGG y-MM-dd: got=\"%s\", expected=\"%s\"%n", got, expected);119errors++;120}121122// test era abbreviation123sdf = new SimpleDateFormat("G y-MM-dd", WAREKI_LOCALE);124got = sdf.format(firstDayOfEra);125expected = SUP_ERA_ABBR+" 1-02-11";126if (!expected.equals(got)) {127System.err.printf("G y-MM-dd: got=\"%s\", expected=\"%s\"%n", got, expected);128errors++;129}130131// confirm the gregorian year132sdf = new SimpleDateFormat("y", Locale.US);133int y = Integer.parseInt(sdf.format(firstDayOfEra));134if (y != year) {135System.err.printf("Gregorian year: got=%d, expected=%d%n", y, year);136errors++;137}138139// test java.time.chrono.JapaneseEra140JapaneseDate jdate = JapaneseDate.of(year, 2, 11);141got = jdate.toString();142expected = "Japanese " + SUP_ERA_NAME + " 1-02-11";143if (!expected.equals(got)) {144System.err.printf("JapaneseDate: got=\"%s\", expected=\"%s\"%n", got, expected);145errors++;146}147JapaneseEra jera = jdate.getEra();148got = jera.getDisplayName(TextStyle.FULL, Locale.US);149if (!SUP_ERA_NAME.equals(got)) {150System.err.printf("JapaneseEra (FULL): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_NAME);151errors++;152}153got = jera.getDisplayName(TextStyle.SHORT, Locale.US);154if (!SUP_ERA_NAME.equals(got)) {155System.err.printf("JapaneseEra (SHORT): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_NAME);156errors++;157}158got = jera.getDisplayName(TextStyle.NARROW, Locale.US);159if (!SUP_ERA_ABBR.equals(got)) {160System.err.printf("JapaneseEra (NARROW): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_ABBR);161errors++;162}163got = jera.getDisplayName(TextStyle.NARROW_STANDALONE, Locale.US);164if (!SUP_ERA_ABBR.equals(got)) {165System.err.printf("JapaneseEra (NARROW_STANDALONE): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_ABBR);166errors++;167}168169// test long/abbreviated names with java.time.format170got = new DateTimeFormatterBuilder()171.appendPattern("GGGG")172.appendLiteral(" ")173.appendPattern("G")174.appendLiteral(" ")175.appendPattern("GGGGG")176.toFormatter(Locale.US)177.withChronology(JapaneseChronology.INSTANCE)178.format(jdate);179expected = SUP_ERA_NAME + " " + SUP_ERA_NAME + " " + SUP_ERA_ABBR;180if (!expected.equals(got)) {181System.err.printf("java.time formatter long/abbr names: got=\"%s\", expected=\"%s\"%n", got, expected);182errors++;183}184}185186private static class CopyFileVisitor extends SimpleFileVisitor<Path> {187private final Path copyFrom;188private final Path copyTo;189private final Path calProps = Paths.get("calendars.properties");190private final String JA_CAL_KEY = "calendar.japanese.eras";191192public CopyFileVisitor(Path copyFrom, Path copyTo) {193this.copyFrom = copyFrom;194this.copyTo = copyTo;195}196197@Override198public FileVisitResult preVisitDirectory(Path file,199BasicFileAttributes attrs) throws IOException {200Path relativePath = copyFrom.relativize(file);201Path destination = copyTo.resolve(relativePath);202if (!destination.toFile().exists()) {203Files.createDirectories(destination);204}205return FileVisitResult.CONTINUE;206}207208@Override209public FileVisitResult visitFile(Path file,210BasicFileAttributes attrs) throws IOException {211if (!file.toFile().isFile()) {212return FileVisitResult.CONTINUE;213}214Path relativePath = copyFrom.relativize(file);215Path destination = copyTo.resolve(relativePath);216if (relativePath.getFileName().equals(calProps)) {217modifyCalendarProperties(file, destination);218} else {219Files.copy(file, destination, StandardCopyOption.COPY_ATTRIBUTES);220}221return FileVisitResult.CONTINUE;222}223224private void modifyCalendarProperties(Path src, Path dst) throws IOException {225Properties p = new Properties();226try (BufferedReader br = Files.newBufferedReader(src)) {227p.load(br);228}229230String eras = p.getProperty(JA_CAL_KEY);231if (eras != null) {232p.setProperty(JA_CAL_KEY,233eras +234"; name=" + SupplementalJapaneseEraTest.SUP_ERA_NAME +235",abbr=" + SupplementalJapaneseEraTest.SUP_ERA_ABBR +236",since=" + since());237}238try (BufferedWriter bw = Files.newBufferedWriter(dst)) {239p.store(bw, "test calendars.properties for supplemental Japanese era");240}241}242243private long since() {244return new Calendar.Builder()245.setCalendarType("japanese")246.setTimeZone(TimeZone.getTimeZone("GMT"))247.setFields(ERA, 5)248.setDate(SupplementalJapaneseEraTest.NEW_ERA_YEAR,249SupplementalJapaneseEraTest.NEW_ERA_MONTH,250SupplementalJapaneseEraTest.NEW_ERA_DAY)251.build()252.getTimeInMillis();253}254}255}256257258