DateFormatProviderTest.java revision 9330:8b1f1c2a400f
130715Sbrian/* 230715Sbrian * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. 330715Sbrian * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 430715Sbrian * 530715Sbrian * This code is free software; you can redistribute it and/or modify it 626940Sbrian * under the terms of the GNU General Public License version 2 only, as 726940Sbrian * published by the Free Software Foundation. 826940Sbrian * 926940Sbrian * This code is distributed in the hope that it will be useful, but WITHOUT 1026940Sbrian * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1130715Sbrian * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1230715Sbrian * version 2 for more details (a copy is included in the LICENSE file that 1330715Sbrian * accompanied this code). 1426940Sbrian * 1530715Sbrian * You should have received a copy of the GNU General Public License version 1630715Sbrian * 2 along with this work; if not, write to the Free Software Foundation, 1726940Sbrian * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1830715Sbrian * 1926940Sbrian * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2026940Sbrian * or visit www.oracle.com if you need additional information or have any 2126940Sbrian * questions. 2230715Sbrian */ 2326940Sbrian/* 2426940Sbrian * 2530715Sbrian */ 2629252Sbrian 2726940Sbrianimport java.text.*; 2830715Sbrianimport java.util.*; 2930715Sbrianimport sun.util.locale.provider.*; 3026940Sbrianimport sun.util.resources.*; 3126940Sbrian 3226940Sbrianpublic class DateFormatProviderTest extends ProviderTest { 3326940Sbrian 3427089Sbrian com.foo.DateFormatProviderImpl dfp = new com.foo.DateFormatProviderImpl(); 3526940Sbrian List<Locale> availloc = Arrays.asList(DateFormat.getAvailableLocales()); 3628679Sbrian List<Locale> providerloc = Arrays.asList(dfp.getAvailableLocales()); 3726940Sbrian List<Locale> jreloc = Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales()); 3829083Sbrian List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getDateFormatProvider().getAvailableLocales()); 3929083Sbrian 4029083Sbrian public static void main(String[] s) { 4129083Sbrian new DateFormatProviderTest(); 4229083Sbrian } 4329083Sbrian 4429252Sbrian DateFormatProviderTest() { 4529252Sbrian availableLocalesTest(); 4629252Sbrian objectValidityTest(); 4729252Sbrian extendedVariantTest(); 4829252Sbrian messageFormatTest(); 4928679Sbrian } 5028679Sbrian 5128679Sbrian void availableLocalesTest() { 5229083Sbrian Set<Locale> localesFromAPI = new HashSet<>(availloc); 5328679Sbrian Set<Locale> localesExpected = new HashSet<>(jreloc); 5428679Sbrian localesExpected.addAll(providerloc); 5528679Sbrian if (localesFromAPI.equals(localesExpected)) { 5626940Sbrian System.out.println("availableLocalesTest passed."); 5728679Sbrian } else { 5828679Sbrian throw new RuntimeException("availableLocalesTest failed"); 5928679Sbrian } 6029083Sbrian } 6128679Sbrian 6228679Sbrian void objectValidityTest() { 6328679Sbrian 6428679Sbrian for (Locale target: availloc) { 6527089Sbrian // Get the key for the date/time patterns which is 6628679Sbrian // specific to each calendar system. 6728679Sbrian Calendar cal = Calendar.getInstance(target); 6828679Sbrian String dkey = "DatePatterns"; 6928679Sbrian String tkey = "TimePatterns"; 7028679Sbrian String dtkey = "DateTimePatterns"; 7129083Sbrian switch (cal.getCalendarType()) { 7228679Sbrian case "java.util.JapaneseImperialCalendar": 7328679Sbrian dkey = "japanese"+ "." + dkey; 7428679Sbrian tkey = "japanese"+ "." + tkey; 7528679Sbrian dtkey = "japanese"+ "." + dtkey; 7628679Sbrian break; 7728679Sbrian case "sun.util.BuddhistCalendar": 7829083Sbrian dkey = "buddhist"+ "." + dkey; 7928679Sbrian tkey = "buddhist"+ "." + tkey; 8028679Sbrian dtkey = "buddhist"+ "." + dtkey; 8128679Sbrian break; 8228679Sbrian case "java.util.GregorianCalendar": 8328679Sbrian default: 8428679Sbrian break; 8526940Sbrian } 8626940Sbrian // pure JRE implementation 8726940Sbrian ResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getDateFormatData(target); 8826940Sbrian boolean jreSupportsLocale = jreimplloc.contains(target); 8926940Sbrian 9028679Sbrian // JRE string arrays 9128679Sbrian String[] jreDatePatterns = null; 9226940Sbrian String[] jreTimePatterns = null; 9329083Sbrian String[] jreDateTimePatterns = null; 9429083Sbrian if (jreSupportsLocale) { 9529083Sbrian try { 9629083Sbrian jreDatePatterns = (String[])rb.getObject(dkey); 9729083Sbrian jreTimePatterns = (String[])rb.getObject(tkey); 9829252Sbrian jreDateTimePatterns = (String[])rb.getObject(dtkey); 9929252Sbrian } catch (MissingResourceException mre) {} 10029252Sbrian } 10129252Sbrian 10229252Sbrian for (int style = DateFormat.FULL; style <= DateFormat.SHORT; style ++) { 10329252Sbrian // result object 10428679Sbrian DateFormat result = DateFormat.getDateTimeInstance(style, style, target); 10528679Sbrian 10628679Sbrian // provider's object (if any) 10729083Sbrian DateFormat providersResult = null; 10828679Sbrian if (providerloc.contains(target)) { 10928679Sbrian providersResult = dfp.getDateTimeInstance(style, style, target); 11028679Sbrian } 11128679Sbrian 11228679Sbrian // JRE's object (if any) 11328679Sbrian DateFormat jresResult = null; 11428679Sbrian if (jreSupportsLocale) { 11528679Sbrian Object[] dateTimeArgs = {jreTimePatterns[style], 11628679Sbrian jreDatePatterns[style]}; 11728679Sbrian String pattern = MessageFormat.format(jreDateTimePatterns[0], dateTimeArgs); 11829083Sbrian jresResult = new SimpleDateFormat(pattern, target); 11928679Sbrian } 12028679Sbrian 12128679Sbrian checkValidity(target, jresResult, providersResult, result, jreSupportsLocale); 12228679Sbrian } 12329083Sbrian } 12428679Sbrian } 12528679Sbrian 12628679Sbrian // Check that fallback correctly occurs with locales with variant including '_'s 12728679Sbrian // This test assumes that the provider supports the ja_JP_osaka locale, and JRE does not. 12828679Sbrian void extendedVariantTest() { 12926940Sbrian Locale[] testlocs = {new Locale("ja", "JP", "osaka_extended"), 13026940Sbrian new Locale("ja", "JP", "osaka_extended_further"), 13126940Sbrian new Locale("ja", "JP", "osaka_")}; 13226940Sbrian for (Locale test: testlocs) { 13326940Sbrian DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test); 13426940Sbrian DateFormat provider = dfp.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test); 13526940Sbrian if (!df.equals(provider)) { 13626940Sbrian throw new RuntimeException("variant fallback failed. test locale: "+test); 13726940Sbrian } 13826940Sbrian } 13926940Sbrian } 14026940Sbrian 14126940Sbrian 14226940Sbrian private static final String[] TYPES = { 143 "date", 144 "time" 145 }; 146 private static final String[] MODIFIERS = { 147 "", 148 "short", 149 "medium", // Same as DEFAULT 150 "long", 151 "full" 152 }; 153 154 void messageFormatTest() { 155 for (Locale target : providerloc) { 156 for (String type : TYPES) { 157 for (String modifier : MODIFIERS) { 158 String pattern, expected; 159 if (modifier.equals("")) { 160 pattern = String.format("%s={0,%s}", type, type); 161 } else { 162 pattern = String.format("%s={0,%s,%s}", type, type, modifier); 163 } 164 if (modifier.equals("medium")) { 165 // medium is default. 166 expected = String.format("%s={0,%s}", type, type); 167 } else { 168 expected = pattern; 169 } 170 MessageFormat mf = new MessageFormat(pattern, target); 171 Format[] fmts = mf.getFormats(); 172 if (fmts[0] instanceof SimpleDateFormat) { 173 continue; 174 } 175 String toPattern = mf.toPattern(); 176 if (!toPattern.equals(expected)) { 177 throw new RuntimeException("messageFormatTest: got '" + toPattern 178 + "', expected '" + expected + "'"); 179 } 180 } 181 } 182 } 183 } 184} 185