changeset 44257:3220d2ac3cee

8176709: JarFileSystem::isMultiReleaseJar is incorrect Reviewed-by: mchung, sherman
author redestad
date Wed, 15 Mar 2017 19:33:00 +0100
parents 12050b22e372
children 2017f44ec3e5
files jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/JarFileSystem.java jdk/test/jdk/nio/zipfs/MultiReleaseJarTest.java jdk/test/lib/testlibrary/java/util/jar/CreateMultiReleaseTestJars.java
diffstat 3 files changed, 58 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/JarFileSystem.java	Tue Mar 14 19:44:52 2017 -0700
+++ b/jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/JarFileSystem.java	Wed Mar 15 19:33:00 2017 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2017, 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
@@ -28,6 +28,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.Runtime.Version;
+import java.nio.file.NoSuchFileException;
 import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -86,12 +87,12 @@
         }
     }
 
-    private boolean isMultiReleaseJar() {
+    private boolean isMultiReleaseJar() throws IOException {
         try (InputStream is = newInputStream(getBytes("/META-INF/MANIFEST.MF"))) {
-            return (new Manifest(is)).getMainAttributes()
-                    .containsKey(new Attributes.Name("Multi-Release"));
-            // fixme change line above after JarFile integration to contain Attributes.Name.MULTI_RELEASE
-        } catch (IOException x) {
+            String multiRelease = new Manifest(is).getMainAttributes()
+                    .getValue(Attributes.Name.MULTI_RELEASE);
+            return "true".equalsIgnoreCase(multiRelease);
+        } catch (NoSuchFileException x) {
             return false;
         }
     }
--- a/jdk/test/jdk/nio/zipfs/MultiReleaseJarTest.java	Tue Mar 14 19:44:52 2017 -0700
+++ b/jdk/test/jdk/nio/zipfs/MultiReleaseJarTest.java	Wed Mar 15 19:33:00 2017 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2017, 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,7 +23,7 @@
 
 /*
  * @test
- * @bug 8144355 8144062
+ * @bug 8144355 8144062 8176709
  * @summary Test aliasing additions to ZipFileSystem for multi-release jar files
  * @library /lib/testlibrary/java/util/jar
  * @build Compiler JarBuilder CreateMultiReleaseTestJars
@@ -42,6 +42,7 @@
 import java.nio.file.*;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.testng.Assert;
 import org.testng.annotations.*;
@@ -50,6 +51,7 @@
     final private int MAJOR_VERSION = Runtime.version().major();
 
     final private String userdir = System.getProperty("user.dir",".");
+    final private CreateMultiReleaseTestJars creator =  new CreateMultiReleaseTestJars();
     final private Map<String,String> stringEnv = new HashMap<>();
     final private Map<String,Integer> integerEnv = new HashMap<>();
     final private Map<String,Version> versionEnv = new HashMap<>();
@@ -63,7 +65,6 @@
 
     @BeforeClass
     public void initialize() throws Exception {
-        CreateMultiReleaseTestJars creator =  new CreateMultiReleaseTestJars();
         creator.compileEntries();
         creator.buildUnversionedJar();
         creator.buildMultiReleaseJar();
@@ -187,6 +188,44 @@
         }
     }
 
+    @Test
+    public void testIsMultiReleaseJar() throws Exception {
+        testCustomMultiReleaseValue("true", true);
+        testCustomMultiReleaseValue("true\r\nOther: value", true);
+        testCustomMultiReleaseValue("true\nOther: value", true);
+        testCustomMultiReleaseValue("true\rOther: value", true);
+
+        testCustomMultiReleaseValue("false", false);
+        testCustomMultiReleaseValue(" true", false);
+        testCustomMultiReleaseValue("true ", false);
+        testCustomMultiReleaseValue("true\n ", false);
+        testCustomMultiReleaseValue("true\r ", false);
+        testCustomMultiReleaseValue("true\n true", false);
+        testCustomMultiReleaseValue("true\r\n true", false);
+    }
+
+    private static final AtomicInteger JAR_COUNT = new AtomicInteger(0);
+
+    private void testCustomMultiReleaseValue(String value, boolean expected)
+            throws Exception {
+        String fileName = "custom-mr" + JAR_COUNT.incrementAndGet() + ".jar";
+        creator.buildCustomMultiReleaseJar(fileName, value, Map.of(),
+                /*addEntries*/true);
+
+        Map<String,String> env = Map.of("multi-release", "runtime");
+        Path filePath = Paths.get(userdir, fileName);
+        String ssp = filePath.toUri().toString();
+        URI customJar = new URI("jar", ssp , null);
+        try (FileSystem fs = FileSystems.newFileSystem(customJar, env)) {
+            if (expected) {
+                Assert.assertTrue(readAndCompare(fs, MAJOR_VERSION));
+            } else {
+                Assert.assertTrue(readAndCompare(fs, 8));
+            }
+        }
+        Files.delete(filePath);
+    }
+
     private static class ByteArrayClassLoader extends ClassLoader {
         final private FileSystem fs;
 
--- a/jdk/test/lib/testlibrary/java/util/jar/CreateMultiReleaseTestJars.java	Tue Mar 14 19:44:52 2017 -0700
+++ b/jdk/test/lib/testlibrary/java/util/jar/CreateMultiReleaseTestJars.java	Wed Mar 15 19:33:00 2017 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2017, 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
@@ -109,9 +109,17 @@
 
     public void buildCustomMultiReleaseJar(String filename, String multiReleaseValue,
             Map<String, String> extraAttributes) throws IOException {
+        buildCustomMultiReleaseJar(filename, multiReleaseValue, extraAttributes, false);
+    }
+
+    public void buildCustomMultiReleaseJar(String filename, String multiReleaseValue,
+            Map<String, String> extraAttributes, boolean addEntries) throws IOException {
         JarBuilder jb = new JarBuilder(filename);
         extraAttributes.entrySet()
                 .forEach(entry -> jb.addAttribute(entry.getKey(), entry.getValue()));
+        if (addEntries) {
+            addEntries(jb);
+        }
         jb.addAttribute("Multi-Release", multiReleaseValue);
         jb.build();
     }