1 /*
   2  * Copyright (c) 2013, 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 import java.util.Collections;
  24 import java.util.Enumeration;
  25 import java.util.Locale;
  26 import java.util.MissingResourceException;
  27 import java.util.PropertyResourceBundle;
  28 import java.util.ResourceBundle;
  29 
  30 /**
  31  * @test
  32  * @bug 4814565 8027930
  33  * @summary tests ResourceBundle.getBaseBundleName();
  34  * @build TestGetBaseBundleName resources.ListBundle resources.ListBundle_fr
  35  * @run main TestGetBaseBundleName
  36  * @author danielfuchs
  37  */
  38 public class TestGetBaseBundleName {
  39 
  40     final static String PROPERTY_BUNDLE_NAME = "resources/PropertyBundle";
  41     final static String LIST_BUNDLE_NAME = "resources.ListBundle";
  42 
  43     public static String getBaseName(ResourceBundle bundle) {
  44         return bundle == null ? null : bundle.getBaseBundleName();
  45     }
  46 
  47     public static void main(String... args) throws Exception {
  48 
  49         Locale defaultLocale = Locale.getDefault();
  50         System.out.println("Default locale is: " + defaultLocale);
  51         for (String baseName : new String[] {
  52                     PROPERTY_BUNDLE_NAME,
  53                     LIST_BUNDLE_NAME
  54         }) {
  55             try {
  56                 Locale.setDefault(Locale.US);
  57                 ResourceBundle bundle = ResourceBundle.getBundle(baseName);
  58                 System.out.println(getBaseName(bundle));
  59                 if (!Locale.ROOT.equals(bundle.getLocale())) {
  60                     throw new RuntimeException("Unexpected locale: "
  61                             + bundle.getLocale());
  62                 }
  63                 if (!baseName.equals(getBaseName(bundle))) {
  64                     throw new RuntimeException("Unexpected base name: "
  65                             + getBaseName(bundle));
  66                 }
  67 
  68                 Locale.setDefault(Locale.FRENCH);
  69                 ResourceBundle bundle_fr = ResourceBundle.getBundle(baseName);
  70                 if (!Locale.FRENCH.equals(bundle_fr.getLocale())) {
  71                     throw new RuntimeException("Unexpected locale: "
  72                             + bundle_fr.getLocale());
  73                 }
  74                 if (!baseName.equals(getBaseName(bundle_fr))) {
  75                     throw new RuntimeException("Unexpected base name: "
  76                             + getBaseName(bundle_fr));
  77                 }
  78             } finally {
  79                 Locale.setDefault(defaultLocale);
  80             }
  81         }
  82 
  83         final ResourceBundle bundle = new ResourceBundle() {
  84             @Override
  85             protected Object handleGetObject(String key) {
  86                 if ("dummy".equals(key)) return "foo";
  87                 throw new MissingResourceException("Missing key",
  88                         this.getClass().getName(), key);
  89             }
  90             @Override
  91             public Enumeration<String> getKeys() {
  92                 return Collections.enumeration(java.util.Arrays.asList(
  93                         new String[] {"dummy"}));
  94             }
  95         };
  96 
  97         if (getBaseName(bundle) != null) {
  98             throw new RuntimeException("Expected null baseName, got "
  99                     + getBaseName(bundle));
 100         }
 101 
 102         final ResourceBundle bundle2 = new ResourceBundle() {
 103             @Override
 104             protected Object handleGetObject(String key) {
 105                 if ("dummy".equals(key)) return "foo";
 106                 throw new MissingResourceException("Missing key",
 107                         this.getClass().getName(), key);
 108             }
 109             @Override
 110             public Enumeration<String> getKeys() {
 111                 return Collections.enumeration(java.util.Arrays.asList(
 112                         new String[] {"dummy"}));
 113             }
 114 
 115             @Override
 116             public String getBaseBundleName() {
 117                 return this.getClass().getName();
 118             }
 119 
 120 
 121         };
 122 
 123         if (!bundle2.getClass().getName().equals(getBaseName(bundle2))) {
 124             throw new RuntimeException("Expected "
 125                     + bundle2.getClass().getName() + ", got "
 126                     + getBaseName(bundle2));
 127         }
 128 
 129         ResourceBundle propertyBundle = new PropertyResourceBundle(
 130                 TestGetBaseBundleName.class.getResourceAsStream(
 131                     PROPERTY_BUNDLE_NAME+".properties"));
 132 
 133         if (getBaseName(propertyBundle) != null) {
 134             throw new RuntimeException("Expected null baseName, got "
 135                     + getBaseName(propertyBundle));
 136         }
 137 
 138         ResourceBundle listBundle = new resources.ListBundle_fr();
 139         if (getBaseName(listBundle) != null) {
 140             throw new RuntimeException("Expected null baseName, got "
 141                     + getBaseName(listBundle));
 142         }
 143 
 144 
 145     }
 146 }