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