Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/ResourceBundle/Bug6287579.java
38812 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 628757925* @summary Make sure that getContents() of ListResourceBundle subclasses is 'protected'26* and returns a different Object[]][] instance in each invocation.27*/2829import java.lang.reflect.*;30import java.util.*;3132public class Bug6287579 {33static final Locale ROOT = new Locale("");3435static final String[] baseNames = {36"sun.text.resources.BreakIteratorInfo",37"sun.text.resources.FormatData",38"sun.text.resources.CollationData",39"sun.util.resources.LocaleNames",40"sun.util.resources.TimeZoneNames",4142// Make sure the properties-to-class conversion tool generates43// the proper getContents().44"sun.awt.resources.awt",45};4647public static void main(String[] args) throws Exception {48int errors = 0;4950List<Locale> locales = new ArrayList<Locale>();51locales.addAll(Arrays.asList(Locale.getAvailableLocales()));52locales.add(ROOT);5354for (Locale locale : locales) {55for (String base : baseNames) {56String className = getResourceName(base, locale);57errors += checkGetContents(className);58}59}60if (errors > 0) {61throw new RuntimeException(errors + " errors found");62}63}6465static int checkGetContents(String className) throws Exception {66int err = 0;67try {68Class clazz = Class.forName(className);69Method getContentsMethod = clazz.getDeclaredMethod("getContents",70(Class[]) null);71if (!Modifier.isProtected(getContentsMethod.getModifiers())) {72System.err.println(className + ": not protected");73err++;74}75getContentsMethod.setAccessible(true);76Object bundle = clazz.newInstance();77Object o1 = getContentsMethod.invoke(bundle, (Object[]) null);78Object o2 = getContentsMethod.invoke(bundle, (Object[]) null);79if (o1 == o2) {80System.err.println(className + ": same instance returned");81err++;82}83} catch (ClassNotFoundException ce) {84// Skip nonexistent classes85} catch (NoSuchMethodException me) {86System.out.println(className + ": no declared getContents()");87}88return err;89}9091static String getResourceName(String base, Locale locale) {92if (locale.equals(ROOT)) {93return base;94}95StringBuilder sb = new StringBuilder(base);96sb.append('_').append(locale.getLanguage());97if (locale.getCountry().length() > 098|| locale.getVariant().length() > 0) {99sb.append('_').append(locale.getCountry());100}101if (locale.getVariant().length() > 0) {102sb.append('_').append(locale.getVariant());103}104return sb.toString();105}106}107108109