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.io.IOException;
  28 import java.nio.file.Path;
  29 import java.util.Collection;
  30 import java.util.List;
  31 import java.util.Objects;
  32 import java.util.function.Function;
  33 import java.util.stream.Collectors;
  34 import java.util.stream.Stream;
  35 import jdk.jpackage.test.Annotations.Parameters;
  36 import jdk.jpackage.test.Annotations.Test;
  37 import jdk.jpackage.test.JPackageCommand;
  38 import jdk.jpackage.test.TKit;
  39 
  40 
  41 /*
  42  * @test
  43  * @summary jpackage with --module-path testing
  44  * @library ../../../../helpers
  45  * @build jdk.jpackage.test.*
  46  * @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
  47  * @compile ModulePathTest.java
  48  * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
  49  *  --jpt-run=jdk.jpackage.tests.ModulePathTest
  50  */
  51 
  52 public final class ModulePathTest {
  53 
  54     @Parameters
  55     public static Collection data() {
  56         return List.of(new String[][]{
  57             {GOOD_PATH, EMPTY_DIR, NON_EXISTING_DIR},
  58             {EMPTY_DIR, NON_EXISTING_DIR, GOOD_PATH},
  59             {GOOD_PATH + "/a/b/c/d", GOOD_PATH},
  60             {String.join(File.pathSeparator, EMPTY_DIR, NON_EXISTING_DIR,
  61                 GOOD_PATH)},
  62             {String.join(File.pathSeparator, EMPTY_DIR, NON_EXISTING_DIR),
  63                 String.join(File.pathSeparator, EMPTY_DIR, NON_EXISTING_DIR,
  64                 GOOD_PATH)},
  65             {},
  66             {EMPTY_DIR}
  67         });
  68     }
  69 
  70     public ModulePathTest(String... modulePathArgs) {
  71         this.modulePathArgs = List.of(modulePathArgs);
  72     }
  73 
  74     @Test
  75     public void test() throws IOException {
  76         final String moduleName = "com.foo";
  77         JPackageCommand cmd = JPackageCommand.helloAppImage(
  78                 "benvenuto.jar:" + moduleName + "/com.foo.Hello");
  79         // Build app jar file.
  80         cmd.executePrerequisiteActions();
  81 
  82         // Ignore runtime that can be set for all tests. Usually if default
  83         // runtime is set, it is fake one to save time on running jlink and
  84         // copying megabytes of data from Java home to application image.
  85         // We need proper runtime for this test.
  86         cmd.ignoreDefaultRuntime(true);
  87 
  88         // --module-path should be set in JPackageCommand.helloAppImage call
  89         String goodModulePath = Objects.requireNonNull(cmd.getArgumentValue(
  90                 "--module-path"));
  91         cmd.removeArgumentWithValue("--module-path");
  92 
  93         Path emptyDir = TKit.createTempDirectory("empty-dir");
  94         Path nonExistingDir = TKit.withTempDirectory("non-existing-dir", x -> {});
  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(1)
 125                     .getOutput();
 126             TKit.assertTextStream(expectedErrorMessage).apply(output.stream());
 127         }
 128     }
 129 
 130     private final List<String> modulePathArgs;
 131 
 132     private final static String GOOD_PATH = "@GoodPath@";
 133     private final static String EMPTY_DIR = "@EmptyDir@";
 134     private final static String NON_EXISTING_DIR = "@NonExistingDir@";
 135 }