Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/ResourceBundle/Control/DefaultControlTest.java
38821 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 5102289 627833425* @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"),147new Locale("") });148candidateData.put(new Locale("ja", "JP"), new Locale[] {149new Locale("ja", "JP"),150new Locale("ja"),151new Locale("") });152candidateData.put(new Locale("ja"), new Locale[] {153new Locale("ja"),154new Locale("") });155156candidateData.put(new Locale("ja", "", "YOK"), new Locale[] {157new Locale("ja", "", "YOK"),158new Locale("ja"),159new Locale("") });160candidateData.put(new Locale("", "JP", "YOK"), new Locale[] {161new Locale("", "JP", "YOK"),162new Locale("", "JP"),163new Locale("") });164candidateData.put(new Locale("", "", "YOK"), new Locale[] {165new Locale("", "", "YOK"),166new Locale("") });167candidateData.put(new Locale("", "JP"), new Locale[] {168new Locale("", "JP"),169new Locale("") });170candidateData.put(new Locale(""), new Locale[] {171new Locale("") });172173for (Locale locale : candidateData.keySet()) {174List<Locale> candidates = CONTROL.getCandidateLocales("any", locale);175List<Locale> expected = Arrays.asList(candidateData.get(locale));176if (!candidates.equals(expected)) {177error("Wrong candidates for %s: got %s, expected %s%n",178toString(locale), candidates, expected);179}180}181final int NARGS = 2;182for (int mask = 0; mask < (1 << NARGS)-1; mask++) {183Object[] data = getNpeArgs(NARGS, mask);184try {185List<Locale> candidates = CONTROL.getCandidateLocales((String) data[0],186(Locale) data[1]);187error("getCandidateLocales(%s, %s) doesn't throw NPE.%n",188data[0], toString((Locale)data[1]));189} catch (NullPointerException e) {190}191}192}193194private static void testGetFallbackLocale() {195Locale current = Locale.getDefault();196Locale.setDefault(Locale.ITALY);197try {198Locale loc = CONTROL.getFallbackLocale("any", Locale.FRANCE);199if (loc != Locale.ITALY) {200error("getFallbackLocale: got %s, expected %s%n",201toString(loc), toString(Locale.ITALY));202}203loc = CONTROL.getFallbackLocale("any", Locale.ITALY);204if (loc != null) {205error("getFallbackLocale: got %s, expected null%n", toString(loc));206}207} finally {208Locale.setDefault(current);209}210211final int NARGS = 2;212for (int mask = 0; mask < (1 << NARGS)-1; mask++) {213Object[] data = getNpeArgs(NARGS, mask);214try {215Locale loc = CONTROL.getFallbackLocale((String) data[0], (Locale) data[1]);216error("getFallbackLocale(%s, %s) doesn't throw NPE.%n", data[0], data[1]);217} catch (NullPointerException e) {218}219}220}221222private static void testNewBundle() {223int testNo = 0;224ResourceBundle rb = null;225try {226testNo = 1;227rb = CONTROL.newBundle("StressOut", Locale.JAPANESE,228PROPERTIES, LOADER, false);229String s = rb.getString("data");230if (!s.equals("Japan")) {231error("newBundle: #%d got %s, expected Japan%n", testNo, s);232}233234testNo = 2;235rb = CONTROL.newBundle("TestResourceRB", new Locale(""),236CLAZZ, LOADER, false);237s = rb.getString("type");238if (!s.equals(CLAZZ)) {239error("newBundle: #%d got %s, expected %s%n", testNo, s, CLAZZ);240}241} catch (Throwable e) {242error("newBundle: #%d threw %s%n", testNo, e);243e.printStackTrace();244}245246// Test exceptions247248try {249// MalformedDataRB contains an invalid Unicode notation which250// causes to throw an IllegalArgumentException.251rb = CONTROL.newBundle("MalformedDataRB", Locale.ENGLISH,252PROPERTIES, LOADER, false);253error("newBundle: doesn't throw IllegalArgumentException with malformed data.");254} catch (IllegalArgumentException iae) {255} catch (Exception e) {256error("newBundle: threw %s%n", e);257}258259try {260rb = CONTROL.newBundle("StressOut", Locale.JAPANESE,261"foo.bar", LOADER, false);262error("newBundle: doesn't throw IllegalArgumentException with invalid format.");263} catch (IllegalArgumentException iae) {264} catch (Exception e) {265error("newBundle: threw %s%n", e);266}267268try {269rb = CONTROL.newBundle("NonResourceBundle", new Locale(""),270"java.class", LOADER, false);271error("newBundle: doesn't throw ClassCastException with a non-ResourceBundle subclass.");272} catch (ClassCastException cce) {273} catch (Exception e) {274error("newBundle: threw %s%n", e);275}276277// NPE test278final int NARGS = 4;279for (int mask = 0; mask < (1 << NARGS)-1; mask++) {280Object[] data = getNpeArgs(NARGS, mask);281Locale loc = (Locale) data[1];282try {283rb = CONTROL.newBundle((String) data[0], loc,284(String) data[2], (ClassLoader) data[3],285false);286error("newBundle(%s, %s, %s, %s, false) doesn't throw NPE.%n",287data[0], toString(loc), data[2], data[3]);288} catch (NullPointerException npe) {289} catch (Exception e) {290error("newBundle(%s, %s, %s, %s, false) threw %s.%n",291data[0], toString(loc), data[2], data[3], e);292}293}294}295296private static void testGetTimeToLive() {297long ttl = CONTROL.getTimeToLive("any", Locale.US);298if (ttl != CONTROL.TTL_NO_EXPIRATION_CONTROL) {299error("getTimeToLive: got %d, expected %d%n", ttl,300CONTROL.TTL_NO_EXPIRATION_CONTROL);301}302final int NARGS = 2;303for (int mask = 0; mask < (1 << NARGS)-1; mask++) {304Object[] data = getNpeArgs(NARGS, mask);305try {306ttl = CONTROL.getTimeToLive((String) data[0], (Locale) data[1]);307error("getTimeToLive(%s, %s) doesn't throw NPE.%n", data[0], data[1]);308} catch (NullPointerException e) {309}310}311}312313// The functionality of needsReload is tested by314// ExpirationTest.sh. Only parameter checking is tested here.315private static void testNeedsReload() {316long loadTime = System.currentTimeMillis();317318// NPE test319final int NARGS = 5;320for (int mask = 0; mask < (1 << NARGS)-1; mask++) {321Object[] data = getNpeArgs(NARGS, mask);322Locale loc = (Locale) data[1];323try {324boolean b = CONTROL.needsReload((String) data[0], loc,325(String) data[2], (ClassLoader) data[3],326(ResourceBundle) data[4], loadTime);327error("needsReload(%s, %s, %s, %s, %s, loadTime) doesn't throw NPE.%n",328data[0], toString(loc), data[2], data[3], data[4]);329} catch (NullPointerException e) {330}331}332}333334private static void testToBundleName() {335final String name = "J2SE";336Map<Locale, String> bundleNames = new HashMap<Locale, String>();337bundleNames.put(new Locale("ja", "JP", "YOK"),338name + "_" + "ja" + "_" + "JP" + "_" + "YOK");339bundleNames.put(new Locale("ja", "JP"),340name + "_" + "ja" + "_" + "JP");341bundleNames.put(new Locale("ja"),342name + "_" + "ja");343bundleNames.put(new Locale("ja", "", "YOK"),344name + "_" + "ja" + "_" + "" + "_" + "YOK");345bundleNames.put(new Locale("", "JP", "YOK"),346name + "_" + "" + "_" + "JP" + "_" + "YOK");347bundleNames.put(new Locale("", "", "YOK"),348name + "_" + "" + "_" + "" + "_" + "YOK");349bundleNames.put(new Locale("", "JP"),350name + "_" + "" + "_" + "JP");351bundleNames.put(new Locale(""),352name);353354for (Locale locale : bundleNames.keySet()) {355String bn = CONTROL.toBundleName(name, locale);356String expected = bundleNames.get(locale);357if (!bn.equals(expected)) {358error("toBundleName: got %s, expected %s%n", bn, expected);359}360}361362final int NARGS = 2;363for (int mask = 0; mask < (1 << NARGS)-1; mask++) {364Object[] data = getNpeArgs(NARGS, mask);365try {366String s = CONTROL.toBundleName((String) data[0], (Locale) data[1]);367error("toBundleName(%s, %s) doesn't throw NPE.%n", data[0], data[1]);368} catch (NullPointerException e) {369}370}371}372373private static void testToResourceName() {374String[][] names = {375// bundleName, suffix, expected result376{ "com.sun.J2SE", "xml", "com/sun/J2SE.xml" },377{ ".J2SE", "xml", "/J2SE.xml" },378{ "J2SE", "xml", "J2SE.xml" },379{ "com/sun/J2SE", "xml", "com/sun/J2SE.xml" },380{ "com.sun.J2SE", "", "com/sun/J2SE." },381{ ".", "", "/." },382{ "", "", "." },383384// data for NPE tests385{ null, "any", null },386{ "any", null, null },387{ null, null, null },388};389390for (String[] data : names) {391String result = null;392boolean npeThrown = false;393try {394result = CONTROL.toResourceName(data[0], data[1]);395} catch (NullPointerException npe) {396npeThrown = true;397}398String expected = data[2];399if (expected != null) {400if (!result.equals(expected)) {401error("toResourceName: got %s, expected %s%n", result, data[2]);402}403} else {404if (!npeThrown) {405error("toResourceName(%s, %s) doesn't throw NPE.%n", data[0], data[1]);406}407}408}409}410411/**412* Produces permutations argument data that contains at least one413* null.414*/415private static Object[] getNpeArgs(int length, int mask) {416Object[] data = new Object[length];417for (int i = 0; i < length; i++) {418if ((mask & (1 << i)) == 0) {419data[i] = null;420} else {421data[i] = FULLARGS[i];422}423}424return data;425}426427private static String toString(Locale loc) {428if (loc == null)429return "null";430return "\"" + loc.getLanguage() + "_" + loc.getCountry() + "_" + loc.getVariant() + "\"";431}432433private static void error(String msg) {434System.out.println(msg);435errors++;436}437438private static void error(String fmt, Object... args) {439System.out.printf(fmt, args);440errors++;441}442}443444445