Path: blob/master/test/jdk/java/util/ResourceBundle/KeySetTest.java
41149 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 4095319 428635825* @summary Test cases for the containsKey, keySet, and handleKeySet26* methods that are new in Mustang.27* @build KeySetMessages KeySetMessages_zh_CN28* @run main KeySetTest29*/3031import java.lang.reflect.*;32import java.util.*;3334public class KeySetTest {35static final List<String> fullKeys = Arrays.asList("food", "drink", "tea");36static final List<String> localKeys = Arrays.asList("food", "tea");3738public static void main(String[] args) {39// Test PropertyResourceBundle40testKeys("KeySetResources", Locale.JAPAN);4142// Test ListResourceBundle43testKeys("KeySetMessages", Locale.CHINA);44}4546static void testKeys(String bundleName, Locale locale) {47ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale);48System.out.println("food = " + rb.getString("food"));4950// Test keySet()51Set<String> allKeys = rb.keySet();52if (!(allKeys.containsAll(fullKeys) && fullKeys.containsAll(allKeys))) {53throw new RuntimeException("got "+allKeys + ", expected " + fullKeys);54}5556// Test containsKey()57for (String key : fullKeys) {58if (!rb.containsKey(key)) {59throw new RuntimeException("rb doesn't contain: " + key);60}61}62for (String key : new String[] { "snack", "beer" }) {63if (rb.containsKey(key)) {64throw new RuntimeException("rb contains: " + key);65}66}6768// Make sure that the default handleKeySet implementation69// returns the subset keys of the given locale.70TestBundle tb = new TestBundle(bundleName, locale);71Set<String> childKeys = tb.handleKeySet();72if (!(childKeys.containsAll(localKeys) || localKeys.containsAll(childKeys))) {73throw new RuntimeException("get " + childKeys + ", expected " + localKeys);74}75}7677static class TestBundle extends ResourceBundle {78ResourceBundle bundle;79Method m;8081public TestBundle() {}8283public TestBundle(String name, Locale locale) {84bundle = ResourceBundle.getBundle(name, locale);8586// Prepare for the handleGetObject call87try {88Class clazz = bundle.getClass();89m = clazz.getMethod("handleGetObject", String.class);90m.setAccessible(true);91} catch (Exception e) {92throw new RuntimeException("method preparation error", e);93}94}9596public Enumeration<String> getKeys() {97return bundle.getKeys();98}99100// handleGetObject() doesn't look up its parent bundles.101protected Object handleGetObject(String key) {102try {103return m.invoke(bundle, key);104} catch (Exception e) {105throw new RuntimeException("handleGetObject error", e);106}107}108109public Set<String> handleKeySet() {110return super.handleKeySet();111}112}113}114115116