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