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 4095319 4286358
0N/A * @summary Test cases for the containsKey, keySet, and handleKeySet
0N/A * methods that are new in Mustang.
0N/A * @build KeySetMessages KeySetMessages_zh_CN
0N/A * @run main KeySetTest
0N/A */
0N/A
0N/Aimport java.lang.reflect.*;
0N/Aimport java.util.*;
0N/A
0N/Apublic class KeySetTest {
0N/A static final List<String> fullKeys = Arrays.asList("food", "drink", "tea");
0N/A static final List<String> localKeys = Arrays.asList("food", "tea");
0N/A
0N/A public static void main(String[] args) {
0N/A // Test PropertyResourceBundle
0N/A testKeys("KeySetResources", Locale.JAPAN);
0N/A
0N/A // Test ListResourceBundle
0N/A testKeys("KeySetMessages", Locale.CHINA);
0N/A }
0N/A
0N/A static void testKeys(String bundleName, Locale locale) {
0N/A ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale);
0N/A System.out.println("food = " + rb.getString("food"));
0N/A
0N/A // Test keySet()
0N/A Set<String> allKeys = rb.keySet();
0N/A if (!(allKeys.containsAll(fullKeys) && fullKeys.containsAll(allKeys))) {
0N/A throw new RuntimeException("got "+allKeys + ", expected " + fullKeys);
0N/A }
0N/A
0N/A // Test containsKey()
0N/A for (String key : fullKeys) {
0N/A if (!rb.containsKey(key)) {
0N/A throw new RuntimeException("rb doesn't contain: " + key);
0N/A }
0N/A }
0N/A for (String key : new String[] { "snack", "beer" }) {
0N/A if (rb.containsKey(key)) {
0N/A throw new RuntimeException("rb contains: " + key);
0N/A }
0N/A }
0N/A
0N/A // Make sure that the default handleKeySet implementation
0N/A // returns the subset keys of the given locale.
0N/A TestBundle tb = new TestBundle(bundleName, locale);
0N/A Set<String> childKeys = tb.handleKeySet();
0N/A if (!(childKeys.containsAll(localKeys) || localKeys.containsAll(childKeys))) {
0N/A throw new RuntimeException("get " + childKeys + ", expected " + localKeys);
0N/A }
0N/A }
0N/A
0N/A static class TestBundle extends ResourceBundle {
0N/A ResourceBundle bundle;
0N/A Method m;
0N/A
0N/A public TestBundle() {}
0N/A
0N/A public TestBundle(String name, Locale locale) {
0N/A bundle = ResourceBundle.getBundle(name, locale);
0N/A
0N/A // Prepare for the handleGetObject call
0N/A try {
0N/A Class clazz = bundle.getClass();
0N/A m = clazz.getMethod("handleGetObject", String.class);
0N/A m.setAccessible(true);
0N/A } catch (Exception e) {
0N/A throw new RuntimeException("method preparation error", e);
0N/A }
0N/A }
0N/A
0N/A public Enumeration<String> getKeys() {
0N/A return bundle.getKeys();
0N/A }
0N/A
0N/A // handleGetObject() doesn't look up its parent bundles.
0N/A protected Object handleGetObject(String key) {
0N/A try {
0N/A return m.invoke(bundle, key);
0N/A } catch (Exception e) {
0N/A throw new RuntimeException("handleGetObject error", e);
0N/A }
0N/A }
0N/A
0N/A public Set<String> handleKeySet() {
0N/A return super.handleKeySet();
0N/A }
0N/A }
0N/A}