J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any 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 | } |
| 48 | |
| 49 | void availableLocalesTest() { |
| 50 | Set<Locale> localesFromAPI = new HashSet<Locale>(availloc); |
| 51 | Set<Locale> localesExpected = new HashSet<Locale>(jreloc); |
| 52 | localesExpected.addAll(providerloc); |
| 53 | if (localesFromAPI.equals(localesExpected)) { |
| 54 | System.out.println("availableLocalesTest passed."); |
| 55 | } else { |
| 56 | throw new RuntimeException("availableLocalesTest failed"); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void objectValidityTest() { |
| 61 | |
| 62 | for (Locale target: availloc) { |
| 63 | // Get the key for the date/time patterns which is |
| 64 | // specific to each calendar system. |
| 65 | Calendar cal = Calendar.getInstance(target); |
| 66 | String key = "DateTimePatterns"; |
| 67 | if (!cal.getClass().getName().equals("java.util.GregorianCalendar")) { |
| 68 | // e.g., "java.util.JapaneseImperialCalendar.DateTimePatterns" |
| 69 | key = cal.getClass().getName() + "." + key; |
| 70 | } |
| 71 | // pure JRE implementation |
| 72 | ResourceBundle rb = LocaleData.getDateFormatData(target); |
| 73 | boolean jreSupportsLocale = jreloc.contains(target); |
| 74 | |
| 75 | // JRE string arrays |
| 76 | String[] jreDateTimePatterns = null; |
| 77 | if (jreSupportsLocale) { |
| 78 | try { |
| 79 | jreDateTimePatterns = (String[])rb.getObject(key); |
| 80 | } catch (MissingResourceException mre) {} |
| 81 | } |
| 82 | |
| 83 | for (int style = DateFormat.FULL; style <= DateFormat.SHORT; style ++) { |
| 84 | // result object |
| 85 | DateFormat result = DateFormat.getDateTimeInstance(style, style, target); |
| 86 | |
| 87 | // provider's object (if any) |
| 88 | DateFormat providersResult = null; |
| 89 | if (providerloc.contains(target)) { |
| 90 | providersResult = dfp.getDateTimeInstance(style, style, target); |
| 91 | } |
| 92 | |
| 93 | // JRE's object (if any) |
| 94 | DateFormat jresResult = null; |
| 95 | if (jreSupportsLocale) { |
| 96 | Object[] dateTimeArgs = {jreDateTimePatterns[style], |
| 97 | jreDateTimePatterns[style + 4]}; |
| 98 | String pattern = MessageFormat.format(jreDateTimePatterns[8], dateTimeArgs); |
| 99 | jresResult = new SimpleDateFormat(pattern, target); |
| 100 | } |
| 101 | |
| 102 | checkValidity(target, jresResult, providersResult, result, jreSupportsLocale); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Check that fallback correctly occurs with locales with variant including '_'s |
| 108 | // This test assumes that the provider supports the ja_JP_osaka locale, and JRE does not. |
| 109 | void extendedVariantTest() { |
| 110 | Locale[] testlocs = {new Locale("ja", "JP", "osaka_extended"), |
| 111 | new Locale("ja", "JP", "osaka_extended_further"), |
| 112 | new Locale("ja", "JP", "osaka_")}; |
| 113 | for (Locale test: testlocs) { |
| 114 | DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test); |
| 115 | DateFormat provider = dfp.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test); |
| 116 | if (!df.equals(provider)) { |
| 117 | throw new RuntimeException("variant fallback failed. test locale: "+test); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |