jdk/test/java/util/ResourceBundle/Control/ControlFactoryTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright (c) 2007 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 5102289
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test the ResourceBundle.Control factory methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import static java.util.ResourceBundle.Control.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class ControlFactoryTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
     * An interface for calling ResourceBundle.Control.getControl or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
     * ResourceBundle.Control.getNoFallbackControl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static interface Factory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        public ResourceBundle.Control getControl(List<String> formats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        public String name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static int errors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        // Test getControl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        testControlFactory(new Factory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                public ResourceBundle.Control getControl(List<String> formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                    return ResourceBundle.Control.getControl(formats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                public String name() { return "getControl"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            }, Locale.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        // Test getNoFallbackControl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        testControlFactory(new Factory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                public ResourceBundle.Control getControl(List<String> formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                    return ResourceBundle.Control.getNoFallbackControl(formats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                public String name() { return "getNoFallbackControl"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            }, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (errors > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            throw new RuntimeException("FAILED: " + errors + " error(s)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static void testControlFactory(Factory factory, Locale loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        testGetControl(factory, loc, FORMAT_DEFAULT, "java.class", "java.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        testGetControl(factory, loc, FORMAT_CLASS, "java.class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        testGetControl(factory, loc, FORMAT_PROPERTIES, "java.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // test IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        String[][] data = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            { "java.class", "java.properties", "java.xml" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            { "java.class", "java.props" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            { "java.properties", "java.class" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            { "java.foo", "java.properties" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            { "java.foo" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            { null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        for (String[] fmts : data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                List<String> fmt = Arrays.asList(fmts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                ResourceBundle.Control control = factory.getControl(fmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                error("getControl: %s%n", fmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // test NPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            ResourceBundle.Control control = factory.getControl(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            error("%s: doesn't throw NPE.%n", factory.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static void testGetControl(Factory factory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                       Locale loc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                       final List<String> FORMATS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                       String... fmtStrings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        final ResourceBundle.Control CONTROL = factory.getControl(FORMATS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        List<String> fmt = CONTROL.getFormats("any");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (fmt != FORMATS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            error("%s: returns %s, expected %s.%n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                  factory.name(), fmt, FORMATS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        ResourceBundle.Control control = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // Check if getControl always returns the expected singleton.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        for (int i = 0; i < 10; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            fmt = Arrays.asList(fmtStrings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            control = factory.getControl(fmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (control != CONTROL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                error("%s: doesn't return the singleton: got %s, expected %s%n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                      factory.name(), control, CONTROL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // Check if getFallbackLocale performs as expected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        Locale defaultLocale = Locale.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        Locale nonDefaultLocale = defaultLocale.equals(Locale.US) ? Locale.JAPAN : Locale.US;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (loc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            // Test ResourceBundle.Control.getControl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            Locale l = CONTROL.getFallbackLocale("any", nonDefaultLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (!defaultLocale.equals(l)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                error("%s: getFallbackLocale doesn't return default locale. got %s, expected %s%n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                      factory.name(), toString(l), toString(defaultLocale));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            l = CONTROL.getFallbackLocale("any", defaultLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                error("%s: getFallbackLocale doesn't return null. got %s%n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                      factory.name(), toString(l));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            // Test ResourceBundle.Control.getNoFallbackControl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            Locale l = CONTROL.getFallbackLocale("any", nonDefaultLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                error("%s: getFallbackLocale doesn't return null. got %s%n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                      factory.name(), toString(l));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            l = CONTROL.getFallbackLocale("any", defaultLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                error("%s: getFallbackLocale doesn't return null. got %s%n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                      factory.name(), toString(l));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private static String toString(Locale loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (loc == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return "\"" + loc.getLanguage() + "_" + loc.getCountry() + "_" + loc.getVariant() + "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static void error(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        System.out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        errors++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private static void error(String fmt, Object... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        System.out.printf(fmt, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        errors++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}