1 /*
   2  * Copyright (c) 2019, 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 package jdk.jpackage.tests;
  25 
  26 import java.io.File;
  27 import java.nio.file.Path;
  28 import java.util.Collection;
  29 import java.util.List;
  30 import java.util.Objects;
  31 import java.util.function.Function;
  32 import java.util.stream.Collectors;
  33 import java.util.stream.Stream;
  34 import jdk.jpackage.test.Annotations.Parameters;
  35 import jdk.jpackage.test.Annotations.Test;
  36 import jdk.jpackage.test.JPackageCommand;
  37 import jdk.jpackage.test.TKit;
  38 
  39 
  40 /*
  41  * @test
  42  * @summary jpackage with --module-path testing
  43  * @library ../../../../helpers
  44  * @build jdk.jpackage.test.*
  45  * @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
  46  * @compile ModulePathTest.java
  47  * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
  48  *  --jpt-run=jdk.jpackage.tests.ModulePathTest
  49  */
  50 
  51 public final class ModulePathTest {
  52 
  53     @Parameters
  54     public static Collection data() {
  55         return List.of(new String[][]{
  56             {GOOD_PATH, EMPTY_DIR, NON_EXISTING_DIR},
  57             {EMPTY_DIR, NON_EXISTING_DIR, GOOD_PATH},
  58             {GOOD_PATH + "/a/b/c/d", GOOD_PATH},
  59             {String.join(File.pathSeparator, EMPTY_DIR, NON_EXISTING_DIR,
  60                 GOOD_PATH)},
  61             {String.join(File.pathSeparator, EMPTY_DIR, NON_EXISTING_DIR),
  62                 String.join(File.pathSeparator, EMPTY_DIR, NON_EXISTING_DIR,
  63                 GOOD_PATH)},
  64             {},
  65             {EMPTY_DIR}
  66         });
  67     }
  68 
  69     public ModulePathTest(String... modulePathArgs) {
  70         this.modulePathArgs = List.of(modulePathArgs);
  71     }
  72 
  73     @Test
  74     public void test() {
  75         final String moduleName = "com.foo";
  76         JPackageCommand cmd = JPackageCommand.helloAppImage(
  77                 "benvenuto.jar:" + moduleName + "/com.foo.Hello");
  78         // Build app jar file.
  79         cmd.executePrerequisiteActions();
  80 
  81         // Ignore runtime that can be set for all tests. Usually if default
  82         // runtime is set, it is fake one to save time on running jlink and
  83         // copying megabytes of data from Java home to application image.
  84         // We need proper runtime for this test.
  85         cmd.ignoreDefaultRuntime(true);
  86 
  87         // --module-path should be set in JPackageCommand.helloAppImage call
  88         String goodModulePath = Objects.requireNonNull(cmd.getArgumentValue(
  89                 "--module-path"));
  90         cmd.removeArgumentWithValue("--module-path");
  91         TKit.withTempDirectory("empty-dir", emptyDir -> {
  92             Path nonExistingDir = TKit.withTempDirectory("non-existing-dir",
  93                     unused -> {
  94                     });
  95 
  96             Function<String, String> substitute = str -> {
  97                 String v = str;
  98                 v = v.replace(GOOD_PATH, goodModulePath);
  99                 v = v.replace(EMPTY_DIR, emptyDir.toString());
 100                 v = v.replace(NON_EXISTING_DIR, nonExistingDir.toString());
 101                 return v;
 102             };
 103 
 104             boolean withGoodPath = modulePathArgs.stream().anyMatch(
 105                     s -> s.contains(GOOD_PATH));
 106 
 107             cmd.addArguments(modulePathArgs.stream().map(arg -> Stream.of(
 108                     "--module-path", substitute.apply(arg))).flatMap(s -> s).collect(
 109                     Collectors.toList()));
 110 
 111             if (withGoodPath) {
 112                 cmd.executeAndAssertHelloAppImageCreated();
 113             } else {
 114                 final String expectedErrorMessage;
 115                 if (modulePathArgs.isEmpty()) {
 116                     expectedErrorMessage = "Error: Missing argument: --runtime-image or --module-path";
 117                 } else {
 118                     expectedErrorMessage = String.format(
 119                             "Error: Module %s not found", moduleName);
 120                 }
 121 
 122                 List<String> output = cmd
 123                         .saveConsoleOutput(true)
 124                         .execute()
 125                         .assertExitCodeIs(1)
 126                         .getOutput();
 127                 TKit.assertTextStream(expectedErrorMessage).apply(output.stream());
 128             }
 129         });
 130     }
 131 
 132     private final List<String> modulePathArgs;
 133 
 134     private final static String GOOD_PATH = "@GoodPath@";
 135     private final static String EMPTY_DIR = "@EmptyDir@";
 136     private final static String NON_EXISTING_DIR = "@NonExistingDir@";
 137 }