1 /*
   2  * Copyright (c) 2015, 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.jlink
  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 -verbose:gc -Xmx1g JImageTest
  59 */
  60 public class JImageTest {
  61 
  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         // JPRT not yet ready for jmods
  98         Helper helper = Helper.newHelper();
  99         if (helper == null) {
 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         String imgName = "bootmodules.jimage";
 111         Path extractedDir = JImageGenerator.getJImageTask()
 112                 .dir(helper.createNewExtractedDir(imgName))
 113                 .image(image.resolve("lib").resolve("modules").resolve(imgName))
 114                 .extract().assertSuccess();
 115 
 116         Path recreatedImage = JImageGenerator.getJImageTask()
 117                 .dir(extractedDir)
 118                 .image(helper.createNewRecreatedDir(extractedDir.getFileName().toString()))
 119                 .recreate().assertSuccess();
 120         JImageValidator.validate(recreatedImage.toFile(), bootClasses, Collections.emptyList());
 121 
 122         // Check replacing the boot image by recreated one
 123         Path destFile = image.resolve("lib").resolve("modules").resolve(imgName);
 124         Files.copy(recreatedImage, destFile, REPLACE_EXISTING);
 125         JImageValidator validator = new JImageValidator(module, Collections.emptyList(),
 126                 image.toFile(), Collections.emptyList(), Collections.emptyList());
 127         validator.validate();
 128 
 129         Path recreatedImage2 = JImageGenerator.getJImageTask()
 130                 .dir(extractedDir)
 131                 .option("--compress-resources")
 132                 .option("on")
 133                 .image(helper.createNewRecreatedDir(extractedDir.getFileName().toString()))
 134                 .recreate().assertSuccess();
 135         JImageValidator.validate(recreatedImage2.toFile(), bootClasses, Collections.emptyList());
 136 
 137         Path recreatedImage3 = JImageGenerator.getJImageTask()
 138                 .dir(extractedDir)
 139                 .option("--strip-java-debug")
 140                 .option("on")
 141                 .image(helper.createNewRecreatedDir(extractedDir.getFileName().toString()))
 142                 .recreate().assertSuccess();
 143         JImageValidator.validate(recreatedImage3.toFile(), bootClasses, Collections.emptyList());
 144 
 145         Path recreatedImage4 = JImageGenerator.getJImageTask()
 146                 .dir(extractedDir)
 147                 .option("--exclude-resources")
 148                 .option("*.jcov, */META-INF/*")
 149                 .image(helper.createNewRecreatedDir(extractedDir.getFileName().toString()))
 150                 .recreate().assertSuccess();
 151         List<String> unexpectedPaths = new ArrayList<>();
 152         unexpectedPaths.add(".jcov");
 153         unexpectedPaths.add("/META-INF/");
 154         JImageValidator.validate(recreatedImage4.toFile(), bootClasses, unexpectedPaths);
 155 
 156         Path recreatedImage5 = JImageGenerator.getJImageTask()
 157                 .dir(extractedDir)
 158                 .option("--compress-resources")
 159                 .option("on")
 160                 .option("--strip-java-debug")
 161                 .option("on")
 162                 .option("--exclude-resources")
 163                 .option("*.jcov, */META-INF/*")
 164                 .image(helper.createNewRecreatedDir(extractedDir.getFileName().toString()))
 165                 .recreate().assertSuccess();
 166         JImageValidator.validate(recreatedImage5.toFile(), bootClasses, unexpectedPaths);
 167     }
 168 }