Path: blob/master/test/jdk/java/util/ResourceBundle/Control/BadControlsTest.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 if ResourceBundle.getBundle detects bad Control implementations.26*/2728import java.io.*;29import java.util.*;30import static java.util.ResourceBundle.Control.*;3132public class BadControlsTest {33public static void main(String[] args) {34ResourceBundle.Control control;3536control = new ResourceBundle.Control() {37public List<String> getFormats(String name) {38return null;39}40};41testControl(control, "getFormats returns null");4243control = new ResourceBundle.Control() {44public List<String> getFormats(String name) {45return Arrays.asList("java.class", null, "java.properties");46}47};48testControl(control, "getFormats returns a List containing null");4950control = new ResourceBundle.Control() {51public List<Locale> getCandidateLocales(String name, Locale loc) {52return null;53}54};55testControl(control, "getCandidateLocales returns null");5657control = new ResourceBundle.Control() {58public List<Locale> getCandidateLocales(String name, Locale loc) {59return Arrays.asList(Locale.US, null, Locale.ENGLISH);60}61};62testControl(control, "getCandidateLocales returns a List containing null");6364long[] badTtls = {65TTL_NO_EXPIRATION_CONTROL - 1,66-10000,67Long.MIN_VALUE68};69for (final long ttl : badTtls) {70control = new ResourceBundle.Control() {71public long getTimeToLive(String name, Locale loc) {72return ttl;73}74};75testControl(control, "getTimeToLive returns " + ttl);76}7778control = new ResourceBundle.Control() {79public String toBundleName(String name, Locale loc) {80return null;81}82};83try {84ResourceBundle rb = ResourceBundle.getBundle("StressOut", control);85throw new RuntimeException("toBundleName returns null");86} catch (MissingResourceException e) {87if (!(e.getCause() instanceof NullPointerException)) {88throw new RuntimeException("toBundleName returns null. The cause isn't NPE.");89}90}9192// null Control tests93control = null;94try {95ResourceBundle rb = ResourceBundle.getBundle("StressOut", control);96throw new RuntimeException("getBundle doesn't throw NPE with null Control");97} catch (NullPointerException e) {98// OK99}100}101102private static void testControl(ResourceBundle.Control control, String testTitle) {103try {104ResourceBundle rb = ResourceBundle.getBundle("StressOut", control);105throw new RuntimeException(testTitle);106} catch (IllegalArgumentException e) {107System.out.println(testTitle + ": PASSED (" + e.getMessage() + ")");108}109}110}111112113