Path: blob/master/test/jdk/java/util/ResourceBundle/Control/ControlFactoryTest.java
41155 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @bug 510228925* @summary Test the ResourceBundle.Control factory methods.26*/2728import java.util.*;29import static java.util.ResourceBundle.Control.*;3031public class ControlFactoryTest {3233/**34* An interface for calling ResourceBundle.Control.getControl or35* ResourceBundle.Control.getNoFallbackControl.36*/37private static interface Factory {38public ResourceBundle.Control getControl(List<String> formats);39public String name();40}4142static int errors;4344public static void main(String[] args) {45// Test getControl.46testControlFactory(new Factory() {47public ResourceBundle.Control getControl(List<String> formats) {48return ResourceBundle.Control.getControl(formats);49}50public String name() { return "getControl"; }51}, Locale.getDefault());5253// Test getNoFallbackControl.54testControlFactory(new Factory() {55public ResourceBundle.Control getControl(List<String> formats) {56return ResourceBundle.Control.getNoFallbackControl(formats);57}58public String name() { return "getNoFallbackControl"; }59}, null);6061if (errors > 0) {62throw new RuntimeException("FAILED: " + errors + " error(s)");63}64}6566private static void testControlFactory(Factory factory, Locale loc) {67testGetControl(factory, loc, FORMAT_DEFAULT, "java.class", "java.properties");68testGetControl(factory, loc, FORMAT_CLASS, "java.class");69testGetControl(factory, loc, FORMAT_PROPERTIES, "java.properties");7071// test IllegalArgumentException72String[][] data = {73{ "java.class", "java.properties", "java.xml" },74{ "java.class", "java.props" },75{ "java.properties", "java.class" },76{ "java.foo", "java.properties" },77{ "java.foo" },78{ null },79};80for (String[] fmts : data) {81try {82List<String> fmt = Arrays.asList(fmts);83ResourceBundle.Control control = factory.getControl(fmt);84error("getControl: %s%n", fmt);85} catch (IllegalArgumentException e) {86}87}8889// test NPE90try {91ResourceBundle.Control control = factory.getControl(null);92error("%s: doesn't throw NPE.%n", factory.name());93} catch (NullPointerException npe) {94}95}9697private static void testGetControl(Factory factory,98Locale loc,99final List<String> FORMATS,100String... fmtStrings) {101final ResourceBundle.Control CONTROL = factory.getControl(FORMATS);102List<String> fmt = CONTROL.getFormats("any");103if (fmt != FORMATS) {104error("%s: returns %s, expected %s.%n",105factory.name(), fmt, FORMATS);106}107ResourceBundle.Control control = null;108109// Check if getControl always returns the expected singleton.110for (int i = 0; i < 10; i++) {111fmt = Arrays.asList(fmtStrings);112control = factory.getControl(fmt);113if (control != CONTROL) {114error("%s: doesn't return the singleton: got %s, expected %s%n",115factory.name(), control, CONTROL);116}117}118119// Check if getFallbackLocale performs as expected.120Locale defaultLocale = Locale.getDefault();121Locale nonDefaultLocale = defaultLocale.equals(Locale.US) ? Locale.JAPAN : Locale.US;122if (loc != null) {123// Test ResourceBundle.Control.getControl()124Locale l = CONTROL.getFallbackLocale("any", nonDefaultLocale);125if (!defaultLocale.equals(l)) {126error("%s: getFallbackLocale doesn't return default locale. got %s, expected %s%n",127factory.name(), toString(l), toString(defaultLocale));128}129l = CONTROL.getFallbackLocale("any", defaultLocale);130if (l != null) {131error("%s: getFallbackLocale doesn't return null. got %s%n",132factory.name(), toString(l));133}134} else {135// Test ResourceBundle.Control.getNoFallbackControl()136Locale l = CONTROL.getFallbackLocale("any", nonDefaultLocale);137if (l != null) {138error("%s: getFallbackLocale doesn't return null. got %s%n",139factory.name(), toString(l));140}141l = CONTROL.getFallbackLocale("any", defaultLocale);142if (l != null) {143error("%s: getFallbackLocale doesn't return null. got %s%n",144factory.name(), toString(l));145}146}147}148149private static String toString(Locale loc) {150if (loc == null)151return "null";152return "\"" + loc.getLanguage() + "_" + loc.getCountry() + "_" + loc.getVariant() + "\"";153}154155private static void error(String msg) {156System.out.println(msg);157errors++;158}159160private static void error(String fmt, Object... args) {161System.out.printf(fmt, args);162errors++;163}164}165166167