/* * 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 * @bug 8156499 * @summary Test image creation from Multi-Release JAR * @author Steve Drach * @library /lib/testlibrary * @modules java.base/jdk.internal.jimage * jdk.jartool/sun.tools.jar * jdk.jlink/jdk.tools.jlink.internal * @build CompilerUtils * @run testng JLinkMultiReleaseJarTest */ import java.io.IOException; import java.io.PrintWriter; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.Arrays; import java.util.Set; import java.util.jar.JarFile; import java.util.stream.Collectors; import java.util.stream.Stream; import jdk.internal.jimage.BasicImageReader; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class JLinkMultiReleaseJarTest { private Path userdir; private Path javahome; @BeforeClass public void initialize() throws IOException { Path srcdir = Paths.get(System.getProperty("test.src")); userdir = Paths.get(System.getProperty("user.dir", ".")); javahome = Paths.get(System.getProperty("java.home")); // create class files from source Path base = srcdir.resolve("base"); Path basemods = userdir.resolve("basemods"); CompilerUtils.compile(base, basemods, "-modulesourcepath", base.toString()); Path rt = srcdir.resolve("rt"); Path rtmods = userdir.resolve("rtmods"); CompilerUtils.compile(rt, rtmods, "-modulesourcepath", rt.toString()); // copy resource into basemods Path resource = base.resolve("m1").resolve("resources.txt"); Path dest = basemods.resolve("m1").resolve("resources.txt"); Files.copy(resource, dest); // build multi-release jar file sun.tools.jar.Main jartool = new sun.tools.jar.Main(System.out, System.err, "jar"); String args = "-cf m1.jar -C basemods/m1 . --release " + JarFile.runtimeVersion().major() + " -C rtmods/m1 ."; jartool.run(args.split(" +")); } //@AfterClass public void close() throws IOException { Files.walkFileTree(userdir, new SimpleFileVisitor<>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (dir.equals(userdir)) { return FileVisitResult.CONTINUE; } if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; } else { // directory iteration failed throw e; } } }); } @Test public void test() throws Throwable { // check that there are only packaged modules (.jmod files) for modulepath Path jmodsdir = javahome.resolve("jmods"); try (Stream jmods = Files.walk(jmodsdir, 1)) { if (!jmods .filter(path -> !path.equals(jmodsdir)) .filter(path -> !path.toString().endsWith(".jmod")) .collect(Collectors.toSet()) .isEmpty() ) { throw new Exception("Exploded modules found"); } } // use jlink to build image from multi-release jar String args = "--output myimage --add-modules m1 --module-path m1.jar:" + jmodsdir.toString(); int exitCode = jdk.tools.jlink.internal.Main.run(args.split(" +"), new PrintWriter(System.out)); Assert.assertEquals(exitCode, 0); // validate image Path jimage = userdir.resolve("myimage").resolve("lib").resolve("modules"); BasicImageReader reader = BasicImageReader.open(jimage); // do we have the right entry names? Set names = Arrays.stream(reader.getEntryNames()) .filter(n -> n.startsWith("/m1")) .collect(Collectors.toSet()); Assert.assertEquals(names, Set.of( "/m1/module-info.class", "/m1/p/Main.class", "/m1/p/Type.class", "/m1/resources.txt")); // do we have the right code? byte[] b = reader.getResource("/m1/p/Main.class"); Class clazz = (new ByteArrayClassLoader()).loadClass("p.Main", b); MethodHandle getVersion = MethodHandles.lookup() .findVirtual(clazz, "getVersion", MethodType.methodType(int.class)); int version = (int)getVersion.invoke(clazz.getConstructor().newInstance()); Assert.assertEquals(version, JarFile.runtimeVersion().major()); } private static class ByteArrayClassLoader extends ClassLoader { public Class loadClass(String name, byte[] bytes) { return defineClass(name, bytes, 0, bytes.length); } } }