1 /* |
|
2 * Copyright 2007-2008 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 /* |
|
25 * @test LocaleTest.java |
|
26 * @bug 5072267 |
|
27 * @summary Test client locales. |
|
28 * @author Eamonn McManus |
|
29 */ |
|
30 |
|
31 import java.lang.management.ManagementFactory; |
|
32 import java.util.Collections; |
|
33 import java.util.ListResourceBundle; |
|
34 import java.util.Locale; |
|
35 import java.util.Map; |
|
36 import java.util.ResourceBundle; |
|
37 import java.util.concurrent.Callable; |
|
38 import javax.management.ClientContext; |
|
39 import java.util.Arrays; |
|
40 import javax.management.MBeanServer; |
|
41 import javax.management.ObjectName; |
|
42 |
|
43 public class LocaleTest { |
|
44 private static String failure; |
|
45 |
|
46 public static void main(String[] args) throws Exception { |
|
47 |
|
48 // Test the translation String -> Locale |
|
49 |
|
50 Locale[] locales = Locale.getAvailableLocales(); |
|
51 System.out.println("Testing String->Locale for " + locales.length + |
|
52 " locales"); |
|
53 for (Locale loc : locales) { |
|
54 Map<String, String> ctx = Collections.singletonMap( |
|
55 ClientContext.LOCALE_KEY, loc.toString()); |
|
56 Locale loc2 = ClientContext.doWithContext( |
|
57 ctx, new Callable<Locale>() { |
|
58 public Locale call() { |
|
59 return ClientContext.getLocale(); |
|
60 } |
|
61 }); |
|
62 assertEquals(loc, loc2); |
|
63 } |
|
64 |
|
65 // Test that a locale-sensitive attribute works |
|
66 |
|
67 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); |
|
68 mbs = ClientContext.newContextForwarder(mbs, null); |
|
69 ObjectName name = new ObjectName("a:type=LocaleSensitive"); |
|
70 mbs.registerMBean(new LocaleSensitive(), name); |
|
71 Locale.setDefault(Locale.US); |
|
72 |
|
73 assertEquals("spectacular failure", |
|
74 mbs.getAttribute(name, "LastProblemDescription")); |
|
75 |
|
76 MBeanServer frmbs = ClientContext.withContext( |
|
77 mbs, ClientContext.LOCALE_KEY, Locale.FRANCE.toString()); |
|
78 assertEquals("\u00e9chec r\u00e9tentissant", |
|
79 frmbs.getAttribute(name, "LastProblemDescription")); |
|
80 |
|
81 if (failure == null) |
|
82 System.out.println("TEST PASSED"); |
|
83 else |
|
84 throw new Exception("TEST FAILED: " + failure); |
|
85 } |
|
86 |
|
87 public static interface LocaleSensitiveMBean { |
|
88 public String getLastProblemDescription(); |
|
89 } |
|
90 |
|
91 public static class LocaleSensitive implements LocaleSensitiveMBean { |
|
92 public String getLastProblemDescription() { |
|
93 Locale loc = ClientContext.getLocale(); |
|
94 ResourceBundle rb = ResourceBundle.getBundle( |
|
95 MyResources.class.getName(), loc); |
|
96 return rb.getString("spectacular"); |
|
97 } |
|
98 } |
|
99 |
|
100 public static class MyResources extends ListResourceBundle { |
|
101 protected Object[][] getContents() { |
|
102 return new Object[][] { |
|
103 {"spectacular", "spectacular failure"}, |
|
104 }; |
|
105 } |
|
106 } |
|
107 |
|
108 public static class MyResources_fr extends ListResourceBundle { |
|
109 protected Object[][] getContents() { |
|
110 return new Object[][] { |
|
111 {"spectacular", "\u00e9chec r\u00e9tentissant"}, |
|
112 }; |
|
113 } |
|
114 } |
|
115 |
|
116 private static void assertEquals(Object x, Object y) { |
|
117 if (!equal(x, y)) |
|
118 failed("expected " + string(x) + "; got " + string(y)); |
|
119 } |
|
120 |
|
121 private static boolean equal(Object x, Object y) { |
|
122 if (x == y) |
|
123 return true; |
|
124 if (x == null || y == null) |
|
125 return false; |
|
126 if (x.getClass().isArray()) |
|
127 return Arrays.deepEquals(new Object[] {x}, new Object[] {y}); |
|
128 return x.equals(y); |
|
129 } |
|
130 |
|
131 private static String string(Object x) { |
|
132 String s = Arrays.deepToString(new Object[] {x}); |
|
133 return s.substring(1, s.length() - 1); |
|
134 } |
|
135 |
|
136 private static void failed(String why) { |
|
137 failure = why; |
|
138 new Throwable("FAILED: " + why).printStackTrace(System.out); |
|
139 } |
|
140 } |
|