8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
authormchung
Fri, 03 Jun 2016 12:26:45 -0700
changeset 38782 ff27bc5c278e
parent 38781 f4e9fc088cdd
child 38783 0a5acb647700
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test Reviewed-by: alanb, okutsu
jdk/src/java.base/share/classes/java/util/spi/AbstractResourceBundleProvider.java
jdk/src/java.base/share/classes/java/util/spi/ResourceBundleProvider.java
jdk/test/java/util/ResourceBundle/modules/appbasic/appbasic.sh
jdk/test/java/util/ResourceBundle/modules/appbasic/src/asiabundles/jdk/test/resources/asia/MyResourcesAsia.java
jdk/test/java/util/ResourceBundle/modules/appbasic/src/eubundles/jdk/test/resources/eu/MyResourcesEU.java
jdk/test/java/util/ResourceBundle/modules/appbasic/src/test/jdk/test/resources/MyControl.java
jdk/test/java/util/ResourceBundle/modules/appbasic/src/test/jdk/test/resources/MyResourcesProviderImpl.java
--- a/jdk/src/java.base/share/classes/java/util/spi/AbstractResourceBundleProvider.java	Fri Jun 03 16:28:53 2016 +0100
+++ b/jdk/src/java.base/share/classes/java/util/spi/AbstractResourceBundleProvider.java	Fri Jun 03 12:26:45 2016 -0700
@@ -40,6 +40,36 @@
  * {@code AbstractResourceBundleProvider} is an abstract class for helping
  * implement the {@link ResourceBundleProvider} interface.
  *
+ * <p>
+ * Resource bundles can be packaged in a named module separated from
+ * the <em>caller module</em> loading the resource bundle, i.e. the module
+ * calling {@link ResourceBundle#getBundle(String)}.  For the caller module
+ * to load a resource bundle "{@code com.example.app.MyResources}"
+ * from another module and a service interface named
+ * "{@code com.example.app.MyResourcesProvider}",
+ * the <em>bundle provider module</em> can provide the implementation class
+ * as follows:
+ *
+ * <pre><code>
+ * import com.example.app.MyResourcesProvider;
+ * class MyResourcesProviderImpl extends AbstractResourceBundleProvider
+ *     implements MyResourcesProvider
+ * {</code>
+ *     {@code @Override
+ *     public ResourceBundle getBundle(String baseName, Locale locale) {
+ *         // this module only provides bundles in french
+ *         if (locale.equals(Locale.FRENCH)) {
+ *              return super.getBundle(baseName, locale);
+ *         }
+ *         return null;
+ *     }
+ * }}</pre>
+ *
+ * @see <a href="../ResourceBundle.html#bundleprovider">
+ *     Resource Bundles in Named Modules</a>
+ * @see <a href="../ResourceBundle.html#RBP_support">
+ *     ResourceBundleProvider Service Providers</a>
+ *
  * @since 9
  */
 public abstract class AbstractResourceBundleProvider implements ResourceBundleProvider {
@@ -125,6 +155,7 @@
         Module module = this.getClass().getModule();
         String bundleName = toBundleName(baseName, locale);
         ResourceBundle bundle = null;
+
         for (String format : formats) {
             try {
                 if (FORMAT_CLASS.equals(format)) {
--- a/jdk/src/java.base/share/classes/java/util/spi/ResourceBundleProvider.java	Fri Jun 03 16:28:53 2016 +0100
+++ b/jdk/src/java.base/share/classes/java/util/spi/ResourceBundleProvider.java	Fri Jun 03 12:26:45 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2016, 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
@@ -30,13 +30,21 @@
 
 /**
  * {@code ResourceBundleProvider} is a provider interface that is used for
- * loading resource bundles. Implementation classes of this interface are loaded
- * with {@link java.util.ServiceLoader ServiceLoader} during a call to the
+ * loading resource bundles for named modules. Implementation classes of
+ * this interface are loaded with {@link java.util.ServiceLoader ServiceLoader}
+ * during a call to the
  * {@link ResourceBundle#getBundle(String, Locale, ClassLoader)
  * ResourceBundle.getBundle} method. The provider service type is determined by
- * {@code basename+"Provider"}. For example, if the base name is
- * "com.example.app.MyResources", {@code com.example.app.MyResourcesProvider}
- * will be the provider service type.
+ * {@code basename+"Provider"}.
+ *
+ * <p>
+ * For example, if the base name is "com.example.app.MyResources",
+ * {@code com.example.app.MyResourcesProvider} will be the provider service type:
+ * <pre>{@code
+ * public interface MyResourcesProvider extends ResourceBundleProvider {
+ * }
+ * }</pre>
+ *
  * <p>
  * This providers's {@link #getBundle(String, Locale) getBundle} method is called
  * through the resource bundle loading process instead of {@link
@@ -44,13 +52,17 @@
  * ResourceBundle.Control.newBundle()}. Refer to {@link ResourceBundle} for
  * details.
  *
+ * @see <a href="../ResourceBundle.html#bundleprovider">
+ *     Resource Bundles in Named Modules</a>
+ * @see <a href="../ResourceBundle.html#RBP_support">
+ *     ResourceBundleProvider Service Providers</a>
  * @since 9
  */
 public interface ResourceBundleProvider {
     /**
      * Returns a {@code ResourceBundle} for the given bundle name and locale.
-     * This method returns null if there is no {@code ResourceBundle} found
-     * for the given parameters.
+     * This method returns {@code null} if there is no {@code ResourceBundle}
+     * found for the given parameters.
      *
      *
      * @param baseName
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/ResourceBundle/modules/appbasic/appbasic.sh	Fri Jun 03 12:26:45 2016 -0700
@@ -0,0 +1,68 @@
+#
+# Copyright (c) 2015, 2016, 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 8044767
+# @summary Basic test for ResourceBundle with modules; named module "test"
+#          contains resource bundles for root and en, and separate named modules
+#          "eubundles" and "asiabundles" contain other resource bundles.
+
+set -e
+
+if [ -z "$TESTJAVA" ]; then
+  if [ $# -lt 1 ]; then exit 1; fi
+  TESTJAVA="$1"; shift
+  COMPILEJAVA="${TESTJAVA}"
+  TESTSRC="`pwd`"
+  TESTCLASSES="`pwd`"
+fi
+
+JAVAC="$COMPILEJAVA/bin/javac"
+JAVA="$TESTJAVA/bin/java"
+
+
+for I in eu asia
+do
+  B=${I}bundles
+  mkdir -p mods/$B
+  CLASSES="`find $TESTSRC/src/$B -name '*.java'`"
+  if [ "x$CLASSES" != x ]; then
+    $JAVAC -g -d mods -modulesourcepath $TESTSRC/src -cp mods/test $CLASSES
+  fi
+  PROPS="`(cd $TESTSRC/src/$B; find . -name '*.properties')`"
+  if [ "x$PROPS" != x ]; then
+      for P in $PROPS
+      do
+        D=`dirname $P`
+        mkdir -p mods/$B/$D
+        cp $TESTSRC/src/$B/$P mods/$B/$D/
+      done
+  fi
+done
+
+mkdir -p mods/test
+$JAVAC -g -d mods -modulesourcepath $TESTSRC/src `find $TESTSRC/src/test -name "*.java"`
+
+$JAVA -mp mods -m test/jdk.test.Main de fr ja zh-tw en de
+
+exit $?
--- a/jdk/test/java/util/ResourceBundle/modules/appbasic/src/asiabundles/jdk/test/resources/asia/MyResourcesAsia.java	Fri Jun 03 16:28:53 2016 +0100
+++ b/jdk/test/java/util/ResourceBundle/modules/appbasic/src/asiabundles/jdk/test/resources/asia/MyResourcesAsia.java	Fri Jun 03 12:26:45 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2016, 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
@@ -23,23 +23,33 @@
 
 package jdk.test.resources.asia;
 
-import java.io.IOException;
 import java.util.Locale;
 import java.util.ResourceBundle;
-import java.util.ResourceBundle.Control;
-import jdk.test.resources.MyControl;
+import java.util.Set;
+import java.util.spi.AbstractResourceBundleProvider;
+
 import jdk.test.resources.MyResourcesProvider;
 
-public class MyResourcesAsia extends MyControl implements MyResourcesProvider {
+public class MyResourcesAsia extends AbstractResourceBundleProvider
+    implements MyResourcesProvider
+{
+    private static Set<Locale> asiaLocales
+        = Set.of(Locale.JAPANESE, Locale.CHINESE, Locale.TAIWAN);
+
+    @Override
+    public String toBundleName(String baseName, Locale locale) {
+        String bundleName = super.toBundleName(baseName, locale);
+        if (asiaLocales.contains(locale)) {
+            int index = bundleName.lastIndexOf('.');
+            return bundleName.substring(0, index + 1) + "asia" + bundleName.substring(index);
+        }
+        return bundleName;
+    }
+
     @Override
     public ResourceBundle getBundle(String baseName, Locale locale) {
-        if (isAsiaLocale(locale)) {
-            try {
-                ClassLoader loader = MyResourcesAsia.class.getClassLoader();
-                return newBundle(baseName, locale, "java.properties", loader, false);
-            } catch (IllegalAccessException | InstantiationException | IOException e) {
-                System.out.println(e);
-            }
+        if (asiaLocales.contains(locale)) {
+            return super.getBundle(baseName, locale);
         }
         return null;
     }
--- a/jdk/test/java/util/ResourceBundle/modules/appbasic/src/eubundles/jdk/test/resources/eu/MyResourcesEU.java	Fri Jun 03 16:28:53 2016 +0100
+++ b/jdk/test/java/util/ResourceBundle/modules/appbasic/src/eubundles/jdk/test/resources/eu/MyResourcesEU.java	Fri Jun 03 12:26:45 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2016, 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
@@ -23,23 +23,36 @@
 
 package jdk.test.resources.eu;
 
-import java.io.IOException;
 import java.util.Locale;
 import java.util.ResourceBundle;
-import java.util.ResourceBundle.Control;
-import jdk.test.resources.MyControl;
+import java.util.Set;
+import java.util.spi.AbstractResourceBundleProvider;
+
 import jdk.test.resources.MyResourcesProvider;
 
-public class MyResourcesEU extends MyControl implements MyResourcesProvider {
+public class MyResourcesEU extends AbstractResourceBundleProvider
+    implements MyResourcesProvider
+{
+    private static final Set<Locale> euLocales = Set.of(Locale.GERMAN, Locale.FRENCH);
+
+    public MyResourcesEU() {
+        super("java.class");
+    }
+
+    @Override
+    public String toBundleName(String baseName, Locale locale) {
+        String bundleName = super.toBundleName(baseName, locale);
+        if (euLocales.contains(locale)) {
+            int index = bundleName.lastIndexOf('.');
+            return bundleName.substring(0, index + 1) + "eu" + bundleName.substring(index);
+        }
+        return bundleName;
+    }
+
     @Override
     public ResourceBundle getBundle(String baseName, Locale locale) {
-        if (isEULocale(locale)) {
-            try {
-                ClassLoader loader = MyResourcesEU.class.getClassLoader();
-                return newBundle(baseName, locale, "java.class", loader, false);
-            } catch (IllegalAccessException | InstantiationException | IOException e) {
-                System.out.println(e);
-            }
+        if (euLocales.contains(locale)) {
+            return super.getBundle(baseName, locale);
         }
         return null;
     }
--- a/jdk/test/java/util/ResourceBundle/modules/appbasic/src/test/jdk/test/resources/MyControl.java	Fri Jun 03 16:28:53 2016 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,59 +0,0 @@
-/*
- * Copyright (c) 2015, 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.
- */
-
-package jdk.test.resources;
-
-import java.util.*;
-
-public class MyControl extends ResourceBundle.Control {
-    private static final Set<Locale> euLocales, asiaLocales;
-
-    static {
-        euLocales = new HashSet<>(Arrays.asList(Locale.GERMAN, Locale.FRENCH));
-        asiaLocales = new HashSet<>(Arrays.asList(Locale.JAPANESE, Locale.CHINESE, Locale.TAIWAN));
-    }
-
-    @Override
-    public String toBundleName(String baseName, Locale locale) {
-        String bundleName = baseName;
-        if (euLocales.contains(locale)) {
-            bundleName = addRegion(baseName, "eu");
-        } else if (asiaLocales.contains(locale)) {
-            bundleName = addRegion(baseName, "asia");
-        }
-        return super.toBundleName(bundleName, locale);
-    }
-
-    private String addRegion(String baseName, String region) {
-        int index = baseName.lastIndexOf('.');
-        return baseName.substring(0, index + 1) + region + baseName.substring(index);
-    }
-
-    protected static boolean isEULocale(Locale locale) {
-        return euLocales.contains(locale);
-    }
-
-    protected static boolean isAsiaLocale(Locale locale) {
-        return asiaLocales.contains(locale);
-    }
-}
--- a/jdk/test/java/util/ResourceBundle/modules/appbasic/src/test/jdk/test/resources/MyResourcesProviderImpl.java	Fri Jun 03 16:28:53 2016 +0100
+++ b/jdk/test/java/util/ResourceBundle/modules/appbasic/src/test/jdk/test/resources/MyResourcesProviderImpl.java	Fri Jun 03 12:26:45 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2016, 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
@@ -23,19 +23,20 @@
 
 package jdk.test.resources;
 
-import java.io.IOException;
 import java.util.Locale;
 import java.util.ResourceBundle;
+import java.util.spi.AbstractResourceBundleProvider;
 
-public class MyResourcesProviderImpl extends MyControl implements MyResourcesProvider {
+public class MyResourcesProviderImpl extends AbstractResourceBundleProvider
+    implements MyResourcesProvider
+{
+    public MyResourcesProviderImpl() {
+        super("java.class");
+    }
     @Override
     public ResourceBundle getBundle(String baseName, Locale locale) {
         if (locale.equals(Locale.ENGLISH) || locale.equals(Locale.ROOT)) {
-            try {
-                ClassLoader loader = MyResourcesProviderImpl.class.getClassLoader();
-                return newBundle(baseName, locale, "java.class", loader, false);
-            } catch (IllegalAccessException | InstantiationException | IOException e) {
-            }
+            return super.getBundle(baseName, locale);
         }
         return null;
     }