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