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.jlink/jdk.tools.jmod
  28  * @build AddModsTest CompilerUtils jdk.testlibrary.*
  29  * @run testng AddModsTest
  30  * @summary Basic test for java -addmods
  31  */
  32 
  33 import java.nio.file.Path;
  34 import java.nio.file.Paths;
  35 
  36 import static jdk.testlibrary.ProcessTools.*;
  37 
  38 import org.testng.annotations.BeforeTest;
  39 import org.testng.annotations.Test;
  40 import static org.testng.Assert.*;
  41 
  42 
  43 @Test
  44 public class AddModsTest {
  45 
  46     private static final String TEST_SRC = System.getProperty("test.src");
  47 
  48     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  49     private static final Path MODS_DIR = Paths.get("mods");
  50 
  51     // the module name of the library module
  52     private static final String LIB_MODULE = "lib";
  53 
  54     // application source directory
  55     private static final String APP_SRC = "app";
  56 
  57     // application is compiled to classes
  58     private static final Path CLASSES_DIR = Paths.get("classes");
  59 
  60     // application main class
  61     private static final String MAIN_CLASS = "app.Main";
  62 
  63 
  64     @BeforeTest
  65     public void compile() throws Exception {
  66 
  67         // javac -d mods/$LIB_MODULE src/$LIB_MODULE/**
  68         boolean compiled = CompilerUtils.compile(
  69             SRC_DIR.resolve(LIB_MODULE),
  70             MODS_DIR.resolve(LIB_MODULE)
  71         );
  72         assertTrue(compiled, "library module did not compile");
  73 
  74         // javac -d classes -mp mods src/$APP_DIR/**
  75         compiled = CompilerUtils.compile(
  76             SRC_DIR.resolve(APP_SRC),
  77             CLASSES_DIR,
  78             "-mp", MODS_DIR.toString()
  79         );
  80         assertTrue(compiled, "app did not compile");
  81     }
  82 
  83 
  84     /**
  85      * Basic test of -addmods, using the output of -listmods to check that the
  86      * module is resolved.
  87      */
  88     public void testAddOneModule() throws Exception {
  89 
  90         int exitValue
  91             = executeTestJava("-limitmods", "java.base",
  92                               "-addmods", "java.sql",
  93                               "-listmods")
  94                 .outputTo(System.out)
  95                 .errorTo(System.out)
  96                 .shouldContain("java.sql")
  97                 .shouldNotContain("java.corba")
  98                 .getExitValue();
  99 
 100         assertTrue(exitValue == 0);
 101     }
 102 
 103 
 104     /**
 105      * Basic test of -addmods, using the output of -listmods to check that the
 106      * module is resolved.
 107      */
 108     public void testAddTwoModules() throws Exception {
 109 
 110         int exitValue
 111             = executeTestJava("-limitmods", "java.base",
 112                               "-addmods", "java.sql,java.naming",
 113                               "-listmods")
 114                 .outputTo(System.out)
 115                 .errorTo(System.out)
 116                 .shouldContain("java.sql")
 117                 .shouldContain("java.naming")
 118                 .shouldNotContain("java.corba")
 119                 .getExitValue();
 120 
 121         assertTrue(exitValue == 0);
 122     }
 123 
 124 
 125     /**
 126      * Basic test of -addmods ALL-SYSTEM, using the output of -listmods to
 127      * check that the a sample of the system modules are resolved
 128      */
 129     public void testAddSystemModules() throws Exception {
 130 
 131         executeTestJava("-addmods", "ALL-SYSTEM",
 132                         "-listmods",
 133                         "-m", "java.base")
 134             .outputTo(System.out)
 135             .errorTo(System.out)
 136             .shouldContain("java.sql")
 137             .shouldContain("java.corba");
 138 
 139         // no exit value to check as -m java.base will likely fail
 140     }
 141 
 142 
 143     /**
 144      * Run application on class path that makes use of module on the
 145      * application module path. Uses -addmods.
 146      */
 147     public void testRunWithAddMods() throws Exception {
 148 
 149         // java -mp mods -addmods lib -cp classes app.Main
 150         int exitValue
 151             = executeTestJava("-mp", MODS_DIR.toString(),
 152                               "-addmods", LIB_MODULE,
 153                               "-cp", CLASSES_DIR.toString(),
 154                               MAIN_CLASS)
 155                 .outputTo(System.out)
 156                 .errorTo(System.out)
 157                 .getExitValue();
 158 
 159         assertTrue(exitValue == 0);
 160 
 161     }
 162 
 163 
 164     /**
 165      * Run application on class path that makes use of module on the
 166      * application module path. Does not use -addmods and so will
 167      * fail at run-time.
 168      */
 169     public void testRunMissingAddMods() throws Exception {
 170 
 171         // java -mp mods -cp classes app.Main
 172         int exitValue
 173             = executeTestJava("-mp", MODS_DIR.toString(),
 174                               "-cp", CLASSES_DIR.toString(),
 175                               MAIN_CLASS)
 176                 .outputTo(System.out)
 177                 .errorTo(System.out)
 178                 .getExitValue();
 179 
 180         // CNFE or other error/exception
 181         assertTrue(exitValue != 0);
 182 
 183     }
 184 
 185 
 186     /**
 187      * Attempt to run with a bad module name specified to -addmods
 188      */
 189     public void testRunWithBadAddMods() throws Exception {
 190 
 191         // java -mp mods -addmods,DoesNotExist lib -cp classes app.Main
 192         int exitValue
 193             = executeTestJava("-mp", MODS_DIR.toString(),
 194                               "-addmods", LIB_MODULE + ",DoesNotExist",
 195                               "-cp", CLASSES_DIR.toString(),
 196                               MAIN_CLASS)
 197                 .outputTo(System.out)
 198                 .errorTo(System.out)
 199                 .getExitValue();
 200 
 201         assertTrue(exitValue != 0);
 202 
 203     }
 204 
 205 }