0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A/*
0N/A * @test
0N/A * @bug 4303146 5102289 6272060
0N/A * @summary Test non-standard loading strategies with ResourceBundle.Control subclasses
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Apublic class LoadingStrategiesTest {
0N/A
0N/A static int errors;
0N/A
0N/A public static void main(String[] args) {
0N/A ResourceBundle rb;
0N/A String s;
0N/A
0N/A // Test zh_TW -> root, zh_CN -> zh -> root
0N/A rb = ResourceBundle.getBundle("Chinese", Locale.TAIWAN, new ChineseControl());
0N/A s = rb.getString("data");
0N/A check("chinese with Locale.TAIWAN", s, "root");
0N/A
0N/A rb = ResourceBundle.getBundle("Chinese", Locale.CHINA, new ChineseControl());
0N/A s = rb.getString("data");
0N/A check("chinese with Locale.CHINA", s, "zh");
0N/A
0N/A
0N/A // Test use of per-locale packaging
0N/A preparePerLocalePackageProperties();
0N/A
0N/A rb = ResourceBundle.getBundle("test.package.Messages", Locale.US,
0N/A new PerLocalePackageControl());
0N/A s = rb.getString("data");
0N/A check("Per-locale package with Locale.US", s, "");
0N/A
0N/A rb = ResourceBundle.getBundle("test.package.Messages", Locale.GERMAN,
0N/A new PerLocalePackageControl());
0N/A s = rb.getString("data");
0N/A check("Per-locale package with Locale.GERMAN", s, "de");
0N/A
0N/A rb = ResourceBundle.getBundle("test.package.Messages", Locale.JAPAN,
0N/A new PerLocalePackageControl());
0N/A s = rb.getString("data");
0N/A check("Per-locale package with Locale.JAPAN", s, "ja_JP");
0N/A
0N/A
0N/A // Check any errors
0N/A if (errors > 0) {
0N/A throw new RuntimeException("FAILED: " + errors + " error(s)");
0N/A }
0N/A }
0N/A
0N/A private static void check(String msg, String got, String expected) {
0N/A if (!got.equals(expected)) {
0N/A error("%s: got \"%s\", expected \"%s\"%n", msg, got, expected);
0N/A }
0N/A }
0N/A
0N/A private static class ChineseControl extends ResourceBundle.Control {
0N/A @Override
0N/A public List<Locale> getCandidateLocales(String baseName,
0N/A Locale locale) {
0N/A if (locale.equals(Locale.TAIWAN)) {
0N/A return Arrays.asList(locale,
0N/A // no Locale("zh")
0N/A new Locale(""));
0N/A }
0N/A return super.getCandidateLocales(baseName, locale);
0N/A }
0N/A }
0N/A
0N/A private static class PerLocalePackageControl extends ResourceBundle.Control {
0N/A @Override
0N/A public String toBundleName(String baseName, Locale locale) {
0N/A if (baseName == null || locale == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A String loc = super.toBundleName("", locale);
0N/A if (loc.length() > 0) {
0N/A return baseName.replaceFirst("^([\\w\\.]+)\\.(\\w+)$",
0N/A "$1." + loc.substring(1) + ".$2");
0N/A }
0N/A return baseName;
0N/A }
0N/A
0N/A // Avoid fallback to the default locale (6272060)
0N/A @Override
0N/A public Locale getFallbackLocale(String baseName, Locale locale) {
0N/A if (baseName == null || locale == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A // Creates:
0N/A // test/package/Messages.properties
0N/A // test/package/de/Messages.properties
0N/A // test/package/ja_JP/Messages.properties
0N/A private static void preparePerLocalePackageProperties() {
0N/A final String DEL = File.separator;
0N/A try {
0N/A String dir = System.getProperty("test.classes", ".");
0N/A String[] subdirs = { "", "de", "ja_JP" };
0N/A for (String subdir : subdirs) {
0N/A StringBuilder sb = new StringBuilder();
0N/A sb.append(dir).append(DEL).append("test").append(DEL).append("package");
0N/A if (subdir.length() > 0) {
0N/A sb.append(DEL).append(subdir);
0N/A }
0N/A File path = new File(sb.toString());
0N/A path.mkdirs();
0N/A File propsfile = new File(path, "Messages.properties");
0N/A OutputStream os = new FileOutputStream(propsfile);
0N/A Properties props = new Properties();
0N/A props.setProperty("data", subdir);
0N/A props.store(os, null);
0N/A System.out.println("Created: " + propsfile);
0N/A os.close();
0N/A }
0N/A } catch (Exception e) {
0N/A throw new RuntimeException("Can't set up per-locale properties", e);
0N/A }
0N/A }
0N/A
0N/A private static void error(String msg) {
0N/A System.out.println(msg);
0N/A errors++;
0N/A }
0N/A
0N/A private static void error(String fmt, Object... args) {
0N/A System.out.printf(fmt, args);
0N/A errors++;
0N/A }
0N/A}