1 /*
   2  * Copyright (c) 2007, 2018, 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  * @test
  26  * @bug 4052440 7003643 8062588 8210406
  27  * @summary DateFormatProvider tests
  28  * @library providersrc/foobarutils
  29  *          providersrc/fooprovider
  30  * @modules java.base/sun.util.locale.provider
  31  *          java.base/sun.util.resources
  32  * @build com.foobar.Utils
  33  *        com.foo.*
  34  * @run main/othervm -Djava.locale.providers=JRE,SPI DateFormatProviderTest
  35  */
  36 
  37 import java.text.DateFormat;
  38 import java.text.Format;
  39 import java.text.MessageFormat;
  40 import java.text.SimpleDateFormat;
  41 import java.util.Arrays;
  42 import java.util.Calendar;
  43 import java.util.HashSet;
  44 import java.util.List;
  45 import java.util.Locale;
  46 import java.util.MissingResourceException;
  47 import java.util.ResourceBundle;
  48 import java.util.Set;
  49 
  50 import com.foo.DateFormatProviderImpl;
  51 
  52 import sun.util.locale.provider.LocaleProviderAdapter;
  53 import sun.util.locale.provider.ResourceBundleBasedAdapter;
  54 
  55 public class DateFormatProviderTest extends ProviderTest {
  56 
  57     DateFormatProviderImpl dfp = new DateFormatProviderImpl();
  58     List<Locale> availloc = Arrays.asList(DateFormat.getAvailableLocales());
  59     List<Locale> providerloc = Arrays.asList(dfp.getAvailableLocales());
  60     List<Locale> jreloc = Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales());
  61     List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getDateFormatProvider().getAvailableLocales());
  62 
  63     public static void main(String[] s) {
  64         new DateFormatProviderTest();
  65     }
  66 
  67     DateFormatProviderTest() {
  68         availableLocalesTest();
  69         objectValidityTest();
  70         extendedVariantTest();
  71         messageFormatTest();
  72     }
  73 
  74     void availableLocalesTest() {
  75         Set<Locale> localesFromAPI = new HashSet<>(availloc);
  76         Set<Locale> localesExpected = new HashSet<>(jreloc);
  77         localesExpected.addAll(providerloc);
  78         if (localesFromAPI.equals(localesExpected)) {
  79             System.out.println("availableLocalesTest passed.");
  80         } else {
  81             throw new RuntimeException("availableLocalesTest failed");
  82         }
  83     }
  84 
  85     void objectValidityTest() {
  86 
  87         for (Locale target: availloc) {
  88             // Get the key for the date/time patterns which is
  89             // specific to each calendar system.
  90             Calendar cal = Calendar.getInstance(target);
  91             String dkey = "DatePatterns";
  92             String tkey = "TimePatterns";
  93             String dtkey = "DateTimePatterns";
  94             switch (cal.getCalendarType()) {
  95                 case "java.util.JapaneseImperialCalendar":
  96                     dkey = "japanese"+ "." + dkey;
  97                     tkey = "japanese"+ "." + tkey;
  98                     dtkey = "japanese"+ "." + dtkey;
  99                     break;
 100                 case "sun.util.BuddhistCalendar":
 101                     dkey = "buddhist"+ "." + dkey;
 102                     tkey = "buddhist"+ "." + tkey;
 103                     dtkey = "buddhist"+ "." + dtkey;
 104                     break;
 105                 case "java.util.GregorianCalendar":
 106                 default:
 107                     break;
 108             }
 109             // pure JRE implementation
 110             ResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getDateFormatData(target);
 111             boolean jreSupportsLocale = jreimplloc.contains(target);
 112 
 113             // JRE string arrays
 114             String[] jreDatePatterns = null;
 115             String[] jreTimePatterns = null;
 116             String[] jreDateTimePatterns = null;
 117             if (jreSupportsLocale) {
 118                 try {
 119                     jreDatePatterns = (String[])rb.getObject(dkey);
 120                     jreTimePatterns = (String[])rb.getObject(tkey);
 121                     jreDateTimePatterns = (String[])rb.getObject(dtkey);
 122                 } catch (MissingResourceException mre) {}
 123             }
 124 
 125             for (int style = DateFormat.FULL; style <= DateFormat.SHORT; style ++) {
 126                 // result object
 127                 DateFormat result = DateFormat.getDateTimeInstance(style, style, target);
 128 
 129                 // provider's object (if any)
 130                 DateFormat providersResult = null;
 131                 if (providerloc.contains(target)) {
 132                     providersResult = dfp.getDateTimeInstance(style, style, target);
 133                 }
 134 
 135                 // JRE's object (if any)
 136                 DateFormat jresResult = null;
 137                 if (jreSupportsLocale) {
 138                     Object[] dateTimeArgs = {jreTimePatterns[style],
 139                                              jreDatePatterns[style]};
 140                     String pattern = MessageFormat.format(jreDateTimePatterns[0], dateTimeArgs);
 141                     jresResult = new SimpleDateFormat(pattern, target);
 142                 }
 143 
 144                 checkValidity(target, jresResult, providersResult, result, jreSupportsLocale);
 145             }
 146         }
 147     }
 148 
 149     // Check that fallback correctly occurs with locales with variant including '_'s
 150     // This test assumes that the provider supports the ja_JP_osaka locale, and JRE does not.
 151     void extendedVariantTest() {
 152         Locale[] testlocs = {new Locale("ja", "JP", "osaka_extended"),
 153                              new Locale("ja", "JP", "osaka_extended_further"),
 154                              new Locale("ja", "JP", "osaka_")};
 155         for (Locale test: testlocs) {
 156             DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);
 157             DateFormat provider = dfp.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);
 158             if (!df.equals(provider)) {
 159                 throw new RuntimeException("variant fallback failed. test locale: "+test);
 160             }
 161         }
 162     }
 163 
 164 
 165     private static final String[] TYPES = {
 166         "date",
 167         "time"
 168     };
 169     private static final String[] MODIFIERS = {
 170         "",
 171         "short",
 172         "medium", // Same as DEFAULT
 173         "long",
 174         "full"
 175     };
 176 
 177     void messageFormatTest() {
 178         for (Locale target : providerloc) {
 179             for (String type : TYPES) {
 180                 for (String modifier : MODIFIERS) {
 181                     String pattern, expected;
 182                     if (modifier.equals("")) {
 183                         pattern = String.format("%s={0,%s}", type, type);
 184                     } else {
 185                         pattern = String.format("%s={0,%s,%s}", type, type, modifier);
 186                     }
 187                     if (modifier.equals("medium")) {
 188                         // medium is default.
 189                         expected = String.format("%s={0,%s}", type, type);
 190                     } else {
 191                         expected = pattern;
 192                     }
 193                     MessageFormat mf = new MessageFormat(pattern, target);
 194                     Format[] fmts = mf.getFormats();
 195                     if (fmts[0] instanceof SimpleDateFormat) {
 196                         continue;
 197                     }
 198                     String toPattern = mf.toPattern();
 199                     if (!toPattern.equals(expected)) {
 200                         throw new RuntimeException("messageFormatTest: got '" + toPattern
 201                                                    + "', expected '" + expected + "'");
 202                     }
 203                 }
 204             }
 205         }
 206     }
 207 }
--- EOF ---