ResourceBundleTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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
18package tests.api.java.util;
19
20import dalvik.annotation.TestTargetNew;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetClass;
24import dalvik.annotation.KnownFailure;
25
26import java.io.File;
27import java.net.MalformedURLException;
28import java.net.URL;
29import java.net.URLClassLoader;
30import java.security.Permission;
31import java.util.Enumeration;
32import java.util.Locale;
33import java.util.MissingResourceException;
34import java.util.ResourceBundle;
35import java.util.StringTokenizer;
36import java.util.Vector;
37
38import tests.api.java.util.support.B;
39import tests.support.resource.Support_Resources;
40
41@TestTargetClass(ResourceBundle.class)
42public class ResourceBundleTest extends junit.framework.TestCase {
43    SecurityManager sm = new SecurityManager() {
44
45        @Override
46        public void checkPermission(Permission perm) {
47        }
48    };
49
50    /**
51     * @tests java.util.ResourceBundle#getBundle(java.lang.String,
52     *        java.util.Locale)
53     */
54    @TestTargetNew(
55        level = TestLevel.COMPLETE,
56        notes = "",
57        method = "getBundle",
58        args = {java.lang.String.class, java.util.Locale.class}
59    )
60    public void test_getBundleLjava_lang_StringLjava_util_Locale() {
61        ResourceBundle bundle;
62        String name = "tests.support.Support_TestResource";
63        Locale defLocale = Locale.getDefault();
64
65        Locale.setDefault(new Locale("en", "US"));
66        bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "VAR"));
67        assertEquals("Wrong bundle fr_FR_VAR", "frFRVARValue4", bundle.getString("parent4")
68                );
69        bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "v1"));
70        assertEquals("Wrong bundle fr_FR_v1",
71                "frFRValue4", bundle.getString("parent4"));
72        bundle = ResourceBundle.getBundle(name, new Locale("fr", "US", "VAR"));
73        assertEquals("Wrong bundle fr_US_var", "frValue4", bundle.getString("parent4")
74                );
75        bundle = ResourceBundle.getBundle(name, new Locale("de", "FR", "VAR"));
76        assertEquals("Wrong bundle de_FR_var", "enUSValue4", bundle.getString("parent4")
77                );
78
79        Locale.setDefault(new Locale("fr", "FR", "VAR"));
80        bundle = ResourceBundle.getBundle(name, new Locale("de", "FR", "v1"));
81        assertEquals("Wrong bundle de_FR_var 2", "frFRVARValue4", bundle.getString("parent4")
82                );
83
84        Locale.setDefault(new Locale("de", "US"));
85        bundle = ResourceBundle.getBundle(name, new Locale("de", "FR", "var"));
86        assertEquals("Wrong bundle de_FR_var 2", "parentValue4", bundle.getString("parent4")
87                );
88
89        // Test with a security manager
90        Locale.setDefault(new Locale("en", "US"));
91        SecurityManager oldSm = System.getSecurityManager();
92        System.setSecurityManager(sm);
93        try {
94            bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR",
95                    "VAR"));
96            assertEquals("Security: Wrong bundle fr_FR_VAR", "frFRVARValue4", bundle.getString(
97                    "parent4"));
98            bundle = ResourceBundle.getBundle(name,
99                    new Locale("fr", "FR", "v1"));
100            assertEquals("Security: Wrong bundle fr_FR_v1", "frFRValue4", bundle.getString(
101                    "parent4"));
102            bundle = ResourceBundle.getBundle(name, new Locale("fr", "US",
103                    "VAR"));
104            assertEquals("Security: Wrong bundle fr_US_var", "frValue4", bundle.getString(
105                    "parent4"));
106            bundle = ResourceBundle.getBundle(name, new Locale("de", "FR",
107                    "VAR"));
108            assertTrue("Security: Wrong bundle de_FR_var: "
109                    + bundle.getString("parent4"), bundle.getString("parent4")
110                    .equals("enUSValue4"));
111        } finally {
112            System.setSecurityManager(oldSm);
113        }
114
115        try {
116            ResourceBundle.getBundle(null, Locale.getDefault());
117            fail("NullPointerException expected");
118        } catch (NullPointerException ee) {
119            //expected
120        }
121
122        try {
123            ResourceBundle.getBundle("", new Locale("xx", "yy"));
124            fail("MissingResourceException expected");
125        } catch (MissingResourceException ee) {
126            //expected
127        }
128
129        Locale.setDefault(defLocale);
130    }
131
132    /**
133     * @tests java.util.ResourceBundle#getBundle(java.lang.String,
134     *        java.util.Locale, java.lang.ClassLoader)
135     */
136    @TestTargetNew(
137        level = TestLevel.COMPLETE,
138        notes = "",
139        method = "getBundle",
140        args = {java.lang.String.class, java.util.Locale.class, java.lang.ClassLoader.class}
141    )
142    @KnownFailure("ToT fixed")
143    public void test_getBundleLjava_lang_StringLjava_util_LocaleLjava_lang_ClassLoader() {
144        String classPath = System.getProperty("java.class.path");
145        StringTokenizer tok = new StringTokenizer(classPath, File.pathSeparator);
146        Vector<URL> urlVec = new Vector<URL>();
147        String resPackage = Support_Resources.RESOURCE_PACKAGE;
148        try {
149            while (tok.hasMoreTokens()) {
150                String path = tok.nextToken();
151                String url;
152                if (new File(path).isDirectory())
153                    url = "file:" + path + resPackage + "subfolder/";
154                else
155                    url = "jar:file:" + path + "!" + resPackage + "subfolder/";
156                urlVec.addElement(new URL(url));
157            }
158        } catch (MalformedURLException e) {
159        }
160        URL[] urls = new URL[urlVec.size()];
161        for (int i = 0; i < urlVec.size(); i++)
162            urls[i] = urlVec.elementAt(i);
163        URLClassLoader loader = new URLClassLoader(urls, null);
164
165        String name = Support_Resources.RESOURCE_PACKAGE_NAME
166                + ".hyts_resource";
167        ResourceBundle bundle = ResourceBundle.getBundle(name, Locale
168                .getDefault());
169            assertEquals("Wrong value read", "parent", bundle.getString("property"));
170        bundle = ResourceBundle.getBundle(name, Locale.getDefault(), loader);
171        assertEquals("Wrong cached value",
172                "resource", bundle.getString("property"));
173
174        try {
175            ResourceBundle.getBundle(null, Locale.getDefault(), loader);
176            fail("NullPointerException expected");
177        } catch (NullPointerException ee) {
178            //expected
179        }
180
181        try {
182            ResourceBundle.getBundle(name, null, loader);
183            fail("NullPointerException expected");
184        } catch (NullPointerException ee) {
185            //expected
186        }
187
188        try {
189            ResourceBundle.getBundle(name, Locale.getDefault(), null);
190            fail("NullPointerException expected");
191        } catch (NullPointerException ee) {
192            //expected
193        }
194
195        try {
196            ResourceBundle.getBundle("", Locale.getDefault(), loader);
197            fail("MissingResourceException expected");
198        } catch (MissingResourceException ee) {
199            //expected
200        }
201
202        // Regression test for Harmony-3823
203        B bb = new B();
204        String s = bb.find("nonexistent");
205        s = bb.find("name");
206        assertEquals("Wrong property got", "Name", s);
207    }
208
209    /**
210     * @tests java.util.ResourceBundle#getString(java.lang.String)
211     */
212    @TestTargetNew(
213        level = TestLevel.COMPLETE,
214        notes = "",
215        method = "getString",
216        args = {java.lang.String.class}
217    )
218    public void test_getStringLjava_lang_String() {
219        ResourceBundle bundle;
220        String name = "tests.support.Support_TestResource";
221        Locale.setDefault(new Locale("en", "US"));
222        bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "VAR"));
223        assertEquals("Wrong value parent4",
224                "frFRVARValue4", bundle.getString("parent4"));
225        assertEquals("Wrong value parent3",
226                "frFRValue3", bundle.getString("parent3"));
227        assertEquals("Wrong value parent2",
228                "frValue2", bundle.getString("parent2"));
229        assertEquals("Wrong value parent1",
230                "parentValue1", bundle.getString("parent1"));
231        assertEquals("Wrong value child3",
232                "frFRVARChildValue3", bundle.getString("child3"));
233        assertEquals("Wrong value child2",
234                "frFRVARChildValue2", bundle.getString("child2"));
235        assertEquals("Wrong value child1",
236                "frFRVARChildValue1", bundle.getString("child1"));
237
238        try {
239            bundle.getString(null);
240            fail("NullPointerException expected");
241        } catch (NullPointerException ee) {
242            //expected
243        }
244
245        try {
246            bundle.getString("");
247            fail("MissingResourceException expected");
248        } catch (MissingResourceException ee) {
249            //expected
250        }
251
252        try {
253            bundle.getString("IntegerVal");
254            fail("ClassCastException expected");
255        } catch (ClassCastException ee) {
256            //expected
257        }
258    }
259    @TestTargetNew(
260        level = TestLevel.PARTIAL_COMPLETE,
261        notes = "Regression test. Doesn't verify NullPointerException.",
262        method = "getBundle",
263        args = {java.lang.String.class}
264    )
265    public void test_getBundle_getClassName() {
266        // Regression test for Harmony-1759
267        Locale locale = Locale.GERMAN;
268        String nonExistentBundle = "Non-ExistentBundle";
269        try {
270            ResourceBundle.getBundle(nonExistentBundle, locale, this.getClass()
271                    .getClassLoader());
272            fail("MissingResourceException expected!");
273        } catch (MissingResourceException e) {
274            assertEquals(nonExistentBundle + "_" + locale, e.getClassName());
275        }
276
277        try {
278            ResourceBundle.getBundle(nonExistentBundle, locale);
279            fail("MissingResourceException expected!");
280        } catch (MissingResourceException e) {
281            assertEquals(nonExistentBundle + "_" + locale, e.getClassName());
282        }
283
284        locale = Locale.getDefault();
285        try {
286            ResourceBundle.getBundle(nonExistentBundle);
287            fail("MissingResourceException expected!");
288        } catch (MissingResourceException e) {
289            assertEquals(nonExistentBundle + "_" + locale, e.getClassName());
290        }
291    }
292
293    class Mock_ResourceBundle extends ResourceBundle {
294        @Override
295        public Enumeration<String> getKeys() {
296            return null;
297        }
298
299        @Override
300        protected Object handleGetObject(String key) {
301            return null;
302        }
303    }
304
305    @TestTargetNew(
306        level = TestLevel.COMPLETE,
307        notes = "",
308        method = "ResourceBundle",
309        args = {}
310    )
311    public void test_constructor() {
312        assertNotNull(new Mock_ResourceBundle());
313    }
314
315    @TestTargetNew(
316        level = TestLevel.COMPLETE,
317        notes = "",
318        method = "getLocale",
319        args = {}
320    )
321    public void test_getLocale() {
322        ResourceBundle bundle;
323        String name = "tests.support.Support_TestResource";
324        Locale loc = Locale.getDefault();
325        Locale.setDefault(new Locale("en", "US"));
326
327        bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "VAR"));
328        assertEquals("fr_FR_VAR", bundle.getLocale().toString());
329
330        bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "v1"));
331        assertEquals("fr_FR", bundle.getLocale().toString());
332
333        bundle = ResourceBundle.getBundle(name, new Locale("fr", "US", "VAR"));
334        assertEquals("fr", bundle.getLocale().toString());
335
336        bundle = ResourceBundle.getBundle(name, new Locale("de", "FR", "VAR"));
337        assertEquals("en_US", bundle.getLocale().toString());
338
339        bundle = ResourceBundle.getBundle(name, new Locale("de", "FR", "v1"));
340        assertEquals("en_US", bundle.getLocale().toString());
341
342        bundle = ResourceBundle.getBundle(name, new Locale("de", "FR", "var"));
343        assertEquals("en_US", bundle.getLocale().toString());
344
345        Locale.setDefault(loc);
346    }
347
348    @TestTargetNew(
349        level = TestLevel.COMPLETE,
350        notes = "",
351        method = "getObject",
352        args = {java.lang.String.class}
353    )
354    public void test_getObjectLjava_lang_String() {
355        ResourceBundle bundle;
356        String name = "tests.support.Support_TestResource";
357        Locale.setDefault(new Locale("en", "US"));
358        bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "VAR"));
359        assertEquals("Wrong value parent4",
360                "frFRVARValue4", (String)bundle.getObject("parent4"));
361        assertEquals("Wrong value parent3",
362                "frFRValue3", (String)bundle.getObject("parent3"));
363        assertEquals("Wrong value parent2",
364                "frValue2", (String)bundle.getObject("parent2"));
365        assertEquals("Wrong value parent1",
366                "parentValue1", (String)bundle.getObject("parent1"));
367        assertEquals("Wrong value child3",
368                "frFRVARChildValue3", (String)bundle.getObject("child3"));
369        assertEquals("Wrong value child2",
370                "frFRVARChildValue2", (String)bundle.getObject("child2"));
371        assertEquals("Wrong value child1",
372                "frFRVARChildValue1", (String)bundle.getObject("child1"));
373        assertEquals("Wrong value IntegerVal",
374                1, bundle.getObject("IntegerVal"));
375
376        try {
377            bundle.getObject(null);
378            fail("NullPointerException expected");
379        } catch (NullPointerException ee) {
380            //expected
381        }
382
383        try {
384            bundle.getObject("");
385            fail("MissingResourceException expected");
386        } catch (MissingResourceException ee) {
387            //expected
388        }
389    }
390
391    @TestTargets({
392        @TestTargetNew(
393            level = TestLevel.COMPLETE,
394            notes = "",
395            method = "getStringArray",
396            args = {java.lang.String.class}
397        ),
398        @TestTargetNew(
399            level = TestLevel.COMPLETE,
400            notes = "",
401            method = "setParent",
402            args = {java.util.ResourceBundle.class}
403        )
404    })
405    public void test_getStringArrayLjava_lang_String() {
406        ResourceBundle bundle;
407        String name = "tests.support.Support_TestResource";
408        Locale.setDefault(new Locale("en", "US"));
409        bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "VAR"));
410
411        String[] array = bundle.getStringArray("StringArray");
412        for(int i = 0; i < array.length; i++) {
413            assertEquals("Str" + (i + 1), array[i]);
414        }
415
416        try {
417            bundle.getStringArray(null);
418            fail("NullPointerException expected");
419        } catch (NullPointerException ee) {
420            //expected
421        }
422
423        try {
424            bundle.getStringArray("");
425            fail("MissingResourceException expected");
426        } catch (MissingResourceException ee) {
427            //expected
428        }
429
430        try {
431            bundle.getStringArray("IntegerVal");
432            fail("ClassCastException expected");
433        } catch (ClassCastException ee) {
434            //expected
435        }
436    }
437
438    @TestTargetNew(
439        level = TestLevel.COMPLETE,
440        notes = "",
441        method = "getBundle",
442        args = {java.lang.String.class}
443    )
444    public void test_getBundleLjava_lang_String() {
445        ResourceBundle bundle;
446        String name = "tests.support.Support_TestResource";
447        Locale defLocale = Locale.getDefault();
448
449        Locale.setDefault(new Locale("en", "US"));
450        bundle = ResourceBundle.getBundle(name);
451        assertEquals("enUSValue4", bundle.getString("parent4")
452                );
453        Locale.setDefault(new Locale("fr", "FR", "v1"));
454        bundle = ResourceBundle.getBundle(name);
455        assertEquals("Wrong bundle fr_FR_v1",
456                "frFRValue4", bundle.getString("parent4"));
457        Locale.setDefault(new Locale("fr", "US", "VAR"));
458        bundle = ResourceBundle.getBundle(name);
459        assertEquals("Wrong bundle fr_US_var", "frValue4", bundle.getString("parent4")
460                );
461        Locale.setDefault(new Locale("de", "FR", "VAR"));
462        bundle = ResourceBundle.getBundle(name);
463        assertEquals("Wrong bundle de_FR_var", "parentValue4", bundle.getString("parent4")
464                );
465        Locale.setDefault(new Locale("de", "FR", "v1"));
466        bundle = ResourceBundle.getBundle(name);
467        assertEquals("Wrong bundle de_FR_var 2", "parentValue4", bundle.getString("parent4")
468                );
469        Locale.setDefault(new Locale("de", "FR", "var"));
470        bundle = ResourceBundle.getBundle(name);
471        assertEquals("Wrong bundle de_FR_var 2", "parentValue4", bundle.getString("parent4")
472                );
473
474        try {
475            ResourceBundle.getBundle(null);
476            fail("NullPointerException expected");
477        } catch (NullPointerException ee) {
478            //expected
479        }
480
481        try {
482            ResourceBundle.getBundle("");
483            fail("MissingResourceException expected");
484        } catch (MissingResourceException ee) {
485            //expected
486        }
487    }
488
489    protected void setUp() {
490    }
491
492    protected void tearDown() {
493    }
494}
495