J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 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 4303146 5102289 6272060 |
| 26 | * @summary Test non-standard loading strategies with ResourceBundle.Control subclasses |
| 27 | */ |
| 28 | |
| 29 | import java.io.*; |
| 30 | import java.util.*; |
| 31 | |
| 32 | public class LoadingStrategiesTest { |
| 33 | |
| 34 | static int errors; |
| 35 | |
| 36 | public static void main(String[] args) { |
| 37 | ResourceBundle rb; |
| 38 | String s; |
| 39 | |
| 40 | // Test zh_TW -> root, zh_CN -> zh -> root |
| 41 | rb = ResourceBundle.getBundle("Chinese", Locale.TAIWAN, new ChineseControl()); |
| 42 | s = rb.getString("data"); |
| 43 | check("chinese with Locale.TAIWAN", s, "root"); |
| 44 | |
| 45 | rb = ResourceBundle.getBundle("Chinese", Locale.CHINA, new ChineseControl()); |
| 46 | s = rb.getString("data"); |
| 47 | check("chinese with Locale.CHINA", s, "zh"); |
| 48 | |
| 49 | |
| 50 | // Test use of per-locale packaging |
| 51 | preparePerLocalePackageProperties(); |
| 52 | |
| 53 | rb = ResourceBundle.getBundle("test.package.Messages", Locale.US, |
| 54 | new PerLocalePackageControl()); |
| 55 | s = rb.getString("data"); |
| 56 | check("Per-locale package with Locale.US", s, ""); |
| 57 | |
| 58 | rb = ResourceBundle.getBundle("test.package.Messages", Locale.GERMAN, |
| 59 | new PerLocalePackageControl()); |
| 60 | s = rb.getString("data"); |
| 61 | check("Per-locale package with Locale.GERMAN", s, "de"); |
| 62 | |
| 63 | rb = ResourceBundle.getBundle("test.package.Messages", Locale.JAPAN, |
| 64 | new PerLocalePackageControl()); |
| 65 | s = rb.getString("data"); |
| 66 | check("Per-locale package with Locale.JAPAN", s, "ja_JP"); |
| 67 | |
| 68 | |
| 69 | // Check any errors |
| 70 | if (errors > 0) { |
| 71 | throw new RuntimeException("FAILED: " + errors + " error(s)"); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private static void check(String msg, String got, String expected) { |
| 76 | if (!got.equals(expected)) { |
| 77 | error("%s: got \"%s\", expected \"%s\"%n", msg, got, expected); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private static class ChineseControl extends ResourceBundle.Control { |
| 82 | @Override |
| 83 | public List<Locale> getCandidateLocales(String baseName, |
| 84 | Locale locale) { |
| 85 | if (locale.equals(Locale.TAIWAN)) { |
| 86 | return Arrays.asList(locale, |
| 87 | // no Locale("zh") |
| 88 | new Locale("")); |
| 89 | } |
| 90 | return super.getCandidateLocales(baseName, locale); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private static class PerLocalePackageControl extends ResourceBundle.Control { |
| 95 | @Override |
| 96 | public String toBundleName(String baseName, Locale locale) { |
| 97 | if (baseName == null || locale == null) { |
| 98 | throw new NullPointerException(); |
| 99 | } |
| 100 | String loc = super.toBundleName("", locale); |
| 101 | if (loc.length() > 0) { |
| 102 | return baseName.replaceFirst("^([\\w\\.]+)\\.(\\w+)$", |
| 103 | "$1." + loc.substring(1) + ".$2"); |
| 104 | } |
| 105 | return baseName; |
| 106 | } |
| 107 | |
| 108 | // Avoid fallback to the default locale (6272060) |
| 109 | @Override |
| 110 | public Locale getFallbackLocale(String baseName, Locale locale) { |
| 111 | if (baseName == null || locale == null) { |
| 112 | throw new NullPointerException(); |
| 113 | } |
| 114 | return null; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Creates: |
| 119 | // test/package/Messages.properties |
| 120 | // test/package/de/Messages.properties |
| 121 | // test/package/ja_JP/Messages.properties |
| 122 | private static void preparePerLocalePackageProperties() { |
| 123 | final String DEL = File.separator; |
| 124 | try { |
| 125 | String dir = System.getProperty("test.classes", "."); |
| 126 | String[] subdirs = { "", "de", "ja_JP" }; |
| 127 | for (String subdir : subdirs) { |
| 128 | StringBuilder sb = new StringBuilder(); |
| 129 | sb.append(dir).append(DEL).append("test").append(DEL).append("package"); |
| 130 | if (subdir.length() > 0) { |
| 131 | sb.append(DEL).append(subdir); |
| 132 | } |
| 133 | File path = new File(sb.toString()); |
| 134 | path.mkdirs(); |
| 135 | File propsfile = new File(path, "Messages.properties"); |
| 136 | OutputStream os = new FileOutputStream(propsfile); |
| 137 | Properties props = new Properties(); |
| 138 | props.setProperty("data", subdir); |
| 139 | props.store(os, null); |
| 140 | System.out.println("Created: " + propsfile); |
| 141 | os.close(); |
| 142 | } |
| 143 | } catch (Exception e) { |
| 144 | throw new RuntimeException("Can't set up per-locale properties", e); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | private static void error(String msg) { |
| 149 | System.out.println(msg); |
| 150 | errors++; |
| 151 | } |
| 152 | |
| 153 | private static void error(String fmt, Object... args) { |
| 154 | System.out.printf(fmt, args); |
| 155 | errors++; |
| 156 | } |
| 157 | } |