Path: blob/master/test/jdk/tools/jimage/JImageTest.java
41144 views
/*1* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import java.io.File;23import java.net.URI;24import java.nio.file.FileSystem;25import java.nio.file.FileSystemNotFoundException;26import java.nio.file.FileSystems;27import java.nio.file.Files;28import java.nio.file.Path;29import java.nio.file.ProviderNotFoundException;30import java.util.ArrayList;31import java.util.Arrays;32import java.util.Collections;33import java.util.List;34import java.util.function.Consumer;35import java.util.stream.Stream;3637import tests.Helper;38import tests.JImageGenerator;39import tests.JImageValidator;4041import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;4243/*44* jimage testing.45* @test46* @summary Test jimage tool47* @bug 822210048* @library ../lib49* @modules java.base/jdk.internal.jimage50* jdk.jdeps/com.sun.tools.classfile51* jdk.jlink/jdk.tools.jmod52* jdk.jlink/jdk.tools.jimage53* jdk.jlink/jdk.tools.jlink.internal54* jdk.compiler55* @run build JImageTest56* @run build tests.*57* @run main/othervm/timeout=360 -verbose:gc -Xmx1g JImageTest58*/59public class JImageTest {6061public static void main(String[] args) throws Exception {62List<String> bootClasses = new ArrayList<>();6364FileSystem fs;65try {66fs = FileSystems.getFileSystem(URI.create("jrt:/"));67} catch (ProviderNotFoundException | FileSystemNotFoundException e) {68System.out.println("Not an image build, test skipped.");69return;70}7172// Build the set of locations expected in the Image73Consumer<Path> c = (p) -> {74// take only the .class resources.75if (Files.isRegularFile(p) && p.toString().endsWith(".class")76&& !p.toString().endsWith("module-info.class")) {77String loc = p.toString().substring("/modules".length());78bootClasses.add(loc);79}80};8182Path javabase = fs.getPath("/modules/java.base");83Path mgtbase = fs.getPath("/modules/java.management");84try (Stream<Path> stream = Files.walk(javabase)) {85stream.forEach(c);86}87try (Stream<Path> stream = Files.walk(mgtbase)) {88stream.forEach(c);89}9091if (bootClasses.isEmpty()) {92throw new RuntimeException("No boot class to check against");93}9495File jdkHome = new File(System.getProperty("test.jdk"));96Helper helper = Helper.newHelper();97if (helper == null) {98// Skip test if the jmods directory is missing (e.g. exploded image)99System.err.println("Test not run, NO jmods directory");100return;101}102103// Generate the sample image104String module = "mod1";105String[] classes = {module + ".Main"};106helper.generateDefaultJModule(module, Arrays.asList(classes), "java.management");107108Path image = helper.generateDefaultImage(module).assertSuccess();109Path extractedDir = JImageGenerator.getJImageTask()110.dir(helper.createNewExtractedDir("modules"))111.image(image.resolve("lib").resolve("modules"))112.extract().assertSuccess();113}114}115116117