author | naoto |
Thu, 07 Nov 2013 10:03:38 -0800 | |
changeset 21623 | e88796f62a8e |
parent 20865 | 164ba3f3484b |
child 32649 | 2ee9017c7597 |
permissions | -rw-r--r-- |
20865
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
1 |
/* |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
4 |
* |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
8 |
* |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
13 |
* accompanied this code). |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
14 |
* |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
18 |
* |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
21 |
* questions. |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
22 |
*/ |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
23 |
import java.util.Collections; |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
24 |
import java.util.Enumeration; |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
25 |
import java.util.Locale; |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
26 |
import java.util.MissingResourceException; |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
27 |
import java.util.PropertyResourceBundle; |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
28 |
import java.util.ResourceBundle; |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
29 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
30 |
/** |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
31 |
* @test |
21623 | 32 |
* @bug 4814565 8027930 |
20865
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
33 |
* @summary tests ResourceBundle.getBaseBundleName(); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
34 |
* @build TestGetBaseBundleName resources.ListBundle resources.ListBundle_fr |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
35 |
* @run main TestGetBaseBundleName |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
36 |
* @author danielfuchs |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
37 |
*/ |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
38 |
public class TestGetBaseBundleName { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
39 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
40 |
final static String PROPERTY_BUNDLE_NAME = "resources/PropertyBundle"; |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
41 |
final static String LIST_BUNDLE_NAME = "resources.ListBundle"; |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
42 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
43 |
public static String getBaseName(ResourceBundle bundle) { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
44 |
return bundle == null ? null : bundle.getBaseBundleName(); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
45 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
46 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
47 |
public static void main(String... args) throws Exception { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
48 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
49 |
Locale defaultLocale = Locale.getDefault(); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
50 |
System.out.println("Default locale is: " + defaultLocale); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
51 |
for (String baseName : new String[] { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
52 |
PROPERTY_BUNDLE_NAME, |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
53 |
LIST_BUNDLE_NAME |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
54 |
}) { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
55 |
try { |
21623 | 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); |
|
20865
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
69 |
ResourceBundle bundle_fr = ResourceBundle.getBundle(baseName); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
70 |
if (!Locale.FRENCH.equals(bundle_fr.getLocale())) { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
71 |
throw new RuntimeException("Unexpected locale: " |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
72 |
+ bundle_fr.getLocale()); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
73 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
74 |
if (!baseName.equals(getBaseName(bundle_fr))) { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
75 |
throw new RuntimeException("Unexpected base name: " |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
76 |
+ getBaseName(bundle_fr)); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
77 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
78 |
} finally { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
79 |
Locale.setDefault(defaultLocale); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
80 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
81 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
82 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
83 |
final ResourceBundle bundle = new ResourceBundle() { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
84 |
@Override |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
85 |
protected Object handleGetObject(String key) { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
86 |
if ("dummy".equals(key)) return "foo"; |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
87 |
throw new MissingResourceException("Missing key", |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
88 |
this.getClass().getName(), key); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
89 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
90 |
@Override |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
91 |
public Enumeration<String> getKeys() { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
92 |
return Collections.enumeration(java.util.Arrays.asList( |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
93 |
new String[] {"dummy"})); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
94 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
95 |
}; |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
96 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
97 |
if (getBaseName(bundle) != null) { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
98 |
throw new RuntimeException("Expected null baseName, got " |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
99 |
+ getBaseName(bundle)); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
100 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
101 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
102 |
final ResourceBundle bundle2 = new ResourceBundle() { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
103 |
@Override |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
104 |
protected Object handleGetObject(String key) { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
105 |
if ("dummy".equals(key)) return "foo"; |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
106 |
throw new MissingResourceException("Missing key", |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
107 |
this.getClass().getName(), key); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
108 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
109 |
@Override |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
110 |
public Enumeration<String> getKeys() { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
111 |
return Collections.enumeration(java.util.Arrays.asList( |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
112 |
new String[] {"dummy"})); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
113 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
114 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
115 |
@Override |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
116 |
public String getBaseBundleName() { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
117 |
return this.getClass().getName(); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
118 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
119 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
120 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
121 |
}; |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
122 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
123 |
if (!bundle2.getClass().getName().equals(getBaseName(bundle2))) { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
124 |
throw new RuntimeException("Expected " |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
125 |
+ bundle2.getClass().getName() + ", got " |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
126 |
+ getBaseName(bundle2)); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
127 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
128 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
129 |
ResourceBundle propertyBundle = new PropertyResourceBundle( |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
130 |
TestGetBaseBundleName.class.getResourceAsStream( |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
131 |
PROPERTY_BUNDLE_NAME+".properties")); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
132 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
133 |
if (getBaseName(propertyBundle) != null) { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
134 |
throw new RuntimeException("Expected null baseName, got " |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
135 |
+ getBaseName(propertyBundle)); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
136 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
137 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
138 |
ResourceBundle listBundle = new resources.ListBundle_fr(); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
139 |
if (getBaseName(listBundle) != null) { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
140 |
throw new RuntimeException("Expected null baseName, got " |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
141 |
+ getBaseName(listBundle)); |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
142 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
143 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
144 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
145 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
diff
changeset
|
146 |
} |