test/jdk/tools/jimage/JImageTest.java
author mikael
Tue, 02 Oct 2018 22:36:08 -0700
changeset 51994 7577686cc9bd
parent 47216 71c04702a3d5
permissions -rw-r--r--
8211350: Remove jprt support Reviewed-by: dholmes, erikj, mchung, alanb, jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
51994
7577686cc9bd 8211350: Remove jprt support
mikael
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
import java.nio.file.FileSystem;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import java.nio.file.FileSystemNotFoundException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.nio.file.FileSystems;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.nio.file.ProviderNotFoundException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.Arrays;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.function.Consumer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.stream.Stream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import tests.Helper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import tests.JImageGenerator;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import tests.JImageValidator;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 * jimage testing.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 * @summary Test jimage tool
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
 * @library ../lib
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 * @modules java.base/jdk.internal.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 *          jdk.jdeps/com.sun.tools.classfile
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
 *          jdk.jlink/jdk.tools.jmod
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
 *          jdk.jlink/jdk.tools.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
 *          jdk.jlink/jdk.tools.jlink.internal
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
 *          jdk.compiler
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
 * @run build JImageTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
 * @run build tests.*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
 * @run main/othervm -verbose:gc -Xmx1g JImageTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
*/
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
public class JImageTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        List<String> bootClasses = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        FileSystem fs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
            fs = FileSystems.getFileSystem(URI.create("jrt:/"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        } catch (ProviderNotFoundException | FileSystemNotFoundException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
            System.out.println("Not an image build, test skipped.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        // Build the set of locations expected in the Image
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        Consumer<Path> c = (p) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
               // take only the .class resources.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
               if (Files.isRegularFile(p) && p.toString().endsWith(".class")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
                       && !p.toString().endsWith("module-info.class")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
                   String loc = p.toString().substring("/modules".length());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
                   bootClasses.add(loc);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
               }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
           };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        Path javabase = fs.getPath("/modules/java.base");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        Path mgtbase = fs.getPath("/modules/java.management");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        try (Stream<Path> stream = Files.walk(javabase)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
            stream.forEach(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        try (Stream<Path> stream = Files.walk(mgtbase)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            stream.forEach(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        if (bootClasses.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
            throw new RuntimeException("No boot class to check against");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        File jdkHome = new File(System.getProperty("test.jdk"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        Helper helper = Helper.newHelper();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        if (helper == null) {
51994
7577686cc9bd 8211350: Remove jprt support
mikael
parents: 47216
diff changeset
    98
            // Skip test if the jmods directory is missing (e.g. exploded image)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
            System.err.println("Test not run, NO jmods directory");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        // Generate the sample image
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        String module = "mod1";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        String[] classes = {module + ".Main"};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        helper.generateDefaultJModule(module, Arrays.asList(classes), "java.management");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        Path image = helper.generateDefaultImage(module).assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        Path extractedDir = JImageGenerator.getJImageTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
                .dir(helper.createNewExtractedDir("modules"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
                .image(image.resolve("lib").resolve("modules"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
                .extract().assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
}