Home | History | Annotate | Download | only in util
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package org.apache.harmony.tests.java.util;
     19 
     20 import dalvik.annotation.KnownFailure;
     21 import java.io.File;
     22 import java.net.MalformedURLException;
     23 import java.net.URL;
     24 import java.net.URLClassLoader;
     25 import java.util.Enumeration;
     26 import java.util.Locale;
     27 import java.util.MissingResourceException;
     28 import java.util.ResourceBundle;
     29 import java.util.StringTokenizer;
     30 import java.util.Vector;
     31 import org.apache.harmony.tests.java.util.support.B;
     32 import tests.support.resource.Support_Resources;
     33 
     34 public class ResourceBundleTest extends junit.framework.TestCase {
     35 
     36     public void test_getCandidateLocales() throws Exception {
     37         ResourceBundle.Control c = ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_DEFAULT);
     38         assertEquals("[en_US, en, ]", c.getCandidateLocales("base", Locale.US).toString());
     39         assertEquals("[de_CH, de, ]", c.getCandidateLocales("base", new Locale("de", "CH")).toString());
     40     }
     41 
     42     /**
     43      * java.util.ResourceBundle#getBundle(java.lang.String,
     44      *        java.util.Locale)
     45      */
     46     public void test_getBundleLjava_lang_StringLjava_util_Locale() {
     47         ResourceBundle bundle;
     48         String name = "tests.support.Support_TestResource";
     49         Locale defLocale = Locale.getDefault();
     50 
     51         Locale.setDefault(new Locale("en", "US"));
     52         bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "VAR"));
     53         assertEquals("Wrong bundle fr_FR_VAR", "frFRVARValue4", bundle.getString("parent4"));
     54 
     55         bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "v1"));
     56         assertEquals("Wrong bundle fr_FR_v1", "frFRValue4", bundle.getString("parent4"));
     57 
     58         bundle = ResourceBundle.getBundle(name, new Locale("fr", "US", "VAR"));
     59         assertEquals("Wrong bundle fr_US_var", "frValue4", bundle.getString("parent4"));
     60 
     61         bundle = ResourceBundle.getBundle(name, new Locale("de", "FR", "VAR"));
     62         assertEquals("Wrong bundle de_FR_var", "enUSValue4", bundle.getString("parent4"));
     63 
     64         Locale.setDefault(new Locale("fr", "FR", "VAR"));
     65         bundle = ResourceBundle.getBundle(name, new Locale("de", "FR", "v1"));
     66         assertEquals("Wrong bundle de_FR_var 2", "frFRVARValue4", bundle.getString("parent4"));
     67 
     68         Locale.setDefault(new Locale("de", "US"));
     69         bundle = ResourceBundle.getBundle(name, new Locale("de", "FR", "var"));
     70         assertEquals("Wrong bundle de_FR_var 2", "parentValue4", bundle.getString("parent4")
     71                 );
     72 
     73         try {
     74             ResourceBundle.getBundle(null, Locale.US);
     75             fail("NullPointerException expected");
     76         } catch (NullPointerException ee) {
     77             //expected
     78         }
     79         try {
     80             ResourceBundle.getBundle("blah", (Locale) null);
     81             fail("NullPointerException expected");
     82         } catch (NullPointerException ee) {
     83             //expected
     84         }
     85 
     86 
     87         try {
     88             ResourceBundle.getBundle("", new Locale("xx", "yy"));
     89             fail("MissingResourceException expected");
     90         } catch (MissingResourceException ee) {
     91             //expected
     92         }
     93     }
     94 
     95     /**
     96      * java.util.ResourceBundle#getBundle(java.lang.String,
     97      *        java.util.Locale, java.lang.ClassLoader)
     98      */
     99     @KnownFailure("It's not allowed to pass null as parent class loader to"
    100             + " a new ClassLoader anymore. Maybe we need to change"
    101             + " URLClassLoader to allow this? It's not specified.")
    102     public void test_getBundleLjava_lang_StringLjava_util_LocaleLjava_lang_ClassLoader() {
    103         String classPath = System.getProperty("java.class.path");
    104         StringTokenizer tok = new StringTokenizer(classPath, File.pathSeparator);
    105         Vector<URL> urlVec = new Vector<URL>();
    106         String resPackage = Support_Resources.RESOURCE_PACKAGE;
    107         try {
    108             while (tok.hasMoreTokens()) {
    109                 String path = tok.nextToken();
    110                 String url;
    111                 if (new File(path).isDirectory())
    112                     url = "file:" + path + resPackage + "subfolder/";
    113                 else
    114                     url = "jar:file:" + path + "!" + resPackage + "subfolder/";
    115                 urlVec.addElement(new URL(url));
    116             }
    117         } catch (MalformedURLException e) {
    118         }
    119         URL[] urls = new URL[urlVec.size()];
    120         for (int i = 0; i < urlVec.size(); i++)
    121             urls[i] = urlVec.elementAt(i);
    122         URLClassLoader loader = new URLClassLoader(urls, null);
    123 
    124         String name = Support_Resources.RESOURCE_PACKAGE_NAME
    125                 + ".hyts_resource";
    126         ResourceBundle bundle = ResourceBundle.getBundle(name, Locale
    127                 .getDefault());
    128             assertEquals("Wrong value read", "parent", bundle.getString("property"));
    129         bundle = ResourceBundle.getBundle(name, Locale.getDefault(), loader);
    130         assertEquals("Wrong cached value",
    131                 "resource", bundle.getString("property"));
    132 
    133         try {
    134             ResourceBundle.getBundle(null, Locale.getDefault(), loader);
    135             fail("NullPointerException expected");
    136         } catch (NullPointerException ee) {
    137             //expected
    138         }
    139 
    140         try {
    141             ResourceBundle.getBundle(name, null, loader);
    142             fail("NullPointerException expected");
    143         } catch (NullPointerException ee) {
    144             //expected
    145         }
    146 
    147         try {
    148             ResourceBundle.getBundle(name, Locale.getDefault(), (ClassLoader) null);
    149             fail("NullPointerException expected");
    150         } catch (NullPointerException ee) {
    151             //expected
    152         }
    153 
    154         try {
    155             ResourceBundle.getBundle("", Locale.getDefault(), loader);
    156             fail("MissingResourceException expected");
    157         } catch (MissingResourceException ee) {
    158             //expected
    159         }
    160 
    161         // Regression test for Harmony-3823
    162         B bb = new B();
    163         String s = bb.find("nonexistent");
    164         s = bb.find("name");
    165         assertEquals("Wrong property got", "Name", s);
    166     }
    167 
    168     /**
    169      * java.util.ResourceBundle#getString(java.lang.String)
    170      */
    171     public void test_getStringLjava_lang_String() {
    172         ResourceBundle bundle;
    173         String name = "tests.support.Support_TestResource";
    174         Locale.setDefault(new Locale("en", "US"));
    175         bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "VAR"));
    176         assertEquals("Wrong value parent4",
    177                 "frFRVARValue4", bundle.getString("parent4"));
    178         assertEquals("Wrong value parent3",
    179                 "frFRValue3", bundle.getString("parent3"));
    180         assertEquals("Wrong value parent2",
    181                 "frValue2", bundle.getString("parent2"));
    182         assertEquals("Wrong value parent1",
    183                 "parentValue1", bundle.getString("parent1"));
    184         assertEquals("Wrong value child3",
    185                 "frFRVARChildValue3", bundle.getString("child3"));
    186         assertEquals("Wrong value child2",
    187                 "frFRVARChildValue2", bundle.getString("child2"));
    188         assertEquals("Wrong value child1",
    189                 "frFRVARChildValue1", bundle.getString("child1"));
    190 
    191         try {
    192             bundle.getString(null);
    193             fail("NullPointerException expected");
    194         } catch (NullPointerException ee) {
    195             //expected
    196         }
    197 
    198         try {
    199             bundle.getString("");
    200             fail("MissingResourceException expected");
    201         } catch (MissingResourceException ee) {
    202             //expected
    203         }
    204 
    205         try {
    206             bundle.getString("IntegerVal");
    207             fail("ClassCastException expected");
    208         } catch (ClassCastException ee) {
    209             //expected
    210         }
    211     }
    212     public void test_getBundle_getClassName() {
    213         // Regression test for Harmony-1759
    214         Locale locale = Locale.GERMAN;
    215         String nonExistentBundle = "Non-ExistentBundle";
    216         try {
    217             ResourceBundle.getBundle(nonExistentBundle, locale, this.getClass()
    218                     .getClassLoader());
    219             fail("MissingResourceException expected!");
    220         } catch (MissingResourceException e) {
    221             assertEquals(nonExistentBundle + "_" + locale, e.getClassName());
    222         }
    223 
    224         try {
    225             ResourceBundle.getBundle(nonExistentBundle, locale);
    226             fail("MissingResourceException expected!");
    227         } catch (MissingResourceException e) {
    228             assertEquals(nonExistentBundle + "_" + locale, e.getClassName());
    229         }
    230 
    231         locale = Locale.getDefault();
    232         try {
    233             ResourceBundle.getBundle(nonExistentBundle);
    234             fail("MissingResourceException expected!");
    235         } catch (MissingResourceException e) {
    236             assertEquals(nonExistentBundle + "_" + locale, e.getClassName());
    237         }
    238     }
    239 
    240     class Mock_ResourceBundle extends ResourceBundle {
    241         @Override
    242         public Enumeration<String> getKeys() {
    243             return null;
    244         }
    245 
    246         @Override
    247         protected Object handleGetObject(String key) {
    248             return null;
    249         }
    250     }
    251 
    252     public void test_constructor() {
    253         assertNotNull(new Mock_ResourceBundle());
    254     }
    255 
    256     public void test_getLocale() {
    257         ResourceBundle bundle;
    258         String name = "tests.support.Support_TestResource";
    259         Locale loc = Locale.getDefault();
    260         Locale.setDefault(new Locale("en", "US"));
    261 
    262         bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "VAR"));
    263         assertEquals("fr_FR_VAR", bundle.getLocale().toString());
    264 
    265         bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "v1"));
    266         assertEquals("fr_FR", bundle.getLocale().toString());
    267 
    268         bundle = ResourceBundle.getBundle(name, new Locale("fr", "US", "VAR"));
    269         assertEquals("fr", bundle.getLocale().toString());
    270 
    271         bundle = ResourceBundle.getBundle(name, new Locale("de", "FR", "VAR"));
    272         assertEquals("en_US", bundle.getLocale().toString());
    273 
    274         bundle = ResourceBundle.getBundle(name, new Locale("de", "FR", "v1"));
    275         assertEquals("en_US", bundle.getLocale().toString());
    276 
    277         bundle = ResourceBundle.getBundle(name, new Locale("de", "FR", "var"));
    278         assertEquals("en_US", bundle.getLocale().toString());
    279 
    280         Locale.setDefault(loc);
    281     }
    282 
    283     public void test_getObjectLjava_lang_String() {
    284         ResourceBundle bundle;
    285         String name = "tests.support.Support_TestResource";
    286         Locale.setDefault(new Locale("en", "US"));
    287         bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "VAR"));
    288         assertEquals("Wrong value parent4",
    289                 "frFRVARValue4", (String)bundle.getObject("parent4"));
    290         assertEquals("Wrong value parent3",
    291                 "frFRValue3", (String)bundle.getObject("parent3"));
    292         assertEquals("Wrong value parent2",
    293                 "frValue2", (String) bundle.getObject("parent2"));
    294         assertEquals("Wrong value parent1",
    295                 "parentValue1", (String)bundle.getObject("parent1"));
    296         assertEquals("Wrong value child3",
    297                 "frFRVARChildValue3", (String)bundle.getObject("child3"));
    298         assertEquals("Wrong value child2",
    299                 "frFRVARChildValue2", (String) bundle.getObject("child2"));
    300         assertEquals("Wrong value child1",
    301                 "frFRVARChildValue1", (String)bundle.getObject("child1"));
    302         assertEquals("Wrong value IntegerVal",
    303                 1, bundle.getObject("IntegerVal"));
    304 
    305         try {
    306             bundle.getObject(null);
    307             fail("NullPointerException expected");
    308         } catch (NullPointerException ee) {
    309             //expected
    310         }
    311 
    312         try {
    313             bundle.getObject("");
    314             fail("MissingResourceException expected");
    315         } catch (MissingResourceException ee) {
    316             //expected
    317         }
    318     }
    319 
    320     public void test_getStringArrayLjava_lang_String() {
    321         ResourceBundle bundle;
    322         String name = "tests.support.Support_TestResource";
    323         Locale.setDefault(new Locale("en", "US"));
    324         bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "VAR"));
    325 
    326         String[] array = bundle.getStringArray("StringArray");
    327         for(int i = 0; i < array.length; i++) {
    328             assertEquals("Str" + (i + 1), array[i]);
    329         }
    330 
    331         try {
    332             bundle.getStringArray(null);
    333             fail("NullPointerException expected");
    334         } catch (NullPointerException ee) {
    335             //expected
    336         }
    337 
    338         try {
    339             bundle.getStringArray("");
    340             fail("MissingResourceException expected");
    341         } catch (MissingResourceException ee) {
    342             //expected
    343         }
    344 
    345         try {
    346             bundle.getStringArray("IntegerVal");
    347             fail("ClassCastException expected");
    348         } catch (ClassCastException ee) {
    349             //expected
    350         }
    351     }
    352 
    353     public void test_getBundleLjava_lang_String() {
    354         ResourceBundle bundle;
    355         String name = "tests.support.Support_TestResource";
    356         Locale defLocale = Locale.getDefault();
    357 
    358         Locale.setDefault(new Locale("en", "US"));
    359         bundle = ResourceBundle.getBundle(name);
    360         assertEquals("enUSValue4", bundle.getString("parent4"));
    361 
    362         Locale.setDefault(new Locale("fr", "FR", "v1"));
    363         bundle = ResourceBundle.getBundle(name);
    364         assertEquals("Wrong bundle fr_FR_v1", "frFRValue4", bundle.getString("parent4"));
    365 
    366         Locale.setDefault(new Locale("fr", "US", "VAR"));
    367         bundle = ResourceBundle.getBundle(name);
    368         assertEquals("Wrong bundle fr_US_var", "frValue4", bundle.getString("parent4"));
    369 
    370         Locale.setDefault(new Locale("de", "FR", "VAR"));
    371         bundle = ResourceBundle.getBundle(name);
    372         assertEquals("Wrong bundle de_FR_var", "parentValue4", bundle.getString("parent4"));
    373 
    374         Locale.setDefault(new Locale("de", "FR", "v1"));
    375         bundle = ResourceBundle.getBundle(name);
    376         assertEquals("Wrong bundle de_FR_var 2", "parentValue4", bundle.getString("parent4"));
    377 
    378         Locale.setDefault(new Locale("de", "FR", "var"));
    379         bundle = ResourceBundle.getBundle(name);
    380         assertEquals("Wrong bundle de_FR_var 2", "parentValue4", bundle.getString("parent4"));
    381 
    382         try {
    383             ResourceBundle.getBundle(null);
    384             fail("NullPointerException expected");
    385         } catch (NullPointerException ee) {
    386             //expected
    387         }
    388 
    389         try {
    390             ResourceBundle.getBundle("");
    391             fail("MissingResourceException expected");
    392         } catch (MissingResourceException ee) {
    393             //expected
    394         }
    395 
    396         Locale.setDefault(defLocale);
    397     }
    398 }
    399