1 /*
   2  * Copyright (c) 2014, 2017, 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  * @library /test/lib
  27  * @modules jdk.compiler
  28  *          jdk.jartool
  29  *          jdk.jlink
  30  * @build BasicTest jdk.test.lib.compiler.CompilerUtils
  31  * @run testng BasicTest
  32  * @summary Basic test of starting an application as a module
  33  */
  34 
  35 import java.io.File;
  36 import java.nio.file.Files;
  37 import java.nio.file.Path;
  38 import java.nio.file.Paths;
  39 import java.util.spi.ToolProvider;
  40 
  41 import jdk.test.lib.compiler.CompilerUtils;
  42 import jdk.test.lib.process.ProcessTools;
  43 
  44 import org.testng.annotations.BeforeTest;
  45 import org.testng.annotations.Test;
  46 import static org.testng.Assert.*;
  47 
  48 
  49 @Test
  50 public class BasicTest {
  51     private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
  52         .orElseThrow(() ->
  53             new RuntimeException("jar tool not found")
  54         );
  55     private static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
  56         .orElseThrow(() ->
  57             new RuntimeException("jmod tool not found")
  58         );
  59 
  60     private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
  61 
  62     private static final String TEST_SRC = System.getProperty("test.src");
  63 
  64     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  65     private static final Path MODS_DIR = Paths.get("mods");
  66 
  67     // the module name of the test module
  68     private static final String TEST_MODULE = "test";
  69 
  70     // the module main class
  71     private static final String MAIN_CLASS = "jdk.test.Main";
  72 
  73 
  74     @BeforeTest
  75     public void compileTestModule() throws Exception {
  76 
  77         // javac -d mods/$TESTMODULE src/$TESTMODULE/**
  78         boolean compiled
  79             = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
  80                                     MODS_DIR.resolve(TEST_MODULE));
  81 
  82         assertTrue(compiled, "test module did not compile");
  83     }
  84 
  85     /**
  86      * Execute "java" with the given arguments, returning the exit code.
  87      */
  88     private int exec(String... args) throws Exception {
  89        return ProcessTools.executeTestJava(args)
  90                 .outputTo(System.out)
  91                 .errorTo(System.out)
  92                 .getExitValue();
  93     }
  94 
  95 
  96     /**
  97      * The initial module is loaded from an exploded module
  98      */
  99     public void testRunWithExplodedModule() throws Exception {
 100         String dir = MODS_DIR.toString();
 101         String subdir = MODS_DIR.resolve(TEST_MODULE).toString();
 102         String mid = TEST_MODULE + "/" + MAIN_CLASS;
 103 
 104         // java --module-path mods -module $TESTMODULE/$MAINCLASS
 105         int exitValue = exec("--module-path", dir, "--module", mid);
 106         assertTrue(exitValue == 0);
 107 
 108         // java --module-path mods/$TESTMODULE --module $TESTMODULE/$MAINCLASS
 109         exitValue = exec("--module-path", subdir, "--module", mid);
 110         assertTrue(exitValue == 0);
 111 
 112         // java --module-path=mods --module=$TESTMODULE/$MAINCLASS
 113         exitValue = exec("--module-path=" + dir, "--module=" + mid);
 114         assertTrue(exitValue == 0);
 115 
 116         // java --module-path=mods/$TESTMODULE --module=$TESTMODULE/$MAINCLASS
 117         exitValue = exec("--module-path=" + subdir, "--module=" + mid);
 118         assertTrue(exitValue == 0);
 119 
 120         // java -p mods -m $TESTMODULE/$MAINCLASS
 121         exitValue = exec("-p", dir, "-m", mid);
 122         assertTrue(exitValue == 0);
 123 
 124         // java -p mods/$TESTMODULE -m $TESTMODULE/$MAINCLASS
 125         exitValue = exec("-p", subdir, "-m", mid);
 126         assertTrue(exitValue == 0);
 127     }
 128 
 129 
 130     /**
 131      * The initial module is loaded from a modular JAR file
 132      */
 133     public void testRunWithModularJar() throws Exception {
 134         Path dir = Files.createTempDirectory(USER_DIR, "mlib");
 135         Path jar = dir.resolve("m.jar");
 136 
 137         // jar --create ...
 138         String classes = MODS_DIR.resolve(TEST_MODULE).toString();
 139         String[] args = {
 140             "--create",
 141             "--file=" + jar,
 142             "--main-class=" + MAIN_CLASS,
 143             "-C", classes, "."
 144         };
 145         int rc = JAR_TOOL.run(System.out, System.out, args);
 146         assertTrue(rc == 0);
 147 
 148         // java --module-path mlib -module $TESTMODULE
 149         int exitValue = exec("--module-path", dir.toString(),
 150                              "--module", TEST_MODULE);
 151         assertTrue(exitValue == 0);
 152 
 153         // java --module-path mlib/m.jar -module $TESTMODULE
 154         exitValue = exec("--module-path", jar.toString(),
 155                          "--module", TEST_MODULE);
 156         assertTrue(exitValue == 0);
 157     }
 158 
 159 
 160     /**
 161      * Attempt to run with the initial module packaged as a JMOD file.
 162      */
 163     public void testTryRunWithJMod() throws Exception {
 164         Path dir = Files.createTempDirectory(USER_DIR, "mlib");
 165 
 166         // jmod create ...
 167         String cp = MODS_DIR.resolve(TEST_MODULE).toString();
 168         String jmod = dir.resolve("m.jmod").toString();
 169         String[] args = {
 170             "create",
 171             "--class-path", cp,
 172             "--main-class", MAIN_CLASS,
 173             jmod
 174         };
 175 
 176         assertEquals(JMOD_TOOL.run(System.out, System.out, args), 0);
 177 
 178         // java --module-path mods --module $TESTMODULE
 179         int exitValue = exec("--module-path", dir.toString(),
 180                              "--module", TEST_MODULE);
 181         assertTrue(exitValue != 0);
 182     }
 183 
 184 
 185     /**
 186      * Run the test with a non-existent file on the application module path.
 187      * It should be silently ignored.
 188      */
 189     public void testRunWithNonExistentEntry() throws Exception {
 190         String mp = "DoesNotExist" + File.pathSeparator + MODS_DIR.toString();
 191         String mid = TEST_MODULE + "/" + MAIN_CLASS;
 192 
 193         // java --module-path mods --module $TESTMODULE/$MAINCLASS
 194         int exitValue = exec("--module-path", mp, "--module", mid);
 195         assertTrue(exitValue == 0);
 196     }
 197 
 198 
 199     /**
 200      * Attempt to run an unknown initial module
 201      */
 202     public void testTryRunWithBadModule() throws Exception {
 203         String modulepath = MODS_DIR.toString();
 204 
 205         // java --module-path mods -m $TESTMODULE
 206         int exitValue = exec("--module-path", modulepath, "-m", "rhubarb");
 207         assertTrue(exitValue != 0);
 208     }
 209 
 210 
 211     /**
 212      * Attempt to run with -m specifying a main class that does not
 213      * exist.
 214      */
 215     public void testTryRunWithBadMainClass() throws Exception {
 216         String modulepath = MODS_DIR.toString();
 217         String mid = TEST_MODULE + "/p.rhubarb";
 218 
 219         // java --module-path mods -m $TESTMODULE/$MAINCLASS
 220         int exitValue = exec("--module-path", modulepath, "-m", mid);
 221         assertTrue(exitValue != 0);
 222     }
 223 
 224 
 225     /**
 226      * Attempt to run with -m specifying a modular JAR that does not have
 227      * a MainClass attribute
 228      */
 229     public void testTryRunWithMissingMainClass() throws Exception {
 230         Path dir = Files.createTempDirectory(USER_DIR, "mlib");
 231 
 232         // jar --create ...
 233         String classes = MODS_DIR.resolve(TEST_MODULE).toString();
 234         String jar = dir.resolve("m.jar").toString();
 235         String[] args = {
 236             "--create",
 237             "--file=" + jar,
 238             "-C", classes, "."
 239         };
 240         int rc = JAR_TOOL.run(System.out, System.out, args);
 241         assertTrue(rc == 0);
 242 
 243         // java --module-path mods -m $TESTMODULE
 244         int exitValue = exec("--module-path", dir.toString(), "-m", TEST_MODULE);
 245         assertTrue(exitValue != 0);
 246     }
 247 
 248 
 249     /**
 250      * Attempt to run with -m specifying a main class that is a different
 251      * module to that specified to -m
 252      */
 253     public void testTryRunWithMainClassInWrongModule() throws Exception {
 254         String modulepath = MODS_DIR.toString();
 255         String mid = "java.base/" + MAIN_CLASS;
 256 
 257         // java --module-path mods --module $TESTMODULE/$MAINCLASS
 258         int exitValue = exec("--module-path", modulepath, "--module", mid);
 259         assertTrue(exitValue != 0);
 260     }
 261 
 262 }