test/java/util/ResourceBundle/getBaseBundleName/TestGetBaseBundleName.java

Print this page
rev 8630 : imported patch 8027930


  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);




  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);