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 5102289 6278334
0N/A * @summary Test the default Control implementation. The expiration
0N/A * functionality of newBundle, getTimeToLive, and needsReload is
0N/A * tested by ExpirationTest.sh. The factory methods are tested
0N/A * separately.
0N/A * @build TestResourceRB
0N/A * @build NonResourceBundle
0N/A * @run main DefaultControlTest
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport static java.util.ResourceBundle.Control.*;
0N/A
0N/Apublic class DefaultControlTest {
0N/A // The ResourceBundle.Control instance
0N/A static final ResourceBundle.Control CONTROL
0N/A = ResourceBundle.Control.getControl(FORMAT_DEFAULT);
0N/A
0N/A static final ResourceBundle BUNDLE = new ResourceBundle() {
0N/A public Enumeration<String> getKeys() { return null; }
0N/A protected Object handleGetObject(String key) { return null; }
0N/A };
0N/A
0N/A static final String CLAZZ = FORMAT_CLASS.get(0);
0N/A
0N/A static final String PROPERTIES = FORMAT_PROPERTIES.get(0);
0N/A
0N/A static final ClassLoader LOADER = DefaultControlTest.class.getClassLoader();
0N/A
0N/A // Full arguments for NPE testing
0N/A static final Object[] FULLARGS = { "any",
0N/A Locale.US,
0N/A FORMAT_PROPERTIES.get(0),
0N/A LOADER,
0N/A BUNDLE };
0N/A
0N/A static int errors;
0N/A
0N/A public static void main(String[] args) {
0N/A checkConstants();
0N/A
0N/A // Test getFormats(String)
0N/A testGetFormats();
0N/A
0N/A // Test getCandidateLocales(String, Locale)
0N/A testGetCandidateLocales();
0N/A
0N/A // Test getFallbackLocale(String, Locale)
0N/A testGetFallbackLocale();
0N/A
0N/A // Test newBundle(String, Locale, String, ClassLoader, boolean)
0N/A testNewBundle();
0N/A
0N/A // Test toBundleName(String, Locale)
0N/A testToBundleName();
0N/A
0N/A // Test getTimeToLive(String, Locale)
0N/A testGetTimeToLive();
0N/A
0N/A // Test needsReload(String, Locale, String, ClassLoader,
0N/A // ResourceBundle, long)
0N/A testNeedsReload();
0N/A
0N/A // Test toResourceName(String, String)
0N/A testToResourceName();
0N/A
0N/A if (errors > 0) {
0N/A throw new RuntimeException("FAILED: " + errors + " error(s)");
0N/A }
0N/A }
0N/A
0N/A private static void checkConstants() {
0N/A // Check FORMAT_*
0N/A if (!CONTROL.FORMAT_DEFAULT.equals(Arrays.asList("java.class",
0N/A "java.properties"))) {
0N/A error("Wrong Control.FORMAT_DEFAULT");
0N/A }
0N/A checkImmutableList(CONTROL.FORMAT_DEFAULT);
0N/A if (!CONTROL.FORMAT_CLASS.equals(Arrays.asList("java.class"))) {
0N/A error("Wrong Control.FORMAT_CLASS");
0N/A }
0N/A checkImmutableList(CONTROL.FORMAT_CLASS);
0N/A if (!CONTROL.FORMAT_PROPERTIES.equals(Arrays.asList("java.properties"))) {
0N/A error("Wrong Control.FORMAT_PROPERTIES");
0N/A }
0N/A checkImmutableList(CONTROL.FORMAT_PROPERTIES);
0N/A
0N/A // Check TTL_*
0N/A if (CONTROL.TTL_DONT_CACHE != -1) {
0N/A error("Wrong Control.TTL_DONT_CACHE: %d%n", CONTROL.TTL_DONT_CACHE);
0N/A }
0N/A if (CONTROL.TTL_NO_EXPIRATION_CONTROL != -2) {
0N/A error("Wrong Control.TTL_NO_EXPIRATION_CONTROL: %d%n",
0N/A CONTROL.TTL_NO_EXPIRATION_CONTROL);
0N/A }
0N/A }
0N/A
0N/A private static void checkImmutableList(List<String> list) {
0N/A try {
0N/A list.add("hello");
0N/A error("%s is mutable%n", list);
0N/A } catch (UnsupportedOperationException e) {
0N/A }
0N/A }
0N/A
0N/A private static void testGetFormats() {
0N/A List<String> list = CONTROL.getFormats("foo");
0N/A if (list != CONTROL.FORMAT_DEFAULT) {
0N/A error("getFormats returned " + list);
0N/A }
0N/A try {
0N/A list = CONTROL.getFormats(null);
0N/A error("getFormats doesn't throw NPE.");
0N/A } catch (NullPointerException e) {
0N/A }
0N/A }
0N/A
0N/A private static void testGetCandidateLocales() {
0N/A Map<Locale, Locale[]> candidateData = new HashMap<Locale, Locale[]>();
0N/A candidateData.put(new Locale("ja", "JP", "YOK"), new Locale[] {
0N/A new Locale("ja", "JP", "YOK"),
0N/A new Locale("ja", "JP"),
0N/A new Locale("ja"),
0N/A new Locale("") });
0N/A candidateData.put(new Locale("ja", "JP"), new Locale[] {
0N/A new Locale("ja", "JP"),
0N/A new Locale("ja"),
0N/A new Locale("") });
0N/A candidateData.put(new Locale("ja"), new Locale[] {
0N/A new Locale("ja"),
0N/A new Locale("") });
0N/A
0N/A candidateData.put(new Locale("ja", "", "YOK"), new Locale[] {
0N/A new Locale("ja", "", "YOK"),
0N/A new Locale("ja"),
0N/A new Locale("") });
0N/A candidateData.put(new Locale("", "JP", "YOK"), new Locale[] {
0N/A new Locale("", "JP", "YOK"),
0N/A new Locale("", "JP"),
0N/A new Locale("") });
0N/A candidateData.put(new Locale("", "", "YOK"), new Locale[] {
0N/A new Locale("", "", "YOK"),
0N/A new Locale("") });
0N/A candidateData.put(new Locale("", "JP"), new Locale[] {
0N/A new Locale("", "JP"),
0N/A new Locale("") });
0N/A candidateData.put(new Locale(""), new Locale[] {
0N/A new Locale("") });
0N/A
0N/A for (Locale locale : candidateData.keySet()) {
0N/A List<Locale> candidates = CONTROL.getCandidateLocales("any", locale);
0N/A List<Locale> expected = Arrays.asList(candidateData.get(locale));
0N/A if (!candidates.equals(expected)) {
0N/A error("Wrong candidates for %s: got %s, expected %s%n",
0N/A toString(locale), candidates, expected);
0N/A }
0N/A }
0N/A final int NARGS = 2;
0N/A for (int mask = 0; mask < (1 << NARGS)-1; mask++) {
0N/A Object[] data = getNpeArgs(NARGS, mask);
0N/A try {
0N/A List<Locale> candidates = CONTROL.getCandidateLocales((String) data[0],
0N/A (Locale) data[1]);
0N/A error("getCandidateLocales(%s, %s) doesn't throw NPE.%n",
0N/A data[0], toString((Locale)data[1]));
0N/A } catch (NullPointerException e) {
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static void testGetFallbackLocale() {
0N/A Locale current = Locale.getDefault();
0N/A Locale.setDefault(Locale.ITALY);
0N/A try {
0N/A Locale loc = CONTROL.getFallbackLocale("any", Locale.FRANCE);
0N/A if (loc != Locale.ITALY) {
0N/A error("getFallbackLocale: got %s, expected %s%n",
0N/A toString(loc), toString(Locale.ITALY));
0N/A }
0N/A loc = CONTROL.getFallbackLocale("any", Locale.ITALY);
0N/A if (loc != null) {
0N/A error("getFallbackLocale: got %s, expected null%n", toString(loc));
0N/A }
0N/A } finally {
0N/A Locale.setDefault(current);
0N/A }
0N/A
0N/A final int NARGS = 2;
0N/A for (int mask = 0; mask < (1 << NARGS)-1; mask++) {
0N/A Object[] data = getNpeArgs(NARGS, mask);
0N/A try {
0N/A Locale loc = CONTROL.getFallbackLocale((String) data[0], (Locale) data[1]);
0N/A error("getFallbackLocale(%s, %s) doesn't throw NPE.%n", data[0], data[1]);
0N/A } catch (NullPointerException e) {
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static void testNewBundle() {
0N/A int testNo = 0;
0N/A ResourceBundle rb = null;
0N/A try {
0N/A testNo = 1;
0N/A rb = CONTROL.newBundle("StressOut", Locale.JAPANESE,
0N/A PROPERTIES, LOADER, false);
0N/A String s = rb.getString("data");
0N/A if (!s.equals("Japan")) {
0N/A error("newBundle: #%d got %s, expected Japan%n", testNo, s);
0N/A }
0N/A
0N/A testNo = 2;
0N/A rb = CONTROL.newBundle("TestResourceRB", new Locale(""),
0N/A CLAZZ, LOADER, false);
0N/A s = rb.getString("type");
0N/A if (!s.equals(CLAZZ)) {
0N/A error("newBundle: #%d got %s, expected %s%n", testNo, s, CLAZZ);
0N/A }
0N/A } catch (Throwable e) {
0N/A error("newBundle: #%d threw %s%n", testNo, e);
0N/A e.printStackTrace();
0N/A }
0N/A
0N/A // Test exceptions
0N/A
0N/A try {
0N/A // MalformedDataRB contains an invalid Unicode notation which
0N/A // causes to throw an IllegalArgumentException.
0N/A rb = CONTROL.newBundle("MalformedDataRB", Locale.ENGLISH,
0N/A PROPERTIES, LOADER, false);
0N/A error("newBundle: doesn't throw IllegalArgumentException with malformed data.");
0N/A } catch (IllegalArgumentException iae) {
0N/A } catch (Exception e) {
0N/A error("newBundle: threw %s%n", e);
0N/A }
0N/A
0N/A try {
0N/A rb = CONTROL.newBundle("StressOut", Locale.JAPANESE,
0N/A "foo.bar", LOADER, false);
0N/A error("newBundle: doesn't throw IllegalArgumentException with invalid format.");
0N/A } catch (IllegalArgumentException iae) {
0N/A } catch (Exception e) {
0N/A error("newBundle: threw %s%n", e);
0N/A }
0N/A
0N/A try {
0N/A rb = CONTROL.newBundle("NonResourceBundle", new Locale(""),
0N/A "java.class", LOADER, false);
0N/A error("newBundle: doesn't throw ClassCastException with a non-ResourceBundle subclass.");
0N/A } catch (ClassCastException cce) {
0N/A } catch (Exception e) {
0N/A error("newBundle: threw %s%n", e);
0N/A }
0N/A
0N/A // NPE test
0N/A final int NARGS = 4;
0N/A for (int mask = 0; mask < (1 << NARGS)-1; mask++) {
0N/A Object[] data = getNpeArgs(NARGS, mask);
0N/A Locale loc = (Locale) data[1];
0N/A try {
0N/A rb = CONTROL.newBundle((String) data[0], loc,
0N/A (String) data[2], (ClassLoader) data[3],
0N/A false);
0N/A error("newBundle(%s, %s, %s, %s, false) doesn't throw NPE.%n",
0N/A data[0], toString(loc), data[2], data[3]);
0N/A } catch (NullPointerException npe) {
0N/A } catch (Exception e) {
0N/A error("newBundle(%s, %s, %s, %s, false) threw %s.%n",
0N/A data[0], toString(loc), data[2], data[3], e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static void testGetTimeToLive() {
0N/A long ttl = CONTROL.getTimeToLive("any", Locale.US);
0N/A if (ttl != CONTROL.TTL_NO_EXPIRATION_CONTROL) {
0N/A error("getTimeToLive: got %d, expected %d%n", ttl,
0N/A CONTROL.TTL_NO_EXPIRATION_CONTROL);
0N/A }
0N/A final int NARGS = 2;
0N/A for (int mask = 0; mask < (1 << NARGS)-1; mask++) {
0N/A Object[] data = getNpeArgs(NARGS, mask);
0N/A try {
0N/A ttl = CONTROL.getTimeToLive((String) data[0], (Locale) data[1]);
0N/A error("getTimeToLive(%s, %s) doesn't throw NPE.%n", data[0], data[1]);
0N/A } catch (NullPointerException e) {
0N/A }
0N/A }
0N/A }
0N/A
0N/A // The functionality of needsReload is tested by
0N/A // ExpirationTest.sh. Only parameter checking is tested here.
0N/A private static void testNeedsReload() {
0N/A long loadTime = System.currentTimeMillis();
0N/A
0N/A // NPE test
0N/A final int NARGS = 5;
0N/A for (int mask = 0; mask < (1 << NARGS)-1; mask++) {
0N/A Object[] data = getNpeArgs(NARGS, mask);
0N/A Locale loc = (Locale) data[1];
0N/A try {
0N/A boolean b = CONTROL.needsReload((String) data[0], loc,
0N/A (String) data[2], (ClassLoader) data[3],
0N/A (ResourceBundle) data[4], loadTime);
0N/A error("needsReload(%s, %s, %s, %s, %s, loadTime) doesn't throw NPE.%n",
0N/A data[0], toString(loc), data[2], data[3], data[4]);
0N/A } catch (NullPointerException e) {
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static void testToBundleName() {
0N/A final String name = "J2SE";
0N/A Map<Locale, String> bundleNames = new HashMap<Locale, String>();
0N/A bundleNames.put(new Locale("ja", "JP", "YOK"),
0N/A name + "_" + "ja" + "_" + "JP" + "_" + "YOK");
0N/A bundleNames.put(new Locale("ja", "JP"),
0N/A name + "_" + "ja" + "_" + "JP");
0N/A bundleNames.put(new Locale("ja"),
0N/A name + "_" + "ja");
0N/A bundleNames.put(new Locale("ja", "", "YOK"),
0N/A name + "_" + "ja" + "_" + "" + "_" + "YOK");
0N/A bundleNames.put(new Locale("", "JP", "YOK"),
0N/A name + "_" + "" + "_" + "JP" + "_" + "YOK");
0N/A bundleNames.put(new Locale("", "", "YOK"),
0N/A name + "_" + "" + "_" + "" + "_" + "YOK");
0N/A bundleNames.put(new Locale("", "JP"),
0N/A name + "_" + "" + "_" + "JP");
0N/A bundleNames.put(new Locale(""),
0N/A name);
0N/A
0N/A for (Locale locale : bundleNames.keySet()) {
0N/A String bn = CONTROL.toBundleName(name, locale);
0N/A String expected = bundleNames.get(locale);
0N/A if (!bn.equals(expected)) {
0N/A error("toBundleName: got %s, expected %s%n", bn, expected);
0N/A }
0N/A }
0N/A
0N/A final int NARGS = 2;
0N/A for (int mask = 0; mask < (1 << NARGS)-1; mask++) {
0N/A Object[] data = getNpeArgs(NARGS, mask);
0N/A try {
0N/A String s = CONTROL.toBundleName((String) data[0], (Locale) data[1]);
0N/A error("toBundleName(%s, %s) doesn't throw NPE.%n", data[0], data[1]);
0N/A } catch (NullPointerException e) {
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static void testToResourceName() {
0N/A String[][] names = {
0N/A // bundleName, suffix, expected result
0N/A { "com.sun.J2SE", "xml", "com/sun/J2SE.xml" },
0N/A { ".J2SE", "xml", "/J2SE.xml" },
0N/A { "J2SE", "xml", "J2SE.xml" },
0N/A { "com/sun/J2SE", "xml", "com/sun/J2SE.xml" },
0N/A { "com.sun.J2SE", "", "com/sun/J2SE." },
0N/A { ".", "", "/." },
0N/A { "", "", "." },
0N/A
0N/A // data for NPE tests
0N/A { null, "any", null },
0N/A { "any", null, null },
0N/A { null, null, null },
0N/A };
0N/A
0N/A for (String[] data : names) {
0N/A String result = null;
0N/A boolean npeThrown = false;
0N/A try {
0N/A result = CONTROL.toResourceName(data[0], data[1]);
0N/A } catch (NullPointerException npe) {
0N/A npeThrown = true;
0N/A }
0N/A String expected = data[2];
0N/A if (expected != null) {
0N/A if (!result.equals(expected)) {
0N/A error("toResourceName: got %s, expected %s%n", result, data[2]);
0N/A }
0N/A } else {
0N/A if (!npeThrown) {
0N/A error("toResourceName(%s, %s) doesn't throw NPE.%n", data[0], data[1]);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Produces permutations argument data that contains at least one
0N/A * null.
0N/A */
0N/A private static Object[] getNpeArgs(int length, int mask) {
0N/A Object[] data = new Object[length];
0N/A for (int i = 0; i < length; i++) {
0N/A if ((mask & (1 << i)) == 0) {
0N/A data[i] = null;
0N/A } else {
0N/A data[i] = FULLARGS[i];
0N/A }
0N/A }
0N/A return data;
0N/A }
0N/A
0N/A private static String toString(Locale loc) {
0N/A if (loc == null)
0N/A return "null";
0N/A return "\"" + loc.getLanguage() + "_" + loc.getCountry() + "_" + loc.getVariant() + "\"";
0N/A }
0N/A
0N/A private static void error(String msg) {
0N/A System.out.println(msg);
0N/A errors++;
0N/A }
0N/A
0N/A private static void error(String fmt, Object... args) {
0N/A System.out.printf(fmt, args);
0N/A errors++;
0N/A }
0N/A}