8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
Reviewed-by: chegar, mchung
This commit is contained in:
parent
e549e87431
commit
fe609fd3cb
@ -220,10 +220,10 @@ class ModuleReferences {
|
|||||||
Optional<URI> implFind(String name) throws IOException {
|
Optional<URI> implFind(String name) throws IOException {
|
||||||
JarEntry je = getEntry(name);
|
JarEntry je = getEntry(name);
|
||||||
if (je != null) {
|
if (je != null) {
|
||||||
|
if (jf.isMultiRelease())
|
||||||
|
name = SharedSecrets.javaUtilJarAccess().getRealName(jf, je);
|
||||||
String encodedPath = ParseUtil.encodePath(name, false);
|
String encodedPath = ParseUtil.encodePath(name, false);
|
||||||
String uris = "jar:" + uri + "!/" + encodedPath;
|
String uris = "jar:" + uri + "!/" + encodedPath;
|
||||||
if (jf.isMultiRelease())
|
|
||||||
uris += "#runtime";
|
|
||||||
return Optional.of(URI.create(uris));
|
return Optional.of(URI.create(uris));
|
||||||
} else {
|
} else {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
216
jdk/test/java/lang/module/ModuleReader/MultiReleaseJarTest.java
Normal file
216
jdk/test/java/lang/module/ModuleReader/MultiReleaseJarTest.java
Normal file
@ -0,0 +1,216 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 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
|
||||||
|
* @library /lib/testlibrary
|
||||||
|
* @modules java.base/jdk.internal.module
|
||||||
|
* @build MultiReleaseJarTest JarUtils
|
||||||
|
* @run testng MultiReleaseJarTest
|
||||||
|
* @run testng/othervm -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarTest
|
||||||
|
* @summary Basic test of ModuleReader with a modular JAR that is also a
|
||||||
|
* multi-release JAR
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.lang.module.ModuleDescriptor;
|
||||||
|
import java.lang.module.ModuleFinder;
|
||||||
|
import java.lang.module.ModuleReader;
|
||||||
|
import java.lang.module.ModuleReference;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.jar.Attributes;
|
||||||
|
import java.util.jar.Manifest;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import jdk.internal.module.ModuleInfoWriter;
|
||||||
|
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
import static org.testng.Assert.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exercises ModuleReader with a modular JAR containing the following files:
|
||||||
|
*
|
||||||
|
* <pre>{@code
|
||||||
|
* module-info.class
|
||||||
|
* META-INF/versions/<version>/module.class
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* The module-info.class in the top-level directory is the binary form of:
|
||||||
|
* <pre>{@code
|
||||||
|
* module jdk.test {
|
||||||
|
* requires java.base;
|
||||||
|
* }
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* The module-info.class in the versioned section is the binary form of:
|
||||||
|
* <pre>{@code
|
||||||
|
* module jdk.test {
|
||||||
|
* requires java.base;
|
||||||
|
* requires jdk.unsupported;
|
||||||
|
* }
|
||||||
|
* }</pre>
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public class MultiReleaseJarTest {
|
||||||
|
|
||||||
|
// Java SE/JDK major release
|
||||||
|
private static final int RELEASE = Runtime.version().major();
|
||||||
|
|
||||||
|
// the name of the test module
|
||||||
|
private static final String MODULE_NAME = "jdk.test";
|
||||||
|
|
||||||
|
private static final String MODULE_INFO_CLASS = "module-info.class";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses the ModuleFinder API to locate the module packaged as a modular
|
||||||
|
* and mutli-release JAR and then creates a ModuleReader to access the
|
||||||
|
* contents of the module.
|
||||||
|
*/
|
||||||
|
public void testMultiReleaseJar() throws IOException {
|
||||||
|
|
||||||
|
// are multi-release JARs enabled?
|
||||||
|
String s = System.getProperty("jdk.util.jar.enableMultiRelease");
|
||||||
|
boolean multiRelease = (s == null || Boolean.parseBoolean(s));
|
||||||
|
|
||||||
|
// create the multi-release modular JAR
|
||||||
|
Path jarfile = createJarFile();
|
||||||
|
|
||||||
|
// find the module
|
||||||
|
ModuleFinder finder = ModuleFinder.of(jarfile);
|
||||||
|
Optional<ModuleReference> omref = finder.find(MODULE_NAME);
|
||||||
|
assertTrue((omref.isPresent()));
|
||||||
|
ModuleReference mref = omref.get();
|
||||||
|
|
||||||
|
// test that correct module-info.class was read
|
||||||
|
checkDescriptor(mref.descriptor(), multiRelease);
|
||||||
|
|
||||||
|
// test ModuleReader
|
||||||
|
try (ModuleReader reader = mref.open()) {
|
||||||
|
|
||||||
|
// open resource
|
||||||
|
Optional<InputStream> oin = reader.open(MODULE_INFO_CLASS);
|
||||||
|
assertTrue(oin.isPresent());
|
||||||
|
try (InputStream in = oin.get()) {
|
||||||
|
checkDescriptor(ModuleDescriptor.read(in), multiRelease);
|
||||||
|
}
|
||||||
|
|
||||||
|
// read resource
|
||||||
|
Optional<ByteBuffer> obb = reader.read(MODULE_INFO_CLASS);
|
||||||
|
assertTrue(obb.isPresent());
|
||||||
|
ByteBuffer bb = obb.get();
|
||||||
|
try {
|
||||||
|
checkDescriptor(ModuleDescriptor.read(bb), multiRelease);
|
||||||
|
} finally {
|
||||||
|
reader.release(bb);
|
||||||
|
}
|
||||||
|
|
||||||
|
// find resource
|
||||||
|
Optional<URI> ouri = reader.find(MODULE_INFO_CLASS);
|
||||||
|
assertTrue(ouri.isPresent());
|
||||||
|
URI uri = ouri.get();
|
||||||
|
|
||||||
|
String expectedTail = "!/";
|
||||||
|
if (multiRelease)
|
||||||
|
expectedTail += "META-INF/versions/" + RELEASE + "/";
|
||||||
|
expectedTail += MODULE_INFO_CLASS;
|
||||||
|
assertTrue(uri.toString().endsWith(expectedTail));
|
||||||
|
|
||||||
|
URLConnection uc = uri.toURL().openConnection();
|
||||||
|
uc.setUseCaches(false);
|
||||||
|
try (InputStream in = uc.getInputStream()) {
|
||||||
|
checkDescriptor(ModuleDescriptor.read(in), multiRelease);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks that the module descriptor is the expected module descriptor.
|
||||||
|
* When the multi release JAR feature is enabled then the module
|
||||||
|
* descriptor is expected to have been read from the versioned section
|
||||||
|
* of the JAR file.
|
||||||
|
*/
|
||||||
|
private void checkDescriptor(ModuleDescriptor descriptor, boolean multiRelease) {
|
||||||
|
Set<String> requires = descriptor.requires().stream()
|
||||||
|
.map(ModuleDescriptor.Requires::name)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
assertTrue(requires.contains("java.base"));
|
||||||
|
assertTrue(requires.contains("jdk.unsupported") == multiRelease);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the modular JAR for the test, returning the Path to the JAR file.
|
||||||
|
*/
|
||||||
|
private Path createJarFile() throws IOException {
|
||||||
|
|
||||||
|
// module descriptor for top-level directory
|
||||||
|
ModuleDescriptor descriptor1
|
||||||
|
= new ModuleDescriptor.Builder(MODULE_NAME)
|
||||||
|
.requires("java.base")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// module descriptor for versioned section
|
||||||
|
ModuleDescriptor descriptor2
|
||||||
|
= new ModuleDescriptor.Builder(MODULE_NAME)
|
||||||
|
.requires("java.base")
|
||||||
|
.requires("jdk.unsupported")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Path top = Paths.get(MODULE_NAME);
|
||||||
|
Files.createDirectories(top);
|
||||||
|
|
||||||
|
Path mi1 = Paths.get(MODULE_INFO_CLASS);
|
||||||
|
try (OutputStream out = Files.newOutputStream(top.resolve(mi1))) {
|
||||||
|
ModuleInfoWriter.write(descriptor1, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
Path vdir = Paths.get("META-INF", "versions", Integer.toString(RELEASE));
|
||||||
|
Files.createDirectories(top.resolve(vdir));
|
||||||
|
|
||||||
|
Path mi2 = vdir.resolve(MODULE_INFO_CLASS);
|
||||||
|
try (OutputStream out = Files.newOutputStream(top.resolve(mi2))) {
|
||||||
|
ModuleInfoWriter.write(descriptor2, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
Manifest man = new Manifest();
|
||||||
|
Attributes attrs = man.getMainAttributes();
|
||||||
|
attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
|
||||||
|
attrs.put(Attributes.Name.MULTI_RELEASE, "true");
|
||||||
|
|
||||||
|
Path jarfile = Paths.get(MODULE_NAME + ".jar");
|
||||||
|
JarUtils.createJarFile(jarfile, man, top, mi1, mi2);
|
||||||
|
|
||||||
|
return jarfile;
|
||||||
|
}
|
||||||
|
}
|
@ -35,6 +35,7 @@ import java.util.Set;
|
|||||||
import java.util.jar.JarEntry;
|
import java.util.jar.JarEntry;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
import java.util.jar.JarOutputStream;
|
import java.util.jar.JarOutputStream;
|
||||||
|
import java.util.jar.Manifest;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@ -49,12 +50,12 @@ public final class JarUtils {
|
|||||||
/**
|
/**
|
||||||
* Creates a JAR file.
|
* Creates a JAR file.
|
||||||
*
|
*
|
||||||
* Equivalent to {@code jar cf <jarfile> -C <dir> file...}
|
* Equivalent to {@code jar cfm <jarfile> <manifest> -C <dir> file...}
|
||||||
*
|
*
|
||||||
* The input files are resolved against the given directory. Any input
|
* The input files are resolved against the given directory. Any input
|
||||||
* files that are directories are processed recursively.
|
* files that are directories are processed recursively.
|
||||||
*/
|
*/
|
||||||
public static void createJarFile(Path jarfile, Path dir, Path... file)
|
public static void createJarFile(Path jarfile, Manifest man, Path dir, Path... file)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
// create the target directory
|
// create the target directory
|
||||||
@ -73,14 +74,36 @@ public final class JarUtils {
|
|||||||
try (OutputStream out = Files.newOutputStream(jarfile);
|
try (OutputStream out = Files.newOutputStream(jarfile);
|
||||||
JarOutputStream jos = new JarOutputStream(out))
|
JarOutputStream jos = new JarOutputStream(out))
|
||||||
{
|
{
|
||||||
|
if (man != null) {
|
||||||
|
JarEntry je = new JarEntry(JarFile.MANIFEST_NAME);
|
||||||
|
jos.putNextEntry(je);
|
||||||
|
man.write(jos);
|
||||||
|
jos.closeEntry();
|
||||||
|
}
|
||||||
|
|
||||||
for (Path entry : entries) {
|
for (Path entry : entries) {
|
||||||
String name = toJarEntryName(entry);
|
String name = toJarEntryName(entry);
|
||||||
jos.putNextEntry(new JarEntry(name));
|
jos.putNextEntry(new JarEntry(name));
|
||||||
Files.copy(dir.resolve(entry), jos);
|
Files.copy(dir.resolve(entry), jos);
|
||||||
|
jos.closeEntry();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a JAR file.
|
||||||
|
*
|
||||||
|
* Equivalent to {@code jar cf <jarfile> -C <dir> file...}
|
||||||
|
*
|
||||||
|
* The input files are resolved against the given directory. Any input
|
||||||
|
* files that are directories are processed recursively.
|
||||||
|
*/
|
||||||
|
public static void createJarFile(Path jarfile, Path dir, Path... file)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
createJarFile(jarfile, null, dir, file);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a JAR file.
|
* Creates a JAR file.
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user