duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame^] | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | * or visit www.oracle.com if you need additional information or have any |
| 21 | * questions. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 22 | */ |
| 23 | /* |
| 24 | * @test |
| 25 | * @bug 4095319 4286358 |
| 26 | * @summary Test cases for the containsKey, keySet, and handleKeySet |
| 27 | * methods that are new in Mustang. |
| 28 | * @build KeySetMessages KeySetMessages_zh_CN |
| 29 | * @run main KeySetTest |
| 30 | */ |
| 31 | |
| 32 | import java.lang.reflect.*; |
| 33 | import java.util.*; |
| 34 | |
| 35 | public class KeySetTest { |
| 36 | static final List<String> fullKeys = Arrays.asList("food", "drink", "tea"); |
| 37 | static final List<String> localKeys = Arrays.asList("food", "tea"); |
| 38 | |
| 39 | public static void main(String[] args) { |
| 40 | // Test PropertyResourceBundle |
| 41 | testKeys("KeySetResources", Locale.JAPAN); |
| 42 | |
| 43 | // Test ListResourceBundle |
| 44 | testKeys("KeySetMessages", Locale.CHINA); |
| 45 | } |
| 46 | |
| 47 | static void testKeys(String bundleName, Locale locale) { |
| 48 | ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale); |
| 49 | System.out.println("food = " + rb.getString("food")); |
| 50 | |
| 51 | // Test keySet() |
| 52 | Set<String> allKeys = rb.keySet(); |
| 53 | if (!(allKeys.containsAll(fullKeys) && fullKeys.containsAll(allKeys))) { |
| 54 | throw new RuntimeException("got "+allKeys + ", expected " + fullKeys); |
| 55 | } |
| 56 | |
| 57 | // Test containsKey() |
| 58 | for (String key : fullKeys) { |
| 59 | if (!rb.containsKey(key)) { |
| 60 | throw new RuntimeException("rb doesn't contain: " + key); |
| 61 | } |
| 62 | } |
| 63 | for (String key : new String[] { "snack", "beer" }) { |
| 64 | if (rb.containsKey(key)) { |
| 65 | throw new RuntimeException("rb contains: " + key); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Make sure that the default handleKeySet implementation |
| 70 | // returns the subset keys of the given locale. |
| 71 | TestBundle tb = new TestBundle(bundleName, locale); |
| 72 | Set<String> childKeys = tb.handleKeySet(); |
| 73 | if (!(childKeys.containsAll(localKeys) || localKeys.containsAll(childKeys))) { |
| 74 | throw new RuntimeException("get " + childKeys + ", expected " + localKeys); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static class TestBundle extends ResourceBundle { |
| 79 | ResourceBundle bundle; |
| 80 | Method m; |
| 81 | |
| 82 | public TestBundle() {} |
| 83 | |
| 84 | public TestBundle(String name, Locale locale) { |
| 85 | bundle = ResourceBundle.getBundle(name, locale); |
| 86 | |
| 87 | // Prepare for the handleGetObject call |
| 88 | try { |
| 89 | Class clazz = bundle.getClass(); |
| 90 | m = clazz.getMethod("handleGetObject", String.class); |
| 91 | m.setAccessible(true); |
| 92 | } catch (Exception e) { |
| 93 | throw new RuntimeException("method preparation error", e); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public Enumeration<String> getKeys() { |
| 98 | return bundle.getKeys(); |
| 99 | } |
| 100 | |
| 101 | // handleGetObject() doesn't look up its parent bundles. |
| 102 | protected Object handleGetObject(String key) { |
| 103 | try { |
| 104 | return m.invoke(bundle, key); |
| 105 | } catch (Exception e) { |
| 106 | throw new RuntimeException("handleGetObject error", e); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | public Set<String> handleKeySet() { |
| 111 | return super.handleKeySet(); |
| 112 | } |
| 113 | } |
| 114 | } |