1 /*
   2  * Copyright (c) 2016, 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 
  24 /*
  25  * @test
  26  * @summary Tests to verify jimage 'extract' action
  27  * @library /test/lib
  28  * @build jdk.test.lib.Asserts
  29  * @modules jdk.jlink/jdk.tools.jimage
  30  * @run main/othervm JImageExtractTest
  31  */
  32 
  33 import java.io.IOException;
  34 import java.nio.file.Files;
  35 import java.nio.file.Path;
  36 import java.nio.file.Paths;
  37 import java.util.Arrays;
  38 import java.util.HashSet;
  39 import java.util.Set;
  40 import java.util.stream.Collectors;
  41 
  42 import static jdk.test.lib.Asserts.assertEquals;
  43 import static jdk.test.lib.Asserts.assertTrue;
  44 
  45 public class JImageExtractTest extends JImageCliTest {
  46     public void testExtract() throws IOException {
  47         jimage("extract", getImagePath())
  48                 .assertSuccess()
  49                 .resultChecker(r -> {
  50                     assertTrue(r.output.isEmpty(), "Output is not expected");
  51                 });
  52         verifyExplodedImage(Paths.get("."));
  53     }
  54 
  55     public void testExtractHelp() {
  56         for (String opt : Arrays.asList("-h", "--help")) {
  57             jimage("extract", "--help")
  58                     .assertSuccess()
  59                     .resultChecker(r -> {
  60                         // extract  -  descriptive text
  61                         assertMatches("\\s+extract\\s+-\\s+.*", r.output);
  62                     });
  63         }
  64     }
  65 
  66     public void testExtractToDir() throws IOException {
  67         Path tmp = Files.createTempDirectory(Paths.get("."), getClass().getName());
  68         jimage("extract", "--dir", tmp.toString(), getImagePath())
  69                 .assertSuccess()
  70                 .resultChecker(r -> {
  71                     assertTrue(r.output.isEmpty(), "Output is not expected");
  72                 });
  73         verifyExplodedImage(tmp);
  74     }
  75 
  76     public void testExtractNoImageSpecified() {
  77         jimage("extract", "")
  78                 .assertFailure()
  79                 .assertShowsError();
  80     }
  81 
  82     public void testExtractNotImage() throws IOException {
  83         Path tmp = Files.createTempFile(Paths.get("."), getClass().getName(), "not_an_image");
  84         jimage("extract", tmp.toString())
  85                 .assertFailure()
  86                 .assertShowsError();
  87     }
  88 
  89     public void testExtractNotExistingImage() throws IOException {
  90         Path tmp = Paths.get(".", "not_existing_image");
  91         Files.deleteIfExists(tmp);
  92         jimage("extract", tmp.toString())
  93                 .assertFailure()
  94                 .assertShowsError();
  95     }
  96 
  97     public void testExtractToUnspecifiedDir() {
  98         jimage("extract", "--dir", "--", getImagePath())
  99                 .assertFailure()
 100                 .assertShowsError();
 101     }
 102 
 103     private void verifyExplodedImage(Path imagePath) throws IOException {
 104         Set<Path> allModules = Files.walk(imagePath, 1).collect(Collectors.toSet());
 105         assertTrue(allModules.stream().anyMatch(p -> "java.base".equals(p.getFileName().toString())),
 106                 "Exploded image contains java.base module.");
 107 
 108         Set<Path> badModules = allModules.stream()
 109                 .filter(p -> !Files.exists(p.resolve("module-info.class")))
 110                 .collect(Collectors.toSet());
 111         assertEquals(badModules, new HashSet<Path>() {{ add(imagePath); }},
 112                 "There are no exploded modules with missing 'module-info.class'");
 113     }
 114 
 115     public static void main(String[] args) throws Throwable {
 116         new JImageExtractTest().runTests();
 117     }
 118 }
 119