2
|
1 |
/*
|
|
2 |
* Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
/*
|
|
24 |
* @test
|
|
25 |
* @bug 5102289
|
|
26 |
* @summary Test the ResourceBundle.Control factory methods.
|
|
27 |
*/
|
|
28 |
|
|
29 |
import java.util.*;
|
|
30 |
import static java.util.ResourceBundle.Control.*;
|
|
31 |
|
|
32 |
public class ControlFactoryTest {
|
|
33 |
|
|
34 |
/**
|
|
35 |
* An interface for calling ResourceBundle.Control.getControl or
|
|
36 |
* ResourceBundle.Control.getNoFallbackControl.
|
|
37 |
*/
|
|
38 |
private static interface Factory {
|
|
39 |
public ResourceBundle.Control getControl(List<String> formats);
|
|
40 |
public String name();
|
|
41 |
}
|
|
42 |
|
|
43 |
static int errors;
|
|
44 |
|
|
45 |
public static void main(String[] args) {
|
|
46 |
// Test getControl.
|
|
47 |
testControlFactory(new Factory() {
|
|
48 |
public ResourceBundle.Control getControl(List<String> formats) {
|
|
49 |
return ResourceBundle.Control.getControl(formats);
|
|
50 |
}
|
|
51 |
public String name() { return "getControl"; }
|
|
52 |
}, Locale.getDefault());
|
|
53 |
|
|
54 |
// Test getNoFallbackControl.
|
|
55 |
testControlFactory(new Factory() {
|
|
56 |
public ResourceBundle.Control getControl(List<String> formats) {
|
|
57 |
return ResourceBundle.Control.getNoFallbackControl(formats);
|
|
58 |
}
|
|
59 |
public String name() { return "getNoFallbackControl"; }
|
|
60 |
}, null);
|
|
61 |
|
|
62 |
if (errors > 0) {
|
|
63 |
throw new RuntimeException("FAILED: " + errors + " error(s)");
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
private static void testControlFactory(Factory factory, Locale loc) {
|
|
68 |
testGetControl(factory, loc, FORMAT_DEFAULT, "java.class", "java.properties");
|
|
69 |
testGetControl(factory, loc, FORMAT_CLASS, "java.class");
|
|
70 |
testGetControl(factory, loc, FORMAT_PROPERTIES, "java.properties");
|
|
71 |
|
|
72 |
// test IllegalArgumentException
|
|
73 |
String[][] data = {
|
|
74 |
{ "java.class", "java.properties", "java.xml" },
|
|
75 |
{ "java.class", "java.props" },
|
|
76 |
{ "java.properties", "java.class" },
|
|
77 |
{ "java.foo", "java.properties" },
|
|
78 |
{ "java.foo" },
|
|
79 |
{ null },
|
|
80 |
};
|
|
81 |
for (String[] fmts : data) {
|
|
82 |
try {
|
|
83 |
List<String> fmt = Arrays.asList(fmts);
|
|
84 |
ResourceBundle.Control control = factory.getControl(fmt);
|
|
85 |
error("getControl: %s%n", fmt);
|
|
86 |
} catch (IllegalArgumentException e) {
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
// test NPE
|
|
91 |
try {
|
|
92 |
ResourceBundle.Control control = factory.getControl(null);
|
|
93 |
error("%s: doesn't throw NPE.%n", factory.name());
|
|
94 |
} catch (NullPointerException npe) {
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
private static void testGetControl(Factory factory,
|
|
99 |
Locale loc,
|
|
100 |
final List<String> FORMATS,
|
|
101 |
String... fmtStrings) {
|
|
102 |
final ResourceBundle.Control CONTROL = factory.getControl(FORMATS);
|
|
103 |
List<String> fmt = CONTROL.getFormats("any");
|
|
104 |
if (fmt != FORMATS) {
|
|
105 |
error("%s: returns %s, expected %s.%n",
|
|
106 |
factory.name(), fmt, FORMATS);
|
|
107 |
}
|
|
108 |
ResourceBundle.Control control = null;
|
|
109 |
|
|
110 |
// Check if getControl always returns the expected singleton.
|
|
111 |
for (int i = 0; i < 10; i++) {
|
|
112 |
fmt = Arrays.asList(fmtStrings);
|
|
113 |
control = factory.getControl(fmt);
|
|
114 |
if (control != CONTROL) {
|
|
115 |
error("%s: doesn't return the singleton: got %s, expected %s%n",
|
|
116 |
factory.name(), control, CONTROL);
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
// Check if getFallbackLocale performs as expected.
|
|
121 |
Locale defaultLocale = Locale.getDefault();
|
|
122 |
Locale nonDefaultLocale = defaultLocale.equals(Locale.US) ? Locale.JAPAN : Locale.US;
|
|
123 |
if (loc != null) {
|
|
124 |
// Test ResourceBundle.Control.getControl()
|
|
125 |
Locale l = CONTROL.getFallbackLocale("any", nonDefaultLocale);
|
|
126 |
if (!defaultLocale.equals(l)) {
|
|
127 |
error("%s: getFallbackLocale doesn't return default locale. got %s, expected %s%n",
|
|
128 |
factory.name(), toString(l), toString(defaultLocale));
|
|
129 |
}
|
|
130 |
l = CONTROL.getFallbackLocale("any", defaultLocale);
|
|
131 |
if (l != null) {
|
|
132 |
error("%s: getFallbackLocale doesn't return null. got %s%n",
|
|
133 |
factory.name(), toString(l));
|
|
134 |
}
|
|
135 |
} else {
|
|
136 |
// Test ResourceBundle.Control.getNoFallbackControl()
|
|
137 |
Locale l = CONTROL.getFallbackLocale("any", nonDefaultLocale);
|
|
138 |
if (l != null) {
|
|
139 |
error("%s: getFallbackLocale doesn't return null. got %s%n",
|
|
140 |
factory.name(), toString(l));
|
|
141 |
}
|
|
142 |
l = CONTROL.getFallbackLocale("any", defaultLocale);
|
|
143 |
if (l != null) {
|
|
144 |
error("%s: getFallbackLocale doesn't return null. got %s%n",
|
|
145 |
factory.name(), toString(l));
|
|
146 |
}
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
private static String toString(Locale loc) {
|
|
151 |
if (loc == null)
|
|
152 |
return "null";
|
|
153 |
return "\"" + loc.getLanguage() + "_" + loc.getCountry() + "_" + loc.getVariant() + "\"";
|
|
154 |
}
|
|
155 |
|
|
156 |
private static void error(String msg) {
|
|
157 |
System.out.println(msg);
|
|
158 |
errors++;
|
|
159 |
}
|
|
160 |
|
|
161 |
private static void error(String fmt, Object... args) {
|
|
162 |
System.out.printf(fmt, args);
|
|
163 |
errors++;
|
|
164 |
}
|
|
165 |
}
|