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