1 /*
2  * Copyright (c) 2015, 2020, 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 import java.io.File;
24 import java.net.URI;
25 import java.nio.file.FileSystem;
26 import java.nio.file.FileSystemNotFoundException;
27 import java.nio.file.FileSystems;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.ProviderNotFoundException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.function.Consumer;
36 import java.util.stream.Stream;
37 
38 import tests.Helper;
39 import tests.JImageGenerator;
40 import tests.JImageValidator;
41 
42 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
43 
44 /*
45  * jimage testing.
46  * @test
47  * @summary Test jimage tool
48  * @bug 8222100
49  * @library ../lib
50  * @modules java.base/jdk.internal.jimage
51  *          jdk.jdeps/com.sun.tools.classfile
52  *          jdk.jlink/jdk.tools.jmod
53  *          jdk.jlink/jdk.tools.jimage
54  *          jdk.jlink/jdk.tools.jlink.internal
55  *          jdk.compiler
56  * @run build JImageTest
57  * @run build tests.*
58  * @run main/othervm/timeout=360 -verbose:gc -Xmx1g JImageTest
59 */
60 public class JImageTest {
61 
main(String[] args)62     public static void main(String[] args) throws Exception {
63         List<String> bootClasses = new ArrayList<>();
64 
65         FileSystem fs;
66         try {
67             fs = FileSystems.getFileSystem(URI.create("jrt:/"));
68         } catch (ProviderNotFoundException | FileSystemNotFoundException e) {
69             System.out.println("Not an image build, test skipped.");
70             return;
71         }
72 
73         // Build the set of locations expected in the Image
74         Consumer<Path> c = (p) -> {
75                // take only the .class resources.
76                if (Files.isRegularFile(p) && p.toString().endsWith(".class")
77                        && !p.toString().endsWith("module-info.class")) {
78                    String loc = p.toString().substring("/modules".length());
79                    bootClasses.add(loc);
80                }
81            };
82 
83         Path javabase = fs.getPath("/modules/java.base");
84         Path mgtbase = fs.getPath("/modules/java.management");
85         try (Stream<Path> stream = Files.walk(javabase)) {
86             stream.forEach(c);
87         }
88         try (Stream<Path> stream = Files.walk(mgtbase)) {
89             stream.forEach(c);
90         }
91 
92         if (bootClasses.isEmpty()) {
93             throw new RuntimeException("No boot class to check against");
94         }
95 
96         File jdkHome = new File(System.getProperty("test.jdk"));
97         Helper helper = Helper.newHelper();
98         if (helper == null) {
99             // Skip test if the jmods directory is missing (e.g. exploded image)
100             System.err.println("Test not run, NO jmods directory");
101             return;
102         }
103 
104         // Generate the sample image
105         String module = "mod1";
106         String[] classes = {module + ".Main"};
107         helper.generateDefaultJModule(module, Arrays.asList(classes), "java.management");
108 
109         Path image = helper.generateDefaultImage(module).assertSuccess();
110         Path extractedDir = JImageGenerator.getJImageTask()
111                 .dir(helper.createNewExtractedDir("modules"))
112                 .image(image.resolve("lib").resolve("modules"))
113                 .extract().assertSuccess();
114     }
115 }
116