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