jdk/test/java/util/PluggableLocale/DateFormatProviderTest.java
author xuelei
Fri, 29 Jul 2011 02:50:58 -0700
changeset 10138 b7572da25d15
parent 7787 d2420a14d0a2
child 13583 dc0017b1a452
permissions -rw-r--r--
7068662: Reserve and restore the default locale Reviewed-by: alanb, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
     2
 * Copyright (c) 2007, 2010, 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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.util.*;
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 DateFormatProviderTest extends ProviderTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    com.foo.DateFormatProviderImpl dfp = new com.foo.DateFormatProviderImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    List<Locale> availloc = Arrays.asList(DateFormat.getAvailableLocales());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    List<Locale> providerloc = Arrays.asList(dfp.getAvailableLocales());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    List<Locale> jreloc = Arrays.asList(LocaleData.getAvailableLocales());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public static void main(String[] s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        new DateFormatProviderTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    DateFormatProviderTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        availableLocalesTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        objectValidityTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        extendedVariantTest();
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
    47
        messageFormatTest();
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() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        Set<Locale> localesFromAPI = new HashSet<Locale>(availloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        Set<Locale> localesExpected = new HashSet<Locale>(jreloc);
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
            // Get the key for the date/time patterns which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            // specific to each calendar system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            Calendar cal = Calendar.getInstance(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            String key = "DateTimePatterns";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            if (!cal.getClass().getName().equals("java.util.GregorianCalendar")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                // e.g., "java.util.JapaneseImperialCalendar.DateTimePatterns"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                key = cal.getClass().getName() + "." + key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            // pure JRE implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            ResourceBundle rb = LocaleData.getDateFormatData(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            boolean jreSupportsLocale = jreloc.contains(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            // JRE string arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            String[] jreDateTimePatterns = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (jreSupportsLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    jreDateTimePatterns = (String[])rb.getObject(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                } catch (MissingResourceException mre) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            for (int style = DateFormat.FULL; style <= DateFormat.SHORT; style ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                // result object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                DateFormat result = DateFormat.getDateTimeInstance(style, style, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                // provider's object (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                DateFormat providersResult = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                if (providerloc.contains(target)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    providersResult = dfp.getDateTimeInstance(style, style, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                // JRE's object (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                DateFormat jresResult = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                if (jreSupportsLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    Object[] dateTimeArgs = {jreDateTimePatterns[style],
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                             jreDateTimePatterns[style + 4]};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    String pattern = MessageFormat.format(jreDateTimePatterns[8], dateTimeArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    jresResult = new SimpleDateFormat(pattern, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                checkValidity(target, jresResult, providersResult, result, jreSupportsLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // Check that fallback correctly occurs with locales with variant including '_'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // This test assumes that the provider supports the ja_JP_osaka locale, and JRE does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    void extendedVariantTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Locale[] testlocs = {new Locale("ja", "JP", "osaka_extended"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                             new Locale("ja", "JP", "osaka_extended_further"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                             new Locale("ja", "JP", "osaka_")};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        for (Locale test: testlocs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            DateFormat provider = dfp.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (!df.equals(provider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                throw new RuntimeException("variant fallback failed. test locale: "+test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   122
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   123
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   124
    private static final String[] TYPES = {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   125
        "date",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   126
        "time"
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   127
    };
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   128
    private static final String[] MODIFIERS = {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   129
        "",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   130
        "short",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   131
        "medium", // Same as DEFAULT
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   132
        "long",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   133
        "full"
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   134
    };
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   135
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   136
    void messageFormatTest() {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   137
        for (Locale target : providerloc) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   138
            for (String type : TYPES) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   139
                for (String modifier : MODIFIERS) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   140
                    String pattern, expected;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   141
                    if (modifier.equals("")) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   142
                        pattern = String.format("%s={0,%s}", type, type);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   143
                    } else {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   144
                        pattern = String.format("%s={0,%s,%s}", type, type, modifier);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   145
                    }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   146
                    if (modifier.equals("medium")) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   147
                        // medium is default.
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   148
                        expected = String.format("%s={0,%s}", type, type);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   149
                    } else {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   150
                        expected = pattern;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   151
                    }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   152
                    MessageFormat mf = new MessageFormat(pattern, target);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   153
                    Format[] fmts = mf.getFormats();
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   154
                    if (fmts[0] instanceof SimpleDateFormat) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   155
                        continue;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   156
                    }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   157
                    String toPattern = mf.toPattern();
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   158
                    if (!toPattern.equals(expected)) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   159
                        throw new RuntimeException("messageFormatTest: got '" + toPattern
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   160
                                                   + "', expected '" + expected + "'");
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   161
                    }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   162
                }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   163
            }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   164
        }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents: 5506
diff changeset
   165
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}