8152143: jlink --include-locales should gracefully detect certain user error
authornaoto
Wed, 23 Mar 2016 17:05:04 -0700
changeset 36666 bf6dce37a2f0
parent 36665 8658fc0ab6e8
child 36667 76d6519f8614
8152143: jlink --include-locales should gracefully detect certain user error Reviewed-by: 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 Mar 23 18:24:35 2016 +0100
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/IncludeLocalesPlugin.java	Wed Mar 23 17:05:04 2016 -0700
@@ -155,9 +155,13 @@
 
     @Override
     public void configure(Map<String, String> config) {
-        priorityList = Arrays.stream(config.get(NAME).split(","))
-            .map(Locale.LanguageRange::new)
-            .collect(Collectors.toList());
+        try {
+            priorityList = Arrays.stream(config.get(NAME).split(","))
+                .map(Locale.LanguageRange::new)
+                .collect(Collectors.toList());
+        } catch (IllegalArgumentException iae) {
+            throw new PluginException(iae.getLocalizedMessage());
+        }
     }
 
     @Override
@@ -168,7 +172,7 @@
         // jdk.localedata module validation
         Set<String> packages = module.getAllPackages();
         if (!packages.containsAll(LOCALEDATA_PACKAGES)) {
-            throw new PluginException("Missing locale data packages in jdk.localedata:\n\t" +
+            throw new PluginException(PluginsResourceBundle.getMessage(NAME + ".missingpackages") +
                 LOCALEDATA_PACKAGES.stream()
                     .filter(pn -> !packages.contains(pn))
                     .collect(Collectors.joining(",\n\t")));
@@ -186,6 +190,10 @@
 
         filtered = filterLocales(available);
 
+        if (filtered.isEmpty()) {
+            throw new PluginException(PluginsResourceBundle.getMessage(NAME + ".nomatchinglocales"));
+        }
+
         try {
             String value = META_FILES + filtered.stream()
                 .map(s -> includeLocaleFilePatterns(s))
--- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/plugins.properties	Wed Mar 23 18:24:35 2016 +0100
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/plugins.properties	Wed Mar 23 17:05:04 2016 -0700
@@ -68,9 +68,18 @@
 
 zip.description=ZIP Compression
 
-include-locales.argument=<langtag>[,<langtag>]*
+include-locales.argument=\
+<langtag>[,<langtag>]*
 
-include-locales.description=BCP 47 language tags separated by a comma, allowing locale matching\ndefined in RFC 4647. eg: en,ja,*-IN
+include-locales.description=\
+BCP 47 language tags separated by a comma, allowing locale matching\n\
+defined in RFC 4647. eg: en,ja,*-IN
+
+include-locales.missingpackages=\
+Missing locale data packages in jdk.localedata:\n\t
+
+include-locales.nomatchinglocales=\
+No matching locales found. Check the specified pattern.
 
 main.status.ok=Functional.
 
--- a/jdk/test/tools/jlink/plugins/IncludeLocalesPluginTest.java	Wed Mar 23 18:24:35 2016 +0100
+++ b/jdk/test/tools/jlink/plugins/IncludeLocalesPluginTest.java	Wed Mar 23 17:05:04 2016 -0700
@@ -21,26 +21,18 @@
  * questions.
  */
 
-import java.io.BufferedReader;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.lang.reflect.Layer;
-import java.nio.file.Files;
 import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
-import java.util.stream.Stream;
 
 import jdk.tools.jlink.plugin.Plugin;
+import jdk.tools.jlink.plugin.PluginException;
 import jdk.tools.jlink.internal.PluginRepository;
+import jdk.tools.jlink.internal.TaskHelper;
+import jdk.tools.jlink.internal.plugins.PluginsResourceBundle;
 import tests.Helper;
 import tests.JImageGenerator;
 import tests.JImageValidator;
+import tests.Result;
 
 /*
  * @test
@@ -50,6 +42,7 @@
  * @modules java.base/jdk.internal.jimage
  *          jdk.jdeps/com.sun.tools.classfile
  *          jdk.jlink/jdk.tools.jlink.internal
+ *          jdk.jlink/jdk.tools.jlink.internal.plugins
  *          jdk.jlink/jdk.tools.jmod
  *          jdk.jlink/jdk.tools.jimage
  *          jdk.compiler
@@ -65,6 +58,7 @@
     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 Object[][] testData = {
         // without --include-locales option: should include all locales
@@ -144,6 +138,7 @@
             "yav_CM yo yo_BJ yo_NG zgh zgh_MA zh zh_CN zh_CN_#Hans zh_HK " +
             "zh_HK_#Hans zh_HK_#Hant zh_MO_#Hans zh_MO_#Hant zh_SG zh_SG_#Hans " +
             "zh_TW zh_TW_#Hant zh__#Hans zh__#Hant zu zu_ZA",
+            "",
         },
 
         // All English/Japanese locales
@@ -173,6 +168,7 @@
             "en_PW en_RW en_SB en_SC en_SD en_SG en_SH en_SL en_SS en_SX en_SZ " +
             "en_TC en_TK en_TO en_TT en_TV en_TZ en_UG en_UM en_US en_US_POSIX " +
             "en_VC en_VG en_VI en_VU en_WS en_ZA en_ZM en_ZW ja ja_JP ja_JP_JP_#u-ca-japanese",
+            "",
         },
 
         // All locales in India
@@ -201,6 +197,7 @@
                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_zh.class"),
             " as_IN bn_IN bo_IN brx_IN en en_IN en_US en_US_POSIX gu_IN hi_IN kn_IN " +
             "kok_IN ks_IN_#Arab ml_IN mr_IN ne_IN or_IN pa_IN_#Guru ta_IN te_IN ur_IN",
+            "",
         },
 
         // Thai
@@ -220,6 +217,7 @@
                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_ja.class",
                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_zh.class"),
             " en en_US en_US_POSIX th th_TH th_TH_TH_#u-nu-thai",
+            "",
         },
 
         // Hong Kong
@@ -242,6 +240,7 @@
                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_ja.class",
                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_th.class"),
             " en en_US en_US_POSIX zh_HK zh_HK_#Hans zh_HK_#Hant",
+            "",
         },
 
         // Norwegian
@@ -265,6 +264,7 @@
                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_ja.class",
                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_th.class"),
             " en en_US en_US_POSIX nb nb_NO nb_SJ nn nn_NO no no_NO no_NO_NY",
+            "",
         },
 
         // Hebrew/Indonesian/Yiddish
@@ -290,6 +290,25 @@
                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_ja.class",
                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_th.class"),
             " en en_US en_US_POSIX in in_ID iw iw_IL ji ji_001",
+            "",
+        },
+
+        // Error case: No matching locales
+        {"--include-locales=xyz",
+            null,
+            null,
+            null,
+            new PluginException(
+                PluginsResourceBundle.getMessage("include-locales.nomatchinglocales"))
+                .toString(),
+        },
+
+        // Error case: Invalid argument
+        {"--include-locales=zh_HK",
+            null,
+            null,
+            null,
+            "range=zh_hk",
         },
     };
 
@@ -304,20 +323,28 @@
 
         for (Object[] data : testData) {
             // create image for each test data
-            Path image = JImageGenerator.getJLinkTask()
+            Result result = JImageGenerator.getJLinkTask()
                     .modulePath(helper.defaultModulePath())
                     .output(helper.createNewImageDir(moduleName))
                     .addMods("jdk.localedata")
                     .option((String)data[INCLUDE_LOCALES_OPTION])
-                    .call().assertSuccess();
+                    .call();
+
+            String errorMsg = (String)data[ERROR_MESSAGE];
+            if (errorMsg.isEmpty()) {
+                Path image = result.assertSuccess();
 
-            // test locale data entries
-            testLocaleDataEntries(image,
-                (List<String>)data[EXPECTED_LOCATIONS],
-                (List<String>)data[UNEXPECTED_PATHS]);
+                // test locale data entries
+                testLocaleDataEntries(image,
+                    (List<String>)data[EXPECTED_LOCATIONS],
+                    (List<String>)data[UNEXPECTED_PATHS]);
 
-            // test available locales
-            testAvailableLocales(image, (String)data[AVAILABLE_LOCALES]);
+                // test available locales
+                testAvailableLocales(image, (String)data[AVAILABLE_LOCALES]);
+            } else {
+                result.assertFailure(new TaskHelper(TaskHelper.JLINK_BUNDLE)
+                    .getMessage("error.prefix") + " " +errorMsg);
+            }
         }
     }