1 /*
2 * Copyright (c) 2007, 2010, 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 *
25 */
26
27 import java.text.*;
28 import java.util.*;
29 import sun.util.*;
30 import sun.util.resources.*;
31
32 public class DateFormatProviderTest extends ProviderTest {
33
34 com.foo.DateFormatProviderImpl dfp = new com.foo.DateFormatProviderImpl();
35 List<Locale> availloc = Arrays.asList(DateFormat.getAvailableLocales());
36 List<Locale> providerloc = Arrays.asList(dfp.getAvailableLocales());
37 List<Locale> jreloc = Arrays.asList(LocaleData.getAvailableLocales());
38
39 public static void main(String[] s) {
40 new DateFormatProviderTest();
41 }
42
43 DateFormatProviderTest() {
44 availableLocalesTest();
45 objectValidityTest();
46 extendedVariantTest();
47 messageFormatTest();
48 }
49
50 void availableLocalesTest() {
51 Set<Locale> localesFromAPI = new HashSet<Locale>(availloc);
52 Set<Locale> localesExpected = new HashSet<Locale>(jreloc);
53 localesExpected.addAll(providerloc);
54 if (localesFromAPI.equals(localesExpected)) {
55 System.out.println("availableLocalesTest passed.");
56 } else {
57 throw new RuntimeException("availableLocalesTest failed");
58 }
59 }
60
61 void objectValidityTest() {
62
63 for (Locale target: availloc) {
64 // Get the key for the date/time patterns which is
65 // specific to each calendar system.
66 Calendar cal = Calendar.getInstance(target);
67 String key = "DateTimePatterns";
68 if (!cal.getClass().getName().equals("java.util.GregorianCalendar")) {
69 // e.g., "java.util.JapaneseImperialCalendar.DateTimePatterns"
70 key = cal.getClass().getName() + "." + key;
71 }
72 // pure JRE implementation
73 ResourceBundle rb = LocaleData.getDateFormatData(target);
74 boolean jreSupportsLocale = jreloc.contains(target);
75
76 // JRE string arrays
77 String[] jreDateTimePatterns = null;
78 if (jreSupportsLocale) {
79 try {
80 jreDateTimePatterns = (String[])rb.getObject(key);
81 } catch (MissingResourceException mre) {}
82 }
83
84 for (int style = DateFormat.FULL; style <= DateFormat.SHORT; style ++) {
85 // result object
86 DateFormat result = DateFormat.getDateTimeInstance(style, style, target);
87
88 // provider's object (if any)
89 DateFormat providersResult = null;
90 if (providerloc.contains(target)) {
91 providersResult = dfp.getDateTimeInstance(style, style, target);
92 }
93
94 // JRE's object (if any)
95 DateFormat jresResult = null;
96 if (jreSupportsLocale) {
97 Object[] dateTimeArgs = {jreDateTimePatterns[style],
98 jreDateTimePatterns[style + 4]};
99 String pattern = MessageFormat.format(jreDateTimePatterns[8], dateTimeArgs);
100 jresResult = new SimpleDateFormat(pattern, target);
101 }
102
103 checkValidity(target, jresResult, providersResult, result, jreSupportsLocale);
104 }
105 }
106 }
107
108 // Check that fallback correctly occurs with locales with variant including '_'s
109 // This test assumes that the provider supports the ja_JP_osaka locale, and JRE does not.
110 void extendedVariantTest() {
111 Locale[] testlocs = {new Locale("ja", "JP", "osaka_extended"),
112 new Locale("ja", "JP", "osaka_extended_further"),
113 new Locale("ja", "JP", "osaka_")};
114 for (Locale test: testlocs) {
115 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);
116 DateFormat provider = dfp.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);
117 if (!df.equals(provider)) {
118 throw new RuntimeException("variant fallback failed. test locale: "+test);
119 }
|
1 /*
2 * Copyright (c) 2007, 2012, 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 *
25 */
26
27 import java.text.*;
28 import java.util.*;
29 import sun.util.locale.provider.*;
30 import sun.util.resources.*;
31
32 public class DateFormatProviderTest extends ProviderTest {
33
34 com.foo.DateFormatProviderImpl dfp = new com.foo.DateFormatProviderImpl();
35 List<Locale> availloc = Arrays.asList(DateFormat.getAvailableLocales());
36 List<Locale> providerloc = Arrays.asList(dfp.getAvailableLocales());
37 List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getDateFormatProvider().getAvailableLocales());
38
39 public static void main(String[] s) {
40 new DateFormatProviderTest();
41 }
42
43 DateFormatProviderTest() {
44 objectValidityTest();
45 extendedVariantTest();
46 messageFormatTest();
47 }
48
49 void objectValidityTest() {
50
51 for (Locale target: availloc) {
52 // Get the key for the date/time patterns which is
53 // specific to each calendar system.
54 Calendar cal = Calendar.getInstance(target);
55 String dkey = "DatePatterns";
56 String tkey = "TimePatterns";
57 String dtkey = "DateTimePatterns";
58 switch (cal.getCalendarType()) {
59 case "java.util.JapaneseImperialCalendar":
60 dkey = "japanese"+ "." + dkey;
61 tkey = "japanese"+ "." + tkey;
62 dtkey = "japanese"+ "." + dtkey;
63 break;
64 case "sun.util.BuddhistCalendar":
65 dkey = "buddhist"+ "." + dkey;
66 tkey = "buddhist"+ "." + tkey;
67 dtkey = "buddhist"+ "." + dtkey;
68 break;
69 case "java.util.GregorianCalendar":
70 default:
71 break;
72 }
73 // pure JRE implementation
74 ResourceBundle rb = LocaleProviderAdapter.forJRE().getLocaleData().getDateFormatData(target);
75 boolean jreSupportsLocale = jreimplloc.contains(target);
76
77 // JRE string arrays
78 String[] jreDatePatterns = null;
79 String[] jreTimePatterns = null;
80 String[] jreDateTimePatterns = null;
81 if (jreSupportsLocale) {
82 try {
83 jreDatePatterns = (String[])rb.getObject(dkey);
84 jreTimePatterns = (String[])rb.getObject(tkey);
85 jreDateTimePatterns = (String[])rb.getObject(dtkey);
86 } catch (MissingResourceException mre) {}
87 }
88
89 for (int style = DateFormat.FULL; style <= DateFormat.SHORT; style ++) {
90 // result object
91 DateFormat result = DateFormat.getDateTimeInstance(style, style, target);
92
93 // provider's object (if any)
94 DateFormat providersResult = null;
95 if (providerloc.contains(target)) {
96 providersResult = dfp.getDateTimeInstance(style, style, target);
97 }
98
99 // JRE's object (if any)
100 DateFormat jresResult = null;
101 if (jreSupportsLocale) {
102 Object[] dateTimeArgs = {jreTimePatterns[style],
103 jreDatePatterns[style]};
104 String pattern = MessageFormat.format(jreDateTimePatterns[0], dateTimeArgs);
105 jresResult = new SimpleDateFormat(pattern, target);
106 }
107
108 checkValidity(target, jresResult, providersResult, result, jreSupportsLocale);
109 }
110 }
111 }
112
113 // Check that fallback correctly occurs with locales with variant including '_'s
114 // This test assumes that the provider supports the ja_JP_osaka locale, and JRE does not.
115 void extendedVariantTest() {
116 Locale[] testlocs = {new Locale("ja", "JP", "osaka_extended"),
117 new Locale("ja", "JP", "osaka_extended_further"),
118 new Locale("ja", "JP", "osaka_")};
119 for (Locale test: testlocs) {
120 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);
121 DateFormat provider = dfp.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);
122 if (!df.equals(provider)) {
123 throw new RuntimeException("variant fallback failed. test locale: "+test);
124 }
|