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
0N/A * @summary Test the ResourceBundle.Control factory methods.
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport static java.util.ResourceBundle.Control.*;
0N/A
0N/Apublic class ControlFactoryTest {
0N/A
0N/A /**
0N/A * An interface for calling ResourceBundle.Control.getControl or
0N/A * ResourceBundle.Control.getNoFallbackControl.
0N/A */
0N/A private static interface Factory {
0N/A public ResourceBundle.Control getControl(List<String> formats);
0N/A public String name();
0N/A }
0N/A
0N/A static int errors;
0N/A
0N/A public static void main(String[] args) {
0N/A // Test getControl.
0N/A testControlFactory(new Factory() {
0N/A public ResourceBundle.Control getControl(List<String> formats) {
0N/A return ResourceBundle.Control.getControl(formats);
0N/A }
0N/A public String name() { return "getControl"; }
0N/A }, Locale.getDefault());
0N/A
0N/A // Test getNoFallbackControl.
0N/A testControlFactory(new Factory() {
0N/A public ResourceBundle.Control getControl(List<String> formats) {
0N/A return ResourceBundle.Control.getNoFallbackControl(formats);
0N/A }
0N/A public String name() { return "getNoFallbackControl"; }
0N/A }, null);
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 testControlFactory(Factory factory, Locale loc) {
0N/A testGetControl(factory, loc, FORMAT_DEFAULT, "java.class", "java.properties");
0N/A testGetControl(factory, loc, FORMAT_CLASS, "java.class");
0N/A testGetControl(factory, loc, FORMAT_PROPERTIES, "java.properties");
0N/A
0N/A // test IllegalArgumentException
0N/A String[][] data = {
0N/A { "java.class", "java.properties", "java.xml" },
0N/A { "java.class", "java.props" },
0N/A { "java.properties", "java.class" },
0N/A { "java.foo", "java.properties" },
0N/A { "java.foo" },
0N/A { null },
0N/A };
0N/A for (String[] fmts : data) {
0N/A try {
0N/A List<String> fmt = Arrays.asList(fmts);
0N/A ResourceBundle.Control control = factory.getControl(fmt);
0N/A error("getControl: %s%n", fmt);
0N/A } catch (IllegalArgumentException e) {
0N/A }
0N/A }
0N/A
0N/A // test NPE
0N/A try {
0N/A ResourceBundle.Control control = factory.getControl(null);
0N/A error("%s: doesn't throw NPE.%n", factory.name());
0N/A } catch (NullPointerException npe) {
0N/A }
0N/A }
0N/A
0N/A private static void testGetControl(Factory factory,
0N/A Locale loc,
0N/A final List<String> FORMATS,
0N/A String... fmtStrings) {
0N/A final ResourceBundle.Control CONTROL = factory.getControl(FORMATS);
0N/A List<String> fmt = CONTROL.getFormats("any");
0N/A if (fmt != FORMATS) {
0N/A error("%s: returns %s, expected %s.%n",
0N/A factory.name(), fmt, FORMATS);
0N/A }
0N/A ResourceBundle.Control control = null;
0N/A
0N/A // Check if getControl always returns the expected singleton.
0N/A for (int i = 0; i < 10; i++) {
0N/A fmt = Arrays.asList(fmtStrings);
0N/A control = factory.getControl(fmt);
0N/A if (control != CONTROL) {
0N/A error("%s: doesn't return the singleton: got %s, expected %s%n",
0N/A factory.name(), control, CONTROL);
0N/A }
0N/A }
0N/A
0N/A // Check if getFallbackLocale performs as expected.
0N/A Locale defaultLocale = Locale.getDefault();
0N/A Locale nonDefaultLocale = defaultLocale.equals(Locale.US) ? Locale.JAPAN : Locale.US;
0N/A if (loc != null) {
0N/A // Test ResourceBundle.Control.getControl()
0N/A Locale l = CONTROL.getFallbackLocale("any", nonDefaultLocale);
0N/A if (!defaultLocale.equals(l)) {
0N/A error("%s: getFallbackLocale doesn't return default locale. got %s, expected %s%n",
0N/A factory.name(), toString(l), toString(defaultLocale));
0N/A }
0N/A l = CONTROL.getFallbackLocale("any", defaultLocale);
0N/A if (l != null) {
0N/A error("%s: getFallbackLocale doesn't return null. got %s%n",
0N/A factory.name(), toString(l));
0N/A }
0N/A } else {
0N/A // Test ResourceBundle.Control.getNoFallbackControl()
0N/A Locale l = CONTROL.getFallbackLocale("any", nonDefaultLocale);
0N/A if (l != null) {
0N/A error("%s: getFallbackLocale doesn't return null. got %s%n",
0N/A factory.name(), toString(l));
0N/A }
0N/A l = CONTROL.getFallbackLocale("any", defaultLocale);
0N/A if (l != null) {
0N/A error("%s: getFallbackLocale doesn't return null. got %s%n",
0N/A factory.name(), toString(l));
0N/A }
0N/A }
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}