Path: blob/master/test/jdk/java/util/ResourceBundle/Control/PackagePrivateTest.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 4175293 510228925* @summary Test if package private ResourceBundles can be loaded.26* @build PackagePrivateRB27* @run main/timeout=300/othervm -esa PackagePrivateTest28*/2930import java.io.*;31import java.util.*;32import static java.util.ResourceBundle.Control.*;3334public class PackagePrivateTest {35public static void main(String[] args) {36ResourceBundle rb;3738// Make sure that the default Control can't load the package39// private resource bundles.40try {41rb = ResourceBundle.getBundle("PackagePrivateRB");42throw new RuntimeException(43"doesn't throw MissingResourceException with the default Control");44} catch (MissingResourceException e) {45}4647// Remove the dummy cache entry48ResourceBundle.clearCache();4950rb = ResourceBundle.getBundle("PackagePrivateRB",51new ResourceBundle.Control() {52@Override53public List<String> getFormats(String baseName) {54return FORMAT_CLASS;55}5657@Override58public ResourceBundle newBundle(String baseName,59Locale locale,60String format,61ClassLoader loader,62boolean reload)63throws IllegalAccessException,64InstantiationException, IOException {65String bn = toBundleName(baseName, locale);66if ("java.class".equals(format)) {67try {68Class<? extends ResourceBundle> cl =69(Class<? extends ResourceBundle>) loader.loadClass(bn);70return cl.newInstance();71} catch (ClassNotFoundException e) {72//System.out.println("ClassNotFoundException: " + e.getMessage());73}74return null;75}76throw new IllegalArgumentException("unknown format: " + format);77}78});79String s = rb.getString("type");80if (!s.equals("class (package1.PackagePrivateRB)")) {81throw new RuntimeException("wrong type: got " + s + ", expected '" +82"class (package1.PackagePrivateRB)'");83}84}85}868788