jdk/test/java/util/PluggableLocale/DateFormatSymbolsProviderTest.java
author naoto
Mon, 14 Jan 2013 11:09:53 -0800
changeset 15260 7af2d7a87806
parent 14418 967376921f20
child 23010 6dadb192ad81
permissions -rw-r--r--
7162007: Clean up i18n related caches Reviewed-by: okutsu, ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
     2
 * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
    29
import sun.util.locale.provider.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.util.resources.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class DateFormatSymbolsProviderTest extends ProviderTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    com.foo.DateFormatSymbolsProviderImpl dfsp = new com.foo.DateFormatSymbolsProviderImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    List<Locale> availloc = Arrays.asList(DateFormatSymbols.getAvailableLocales());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    List<Locale> providerloc = Arrays.asList(dfsp.getAvailableLocales());
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
    37
    List<Locale> jreloc = Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales());
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
    38
    List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getDateFormatSymbolsProvider().getAvailableLocales());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static void main(String[] s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        new DateFormatSymbolsProviderTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    DateFormatSymbolsProviderTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        availableLocalesTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        objectValidityTest();
14172
fad44417edf8 7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances
naoto
parents: 13583
diff changeset
    47
        hashCodeTest();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    void availableLocalesTest() {
14418
967376921f20 8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents: 14172
diff changeset
    51
        Set<Locale> localesFromAPI = new HashSet<>(availloc);
967376921f20 8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents: 14172
diff changeset
    52
        Set<Locale> localesExpected = new HashSet<>(jreloc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        localesExpected.addAll(providerloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (localesFromAPI.equals(localesExpected)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            System.out.println("availableLocalesTest passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            throw new RuntimeException("availableLocalesTest failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    void objectValidityTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        for (Locale target: availloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            // pure JRE implementation
15260
7af2d7a87806 7162007: Clean up i18n related caches
naoto
parents: 14418
diff changeset
    65
            ResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getDateFormatData(target);
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 5506
diff changeset
    66
            boolean jreSupportsLocale = jreimplloc.contains(target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            // JRE string arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            String[][] jres = new String[6][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (jreSupportsLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    jres[0] = (String[])rb.getObject("MonthNames");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    jres[1] = (String[])rb.getObject("MonthAbbreviations");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    jres[2] = (String[])rb.getObject("DayNames");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    jres[3] = (String[])rb.getObject("DayAbbreviations");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    jres[4] = (String[])rb.getObject("AmPmMarkers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    jres[5] = (String[])rb.getObject("Eras");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                } catch (MissingResourceException mre) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            // result object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            DateFormatSymbols dfs = DateFormatSymbols.getInstance(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            String[][] result = new String[6][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            result[0] = dfs.getMonths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            result[1] = dfs.getShortMonths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            // note that weekdays are 1-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            String[] tmp = dfs.getWeekdays();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            result[2] = new String[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            System.arraycopy(tmp, 1, result[2], 0, result[2].length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            tmp = dfs.getShortWeekdays();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            result[3] = new String[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            System.arraycopy(tmp, 1, result[3], 0, result[3].length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            result[4] = dfs.getAmPmStrings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            result[5] = dfs.getEras();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // provider's object (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            DateFormatSymbols providersDfs= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            String[][] providers = new String[6][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (providerloc.contains(target)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                providersDfs = dfsp.getInstance(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                providers[0] = providersDfs.getMonths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                providers[1] = providersDfs.getShortMonths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                // note that weekdays are 1-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                tmp = dfs.getWeekdays();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                providers[2] = new String[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                System.arraycopy(tmp, 1, providers[2], 0, providers[2].length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                tmp = dfs.getShortWeekdays();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                providers[3] = new String[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                System.arraycopy(tmp, 1, providers[3], 0, providers[3].length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                providers[4] = providersDfs.getAmPmStrings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                providers[5] = providersDfs.getEras();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            for (int i = 0; i < result.length; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                for (int j = 0; j < result[i].length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    String jresStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        (jres[i] != null ? jres[i][j] : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    String providersStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        (providers[i] != null ? providers[i][j] : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    String resultStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        (result[i] != null ? result[i][j] : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    checkValidity(target, jresStr, providersStr, resultStr, jreSupportsLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
14172
fad44417edf8 7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances
naoto
parents: 13583
diff changeset
   127
fad44417edf8 7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances
naoto
parents: 13583
diff changeset
   128
    // Bug 7200341.
fad44417edf8 7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances
naoto
parents: 13583
diff changeset
   129
    void hashCodeTest() {
fad44417edf8 7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances
naoto
parents: 13583
diff changeset
   130
        for (Locale target: availloc) {
fad44417edf8 7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances
naoto
parents: 13583
diff changeset
   131
            // look for provider's object
fad44417edf8 7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances
naoto
parents: 13583
diff changeset
   132
            DateFormatSymbols dfs = DateFormatSymbols.getInstance(target);
fad44417edf8 7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances
naoto
parents: 13583
diff changeset
   133
            if (dfs.getClass().getSimpleName().equals("FooDateFormatSymbols")) {
fad44417edf8 7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances
naoto
parents: 13583
diff changeset
   134
                // call its hashCode(). success if no ArrayIndexOutOfBoundsException is thrown.
fad44417edf8 7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances
naoto
parents: 13583
diff changeset
   135
                dfs.hashCode();
fad44417edf8 7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances
naoto
parents: 13583
diff changeset
   136
                break;
fad44417edf8 7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances
naoto
parents: 13583
diff changeset
   137
            }
fad44417edf8 7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances
naoto
parents: 13583
diff changeset
   138
        }
fad44417edf8 7200341: DateFormatSymbols.hashCode() throws ArrayIndexOutOfBoundsException in some circumstances
naoto
parents: 13583
diff changeset
   139
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}