1 /*
2 * Copyright (c) 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 */
43 import java.time.chrono.JapaneseChronology;
44 import java.time.chrono.JapaneseDate;
45 import java.time.chrono.JapaneseEra;
46 import java.time.format.DateTimeFormatterBuilder;
47 import java.time.format.TextStyle;
48 import java.util.Calendar;
49 import java.util.Date;
50 import java.util.GregorianCalendar;
51 import static java.util.GregorianCalendar.*;
52 import java.util.Locale;
53 import java.util.Properties;
54 import java.util.TimeZone;
55
56 import jdk.testlibrary.ProcessTools;
57 import org.testng.annotations.BeforeTest;
58 import org.testng.annotations.Test;
59 import static org.testng.Assert.*;
60
61 public class SupplementalJapaneseEraTest {
62 private static final Locale WAREKI_LOCALE = Locale.forLanguageTag("ja-JP-u-ca-japanese");
63 private static final String NEW_ERA_NAME = "NewEra";
64 private static final String NEW_ERA_ABBR = "N.E.";
65 private static final int NEW_ERA_YEAR = 200;
66 private static final int NEW_ERA_MONTH = FEBRUARY;
67 private static final int NEW_ERA_DAY = 11;
68 private static int errors = 0;
69
70 @BeforeTest
71 public void prepareTestJDK() throws IOException {
72 Path src = Paths.get(System.getProperty("test.jdk")).toAbsolutePath();
73 Path dst = Paths.get("testjava").toAbsolutePath();
74 Files.walkFileTree(src, new CopyFileVisitor(src, dst));
75 }
76
77 @Test
78 public void invokeTestJDK() throws Throwable {
79 assertTrue(ProcessTools.executeCommand(
80 Paths.get("testjava", "bin", "java").toAbsolutePath().toString(),
81 "-cp",
82 System.getProperty("test.classes"),
83 "SupplementalJapaneseEraTest")
84 .getExitValue() == 0);
85 }
86
87 public static void main(String[] args) {
88 testProperty();
89
90 if (errors != 0) {
91 throw new RuntimeException("test failed");
92 }
93 }
94
95 // copied from JDK9's test
96 private static void testProperty() {
97 Calendar jcal = new Calendar.Builder()
98 .setCalendarType("japanese")
99 .setFields(YEAR, 1, DAY_OF_YEAR, 1)
100 .build();
101 Date firstDayOfEra = jcal.getTime();
102
103 jcal.set(ERA, jcal.get(ERA) - 1); // previous era
104 jcal.set(YEAR, 1);
105 jcal.set(DAY_OF_YEAR, 1);
106 Calendar cal = new GregorianCalendar();
107 cal.setTimeInMillis(jcal.getTimeInMillis());
108 cal.add(YEAR, 199);
109 int year = cal.get(YEAR);
110
111 SimpleDateFormat sdf;
112 String expected, got;
113
114 // test long era name
115 sdf = new SimpleDateFormat("GGGG y-MM-dd", WAREKI_LOCALE);
116 got = sdf.format(firstDayOfEra);
117 expected = NEW_ERA_NAME + " 1-02-11";
118 if (!expected.equals(got)) {
119 System.err.printf("GGGG y-MM-dd: got=\"%s\", expected=\"%s\"%n", got, expected);
120 errors++;
121 }
122
123 // test era abbreviation
124 sdf = new SimpleDateFormat("G y-MM-dd", WAREKI_LOCALE);
125 got = sdf.format(firstDayOfEra);
126 expected = NEW_ERA_ABBR+" 1-02-11";
127 if (!expected.equals(got)) {
128 System.err.printf("G y-MM-dd: got=\"%s\", expected=\"%s\"%n", got, expected);
129 errors++;
130 }
131
132 // confirm the gregorian year
133 sdf = new SimpleDateFormat("y", Locale.US);
134 int y = Integer.parseInt(sdf.format(firstDayOfEra));
135 if (y != year) {
136 System.err.printf("Gregorian year: got=%d, expected=%d%n", y, year);
137 errors++;
138 }
139
140 // test java.time.chrono.JapaneseEra
141 JapaneseDate jdate = JapaneseDate.of(year, 2, 11);
142 got = jdate.toString();
143 expected = "Japanese " + NEW_ERA_NAME + " 1-02-11";
144 if (!expected.equals(got)) {
145 System.err.printf("JapaneseDate: got=\"%s\", expected=\"%s\"%n", got, expected);
146 errors++;
147 }
148 JapaneseEra jera = jdate.getEra();
149 got = jera.getDisplayName(TextStyle.FULL, Locale.US);
150 if (!NEW_ERA_NAME.equals(got)) {
151 System.err.printf("JapaneseEra (FULL): got=\"%s\", expected=\"%s\"%n", got, NEW_ERA_NAME);
152 errors++;
153 }
154 got = jera.getDisplayName(TextStyle.SHORT, Locale.US);
155 if (!NEW_ERA_NAME.equals(got)) {
156 System.err.printf("JapaneseEra (SHORT): got=\"%s\", expected=\"%s\"%n", got, NEW_ERA_NAME);
157 errors++;
158 }
159 got = jera.getDisplayName(TextStyle.NARROW, Locale.US);
160 if (!NEW_ERA_ABBR.equals(got)) {
161 System.err.printf("JapaneseEra (NARROW): got=\"%s\", expected=\"%s\"%n", got, NEW_ERA_ABBR);
162 errors++;
163 }
164 got = jera.getDisplayName(TextStyle.NARROW_STANDALONE, Locale.US);
165 if (!NEW_ERA_ABBR.equals(got)) {
166 System.err.printf("JapaneseEra (NARROW_STANDALONE): got=\"%s\", expected=\"%s\"%n", got, NEW_ERA_ABBR);
167 errors++;
168 }
169
170 // test long/abbreviated names with java.time.format
171 got = new DateTimeFormatterBuilder()
172 .appendPattern("GGGG")
173 .appendLiteral(" ")
174 .appendPattern("G")
175 .toFormatter(Locale.US)
176 .withChronology(JapaneseChronology.INSTANCE)
177 .format(jdate);
178 expected = NEW_ERA_NAME + " " + NEW_ERA_ABBR;
179 if (!expected.equals(got)) {
180 System.err.printf("java.time formatter long/abbr names: got=\"%s\", expected=\"%s\"%n", got, expected);
181 errors++;
182 }
183 }
184
185 private static class CopyFileVisitor extends SimpleFileVisitor<Path> {
186 private final Path copyFrom;
187 private final Path copyTo;
188 private final Path calProps = Paths.get("calendars.properties");
189 private final String JA_CAL_KEY = "calendar.japanese.eras";
190
191 public CopyFileVisitor(Path copyFrom, Path copyTo) {
192 this.copyFrom = copyFrom;
193 this.copyTo = copyTo;
194 }
195
196 @Override
197 public FileVisitResult preVisitDirectory(Path file,
198 BasicFileAttributes attrs) throws IOException {
213 Path relativePath = copyFrom.relativize(file);
214 Path destination = copyTo.resolve(relativePath);
215 if (relativePath.getFileName().equals(calProps)) {
216 modifyCalendarProperties(file, destination);
217 } else {
218 Files.copy(file, destination, StandardCopyOption.COPY_ATTRIBUTES);
219 }
220 return FileVisitResult.CONTINUE;
221 }
222
223 private void modifyCalendarProperties(Path src, Path dst) throws IOException {
224 Properties p = new Properties();
225 try (BufferedReader br = Files.newBufferedReader(src)) {
226 p.load(br);
227 }
228
229 String eras = p.getProperty(JA_CAL_KEY);
230 if (eras != null) {
231 p.setProperty(JA_CAL_KEY,
232 eras +
233 "; name=" + SupplementalJapaneseEraTest.NEW_ERA_NAME +
234 ",abbr=" + SupplementalJapaneseEraTest.NEW_ERA_ABBR +
235 ",since=" + since());
236 }
237 try (BufferedWriter bw = Files.newBufferedWriter(dst)) {
238 p.store(bw, "test calendars.properties for supplemental Japanese era");
239 }
240 }
241
242 private long since() {
243 return new Calendar.Builder()
244 .setCalendarType("japanese")
245 .setTimeZone(TimeZone.getTimeZone("GMT"))
246 .setDate(SupplementalJapaneseEraTest.NEW_ERA_YEAR,
247 SupplementalJapaneseEraTest.NEW_ERA_MONTH,
248 SupplementalJapaneseEraTest.NEW_ERA_DAY)
249 .build()
250 .getTimeInMillis();
251 }
252 }
253 }
|
1 /*
2 * Copyright (c) 2017, 2019, 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 */
43 import java.time.chrono.JapaneseChronology;
44 import java.time.chrono.JapaneseDate;
45 import java.time.chrono.JapaneseEra;
46 import java.time.format.DateTimeFormatterBuilder;
47 import java.time.format.TextStyle;
48 import java.util.Calendar;
49 import java.util.Date;
50 import java.util.GregorianCalendar;
51 import static java.util.GregorianCalendar.*;
52 import java.util.Locale;
53 import java.util.Properties;
54 import java.util.TimeZone;
55
56 import jdk.testlibrary.ProcessTools;
57 import org.testng.annotations.BeforeTest;
58 import org.testng.annotations.Test;
59 import static org.testng.Assert.*;
60
61 public class SupplementalJapaneseEraTest {
62 private static final Locale WAREKI_LOCALE = Locale.forLanguageTag("ja-JP-u-ca-japanese");
63 private static final String SUP_ERA_NAME = "SupEra";
64 private static final String SUP_ERA_ABBR = "S.E.";
65 private static final int NEW_ERA_YEAR = 200;
66 private static final int NEW_ERA_MONTH = FEBRUARY;
67 private static final int NEW_ERA_DAY = 11;
68 private static int errors = 0;
69
70 @BeforeTest
71 public void prepareTestJDK() throws IOException {
72 Path src = Paths.get(System.getProperty("test.jdk")).toAbsolutePath();
73 Path dst = Paths.get("testjava").toAbsolutePath();
74 Files.walkFileTree(src, new CopyFileVisitor(src, dst));
75 }
76
77 @Test
78 public void invokeTestJDK() throws Throwable {
79 assertTrue(ProcessTools.executeCommand(
80 Paths.get("testjava", "bin", "java").toAbsolutePath().toString(),
81 "-cp",
82 System.getProperty("test.classes"),
83 "SupplementalJapaneseEraTest")
84 .getExitValue() == 0);
85 }
86
87 public static void main(String[] args) {
88 testProperty();
89
90 if (errors != 0) {
91 throw new RuntimeException("test failed");
92 }
93 }
94
95 // copied from JDK9's test
96 private static void testProperty() {
97 Calendar jcal = new Calendar.Builder()
98 .setCalendarType("japanese")
99 .setFields(ERA, 6, YEAR, 1, DAY_OF_YEAR, 1)
100 .build();
101 Date firstDayOfEra = jcal.getTime();
102
103 jcal.set(ERA, jcal.get(ERA) - 1); // previous era
104 jcal.set(YEAR, 1);
105 jcal.set(DAY_OF_YEAR, 1);
106 Calendar cal = new GregorianCalendar();
107 cal.setTimeInMillis(jcal.getTimeInMillis());
108 cal.add(YEAR, 199);
109 int year = cal.get(YEAR);
110
111 SimpleDateFormat sdf;
112 String expected, got;
113
114 // test long era name
115 sdf = new SimpleDateFormat("GGGG y-MM-dd", WAREKI_LOCALE);
116 got = sdf.format(firstDayOfEra);
117 expected = SUP_ERA_NAME + " 1-02-11";
118 if (!expected.equals(got)) {
119 System.err.printf("GGGG y-MM-dd: got=\"%s\", expected=\"%s\"%n", got, expected);
120 errors++;
121 }
122
123 // test era abbreviation
124 sdf = new SimpleDateFormat("G y-MM-dd", WAREKI_LOCALE);
125 got = sdf.format(firstDayOfEra);
126 expected = SUP_ERA_ABBR+" 1-02-11";
127 if (!expected.equals(got)) {
128 System.err.printf("G y-MM-dd: got=\"%s\", expected=\"%s\"%n", got, expected);
129 errors++;
130 }
131
132 // confirm the gregorian year
133 sdf = new SimpleDateFormat("y", Locale.US);
134 int y = Integer.parseInt(sdf.format(firstDayOfEra));
135 if (y != year) {
136 System.err.printf("Gregorian year: got=%d, expected=%d%n", y, year);
137 errors++;
138 }
139
140 // test java.time.chrono.JapaneseEra
141 JapaneseDate jdate = JapaneseDate.of(year, 2, 11);
142 got = jdate.toString();
143 expected = "Japanese " + SUP_ERA_NAME + " 1-02-11";
144 if (!expected.equals(got)) {
145 System.err.printf("JapaneseDate: got=\"%s\", expected=\"%s\"%n", got, expected);
146 errors++;
147 }
148 JapaneseEra jera = jdate.getEra();
149 got = jera.getDisplayName(TextStyle.FULL, Locale.US);
150 if (!SUP_ERA_NAME.equals(got)) {
151 System.err.printf("JapaneseEra (FULL): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_NAME);
152 errors++;
153 }
154 got = jera.getDisplayName(TextStyle.SHORT, Locale.US);
155 if (!SUP_ERA_NAME.equals(got)) {
156 System.err.printf("JapaneseEra (SHORT): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_NAME);
157 errors++;
158 }
159 got = jera.getDisplayName(TextStyle.NARROW, Locale.US);
160 if (!SUP_ERA_ABBR.equals(got)) {
161 System.err.printf("JapaneseEra (NARROW): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_ABBR);
162 errors++;
163 }
164 got = jera.getDisplayName(TextStyle.NARROW_STANDALONE, Locale.US);
165 if (!SUP_ERA_ABBR.equals(got)) {
166 System.err.printf("JapaneseEra (NARROW_STANDALONE): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_ABBR);
167 errors++;
168 }
169
170 // test long/abbreviated names with java.time.format
171 got = new DateTimeFormatterBuilder()
172 .appendPattern("GGGG")
173 .appendLiteral(" ")
174 .appendPattern("G")
175 .appendLiteral(" ")
176 .appendPattern("GGGGG")
177 .toFormatter(Locale.US)
178 .withChronology(JapaneseChronology.INSTANCE)
179 .format(jdate);
180 expected = SUP_ERA_NAME + " " + SUP_ERA_NAME + " " + SUP_ERA_ABBR;
181 if (!expected.equals(got)) {
182 System.err.printf("java.time formatter long/abbr names: got=\"%s\", expected=\"%s\"%n", got, expected);
183 errors++;
184 }
185 }
186
187 private static class CopyFileVisitor extends SimpleFileVisitor<Path> {
188 private final Path copyFrom;
189 private final Path copyTo;
190 private final Path calProps = Paths.get("calendars.properties");
191 private final String JA_CAL_KEY = "calendar.japanese.eras";
192
193 public CopyFileVisitor(Path copyFrom, Path copyTo) {
194 this.copyFrom = copyFrom;
195 this.copyTo = copyTo;
196 }
197
198 @Override
199 public FileVisitResult preVisitDirectory(Path file,
200 BasicFileAttributes attrs) throws IOException {
215 Path relativePath = copyFrom.relativize(file);
216 Path destination = copyTo.resolve(relativePath);
217 if (relativePath.getFileName().equals(calProps)) {
218 modifyCalendarProperties(file, destination);
219 } else {
220 Files.copy(file, destination, StandardCopyOption.COPY_ATTRIBUTES);
221 }
222 return FileVisitResult.CONTINUE;
223 }
224
225 private void modifyCalendarProperties(Path src, Path dst) throws IOException {
226 Properties p = new Properties();
227 try (BufferedReader br = Files.newBufferedReader(src)) {
228 p.load(br);
229 }
230
231 String eras = p.getProperty(JA_CAL_KEY);
232 if (eras != null) {
233 p.setProperty(JA_CAL_KEY,
234 eras +
235 "; name=" + SupplementalJapaneseEraTest.SUP_ERA_NAME +
236 ",abbr=" + SupplementalJapaneseEraTest.SUP_ERA_ABBR +
237 ",since=" + since());
238 }
239 try (BufferedWriter bw = Files.newBufferedWriter(dst)) {
240 p.store(bw, "test calendars.properties for supplemental Japanese era");
241 }
242 }
243
244 private long since() {
245 return new Calendar.Builder()
246 .setCalendarType("japanese")
247 .setTimeZone(TimeZone.getTimeZone("GMT"))
248 .setFields(ERA, 5)
249 .setDate(SupplementalJapaneseEraTest.NEW_ERA_YEAR,
250 SupplementalJapaneseEraTest.NEW_ERA_MONTH,
251 SupplementalJapaneseEraTest.NEW_ERA_DAY)
252 .build()
253 .getTimeInMillis();
254 }
255 }
256 }
|