Path: blob/master/test/jdk/java/util/ResourceBundle/Control/LoadingStrategiesTest.java
41155 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @bug 4303146 5102289 627206025* @summary Test non-standard loading strategies with ResourceBundle.Control subclasses26*/2728import java.io.*;29import java.util.*;3031public class LoadingStrategiesTest {3233static int errors;3435public static void main(String[] args) {36ResourceBundle rb;37String s;3839// Test zh_TW -> root, zh_CN -> zh -> root40rb = ResourceBundle.getBundle("Chinese", Locale.TAIWAN, new ChineseControl());41s = rb.getString("data");42check("chinese with Locale.TAIWAN", s, "root");4344rb = ResourceBundle.getBundle("Chinese", Locale.CHINA, new ChineseControl());45s = rb.getString("data");46check("chinese with Locale.CHINA", s, "zh");474849// Test use of per-locale packaging50preparePerLocalePackageProperties();5152rb = ResourceBundle.getBundle("test.package.Messages", Locale.US,53new PerLocalePackageControl());54s = rb.getString("data");55check("Per-locale package with Locale.US", s, "");5657rb = ResourceBundle.getBundle("test.package.Messages", Locale.GERMAN,58new PerLocalePackageControl());59s = rb.getString("data");60check("Per-locale package with Locale.GERMAN", s, "de");6162rb = ResourceBundle.getBundle("test.package.Messages", Locale.JAPAN,63new PerLocalePackageControl());64s = rb.getString("data");65check("Per-locale package with Locale.JAPAN", s, "ja_JP");666768// Check any errors69if (errors > 0) {70throw new RuntimeException("FAILED: " + errors + " error(s)");71}72}7374private static void check(String msg, String got, String expected) {75if (!got.equals(expected)) {76error("%s: got \"%s\", expected \"%s\"%n", msg, got, expected);77}78}7980private static class ChineseControl extends ResourceBundle.Control {81@Override82public List<Locale> getCandidateLocales(String baseName,83Locale locale) {84if (locale.equals(Locale.TAIWAN)) {85return Arrays.asList(locale,86// no Locale("zh")87new Locale(""));88}89return super.getCandidateLocales(baseName, locale);90}91}9293private static class PerLocalePackageControl extends ResourceBundle.Control {94@Override95public String toBundleName(String baseName, Locale locale) {96if (baseName == null || locale == null) {97throw new NullPointerException();98}99String loc = super.toBundleName("", locale);100if (loc.length() > 0) {101return baseName.replaceFirst("^([\\w\\.]+)\\.(\\w+)$",102"$1." + loc.substring(1) + ".$2");103}104return baseName;105}106107// Avoid fallback to the default locale (6272060)108@Override109public Locale getFallbackLocale(String baseName, Locale locale) {110if (baseName == null || locale == null) {111throw new NullPointerException();112}113return null;114}115}116117// Creates:118// test/package/Messages.properties119// test/package/de/Messages.properties120// test/package/ja_JP/Messages.properties121private static void preparePerLocalePackageProperties() {122final String DEL = File.separator;123try {124String dir = System.getProperty("test.classes", ".");125String[] subdirs = { "", "de", "ja_JP" };126for (String subdir : subdirs) {127StringBuilder sb = new StringBuilder();128sb.append(dir).append(DEL).append("test").append(DEL).append("package");129if (subdir.length() > 0) {130sb.append(DEL).append(subdir);131}132File path = new File(sb.toString());133path.mkdirs();134File propsfile = new File(path, "Messages.properties");135OutputStream os = new FileOutputStream(propsfile);136Properties props = new Properties();137props.setProperty("data", subdir);138props.store(os, null);139System.out.println("Created: " + propsfile);140os.close();141}142} catch (Exception e) {143throw new RuntimeException("Can't set up per-locale properties", e);144}145}146147private static void error(String msg) {148System.out.println(msg);149errors++;150}151152private static void error(String fmt, Object... args) {153System.out.printf(fmt, args);154errors++;155}156}157158159