0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A/*
0N/A @test
0N/A @bug 4049325 4073127 4083270 4106034 4108126
0N/A @summary test Resource Bundle
0N/A @build TestResource TestResource_de TestResource_fr TestResource_fr_CH
0N/A @build TestResource_it FakeTestResource
0N/A @run main ResourceBundleTest
0N/A*/
0N/A/*
0N/A *
0N/A *
0N/A * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
0N/A * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
0N/A *
0N/A * Portions copyright (c) 2007 Sun Microsystems, Inc.
0N/A * All Rights Reserved.
0N/A *
0N/A * The original version of this source code and documentation
0N/A * is copyrighted and owned by Taligent, Inc., a wholly-owned
0N/A * subsidiary of IBM. These materials are provided under terms
0N/A * of a License Agreement between Taligent and Sun. This technology
0N/A * is protected by multiple US and International patents.
0N/A *
0N/A * This notice and attribution to Taligent may not be removed.
0N/A * Taligent is a registered trademark of Taligent, Inc.
0N/A *
0N/A * Permission to use, copy, modify, and distribute this software
0N/A * and its documentation for NON-COMMERCIAL purposes and without
0N/A * fee is hereby granted provided that this copyright notice
0N/A * appears in all copies. Please refer to the file "copyright.html"
0N/A * for further important copyright and licensing information.
0N/A *
0N/A * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
0N/A * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
0N/A * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
0N/A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
0N/A * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
0N/A * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
0N/A *
0N/A */
0N/A
0N/Aimport java.text.*;
0N/Aimport java.util.*;
0N/Aimport java.io.*;
0N/A
0N/Apublic class ResourceBundleTest extends RBTestFmwk {
0N/A public static void main(String[] args) throws Exception {
0N/A new ResourceBundleTest().run(args);
0N/A }
0N/A
0N/A public ResourceBundleTest() {
0N/A makePropertiesFile();
0N/A }
0N/A
0N/A public void TestResourceBundle() {
0N/A Locale saveDefault = Locale.getDefault();
0N/A Locale.setDefault(new Locale("fr", "FR"));
0N/A
0N/A // load up the resource bundle, and make sure we got the right one
0N/A ResourceBundle bundle = ResourceBundle.getBundle("TestResource");
0N/A if (!bundle.getClass().getName().equals("TestResource_fr"))
0N/A errln("Expected TestResource_fr, got " + bundle.getClass().getName());
0N/A
0N/A // these resources are defines in ResourceBundle_fr
0N/A String test1 = bundle.getString("Time");
0N/A if (!test1.equals("Time keeps on slipping..."))
0N/A errln("TestResource_fr returned wrong value for \"Time\": got " + test1);
0N/A
0N/A test1 = bundle.getString("For");
0N/A if (!test1.equals("Four score and seven years ago..."))
0N/A errln("TestResource_fr returned wrong value for \"For\": got " + test1);
0N/A
0N/A String[] test2 = bundle.getStringArray("All");
0N/A if (test2.length != 4)
0N/A errln("TestResource_fr returned wrong number of elements for \"All\": got " + test2.length);
0N/A else if (!test2[0].equals("'Twas brillig, and the slithy toves") ||
0N/A !test2[1].equals("Did gyre and gimble in the wabe.") ||
0N/A !test2[2].equals("All mimsy were the borogoves,") ||
0N/A !test2[3].equals("And the mome raths outgrabe."))
0N/A errln("TestResource_fr returned the wrong value for one of the elements in \"All\"");
0N/A
0N/A Object test3 = bundle.getObject("Good");
0N/A if (test3 == null || test3.getClass() != Integer.class)
0N/A errln("TestResource_fr returned an object of the wrong class for \"Good\"");
0N/A else if (((Integer)test3).intValue() != 3)
0N/A errln("TestResource_fr returned the wrong value for \"Good\": got " + test3);
0N/A
0N/A // This resource is defined in TestResource and inherited by TestResource_fr
0N/A test2 = bundle.getStringArray("Men");
0N/A if (test2.length != 3)
0N/A errln("TestResource_fr returned wrong number of elements for \"Men\": got " + test2.length);
0N/A else if (!test2[0].equals("1") ||
0N/A !test2[1].equals("2") ||
0N/A !test2[2].equals("C"))
0N/A errln("TestResource_fr returned the wrong value for one of the elements in \"All\"");
0N/A
0N/A // This resource is defined in neither TestResource not TestResource_fr
0N/A try {
0N/A test3 = bundle.getObject("Is");
0N/A errln("TestResource_fr returned a value for \"Is\" when it shouldn't: got " + test3);
0N/A }
0N/A catch (MissingResourceException e) {
0N/A }
0N/A
0N/A String[] keys = { "Now", "Time", "For", "All", "Good", "Men", "Come" };
0N/A checkKeys(bundle.getKeys(), keys);
0N/A
0N/A Locale.setDefault(saveDefault);
0N/A }
0N/A
0N/A public void TestListResourceBundle() {
0N/A // load up the resource and check to make sure we got the right class
0N/A // (we don't define be_BY or be, so we fall back on the root default)
0N/A ResourceBundle bundle = ResourceBundle.getBundle("TestResource",
0N/A new Locale("be", "BY"));
0N/A if (!bundle.getClass().getName().equals("TestResource"))
0N/A errln("Expected TestResource, got " + bundle.getClass().getName());
0N/A
0N/A doListResourceBundleTest(bundle);
0N/A }
0N/A
0N/A /**
0N/A * @bug 4073127
0N/A * Repeat TestListResourceBundle on TestResource_it, which is a ListResourceBundle
0N/A * with NO contents. It should gracefully inherit everything from the root
0N/A * TestResource.
0N/A */
0N/A public void TestEmptyListResourceBundle() {
0N/A ResourceBundle bundle = ResourceBundle.getBundle("TestResource",
0N/A new Locale("it", "IT"));
0N/A doListResourceBundleTest(bundle);
0N/A }
0N/A
0N/A private void doListResourceBundleTest(ResourceBundle bundle) {
0N/A // load up the resource and check to make sure we got the right class
0N/A // all of these resources are defined in TestResource; it doesn' inherit from anybody
0N/A String test1 = bundle.getString("Now");
0N/A if (!test1.equals("Now is the time for all..."))
0N/A errln("TestResource returned wrong value for \"Now\": got " + test1);
0N/A
0N/A test1 = bundle.getString("Time");
0N/A if (!test1.equals("Howdy Doody Time!"))
0N/A errln("TestResource returned wrong value for \"Time\": got " + test1);
0N/A
0N/A test1 = bundle.getString("Come");
0N/A if (!test1.equals("Come into my parlor..."))
0N/A errln("TestResource returned wrong value for \"Come\": got " + test1);
0N/A
0N/A Object test3 = bundle.getObject("Good");
0N/A if (test3 == null || test3.getClass() != Integer.class)
0N/A errln("TestResource returned an object of the wrong class for \"Good\"");
0N/A else if (((Integer)test3).intValue() != 27)
0N/A errln("TestResource returned the wrong value for \"Good\": got " + test3);
0N/A
0N/A String[] test2 = bundle.getStringArray("Men");
0N/A if (test2.length != 3)
0N/A errln("TestResource returned wrong number of elements for \"Men\": got " + test2.length);
0N/A else if (!test2[0].equals("1") ||
0N/A !test2[1].equals("2") ||
0N/A !test2[2].equals("C"))
0N/A errln("TestResource returned the wrong value for one of the elements in \"All\"");
0N/A
0N/A // this item isn't defined in TestResource
0N/A try {
0N/A test3 = bundle.getObject("All");
0N/A errln("TestResource_en returned a value for \"All\" when it shouldn't: got " + test3);
0N/A }
0N/A catch (MissingResourceException e) {
0N/A }
0N/A
0N/A String[] keys = { "Now", "Time", "Good", "Men", "Come" };
0N/A checkKeys(bundle.getKeys(), keys);
0N/A }
0N/A
0N/A /**
0N/A * @bug 4049325
0N/A * @ summary Bug 4049325 says ResourceBundle.findBundle() uses a hard-coded '/' as
0N/A * the directory separator when searching for properties files. Interestingly, it
0N/A * still works on my NT installation. I can't tell whether this is a required
0N/A * property of all Java implementations (the magic appears to happen ClassLoader.
0N/A * getResourceAsStream(), which is a native function) or a lucky property of my
0N/A * particular implementation. If this bug regresses, this test may still pass
0N/A * because a lower-level facility translates the / to the platform-specific separator
0N/A * for us.
0N/A */
0N/A public void TestPropertyResourceBundle() {
0N/A ResourceBundle bundle = ResourceBundle.getBundle("TestResource",
0N/A new Locale("es", "ES"));
0N/A
0N/A // these resources are defined in TestResource_es.properties
0N/A String test = bundle.getString("Now");
0N/A if (!test.equals("How now brown cow"))
0N/A errln("TestResource_es returned wrong value for \"Now\": got " + test);
0N/A
0N/A test = bundle.getString("Is");
0N/A if (!test.equals("Is there a dog?"))
0N/A errln("TestResource_es returned wrong value for \"Is\": got " + test);
0N/A
0N/A test = bundle.getString("The");
0N/A if (!test.equals("The rain in Spain"))
0N/A errln("TestResource_es returned wrong value for \"The\": got " + test);
0N/A
0N/A test = bundle.getString("Time");
0N/A if (!test.equals("Time marches on..."))
0N/A errln("TestResource_es returned wrong value for \"Time\": got " + test);
0N/A
0N/A // this resource is defined in TestResource and inherited by TestResource_es
0N/A String[] test2 = bundle.getStringArray("Men");
0N/A if (test2.length != 3)
0N/A errln("TestResource returned wrong number of elements for \"Men\": got " + test2.length);
0N/A else if (!test2[0].equals("1") ||
0N/A !test2[1].equals("2") ||
0N/A !test2[2].equals("C"))
0N/A errln("TestResource returned the wrong value for one of the elements in \"All\"");
0N/A
0N/A // this resource is defined in neither TestResource nor TestResource_es
0N/A try {
0N/A test = bundle.getString("All");
0N/A errln("TestResource_es returned a value for \"All\" when it shouldn't: got " + test);
0N/A }
0N/A catch (MissingResourceException e) {
0N/A }
0N/A
0N/A String[] keys = { "Now", "Is", "The", "Time", "Good", "Men", "Come" };
0N/A checkKeys(bundle.getKeys(), keys);
0N/A }
0N/A
0N/A /**
0N/A * @bug 4108126
0N/A */
0N/A public void TestGetLocale() {
0N/A // try to find TestResource_fr_CH. Should get fr_CH as its locale
0N/A ResourceBundle test = ResourceBundle.getBundle("TestResource",
0N/A new Locale("fr", "CH", ""));
0N/A Locale locale = test.getLocale();
0N/A if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
0N/A errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
0N/A
0N/A // try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale
0N/A test = ResourceBundle.getBundle("TestResource",
0N/A new Locale("fr", "BE", ""));
0N/A locale = test.getLocale();
0N/A if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
0N/A errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
0N/A
0N/A // try to find TestResource_iw_IL, which doesn't exist. Should get root locale
0N/A // as its locale
0N/A test = ResourceBundle.getBundle("TestResource",
0N/A new Locale("iw", "IL", ""));
0N/A locale = test.getLocale();
0N/A if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
0N/A errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
0N/A + locale);
0N/A }
0N/A
0N/A /*
0N/A * @bug 4083270
0N/A */
0N/A public void TestNonSubclass() {
0N/A // ResourceBundle.getBundle should never return an object that isn't an instance
0N/A // of ResourceBundle or one of its subclasses. We have a class called FakeTestResource
0N/A // in this package that isn't a ResourceBundle. If we get that back, we barf.
0N/A // (Actually, at the time I fixed this bug, getResource() would throw a
0N/A // ClassCastException in that case.)
0N/A // There's also a properties file called FakeTestResource; we should get back a
0N/A // PropertyResourceBundle pointing to that file.
0N/A Object test1 = ResourceBundle.getBundle("FakeTestResource",
0N/A Locale.US);
0N/A
0N/A if (!(test1 instanceof ResourceBundle))
0N/A errln("Got back a " + test1.getClass().getName() + " instead of a PropertyResourceBundle when looking for FakeTestResource.");
0N/A
0N/A ResourceBundle test = (ResourceBundle)test1;
0N/A
0N/A // there's also a properties file called FakeTestResource. getBundle() should
0N/A // find it, and it should have the following contents
0N/A String message = test.getString("message");
0N/A if (!message.equals("Hello!"))
0N/A errln("Supposedly found FakeTestResource.properties, but it had the wrong contents.");
0N/A }
0N/A
0N/A /*
0N/A * @bug 4106034
0N/A */
0N/A public void TestErrorMessage() {
0N/A // Ensure that the message produced by the exception thrown
0N/A // by ResourceBundle.getObject contains both the class name and
0N/A // the key name.
0N/A final String className = "TestResource";
0N/A final String keyName = "DontGetThis";
0N/A ResourceBundle bundle = ResourceBundle.getBundle(className,
0N/A new Locale("it", "IT"));
0N/A try {
0N/A Object o = bundle.getObject(keyName);
0N/A errln(bundle.getClass().getName()+" returned a value for tag \""+keyName+"\" when it should have thrown an exception. It returned "+o);
0N/A } catch (MissingResourceException e) {
0N/A String message = e.getMessage();
0N/A boolean found = false;
0N/A if (message.indexOf(className) < 0) {
0N/A errln("MissingResourceException error message did not contain class name.");
0N/A }
0N/A if (message.indexOf(keyName) < 0) {
0N/A errln("MissingResourceException error message did not contain resource key name.");
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A private void makePropertiesFile() {
0N/A try {
0N/A //
0N/A // The getProperty call is to ensure that this test will work with
0N/A // the JTREG test harness. When running in the harness, the current
0N/A // directory is often set to someplace that isn't on the CLASSPATH,
0N/A // so we can't just create the properties files in the current
0N/A // directory. But the harness uses the "test.classes" property to
0N/A // tell us where the classes directory is.
0N/A //
0N/A String classesDir = System.getProperty("test.classes", ".");
0N/A File file = new File(classesDir, "TestResource_es.properties");
0N/A if (!file.exists()) {
0N/A FileOutputStream stream = new FileOutputStream(file);
0N/A Properties props = new Properties();
0N/A
0N/A props.put("Now", "How now brown cow");
0N/A props.put("Is", "Is there a dog?");
0N/A props.put("The", "The rain in Spain");
0N/A props.put("Time", "Time marches on...");
0N/A
0N/A props.save(stream, "Test property list");
0N/A
0N/A stream.close();
0N/A }
0N/A
0N/A file = new File(classesDir, "FakeTestResource.properties");
0N/A if (!file.exists()) {
0N/A FileOutputStream stream = new FileOutputStream(file);
0N/A Properties props = new Properties();
0N/A
0N/A props.put("message", "Hello!");
0N/A
0N/A props.save(stream, "Test property list");
0N/A
0N/A stream.close();
0N/A }
0N/A }
0N/A catch (java.io.IOException e) {
0N/A errln("Got exception: " + e);
0N/A }
0N/A }
0N/A
0N/A private void checkKeys(Enumeration testKeys, String[] expectedKeys) {
0N/A Hashtable hash = new Hashtable();
0N/A String element;
0N/A int elementCount = 0;
0N/A
0N/A for (int i=0; i < expectedKeys.length; i++)
0N/A hash.put(expectedKeys[i], expectedKeys[i]);
0N/A
0N/A while (testKeys.hasMoreElements()) {
0N/A element = (String)testKeys.nextElement();
0N/A elementCount++;
0N/A if (!hash.containsKey(element))
0N/A errln(element + " missing from key list.");
0N/A }
0N/A
0N/A if (elementCount != expectedKeys.length)
0N/A errln("Wrong number of elements in key list: expected " + expectedKeys.length +
0N/A " got " + elementCount);
0N/A }
0N/A}