J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | /* |
| 24 | * @test |
| 25 | * @bug 6287579 |
| 26 | * @summary Make sure that getContents() of ListResourceBundle subclasses is 'protected' |
| 27 | * and returns a different Object[]][] instance in each invocation. |
| 28 | */ |
| 29 | |
| 30 | import java.lang.reflect.*; |
| 31 | import java.util.*; |
| 32 | |
| 33 | public class Bug6287579 { |
| 34 | static final Locale ROOT = new Locale(""); |
| 35 | |
| 36 | static final String[] baseNames = { |
| 37 | "sun.text.resources.BreakIteratorInfo", |
| 38 | "sun.text.resources.FormatData", |
| 39 | "sun.text.resources.CollationData", |
| 40 | "sun.util.resources.LocaleNames", |
| 41 | "sun.util.resources.TimeZoneNames", |
| 42 | |
| 43 | // Make sure the properties-to-class conversion tool generates |
| 44 | // the proper getContents(). |
| 45 | "sun.awt.resources.awt", |
| 46 | }; |
| 47 | |
| 48 | public static void main(String[] args) throws Exception { |
| 49 | int errors = 0; |
| 50 | |
| 51 | List<Locale> locales = new ArrayList<Locale>(); |
| 52 | locales.addAll(Arrays.asList(Locale.getAvailableLocales())); |
| 53 | locales.add(ROOT); |
| 54 | |
| 55 | for (Locale locale : locales) { |
| 56 | for (String base : baseNames) { |
| 57 | String className = getResourceName(base, locale); |
| 58 | errors += checkGetContents(className); |
| 59 | } |
| 60 | } |
| 61 | if (errors > 0) { |
| 62 | throw new RuntimeException(errors + " errors found"); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static int checkGetContents(String className) throws Exception { |
| 67 | int err = 0; |
| 68 | try { |
| 69 | Class clazz = Class.forName(className); |
| 70 | Method getContentsMethod = clazz.getDeclaredMethod("getContents", |
| 71 | (Class[]) null); |
| 72 | if (!Modifier.isProtected(getContentsMethod.getModifiers())) { |
| 73 | System.err.println(className + ": not protected"); |
| 74 | err++; |
| 75 | } |
| 76 | getContentsMethod.setAccessible(true); |
| 77 | Object bundle = clazz.newInstance(); |
| 78 | Object o1 = getContentsMethod.invoke(bundle, (Object[]) null); |
| 79 | Object o2 = getContentsMethod.invoke(bundle, (Object[]) null); |
| 80 | if (o1 == o2) { |
| 81 | System.err.println(className + ": same instance returned"); |
| 82 | err++; |
| 83 | } |
| 84 | } catch (ClassNotFoundException ce) { |
| 85 | // Skip nonexistent classes |
| 86 | } catch (NoSuchMethodException me) { |
| 87 | System.out.println(className + ": no declared getContents()"); |
| 88 | } |
| 89 | return err; |
| 90 | } |
| 91 | |
| 92 | static String getResourceName(String base, Locale locale) { |
| 93 | if (locale.equals(ROOT)) { |
| 94 | return base; |
| 95 | } |
| 96 | StringBuilder sb = new StringBuilder(base); |
| 97 | sb.append('_').append(locale.getLanguage()); |
| 98 | if (locale.getCountry().length() > 0 |
| 99 | || locale.getVariant().length() > 0) { |
| 100 | sb.append('_').append(locale.getCountry()); |
| 101 | } |
| 102 | if (locale.getVariant().length() > 0) { |
| 103 | sb.append('_').append(locale.getVariant()); |
| 104 | } |
| 105 | return sb.toString(); |
| 106 | } |
| 107 | } |