8155649: IncludeLocalesPlugin throws NPE when jdk.localedata not resolved
authornaoto
Wed, 04 May 2016 08:53:09 -0700
changeset 37790 71c730d5e73a
parent 37789 d18410086785
child 37791 ae33107fd8b3
8155649: IncludeLocalesPlugin throws NPE when jdk.localedata not resolved Reviewed-by: alanb, mchung
jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/IncludeLocalesPlugin.java
jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/plugins.properties
jdk/test/tools/jlink/plugins/IncludeLocalesPluginTest.java
--- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/IncludeLocalesPlugin.java	Wed May 04 08:04:22 2016 -0700
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/IncludeLocalesPlugin.java	Wed May 04 08:53:09 2016 -0700
@@ -177,24 +177,28 @@
         Pool.Module module = resources.getModule(MODULENAME);
 
         // jdk.localedata module validation
-        Set<String> packages = module.getAllPackages();
-        if (!packages.containsAll(LOCALEDATA_PACKAGES)) {
-            throw new PluginException(PluginsResourceBundle.getMessage(NAME + ".missingpackages") +
-                LOCALEDATA_PACKAGES.stream()
-                    .filter(pn -> !packages.contains(pn))
-                    .collect(Collectors.joining(",\n\t")));
-        }
+        if (module != null) {
+            Set<String> packages = module.getAllPackages();
+            if (!packages.containsAll(LOCALEDATA_PACKAGES)) {
+                throw new PluginException(PluginsResourceBundle.getMessage(NAME + ".missingpackages") +
+                    LOCALEDATA_PACKAGES.stream()
+                        .filter(pn -> !packages.contains(pn))
+                        .collect(Collectors.joining(",\n\t")));
+            }
 
-        available = Stream.concat(module.getContent().stream()
-                                    .map(md -> p.matcher(md.getPath()))
-                                    .filter(m -> m.matches())
-                                    .map(m -> m.group("tag").replaceAll("_", "-")),
-                                Stream.concat(Stream.of(jaJPJPTag), Stream.of(thTHTHTag)))
-            .distinct()
-            .sorted()
-            .map(IncludeLocalesPlugin::tagToLocale)
-            .collect(Collectors.toList());
-
+            available = Stream.concat(module.getContent().stream()
+                                        .map(md -> p.matcher(md.getPath()))
+                                        .filter(m -> m.matches())
+                                        .map(m -> m.group("tag").replaceAll("_", "-")),
+                                    Stream.concat(Stream.of(jaJPJPTag), Stream.of(thTHTHTag)))
+                .distinct()
+                .sorted()
+                .map(IncludeLocalesPlugin::tagToLocale)
+                .collect(Collectors.toList());
+        } else {
+            // jdk.localedata is not added.
+            throw new PluginException(PluginsResourceBundle.getMessage(NAME + ".localedatanotfound"));
+        }
         filtered = filterLocales(available);
 
         if (filtered.isEmpty()) {
--- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/plugins.properties	Wed May 04 08:04:22 2016 -0700
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/plugins.properties	Wed May 04 08:53:09 2016 -0700
@@ -89,6 +89,9 @@
 include-locales.invalidtag=\
 Invalid language tag: %s
 
+include-locales.localedatanotfound=\
+jdk.localedata module was not specified with --addmods option
+
 main.status.ok=Functional.
 
 main.status.not.ok= Not functional.
--- a/jdk/test/tools/jlink/plugins/IncludeLocalesPluginTest.java	Wed May 04 08:04:22 2016 -0700
+++ b/jdk/test/tools/jlink/plugins/IncludeLocalesPluginTest.java	Wed May 04 08:53:09 2016 -0700
@@ -36,6 +36,7 @@
 
 /*
  * @test
+ * @bug 8152143 8152704 8155649
  * @summary IncludeLocalesPlugin tests
  * @author Naoto Sato
  * @library ../../lib
@@ -55,15 +56,17 @@
     private final static String moduleName = "IncludeLocalesTest";
     private static Helper helper;
     private final static int INCLUDE_LOCALES_OPTION = 0;
-    private final static int EXPECTED_LOCATIONS     = 1;
-    private final static int UNEXPECTED_PATHS       = 2;
-    private final static int AVAILABLE_LOCALES      = 3;
-    private final static int ERROR_MESSAGE          = 4;
+    private final static int ADDMODS_OPTION         = 1;
+    private final static int EXPECTED_LOCATIONS     = 2;
+    private final static int UNEXPECTED_PATHS       = 3;
+    private final static int AVAILABLE_LOCALES      = 4;
+    private final static int ERROR_MESSAGE          = 5;
 
     private final static Object[][] testData = {
         // without --include-locales option: should include all locales
         {
             "",
+            "jdk.localedata",
             List.of(
                 "/jdk.localedata/sun/text/resources/ext/FormatData_en_GB.class",
                 "/jdk.localedata/sun/text/resources/ext/FormatData_ja.class",
@@ -144,6 +147,7 @@
         // All English/Japanese locales
         {
             "--include-locales=en,ja",
+            "jdk.localedata",
             List.of(
                 "/jdk.localedata/sun/text/resources/ext/FormatData_en_GB.class",
                 "/jdk.localedata/sun/text/resources/ext/FormatData_ja.class",
@@ -174,6 +178,7 @@
         // All locales in India
         {
             "--include-locales=*-IN",
+            "jdk.localedata",
             List.of(
                 "/jdk.localedata/sun/text/resources/ext/FormatData_en_IN.class",
                 "/jdk.localedata/sun/text/resources/ext/FormatData_hi_IN.class",
@@ -201,7 +206,9 @@
         },
 
         // Thai
-        {"--include-locales=th",
+        {
+            "--include-locales=th",
+            "jdk.localedata",
             List.of(
                 "/jdk.localedata/sun/text/resources/LineBreakIteratorData_th",
                 "/jdk.localedata/sun/text/resources/thai_dict",
@@ -221,7 +228,9 @@
         },
 
         // Hong Kong
-        {"--include-locales=zh-HK",
+        {
+            "--include-locales=zh-HK",
+            "jdk.localedata",
             List.of(
                 "/jdk.localedata/sun/text/resources/ext/FormatData_zh.class",
                 "/jdk.localedata/sun/text/resources/ext/FormatData_zh_HK.class",
@@ -244,7 +253,9 @@
         },
 
         // Norwegian
-        {"--include-locales=nb,nn,no",
+        {
+            "--include-locales=nb,nn,no",
+            "jdk.localedata",
             List.of(
                 "/jdk.localedata/sun/text/resources/ext/FormatData_no.class",
                 "/jdk.localedata/sun/text/resources/ext/FormatData_no_NO.class",
@@ -268,7 +279,9 @@
         },
 
         // Hebrew/Indonesian/Yiddish
-        {"--include-locales=he,id,yi",
+        {
+            "--include-locales=he,id,yi",
+            "jdk.localedata",
             List.of(
                 "/jdk.localedata/sun/text/resources/ext/FormatData_in.class",
                 "/jdk.localedata/sun/text/resources/ext/FormatData_in_ID.class",
@@ -294,7 +307,9 @@
         },
 
         // Error case: No matching locales
-        {"--include-locales=xyz",
+        {
+            "--include-locales=xyz",
+            "jdk.localedata",
             null,
             null,
             null,
@@ -304,7 +319,9 @@
         },
 
         // Error case: Invalid argument
-        {"--include-locales=en,zh_HK",
+        {
+            "--include-locales=en,zh_HK",
+            "jdk.localedata",
             null,
             null,
             null,
@@ -312,6 +329,18 @@
                 PluginsResourceBundle.getMessage("include-locales.invalidtag"), "zh_HK"))
                 .getMessage(),
         },
+
+        // Error case: jdk.localedata is not added
+        {
+            "--include-locales=en-US",
+            "java.base",
+            null,
+            null,
+            null,
+            new PluginException(
+                PluginsResourceBundle.getMessage("include-locales.localedatanotfound"))
+                .getMessage(),
+        },
     };
 
     public static void main(String[] args) throws Exception {
@@ -328,7 +357,7 @@
             Result result = JImageGenerator.getJLinkTask()
                     .modulePath(helper.defaultModulePath())
                     .output(helper.createNewImageDir(moduleName))
-                    .addMods("jdk.localedata")
+                    .addMods((String)data[ADDMODS_OPTION])
                     .option((String)data[INCLUDE_LOCALES_OPTION])
                     .call();