jdk/test/java/util/ResourceBundle/ResourceBundleTest.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 4049325 4073127 4083270 4106034 4108126
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
    @summary test Resource Bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
    @build TestResource TestResource_de TestResource_fr TestResource_fr_CH
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
    @build TestResource_it FakeTestResource
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
    @run main ResourceBundleTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Portions copyright (c) 2007 Sun Microsystems, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The original version of this source code and documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * subsidiary of IBM. These materials are provided under terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * of a License Agreement between Taligent and Sun. This technology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * is protected by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Permission to use, copy, modify, and distribute this software
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * and its documentation for NON-COMMERCIAL purposes and without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * fee is hereby granted provided that this copyright notice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * appears in all copies. Please refer to the file "copyright.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * for further important copyright and licensing information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public class ResourceBundleTest extends RBTestFmwk {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        new ResourceBundleTest().run(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public ResourceBundleTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        makePropertiesFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public void TestResourceBundle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Locale  saveDefault = Locale.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        Locale.setDefault(new Locale("fr", "FR"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // load up the resource bundle, and make sure we got the right one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        ResourceBundle  bundle = ResourceBundle.getBundle("TestResource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (!bundle.getClass().getName().equals("TestResource_fr"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            errln("Expected TestResource_fr, got " + bundle.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // these resources are defines in ResourceBundle_fr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        String  test1 = bundle.getString("Time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (!test1.equals("Time keeps on slipping..."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            errln("TestResource_fr returned wrong value for \"Time\":  got " + test1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        test1 = bundle.getString("For");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (!test1.equals("Four score and seven years ago..."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            errln("TestResource_fr returned wrong value for \"For\":  got " + test1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        String[] test2 = bundle.getStringArray("All");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (test2.length != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            errln("TestResource_fr returned wrong number of elements for \"All\": got " + test2.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        else if (!test2[0].equals("'Twas brillig, and the slithy toves") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                 !test2[1].equals("Did gyre and gimble in the wabe.") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                 !test2[2].equals("All mimsy were the borogoves,") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                 !test2[3].equals("And the mome raths outgrabe."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            errln("TestResource_fr returned the wrong value for one of the elements in \"All\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        Object  test3 = bundle.getObject("Good");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (test3 == null || test3.getClass() != Integer.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            errln("TestResource_fr returned an object of the wrong class for \"Good\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        else if (((Integer)test3).intValue() != 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            errln("TestResource_fr returned the wrong value for \"Good\": got " + test3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // This resource is defined in TestResource and inherited by TestResource_fr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        test2 = bundle.getStringArray("Men");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (test2.length != 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            errln("TestResource_fr returned wrong number of elements for \"Men\": got " + test2.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        else if (!test2[0].equals("1") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                 !test2[1].equals("2") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                 !test2[2].equals("C"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            errln("TestResource_fr returned the wrong value for one of the elements in \"All\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // This resource is defined in neither TestResource not TestResource_fr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            test3 = bundle.getObject("Is");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            errln("TestResource_fr returned a value for \"Is\" when it shouldn't: got " + test3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        String[] keys = { "Now", "Time", "For", "All", "Good", "Men", "Come" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        checkKeys(bundle.getKeys(),  keys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Locale.setDefault(saveDefault);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public void TestListResourceBundle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // load up the resource and check to make sure we got the right class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // (we don't define be_BY or be, so we fall back on the root default)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        ResourceBundle  bundle = ResourceBundle.getBundle("TestResource",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                            new Locale("be", "BY"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (!bundle.getClass().getName().equals("TestResource"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            errln("Expected TestResource, got " + bundle.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        doListResourceBundleTest(bundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @bug 4073127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Repeat TestListResourceBundle on TestResource_it, which is a ListResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * with NO contents.  It should gracefully inherit everything from the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * TestResource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public void TestEmptyListResourceBundle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        ResourceBundle bundle = ResourceBundle.getBundle("TestResource",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                            new Locale("it", "IT"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        doListResourceBundleTest(bundle);
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 void doListResourceBundleTest(ResourceBundle bundle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // load up the resource and check to make sure we got the right class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // all of these resources are defined in TestResource; it doesn' inherit from anybody
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        String  test1 = bundle.getString("Now");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (!test1.equals("Now is the time for all..."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            errln("TestResource returned wrong value for \"Now\":  got " + test1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        test1 = bundle.getString("Time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (!test1.equals("Howdy Doody Time!"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            errln("TestResource returned wrong value for \"Time\":  got " + test1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        test1 = bundle.getString("Come");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (!test1.equals("Come into my parlor..."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            errln("TestResource returned wrong value for \"Come\":  got " + test1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        Object  test3 = bundle.getObject("Good");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (test3 == null || test3.getClass() != Integer.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            errln("TestResource returned an object of the wrong class for \"Good\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        else if (((Integer)test3).intValue() != 27)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            errln("TestResource returned the wrong value for \"Good\": got " + test3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        String[] test2 = bundle.getStringArray("Men");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (test2.length != 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            errln("TestResource returned wrong number of elements for \"Men\": got " + test2.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        else if (!test2[0].equals("1") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                 !test2[1].equals("2") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                 !test2[2].equals("C"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            errln("TestResource returned the wrong value for one of the elements in \"All\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // this item isn't defined in TestResource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            test3 = bundle.getObject("All");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            errln("TestResource_en returned a value for \"All\" when it shouldn't: got " + test3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        String[] keys = { "Now", "Time", "Good", "Men", "Come" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        checkKeys(bundle.getKeys(), keys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @bug 4049325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @ summary Bug 4049325 says ResourceBundle.findBundle() uses a hard-coded '/' as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * the directory separator when searching for properties files.  Interestingly, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * still works on my NT installation.  I can't tell whether this is a required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * property of all Java implementations (the magic appears to happen ClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * getResourceAsStream(), which is a native function) or a lucky property of my
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * particular implementation.  If this bug regresses, this test may still pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * because a lower-level facility translates the / to the platform-specific separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * for us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public void TestPropertyResourceBundle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        ResourceBundle  bundle = ResourceBundle.getBundle("TestResource",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                            new Locale("es", "ES"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // these resources are defined in TestResource_es.properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        String  test = bundle.getString("Now");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (!test.equals("How now brown cow"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            errln("TestResource_es returned wrong value for \"Now\":  got " + test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        test = bundle.getString("Is");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (!test.equals("Is there a dog?"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            errln("TestResource_es returned wrong value for \"Is\":  got " + test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        test = bundle.getString("The");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (!test.equals("The rain in Spain"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            errln("TestResource_es returned wrong value for \"The\":  got " + test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        test = bundle.getString("Time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (!test.equals("Time marches on..."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            errln("TestResource_es returned wrong value for \"Time\":  got " + test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // this resource is defined in TestResource and inherited by TestResource_es
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        String[] test2 = bundle.getStringArray("Men");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (test2.length != 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            errln("TestResource returned wrong number of elements for \"Men\": got " + test2.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        else if (!test2[0].equals("1") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                 !test2[1].equals("2") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                 !test2[2].equals("C"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            errln("TestResource returned the wrong value for one of the elements in \"All\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // this resource is defined in neither TestResource nor TestResource_es
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            test = bundle.getString("All");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            errln("TestResource_es returned a value for \"All\" when it shouldn't: got " + test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        String[] keys = { "Now", "Is", "The", "Time", "Good", "Men", "Come" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        checkKeys(bundle.getKeys(), keys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @bug 4108126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public void TestGetLocale() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // try to find TestResource_fr_CH.  Should get fr_CH as its locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        ResourceBundle test = ResourceBundle.getBundle("TestResource",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        new Locale("fr", "CH", ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        Locale locale = test.getLocale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // try to find TestResource_fr_BE, which doesn't exist.  Should get fr as its locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        test = ResourceBundle.getBundle("TestResource",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        new Locale("fr", "BE", ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        locale = test.getLocale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // try to find TestResource_iw_IL, which doesn't exist.  Should get root locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // as its locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        test = ResourceBundle.getBundle("TestResource",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        new Locale("iw", "IL", ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        locale = test.getLocale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                            + locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @bug 4083270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public void TestNonSubclass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // ResourceBundle.getBundle should never return an object that isn't an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // of ResourceBundle or one of its subclasses.  We have a class called FakeTestResource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // in this package that isn't a ResourceBundle.  If we get that back, we barf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // (Actually, at the time I fixed this bug, getResource() would throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // ClassCastException in that case.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // There's also a properties file called FakeTestResource; we should get back a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        // PropertyResourceBundle pointing to that file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        Object test1 = ResourceBundle.getBundle("FakeTestResource",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                Locale.US);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (!(test1 instanceof ResourceBundle))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            errln("Got back a " + test1.getClass().getName() + " instead of a PropertyResourceBundle when looking for FakeTestResource.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        ResourceBundle test = (ResourceBundle)test1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        // there's also a properties file called FakeTestResource.  getBundle() should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // find it, and it should have the following contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        String message = test.getString("message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (!message.equals("Hello!"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            errln("Supposedly found FakeTestResource.properties, but it had the wrong contents.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @bug 4106034
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public void TestErrorMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        // Ensure that the message produced by the exception thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        // by ResourceBundle.getObject contains both the class name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        // the key name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        final String className = "TestResource";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        final String keyName = "DontGetThis";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        ResourceBundle bundle = ResourceBundle.getBundle(className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                            new Locale("it", "IT"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            Object o = bundle.getObject(keyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            errln(bundle.getClass().getName()+" returned a value for tag \""+keyName+"\" when it should have thrown an exception.  It returned "+o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            String message = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (message.indexOf(className) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    errln("MissingResourceException error message did not contain class name.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (message.indexOf(keyName) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    errln("MissingResourceException error message did not contain resource key name.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private void makePropertiesFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // The getProperty call is to ensure that this test will work with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            // the JTREG test harness.  When running in the harness, the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            // directory is often set to someplace that isn't on the CLASSPATH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            // so we can't just create the properties files in the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            // directory.  But the harness uses the "test.classes" property to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            // tell us where the classes directory is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            String classesDir = System.getProperty("test.classes", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            File    file = new File(classesDir, "TestResource_es.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            if (!file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                FileOutputStream stream = new FileOutputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                Properties  props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                props.put("Now", "How now brown cow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                props.put("Is", "Is there a dog?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                props.put("The", "The rain in Spain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                props.put("Time", "Time marches on...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                props.save(stream, "Test property list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                stream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            file = new File(classesDir, "FakeTestResource.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (!file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                FileOutputStream stream = new FileOutputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                props.put("message", "Hello!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                props.save(stream, "Test property list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                stream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        catch (java.io.IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            errln("Got exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    private void checkKeys(Enumeration testKeys, String[] expectedKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        Hashtable   hash = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        String      element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        int         elementCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        for (int i=0; i < expectedKeys.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            hash.put(expectedKeys[i], expectedKeys[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        while (testKeys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            element = (String)testKeys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            elementCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (!hash.containsKey(element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                errln(element + " missing from key list.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (elementCount != expectedKeys.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            errln("Wrong number of elements in key list: expected " + expectedKeys.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                " got " + elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
}