8134779: (jmod) ZipException is thrown if there are duplicate resources
authorchegar
Thu, 28 Jul 2016 17:25:29 +0100
changeset 39882 2a5433a2eca5
parent 39881 c082c149d6eb
child 39883 9c59b6343fa0
child 40196 0b3f554d2480
8134779: (jmod) ZipException is thrown if there are duplicate resources 8134847: (jmod) module-info encountered in the cmds, libs or config is not added to jmod file Reviewed-by: alanb
jdk/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java
jdk/src/jdk.jlink/share/classes/jdk/tools/jmod/resources/jmod.properties
jdk/test/tools/jmod/JmodNegativeTest.java
jdk/test/tools/jmod/JmodTest.java
--- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java	Thu Jul 28 16:16:38 2016 -0700
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java	Thu Jul 28 17:25:29 2016 +0100
@@ -666,6 +666,10 @@
                     throws IOException
                 {
                     Path relPath = top.relativize(file);
+                    if (relPath.toString().equals(MODULE_INFO)
+                            && !Section.CLASSES.equals(section))
+                        warning("warn.ignore.entry", MODULE_INFO, section);
+
                     if (!relPath.toString().equals(MODULE_INFO)
                             && !matches(relPath, excludes)) {
                         try (InputStream in = Files.newInputStream(file)) {
@@ -693,9 +697,17 @@
             String name = Paths.get(prefix, other).toString()
                                .replace(File.separatorChar, '/');
             ZipEntry ze = new ZipEntry(name);
-            zos.putNextEntry(ze);
-            in.transferTo(zos);
-            zos.closeEntry();
+            try {
+                zos.putNextEntry(ze);
+                in.transferTo(zos);
+                zos.closeEntry();
+            } catch (ZipException x) {
+                if (x.getMessage().contains("duplicate entry")) {
+                    warning("warn.ignore.duplicate.entry", name, prefix);
+                    return;
+                }
+                throw x;
+            }
         }
 
         class JarEntryConsumer implements Consumer<JarEntry>, Predicate<JarEntry> {
--- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jmod/resources/jmod.properties	Thu Jul 28 16:16:38 2016 -0700
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jmod/resources/jmod.properties	Thu Jul 28 17:25:29 2016 +0100
@@ -97,4 +97,7 @@
 warn.invalid.arg=Invalid classname or pathname not exist: {0}
 warn.no.module.hashes=No hashes recorded: no module specified for hashing depends on {0}
 warn.module.resolution.fail=No hashes recorded: {0}
+warn.ignore.entry=ignoring entry {0}, in section {1}
+warn.ignore.duplicate.entry=ignoring duplicate entry {0}, in section{1}
 
+
--- a/jdk/test/tools/jmod/JmodNegativeTest.java	Thu Jul 28 16:16:38 2016 -0700
+++ b/jdk/test/tools/jmod/JmodNegativeTest.java	Thu Jul 28 17:25:29 2016 +0100
@@ -229,21 +229,6 @@
         }
     }
 
-    @Test(enabled = false)  // TODO: jmod should check for duplicates before creating.
-    public void testDuplicates() throws IOException {
-        Path jmod = MODS_DIR.resolve("testDuplicates.jmod");
-        FileUtils.deleteFileIfExistsWithRetry(jmod);
-        String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
-
-        jmod("create",
-             "--class-path", cp + pathSeparator + cp,
-             jmod.toString())
-            .assertFailure()
-            .resultChecker(r ->
-                assertContains(r.output, "Error: duplicate resource found, etc..")
-            );
-    }
-
     @Test
     public void testEmptyFileInClasspath() throws IOException {
         Path jmod = MODS_DIR.resolve("testEmptyFileInClasspath.jmod");
--- a/jdk/test/tools/jmod/JmodTest.java	Thu Jul 28 16:16:38 2016 -0700
+++ b/jdk/test/tools/jmod/JmodTest.java	Thu Jul 28 17:25:29 2016 +0100
@@ -42,6 +42,8 @@
 import jdk.testlibrary.FileUtils;
 import org.testng.annotations.BeforeTest;
 import org.testng.annotations.Test;
+
+import static java.io.File.pathSeparator;
 import static java.lang.module.ModuleDescriptor.Version;
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static java.util.stream.Collectors.toSet;
@@ -283,6 +285,58 @@
     }
 
     @Test
+    public void testDuplicateEntries() throws IOException {
+        Path jmod = MODS_DIR.resolve("testDuplicates.jmod");
+        FileUtils.deleteFileIfExistsWithRetry(jmod);
+        String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
+        Path lp = EXPLODED_DIR.resolve("foo").resolve("lib");
+
+        jmod("create",
+             "--class-path", cp + pathSeparator + cp,
+             jmod.toString())
+             .assertSuccess()
+             .resultChecker(r ->
+                 assertContains(r.output, "Warning: ignoring duplicate entry")
+             );
+
+        FileUtils.deleteFileIfExistsWithRetry(jmod);
+        jmod("create",
+             "--class-path", cp,
+             "--libs", lp.toString() + pathSeparator + lp.toString(),
+             jmod.toString())
+             .assertSuccess()
+             .resultChecker(r ->
+                 assertContains(r.output, "Warning: ignoring duplicate entry")
+             );
+    }
+
+    @Test
+    public void testIgnoreModuleInfoInOtherSections() throws IOException {
+        Path jmod = MODS_DIR.resolve("testIgnoreModuleInfoInOtherSections.jmod");
+        FileUtils.deleteFileIfExistsWithRetry(jmod);
+        String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
+
+        jmod("create",
+            "--class-path", cp,
+            "--libs", cp,
+            jmod.toString())
+            .assertSuccess()
+            .resultChecker(r ->
+                assertContains(r.output, "Warning: ignoring entry")
+            );
+
+        FileUtils.deleteFileIfExistsWithRetry(jmod);
+        jmod("create",
+             "--class-path", cp,
+             "--cmds", cp,
+             jmod.toString())
+             .assertSuccess()
+             .resultChecker(r ->
+                 assertContains(r.output, "Warning: ignoring entry")
+             );
+    }
+
+    @Test
     public void testVersion() {
         jmod("--version")
             .assertSuccess()