Path: blob/master/test/jdk/java/util/ResourceBundle/Control/XMLResourceBundleTest.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 5102289 440372125* @summary Test XML support as shown in the ResourceBundle.Control description.26*/2728import java.io.*;29import java.net.*;30import java.util.*;31import static java.util.ResourceBundle.Control.*;3233public class XMLResourceBundleTest {34public static void main(String[] args) {35ResourceBundle.Control xmlControl = new ResourceBundle.Control() {36@Override37public List<String> getFormats(String baseName) {38if (baseName == null) {39throw new NullPointerException();40}41return Arrays.asList("xml");42}43@Override44public ResourceBundle newBundle(String baseName, Locale locale,45String format,46ClassLoader loader,47boolean reload)48throws IllegalAccessException,49InstantiationException, IOException {50if (baseName == null || locale == null51|| format == null || loader == null) {52throw new NullPointerException();53}54ResourceBundle bundle = null;55if (format.equals("xml")) {56String bundleName = toBundleName(baseName, locale);57String resourceName = toResourceName(bundleName, format);58URL url = loader.getResource(resourceName);59if (url != null) {60URLConnection connection = url.openConnection();61if (connection != null) {62if (reload) {63// disable caches if reloading64connection.setUseCaches(false);65}66InputStream stream = connection.getInputStream();67if (stream != null) {68BufferedInputStream bis = new BufferedInputStream(stream);69bundle = new XMLResourceBundle(bis);70bis.close();71}72}73}74}75return bundle;76}77};78ResourceBundle rb = ResourceBundle.getBundle("XmlRB", new Locale(""), xmlControl);79String type = rb.getString("type");80if (!type.equals("XML")) {81throw new RuntimeException("Root Locale: type: got " + type82+ ", expected XML (ASCII)");83}8485rb = ResourceBundle.getBundle("XmlRB", Locale.JAPAN, xmlControl);86type = rb.getString("type");87// Expect fullwidth "XML"88if (!type.equals("\uff38\uff2d\uff2c")) {89throw new RuntimeException("Locale.JAPAN: type: got " + type90+ ", expected \uff38\uff2d\uff2c (fullwidth XML)");91}92}9394private static class XMLResourceBundle extends ResourceBundle {95private Properties props;9697XMLResourceBundle(InputStream stream) throws IOException {98props = new Properties();99props.loadFromXML(stream);100}101102protected Object handleGetObject(String key) {103if (key == null) {104throw new NullPointerException();105}106return props.get(key);107}108109public Enumeration<String> getKeys() {110// Not implemented111return null;112}113}114}115116117