001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.io.File;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.net.URLClassLoader;
024: import java.util.Locale;
025: import java.util.MissingResourceException;
026: import java.util.ResourceBundle;
027: import java.util.StringTokenizer;
028: import java.util.Vector;
029:
030: import org.apache.harmony.luni.tests.support.B;
031:
032: import tests.support.resource.Support_Resources;
033:
034: public class ResourceBundleTest extends junit.framework.TestCase {
035:
036: /**
037: * @tests java.util.ResourceBundle#getBundle(java.lang.String,
038: * java.util.Locale)
039: */
040: public void test_getBundleLjava_lang_StringLjava_util_Locale() {
041: ResourceBundle bundle;
042: String name = "tests.support.Support_TestResource";
043: Locale defLocale = Locale.getDefault();
044:
045: Locale.setDefault(new Locale("en", "US"));
046: bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR",
047: "VAR"));
048: assertEquals("Wrong bundle fr_FR_VAR", "frFRVARValue4", bundle
049: .getString("parent4"));
050: bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR",
051: "v1"));
052: assertEquals("Wrong bundle fr_FR_v1", "frFRValue4", bundle
053: .getString("parent4"));
054: bundle = ResourceBundle.getBundle(name, new Locale("fr", "US",
055: "VAR"));
056: assertEquals("Wrong bundle fr_US_var", "frValue4", bundle
057: .getString("parent4"));
058: bundle = ResourceBundle.getBundle(name, new Locale("de", "FR",
059: "VAR"));
060: assertEquals("Wrong bundle de_FR_var", "enUSValue4", bundle
061: .getString("parent4"));
062:
063: Locale.setDefault(new Locale("fr", "FR", "VAR"));
064: bundle = ResourceBundle.getBundle(name, new Locale("de", "FR",
065: "v1"));
066: assertEquals("Wrong bundle de_FR_var 2", "frFRVARValue4",
067: bundle.getString("parent4"));
068:
069: Locale.setDefault(new Locale("de", "US"));
070: bundle = ResourceBundle.getBundle(name, new Locale("de", "FR",
071: "var"));
072: assertEquals("Wrong bundle de_FR_var 2", "parentValue4", bundle
073: .getString("parent4"));
074:
075: // Test with a security manager
076: Locale.setDefault(new Locale("en", "US"));
077: System.setSecurityManager(new SecurityManager());
078: try {
079: bundle = ResourceBundle.getBundle(name, new Locale("fr",
080: "FR", "VAR"));
081: assertEquals("Security: Wrong bundle fr_FR_VAR",
082: "frFRVARValue4", bundle.getString("parent4"));
083: bundle = ResourceBundle.getBundle(name, new Locale("fr",
084: "FR", "v1"));
085: assertEquals("Security: Wrong bundle fr_FR_v1",
086: "frFRValue4", bundle.getString("parent4"));
087: bundle = ResourceBundle.getBundle(name, new Locale("fr",
088: "US", "VAR"));
089: assertEquals("Security: Wrong bundle fr_US_var",
090: "frValue4", bundle.getString("parent4"));
091: bundle = ResourceBundle.getBundle(name, new Locale("de",
092: "FR", "VAR"));
093: assertTrue("Security: Wrong bundle de_FR_var: "
094: + bundle.getString("parent4"), bundle.getString(
095: "parent4").equals("enUSValue4"));
096: } finally {
097: System.setSecurityManager(null);
098: }
099:
100: Locale.setDefault(defLocale);
101: }
102:
103: /**
104: * @tests java.util.ResourceBundle#getBundle(java.lang.String,
105: * java.util.Locale, java.lang.ClassLoader)
106: */
107: public void test_getBundleLjava_lang_StringLjava_util_LocaleLjava_lang_ClassLoader() {
108: String classPath = System.getProperty("java.class.path");
109: StringTokenizer tok = new StringTokenizer(classPath,
110: File.pathSeparator);
111: Vector urlVec = new Vector();
112: String resPackage = Support_Resources.RESOURCE_PACKAGE;
113: try {
114: while (tok.hasMoreTokens()) {
115: String path = (String) tok.nextToken();
116: String url;
117: if (new File(path).isDirectory())
118: url = "file:" + path + resPackage + "subfolder/";
119: else
120: url = "jar:file:" + path + "!" + resPackage
121: + "subfolder/";
122: urlVec.addElement(new URL(url));
123: }
124: } catch (MalformedURLException e) {
125: }
126: URL[] urls = new URL[urlVec.size()];
127: for (int i = 0; i < urlVec.size(); i++)
128: urls[i] = (URL) urlVec.elementAt(i);
129: URLClassLoader loader = new URLClassLoader(urls, null);
130:
131: String name = Support_Resources.RESOURCE_PACKAGE_NAME
132: + ".hyts_resource";
133: ResourceBundle bundle = ResourceBundle.getBundle(name, Locale
134: .getDefault());
135: assertEquals("Wrong value read", "parent", bundle
136: .getString("property"));
137: bundle = ResourceBundle.getBundle(name, Locale.getDefault(),
138: loader);
139: assertEquals("Wrong cached value", "resource", bundle
140: .getString("property"));
141:
142: // Regression test for Harmony-3823
143: B bb = new B();
144: String s = bb.find("nonexistent");
145: s = bb.find("name");
146: assertEquals("Wrong property got", "Name", s);
147: }
148:
149: /**
150: * @tests java.util.ResourceBundle#getString(java.lang.String)
151: */
152: public void test_getStringLjava_lang_String() {
153: ResourceBundle bundle;
154: String name = "tests.support.Support_TestResource";
155: Locale.setDefault(new Locale("en", "US"));
156: bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR",
157: "VAR"));
158: assertEquals("Wrong value parent4", "frFRVARValue4", bundle
159: .getString("parent4"));
160: assertEquals("Wrong value parent3", "frFRValue3", bundle
161: .getString("parent3"));
162: assertEquals("Wrong value parent2", "frValue2", bundle
163: .getString("parent2"));
164: assertEquals("Wrong value parent1", "parentValue1", bundle
165: .getString("parent1"));
166: assertEquals("Wrong value child3", "frFRVARChildValue3", bundle
167: .getString("child3"));
168: assertEquals("Wrong value child2", "frFRVARChildValue2", bundle
169: .getString("child2"));
170: assertEquals("Wrong value child1", "frFRVARChildValue1", bundle
171: .getString("child1"));
172: }
173:
174: public void test_getBundle_getClassName() {
175: // Regression test for Harmony-1759
176: Locale locale = Locale.GERMAN;
177: String nonExistentBundle = "Non-ExistentBundle";
178: try {
179: ResourceBundle.getBundle(nonExistentBundle, locale, this
180: .getClass().getClassLoader());
181: fail("MissingResourceException expected!");
182: } catch (MissingResourceException e) {
183: assertEquals(nonExistentBundle + "_" + locale, e
184: .getClassName());
185: }
186:
187: try {
188: ResourceBundle.getBundle(nonExistentBundle, locale);
189: fail("MissingResourceException expected!");
190: } catch (MissingResourceException e) {
191: assertEquals(nonExistentBundle + "_" + locale, e
192: .getClassName());
193: }
194:
195: locale = Locale.getDefault();
196: try {
197: ResourceBundle.getBundle(nonExistentBundle);
198: fail("MissingResourceException expected!");
199: } catch (MissingResourceException e) {
200: assertEquals(nonExistentBundle + "_" + locale, e
201: .getClassName());
202: }
203:
204: }
205:
206: protected void setUp() {
207: }
208:
209: protected void tearDown() {
210: }
211: }
|