1/*
2 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8156499
27 * @summary Test image creation from Multi-Release JAR
28 * @author Steve Drach
29 * @library /test/lib
30 * @modules java.base/jdk.internal.jimage
31 *          java.base/jdk.internal.module
32 * @build jdk.test.lib.process.*
33 * @run testng JLinkMultiReleaseJarTest
34*/
35
36import java.io.ByteArrayInputStream;
37import java.io.IOException;
38import java.lang.invoke.MethodHandle;
39import java.lang.invoke.MethodHandles;
40import java.lang.invoke.MethodType;
41import java.lang.module.ModuleDescriptor;
42import java.nio.file.Files;
43import java.nio.file.Path;
44import java.nio.file.Paths;
45import java.nio.file.StandardCopyOption;
46import java.util.Arrays;
47import java.util.Set;
48import java.util.jar.JarFile;
49import java.util.spi.ToolProvider;
50import java.util.stream.Collectors;
51import java.util.stream.Stream;
52
53import jdk.internal.jimage.BasicImageReader;
54import jdk.test.lib.process.ProcessTools;
55import jdk.test.lib.process.OutputAnalyzer;
56
57import org.testng.Assert;
58import org.testng.annotations.BeforeClass;
59import org.testng.annotations.Test;
60
61public class JLinkMultiReleaseJarTest {
62    private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
63            .orElseThrow(() -> new RuntimeException("jar tool not found"));
64    private static final ToolProvider JAVAC_TOOL = ToolProvider.findFirst("javac")
65            .orElseThrow(() -> new RuntimeException("javac tool not found"));
66    private static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
67            .orElseThrow(() -> new RuntimeException("jlink tool not found"));
68
69    private final Path userdir = Paths.get(System.getProperty("user.dir", "."));
70    private final Path javahome = Paths.get(System.getProperty("java.home"));
71    private final Path jmodsdir = javahome.resolve("jmods");
72
73    private final String pathsep = System.getProperty("path.separator");
74
75    private byte[] resource = (Runtime.version().major() + " resource file").getBytes();
76
77    @BeforeClass
78    public void initialize() throws IOException {
79        Path srcdir = Paths.get(System.getProperty("test.src"));
80
81        // create class files from source
82        Path base = srcdir.resolve("base");
83        Path basemods = userdir.resolve("basemods");
84        javac(base, basemods, base.toString());
85
86        Path rt = srcdir.resolve("rt");
87        Path rtmods = userdir.resolve("rtmods");
88        javac(rt, rtmods, rt.toString());
89
90        // create resources in basemods and rtmods
91        Path dest = basemods.resolve("m1").resolve("resource.txt");
92        byte[] text = "base resource file".getBytes();
93        ByteArrayInputStream is = new ByteArrayInputStream(text);
94        Files.copy(is, dest);
95
96        dest = rtmods.resolve("m1").resolve("resource.txt");
97        is = new ByteArrayInputStream(resource);
98        Files.copy(is, dest);
99
100        // build multi-release jar file with different module-infos
101        String[] args = {
102                "-cf", "m1.jar",
103                "-C", basemods.resolve("m1").toString(), ".",
104                "--release ", String.valueOf(JarFile.runtimeVersion().major()),
105                "-C", rtmods.resolve("m1").toString(), "."
106        };
107        JAR_TOOL.run(System.out, System.err, args);
108
109        // now move the module-info that requires logging to temporary place
110        Files.move(rtmods.resolve("m1").resolve("module-info.class"),
111                userdir.resolve("module-info.class"));
112
113        // and build another jar
114        args[1] = "m1-no-logging.jar";
115        JAR_TOOL.run(System.out, System.err, args);
116
117        // replace the no logging module-info with the logging module-info
118        Files.move(userdir.resolve("module-info.class"),
119                basemods.resolve("m1").resolve("module-info.class"),
120                StandardCopyOption.REPLACE_EXISTING);
121
122        // and build another jar
123        args[1] = "m1-logging.jar";
124        JAR_TOOL.run(System.out, System.err, args);
125    }
126
127    private void javac(Path source, Path destination, String srcpath) throws IOException {
128        String[] args = Stream.concat(
129                Stream.of("-d", destination.toString(), "--module-source-path", srcpath),
130                Files.walk(source)
131                        .map(Path::toString)
132                        .filter(s -> s.endsWith(".java"))
133        ).toArray(String[]::new);
134        int rc = JAVAC_TOOL.run(System.out, System.err, args);
135        Assert.assertEquals(rc, 0);
136    }
137
138    @Test
139    public void basicTest() throws Throwable {
140        if (ignoreTest()) return;
141
142        // use jlink to build image from multi-release jar
143       jlink("m1.jar", "myimage");
144
145        // validate image
146        Path jimage = userdir.resolve("myimage").resolve("lib").resolve("modules");
147        try (BasicImageReader reader = BasicImageReader.open(jimage)) {
148
149            // do we have the right entry names?
150            Set<String> names = Arrays.stream(reader.getEntryNames())
151                    .filter(n -> n.startsWith("/m1"))
152                    .collect(Collectors.toSet());
153            Assert.assertEquals(names, Set.of(
154                    "/m1/module-info.class",
155                    "/m1/p/Main.class",
156                    "/m1/p/Type.class",
157                    "/m1/q/PublicClass.class",
158                    "/m1/META-INF/MANIFEST.MF",
159                    "/m1/resource.txt"));
160
161            // do we have the right module-info.class?
162            byte[] b = reader.getResource("/m1/module-info.class");
163            Set<String> requires = ModuleDescriptor
164                    .read(new ByteArrayInputStream(b))
165                    .requires()
166                    .stream()
167                    .map(mdr -> mdr.name())
168                    .filter(nm -> !nm.equals("java.base"))
169                    .collect(Collectors.toSet());
170            Assert.assertEquals(requires, Set.of("java.logging"));
171
172            // do we have the right resource?
173            b = reader.getResource("/m1/resource.txt");
174            Assert.assertEquals(b, resource);
175
176            // do we have the right class?
177            b = reader.getResource("/m1/p/Main.class");
178            Class<?> clazz = (new ByteArrayClassLoader()).loadClass("p.Main", b);
179            MethodHandle getVersion = MethodHandles.lookup()
180                    .findVirtual(clazz, "getVersion", MethodType.methodType(int.class));
181            int version = (int) getVersion.invoke(clazz.getConstructor().newInstance());
182            Assert.assertEquals(version, JarFile.runtimeVersion().major());
183        }
184    }
185
186    @Test
187    public void noLoggingTest() throws Throwable {
188        if (ignoreTest()) return;
189
190        jlink("m1-no-logging.jar", "no-logging-image");
191        runImage("no-logging-image", false);
192    }
193
194    @Test
195    public void loggingTest() throws Throwable {
196        if (ignoreTest()) return;
197
198        jlink("m1-logging.jar", "logging-image");
199        runImage("logging-image", true);
200
201    }
202
203    // java.base.jmod must exist for this test to make sense
204    private boolean ignoreTest() {
205        if (Files.isRegularFile(jmodsdir.resolve("java.base.jmod"))) {
206            return false;
207        }
208        System.err.println("Test skipped. NO jmods/java.base.jmod");
209        return true;
210    }
211
212
213    private void jlink(String jar, String image) {
214        String args = "--output " + image + " --add-modules m1 --module-path " +
215                jar + pathsep + jmodsdir.toString();
216        int exitCode = JLINK_TOOL.run(System.out, System.err, args.split(" +"));
217        Assert.assertEquals(exitCode, 0);
218    }
219
220    public void runImage(String image, boolean expected) throws Throwable {
221        Path java = Paths.get(image, "bin", "java");
222        OutputAnalyzer oa = ProcessTools.executeProcess(java.toString(), "-m", "m1/p.Main");
223        String sout = oa.getStdout();
224        boolean actual = sout.contains("logging found");
225        Assert.assertEquals(actual, expected);
226        System.out.println(sout);
227        System.err.println(oa.getStderr());
228        Assert.assertEquals(oa.getExitValue(), 0);
229    }
230
231    private static class ByteArrayClassLoader extends ClassLoader {
232        public Class<?> loadClass(String name, byte[] bytes) {
233            return defineClass(name, bytes, 0, bytes.length);
234        }
235    }
236}
237