jdk/test/java/util/ResourceBundle/Bug6287579.java
changeset 28692 cf8fe951ca93
parent 28691 bdc353778a28
parent 28688 91c9e972559b
child 28699 06e2c7a14944
equal deleted inserted replaced
28691:bdc353778a28 28692:cf8fe951ca93
     1 /*
       
     2  * Copyright (c) 2007, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * 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 }