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 DateFormatSymbolsProviderTest extends ProviderTest {
  33 
  34     com.foo.DateFormatSymbolsProviderImpl dfsp = new com.foo.DateFormatSymbolsProviderImpl();
  35     List<Locale> availloc = Arrays.asList(DateFormatSymbols.getAvailableLocales());
  36     List<Locale> providerloc = Arrays.asList(dfsp.getAvailableLocales());
  37     List<Locale> jreloc = Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales());
  38     List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getDateFormatSymbolsProvider().getAvailableLocales());
  39 
  40     public static void main(String[] s) {
  41         new DateFormatSymbolsProviderTest();
  42     }
  43 
  44     DateFormatSymbolsProviderTest() {
  45         availableLocalesTest();
  46         objectValidityTest();
  47         hashCodeTest();
  48     }
  49 
  50     void availableLocalesTest() {
  51         Set<Locale> localesFromAPI = new HashSet<Locale>(availloc);
  52         Set<Locale> localesExpected = new HashSet<Locale>(jreimplloc);
  53         localesExpected.remove(Locale.ROOT);
  54         localesExpected.addAll(providerloc);
  55         if (localesFromAPI.equals(localesExpected)) {
  56             System.out.println("availableLocalesTest passed.");
  57         } else {
  58             throw new RuntimeException("availableLocalesTest failed");
  59         }
  60     }
  61 
  62     void objectValidityTest() {
  63 
  64         for (Locale target: availloc) {
  65             // pure JRE implementation
  66             ResourceBundle rb = LocaleProviderAdapter.forJRE().getLocaleData().getDateFormatData(target);
  67             boolean jreSupportsLocale = jreimplloc.contains(target);
  68 
  69             // JRE string arrays
  70             String[][] jres = new String[6][];
  71             if (jreSupportsLocale) {
  72                 try {
  73                     jres[0] = (String[])rb.getObject("MonthNames");
  74                     jres[1] = (String[])rb.getObject("MonthAbbreviations");
  75                     jres[2] = (String[])rb.getObject("DayNames");
  76                     jres[3] = (String[])rb.getObject("DayAbbreviations");
  77                     jres[4] = (String[])rb.getObject("AmPmMarkers");
  78                     jres[5] = (String[])rb.getObject("Eras");
  79                 } catch (MissingResourceException mre) {}
  80             }
  81 
  82             // result object
  83             DateFormatSymbols dfs = DateFormatSymbols.getInstance(target);
  84             String[][] result = new String[6][];
  85             result[0] = dfs.getMonths();
  86             result[1] = dfs.getShortMonths();
  87             // note that weekdays are 1-based
  88             String[] tmp = dfs.getWeekdays();
  89             result[2] = new String[7];
  90             System.arraycopy(tmp, 1, result[2], 0, result[2].length);
  91             tmp = dfs.getShortWeekdays();
  92             result[3] = new String[7];
  93             System.arraycopy(tmp, 1, result[3], 0, result[3].length);
  94             result[4] = dfs.getAmPmStrings();
  95             result[5] = dfs.getEras();
  96 
  97             // provider's object (if any)
  98             DateFormatSymbols providersDfs= null;
  99             String[][] providers = new String[6][];
 100             if (providerloc.contains(target)) {
 101                 providersDfs = dfsp.getInstance(target);
 102                 providers[0] = providersDfs.getMonths();
 103                 providers[1] = providersDfs.getShortMonths();
 104                 // note that weekdays are 1-based
 105                 tmp = dfs.getWeekdays();
 106                 providers[2] = new String[7];
 107                 System.arraycopy(tmp, 1, providers[2], 0, providers[2].length);
 108                 tmp = dfs.getShortWeekdays();
 109                 providers[3] = new String[7];
 110                 System.arraycopy(tmp, 1, providers[3], 0, providers[3].length);
 111                 providers[4] = providersDfs.getAmPmStrings();
 112                 providers[5] = providersDfs.getEras();
 113             }
 114 
 115             for (int i = 0; i < result.length; i ++) {
 116                 for (int j = 0; j < result[i].length; j++) {
 117                     String jresStr =
 118                         (jres[i] != null ? jres[i][j] : null);
 119                     String providersStr =
 120                         (providers[i] != null ? providers[i][j] : null);
 121                     String resultStr =
 122                         (result[i] != null ? result[i][j] : null);
 123                     checkValidity(target, jresStr, providersStr, resultStr, jreSupportsLocale);
 124                 }
 125             }
 126         }
 127     }
 128 
 129     // Bug 7200341.
 130     void hashCodeTest() {
 131         for (Locale target: availloc) {
 132             // look for provider's object
 133             DateFormatSymbols dfs = DateFormatSymbols.getInstance(target);
 134             if (dfs.getClass().getSimpleName().equals("FooDateFormatSymbols")) {
 135                 // call its hashCode(). success if no ArrayIndexOutOfBoundsException is thrown.
 136                 dfs.hashCode();
 137                 break;
 138             }
 139         }
 140     }
 141 }