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