8213516: jck test api/javax_accessibility/AccessibleState/fields.html fails intermittent
authorserb
Mon, 20 May 2019 12:17:34 -0700
changeset 55175 e2a90479dfe5
parent 55174 c53905e7dc57
child 55176 3e0a90050182
8213516: jck test api/javax_accessibility/AccessibleState/fields.html fails intermittent Reviewed-by: prr
src/java.desktop/share/classes/javax/accessibility/AccessibleBundle.java
test/jdk/javax/accessibility/8197785/AccessibilityBundleMemoryLeakTest.java
test/jdk/javax/accessibility/AccessibleBundle/Basic.java
--- a/src/java.desktop/share/classes/javax/accessibility/AccessibleBundle.java	Mon May 20 15:42:20 2019 +0100
+++ b/src/java.desktop/share/classes/javax/accessibility/AccessibleBundle.java	Mon May 20 12:17:34 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,8 +25,6 @@
 
 package javax.accessibility;
 
-import java.util.Enumeration;
-import java.util.Hashtable;
 import java.util.Locale;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
@@ -50,8 +48,6 @@
  */
 public abstract class AccessibleBundle {
 
-    private static Hashtable<Locale, Hashtable<String, Object>> table = new Hashtable<>();
-
     private final String defaultResourceBundleName
         = "com.sun.accessibility.internal.resources.accessibility";
 
@@ -87,26 +83,16 @@
      * they can specify their own resource bundles which contain localized
      * strings for their keys.
      *
-     * @param  resourceBundleName the name of the resource bundle to use for
-     *         lookup
+     * @param  name the name of the resource bundle to use for lookup
      * @param  locale the locale for which to obtain a localized string
      * @return a localized string for the key
      */
-    protected String toDisplayString(String resourceBundleName,
-                                     Locale locale) {
-
-        // loads the resource bundle if necessary
-        loadResourceBundle(resourceBundleName, locale);
-
-        // returns the localized string
-        Hashtable<String, Object> ht = table.get(locale);
-        if (ht != null) {
-            Object o = ht.get(key);
-            if (o != null && o instanceof String) {
-                return (String)o;
-            }
+    protected String toDisplayString(final String name, final Locale locale) {
+        try {
+            return ResourceBundle.getBundle(name, locale).getString(key);
+        } catch (ClassCastException | MissingResourceException ignored) {
+            return key; // return the non-localized key
         }
-        return key;
     }
 
     /**
@@ -139,33 +125,4 @@
     public String toString() {
         return toDisplayString();
     }
-
-    /**
-     * Loads the Accessibility resource bundle if necessary.
-     */
-    private void loadResourceBundle(String resourceBundleName,
-                                    Locale locale) {
-        if (! table.containsKey(locale)) {
-
-            try {
-                Hashtable<String, Object> resourceTable = new Hashtable<>();
-
-                ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName, locale);
-
-                Enumeration<String> iter = bundle.getKeys();
-                while(iter.hasMoreElements()) {
-                    String key = iter.nextElement();
-                    resourceTable.put(key, bundle.getObject(key));
-                }
-
-                table.put(locale, resourceTable);
-            }
-            catch (MissingResourceException e) {
-                System.err.println("loadResourceBundle: " + e);
-                // Just return so toDisplayString() returns the
-                // non-localized key.
-                return;
-            }
-        }
-    }
 }
--- a/test/jdk/javax/accessibility/8197785/AccessibilityBundleMemoryLeakTest.java	Mon May 20 15:42:20 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,64 +0,0 @@
-/*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-/**
-  * @test
-  * @bug 8197785
-  * @summary Tests if AccessibleBundle will reload the ResourceBundle for every
-  * call to toDisplayString.
-  * @run main AccessibilityBundleMemoryLeakTest
-  */
-import java.lang.reflect.Field;
-import java.util.Hashtable;
-import java.util.Locale;
-
-import javax.accessibility.AccessibleRole;
-import javax.accessibility.AccessibleBundle;
-
-public class AccessibilityBundleMemoryLeakTest extends AccessibleRole {
-    public AccessibilityBundleMemoryLeakTest() {
-        super("");
-    }
-
-    public static void main(String... args) throws Exception {
-        AccessibilityBundleMemoryLeakTest role = new AccessibilityBundleMemoryLeakTest();
-
-        Field field = AccessibleBundle.class.getDeclaredField("table");
-        field.setAccessible(true);
-
-        final Hashtable table = (Hashtable)field.get(role);
-        Locale locale = Locale.getDefault();
-
-        role.toDisplayString();
-        Object obj = table.get(locale);
-
-        role.toDisplayString();
-        Object obj1 = table.get(locale);
-
-        if (obj != obj1) {
-            throw new RuntimeException("Test case failed: AccessibleBundle allocates new value for existing key!");
-        }
-
-        System.out.println("Test passed.");
-
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/accessibility/AccessibleBundle/Basic.java	Mon May 20 12:17:34 2019 -0700
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.util.Locale;
+
+import javax.accessibility.AccessibleBundle;
+
+import static javax.accessibility.AccessibleRole.ALERT;
+import static javax.accessibility.AccessibleRole.LABEL;
+import static javax.accessibility.AccessibleRole.PANEL;
+import static javax.accessibility.AccessibleState.MANAGES_DESCENDANTS;
+
+/**
+ * @test
+ * @bug 8213516
+ * @summary Checks basic functionality of AccessibleBundle class
+ */
+public final class Basic extends AccessibleBundle {
+
+    private Basic(final String key) {
+        this.key = key;
+    }
+
+    public static void main(final String[] args) {
+        testStandardResource();
+        testCustomResource();
+    }
+
+    private static void testCustomResource() {
+        final Basic bundle = new Basic("managesDescendants");
+        test(bundle.toDisplayString(Locale.ENGLISH), "manages descendants");
+        test(bundle.toDisplayString("NonExistedBundle", Locale.ENGLISH),
+             "managesDescendants");
+    }
+
+    private static void testStandardResource() {
+        test(ALERT.toDisplayString(Locale.ENGLISH), "alert");
+        test(ALERT.toDisplayString(Locale.JAPAN), "\u30a2\u30e9\u30fc\u30c8");
+        test(LABEL.toDisplayString(Locale.ENGLISH), "label");
+        test(LABEL.toDisplayString(Locale.JAPAN), "\u30e9\u30d9\u30eb");
+        test(PANEL.toDisplayString(Locale.ENGLISH), "panel");
+        test(PANEL.toDisplayString(Locale.JAPAN), "\u30D1\u30CD\u30EB");
+        test(MANAGES_DESCENDANTS.toDisplayString(Locale.ENGLISH),
+             "manages descendants");
+        test(MANAGES_DESCENDANTS.toDisplayString(Locale.JAPAN),
+             "\u5B50\u5B6B\u3092\u7BA1\u7406");
+    }
+
+    private static void test(final String actual, final String expected) {
+        if (!actual.equals(expected)) {
+            System.err.println("Expected: " + expected);
+            System.err.println("Actual: " + actual);
+            throw new RuntimeException("Wrong text");
+        }
+    }
+}