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
  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             ResourceBundle bundle = ResourceBundle.getBundle(baseName);
  56             System.out.println(getBaseName(bundle));
  57             if (!Locale.ROOT.equals(bundle.getLocale())) {
  58                 throw new RuntimeException("Unexpected locale: "
  59                         + bundle.getLocale());
  60             }
  61             if (!baseName.equals(getBaseName(bundle))) {
  62                 throw new RuntimeException("Unexpected base name: "
  63                         + getBaseName(bundle));
  64             }
  65             Locale.setDefault(Locale.FRENCH);
  66             try {
  67                 ResourceBundle bundle_fr = ResourceBundle.getBundle(baseName);
  68                 if (!Locale.FRENCH.equals(bundle_fr.getLocale())) {
  69                     throw new RuntimeException("Unexpected locale: "
  70                             + bundle_fr.getLocale());
  71                 }
  72                 if (!baseName.equals(getBaseName(bundle_fr))) {
  73                     throw new RuntimeException("Unexpected base name: "
  74                             + getBaseName(bundle_fr));
  75                 }
  76             } finally {
  77                 Locale.setDefault(defaultLocale);
  78             }
  79         }
  80 
  81         final ResourceBundle bundle = new ResourceBundle() {
  82             @Override
  83             protected Object handleGetObject(String key) {
  84                 if ("dummy".equals(key)) return "foo";
  85                 throw new MissingResourceException("Missing key",
  86                         this.getClass().getName(), key);
  87             }
  88             @Override
  89             public Enumeration<String> getKeys() {
  90                 return Collections.enumeration(java.util.Arrays.asList(
  91                         new String[] {"dummy"}));
  92             }
  93         };
  94 
  95         if (getBaseName(bundle) != null) {
  96             throw new RuntimeException("Expected null baseName, got "
  97                     + getBaseName(bundle));
  98         }
  99 
 100         final ResourceBundle bundle2 = new ResourceBundle() {
 101             @Override
 102             protected Object handleGetObject(String key) {
 103                 if ("dummy".equals(key)) return "foo";
 104                 throw new MissingResourceException("Missing key",
 105                         this.getClass().getName(), key);
 106             }
 107             @Override
 108             public Enumeration<String> getKeys() {
 109                 return Collections.enumeration(java.util.Arrays.asList(
 110                         new String[] {"dummy"}));
 111             }
 112 
 113             @Override
 114             public String getBaseBundleName() {
 115                 return this.getClass().getName();
 116             }
 117 
 118 
 119         };
 120 
 121         if (!bundle2.getClass().getName().equals(getBaseName(bundle2))) {
 122             throw new RuntimeException("Expected "
 123                     + bundle2.getClass().getName() + ", got "
 124                     + getBaseName(bundle2));
 125         }
 126 
 127         ResourceBundle propertyBundle = new PropertyResourceBundle(
 128                 TestGetBaseBundleName.class.getResourceAsStream(
 129                     PROPERTY_BUNDLE_NAME+".properties"));
 130 
 131         if (getBaseName(propertyBundle) != null) {
 132             throw new RuntimeException("Expected null baseName, got "
 133                     + getBaseName(propertyBundle));
 134         }
 135 
 136         ResourceBundle listBundle = new resources.ListBundle_fr();
 137         if (getBaseName(listBundle) != null) {
 138             throw new RuntimeException("Expected null baseName, got "
 139                     + getBaseName(listBundle));
 140         }
 141 
 142 
 143     }
 144 }