Path: blob/master/test/jdk/java/util/ResourceBundle/Control/DefaultControlTest.java
41155 views
/*1* Copyright (c) 2007, 2021, 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 5102289 6278334 826117925* @summary Test the default Control implementation. The expiration26* functionality of newBundle, getTimeToLive, and needsReload is27* tested by ExpirationTest.sh. The factory methods are tested28* separately.29* @build TestResourceRB30* @build NonResourceBundle31* @run main DefaultControlTest32*/3334import java.util.*;35import static java.util.ResourceBundle.Control.*;3637public class DefaultControlTest {38// The ResourceBundle.Control instance39static final ResourceBundle.Control CONTROL40= ResourceBundle.Control.getControl(FORMAT_DEFAULT);4142static final ResourceBundle BUNDLE = new ResourceBundle() {43public Enumeration<String> getKeys() { return null; }44protected Object handleGetObject(String key) { return null; }45};4647static final String CLAZZ = FORMAT_CLASS.get(0);4849static final String PROPERTIES = FORMAT_PROPERTIES.get(0);5051static final ClassLoader LOADER = DefaultControlTest.class.getClassLoader();5253// Full arguments for NPE testing54static final Object[] FULLARGS = { "any",55Locale.US,56FORMAT_PROPERTIES.get(0),57LOADER,58BUNDLE };5960static int errors;6162public static void main(String[] args) {63checkConstants();6465// Test getFormats(String)66testGetFormats();6768// Test getCandidateLocales(String, Locale)69testGetCandidateLocales();7071// Test getFallbackLocale(String, Locale)72testGetFallbackLocale();7374// Test newBundle(String, Locale, String, ClassLoader, boolean)75testNewBundle();7677// Test toBundleName(String, Locale)78testToBundleName();7980// Test getTimeToLive(String, Locale)81testGetTimeToLive();8283// Test needsReload(String, Locale, String, ClassLoader,84// ResourceBundle, long)85testNeedsReload();8687// Test toResourceName(String, String)88testToResourceName();8990if (errors > 0) {91throw new RuntimeException("FAILED: " + errors + " error(s)");92}93}9495private static void checkConstants() {96// Check FORMAT_*97if (!CONTROL.FORMAT_DEFAULT.equals(Arrays.asList("java.class",98"java.properties"))) {99error("Wrong Control.FORMAT_DEFAULT");100}101checkImmutableList(CONTROL.FORMAT_DEFAULT);102if (!CONTROL.FORMAT_CLASS.equals(Arrays.asList("java.class"))) {103error("Wrong Control.FORMAT_CLASS");104}105checkImmutableList(CONTROL.FORMAT_CLASS);106if (!CONTROL.FORMAT_PROPERTIES.equals(Arrays.asList("java.properties"))) {107error("Wrong Control.FORMAT_PROPERTIES");108}109checkImmutableList(CONTROL.FORMAT_PROPERTIES);110111// Check TTL_*112if (CONTROL.TTL_DONT_CACHE != -1) {113error("Wrong Control.TTL_DONT_CACHE: %d%n", CONTROL.TTL_DONT_CACHE);114}115if (CONTROL.TTL_NO_EXPIRATION_CONTROL != -2) {116error("Wrong Control.TTL_NO_EXPIRATION_CONTROL: %d%n",117CONTROL.TTL_NO_EXPIRATION_CONTROL);118}119}120121private static void checkImmutableList(List<String> list) {122try {123list.add("hello");124error("%s is mutable%n", list);125} catch (UnsupportedOperationException e) {126}127}128129private static void testGetFormats() {130List<String> list = CONTROL.getFormats("foo");131if (list != CONTROL.FORMAT_DEFAULT) {132error("getFormats returned " + list);133}134try {135list = CONTROL.getFormats(null);136error("getFormats doesn't throw NPE.");137} catch (NullPointerException e) {138}139}140141private static void testGetCandidateLocales() {142Map<Locale, Locale[]> candidateData = new HashMap<Locale, Locale[]>();143candidateData.put(new Locale("ja", "JP", "YOK"), new Locale[] {144new Locale("ja", "JP", "YOK"),145new Locale("ja", "JP"),146new Locale("ja"),147Locale.ROOT });148candidateData.put(new Locale("ja", "JP"), new Locale[] {149new Locale("ja", "JP"),150new Locale("ja"),151Locale.ROOT });152candidateData.put(new Locale("ja"), new Locale[] {153new Locale("ja"),154Locale.ROOT });155156candidateData.put(new Locale("ja", "", "YOK"), new Locale[] {157new Locale("ja", "", "YOK"),158new Locale("ja"),159Locale.ROOT });160candidateData.put(new Locale("", "JP", "YOK"), new Locale[] {161new Locale("", "JP", "YOK"),162new Locale("", "JP"),163Locale.ROOT });164candidateData.put(new Locale("", "", "YOK"), new Locale[] {165new Locale("", "", "YOK"),166Locale.ROOT });167candidateData.put(new Locale("", "JP"), new Locale[] {168new Locale("", "JP"),169Locale.ROOT });170candidateData.put(Locale.ROOT, new Locale[] {171Locale.ROOT });172173// Norwegian Bokmal174candidateData.put(Locale.forLanguageTag("nb-NO-POSIX"), new Locale[] {175Locale.forLanguageTag("nb-NO-POSIX"),176Locale.forLanguageTag("no-NO-POSIX"),177Locale.forLanguageTag("nb-NO"),178Locale.forLanguageTag("no-NO"),179Locale.forLanguageTag("nb"),180Locale.forLanguageTag("no"),181Locale.ROOT});182candidateData.put(Locale.forLanguageTag("no-NO-POSIX"), new Locale[] {183Locale.forLanguageTag("no-NO-POSIX"),184Locale.forLanguageTag("nb-NO-POSIX"),185Locale.forLanguageTag("no-NO"),186Locale.forLanguageTag("nb-NO"),187Locale.forLanguageTag("no"),188Locale.forLanguageTag("nb"),189Locale.ROOT});190191192for (Locale locale : candidateData.keySet()) {193List<Locale> candidates = CONTROL.getCandidateLocales("any", locale);194List<Locale> expected = Arrays.asList(candidateData.get(locale));195if (!candidates.equals(expected)) {196error("Wrong candidates for %s: got %s, expected %s%n",197toString(locale), candidates, expected);198}199}200final int NARGS = 2;201for (int mask = 0; mask < (1 << NARGS)-1; mask++) {202Object[] data = getNpeArgs(NARGS, mask);203try {204List<Locale> candidates = CONTROL.getCandidateLocales((String) data[0],205(Locale) data[1]);206error("getCandidateLocales(%s, %s) doesn't throw NPE.%n",207data[0], toString((Locale)data[1]));208} catch (NullPointerException e) {209}210}211}212213private static void testGetFallbackLocale() {214Locale current = Locale.getDefault();215Locale.setDefault(Locale.ITALY);216try {217Locale loc = CONTROL.getFallbackLocale("any", Locale.FRANCE);218if (loc != Locale.ITALY) {219error("getFallbackLocale: got %s, expected %s%n",220toString(loc), toString(Locale.ITALY));221}222loc = CONTROL.getFallbackLocale("any", Locale.ITALY);223if (loc != null) {224error("getFallbackLocale: got %s, expected null%n", toString(loc));225}226} finally {227Locale.setDefault(current);228}229230final int NARGS = 2;231for (int mask = 0; mask < (1 << NARGS)-1; mask++) {232Object[] data = getNpeArgs(NARGS, mask);233try {234Locale loc = CONTROL.getFallbackLocale((String) data[0], (Locale) data[1]);235error("getFallbackLocale(%s, %s) doesn't throw NPE.%n", data[0], data[1]);236} catch (NullPointerException e) {237}238}239}240241private static void testNewBundle() {242int testNo = 0;243ResourceBundle rb = null;244try {245testNo = 1;246rb = CONTROL.newBundle("StressOut", Locale.JAPANESE,247PROPERTIES, LOADER, false);248String s = rb.getString("data");249if (!s.equals("Japan")) {250error("newBundle: #%d got %s, expected Japan%n", testNo, s);251}252253testNo = 2;254rb = CONTROL.newBundle("TestResourceRB", Locale.ROOT,255CLAZZ, LOADER, false);256s = rb.getString("type");257if (!s.equals(CLAZZ)) {258error("newBundle: #%d got %s, expected %s%n", testNo, s, CLAZZ);259}260} catch (Throwable e) {261error("newBundle: #%d threw %s%n", testNo, e);262e.printStackTrace();263}264265// Test exceptions266267try {268// MalformedDataRB contains an invalid Unicode notation which269// causes to throw an IllegalArgumentException.270rb = CONTROL.newBundle("MalformedDataRB", Locale.ENGLISH,271PROPERTIES, LOADER, false);272error("newBundle: doesn't throw IllegalArgumentException with malformed data.");273} catch (IllegalArgumentException iae) {274} catch (Exception e) {275error("newBundle: threw %s%n", e);276}277278try {279rb = CONTROL.newBundle("StressOut", Locale.JAPANESE,280"foo.bar", LOADER, false);281error("newBundle: doesn't throw IllegalArgumentException with invalid format.");282} catch (IllegalArgumentException iae) {283} catch (Exception e) {284error("newBundle: threw %s%n", e);285}286287try {288rb = CONTROL.newBundle("NonResourceBundle", Locale.ROOT,289"java.class", LOADER, false);290error("newBundle: doesn't throw ClassCastException with a non-ResourceBundle subclass.");291} catch (ClassCastException cce) {292} catch (Exception e) {293error("newBundle: threw %s%n", e);294}295296// NPE test297final int NARGS = 4;298for (int mask = 0; mask < (1 << NARGS)-1; mask++) {299Object[] data = getNpeArgs(NARGS, mask);300Locale loc = (Locale) data[1];301try {302rb = CONTROL.newBundle((String) data[0], loc,303(String) data[2], (ClassLoader) data[3],304false);305error("newBundle(%s, %s, %s, %s, false) doesn't throw NPE.%n",306data[0], toString(loc), data[2], data[3]);307} catch (NullPointerException npe) {308} catch (Exception e) {309error("newBundle(%s, %s, %s, %s, false) threw %s.%n",310data[0], toString(loc), data[2], data[3], e);311}312}313}314315private static void testGetTimeToLive() {316long ttl = CONTROL.getTimeToLive("any", Locale.US);317if (ttl != CONTROL.TTL_NO_EXPIRATION_CONTROL) {318error("getTimeToLive: got %d, expected %d%n", ttl,319CONTROL.TTL_NO_EXPIRATION_CONTROL);320}321final int NARGS = 2;322for (int mask = 0; mask < (1 << NARGS)-1; mask++) {323Object[] data = getNpeArgs(NARGS, mask);324try {325ttl = CONTROL.getTimeToLive((String) data[0], (Locale) data[1]);326error("getTimeToLive(%s, %s) doesn't throw NPE.%n", data[0], data[1]);327} catch (NullPointerException e) {328}329}330}331332// The functionality of needsReload is tested by333// ExpirationTest.sh. Only parameter checking is tested here.334private static void testNeedsReload() {335long loadTime = System.currentTimeMillis();336337// NPE test338final int NARGS = 5;339for (int mask = 0; mask < (1 << NARGS)-1; mask++) {340Object[] data = getNpeArgs(NARGS, mask);341Locale loc = (Locale) data[1];342try {343boolean b = CONTROL.needsReload((String) data[0], loc,344(String) data[2], (ClassLoader) data[3],345(ResourceBundle) data[4], loadTime);346error("needsReload(%s, %s, %s, %s, %s, loadTime) doesn't throw NPE.%n",347data[0], toString(loc), data[2], data[3], data[4]);348} catch (NullPointerException e) {349}350}351}352353private static void testToBundleName() {354final String name = "J2SE";355Map<Locale, String> bundleNames = new HashMap<Locale, String>();356bundleNames.put(new Locale("ja", "JP", "YOK"),357name + "_" + "ja" + "_" + "JP" + "_" + "YOK");358bundleNames.put(new Locale("ja", "JP"),359name + "_" + "ja" + "_" + "JP");360bundleNames.put(new Locale("ja"),361name + "_" + "ja");362bundleNames.put(new Locale("ja", "", "YOK"),363name + "_" + "ja" + "_" + "" + "_" + "YOK");364bundleNames.put(new Locale("", "JP", "YOK"),365name + "_" + "" + "_" + "JP" + "_" + "YOK");366bundleNames.put(new Locale("", "", "YOK"),367name + "_" + "" + "_" + "" + "_" + "YOK");368bundleNames.put(new Locale("", "JP"),369name + "_" + "" + "_" + "JP");370bundleNames.put(Locale.ROOT,371name);372373for (Locale locale : bundleNames.keySet()) {374String bn = CONTROL.toBundleName(name, locale);375String expected = bundleNames.get(locale);376if (!bn.equals(expected)) {377error("toBundleName: got %s, expected %s%n", bn, expected);378}379}380381final int NARGS = 2;382for (int mask = 0; mask < (1 << NARGS)-1; mask++) {383Object[] data = getNpeArgs(NARGS, mask);384try {385String s = CONTROL.toBundleName((String) data[0], (Locale) data[1]);386error("toBundleName(%s, %s) doesn't throw NPE.%n", data[0], data[1]);387} catch (NullPointerException e) {388}389}390}391392private static void testToResourceName() {393String[][] names = {394// bundleName, suffix, expected result395{ "com.sun.J2SE", "xml", "com/sun/J2SE.xml" },396{ ".J2SE", "xml", "/J2SE.xml" },397{ "J2SE", "xml", "J2SE.xml" },398{ "com/sun/J2SE", "xml", "com/sun/J2SE.xml" },399{ "com.sun.J2SE", "", "com/sun/J2SE." },400{ ".", "", "/." },401{ "", "", "." },402403// data for NPE tests404{ null, "any", null },405{ "any", null, null },406{ null, null, null },407};408409for (String[] data : names) {410String result = null;411boolean npeThrown = false;412try {413result = CONTROL.toResourceName(data[0], data[1]);414} catch (NullPointerException npe) {415npeThrown = true;416}417String expected = data[2];418if (expected != null) {419if (!result.equals(expected)) {420error("toResourceName: got %s, expected %s%n", result, data[2]);421}422} else {423if (!npeThrown) {424error("toResourceName(%s, %s) doesn't throw NPE.%n", data[0], data[1]);425}426}427}428}429430/**431* Produces permutations argument data that contains at least one432* null.433*/434private static Object[] getNpeArgs(int length, int mask) {435Object[] data = new Object[length];436for (int i = 0; i < length; i++) {437if ((mask & (1 << i)) == 0) {438data[i] = null;439} else {440data[i] = FULLARGS[i];441}442}443return data;444}445446private static String toString(Locale loc) {447if (loc == null)448return "null";449return "\"" + loc.getLanguage() + "_" + loc.getCountry() + "_" + loc.getVariant() + "\"";450}451452private static void error(String msg) {453System.out.println(msg);454errors++;455}456457private static void error(String fmt, Object... args) {458System.out.printf(fmt, args);459errors++;460}461}462463464