1 /*
   2  * Copyright (c) 2014, 2016, 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/testlibrary
  27  * @modules java.desktop java.logging jdk.compiler
  28  * @build LimitModsTest CompilerUtils jdk.testlibrary.*
  29  * @run testng LimitModsTest
  30  * @summary Basic tests for java --limit-modules
  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 LimitModsTest {
  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 / main class of the test module
  52     private static final String TEST_MODULE = "test";
  53     private static final String MAIN_CLASS = "jdk.test.UseAWT";
  54 
  55 
  56     @BeforeTest
  57     public void compileTestModule() throws Exception {
  58 
  59         // javac -d mods/$TESTMODULE src/$TESTMODULE/**
  60         boolean compiled
  61             = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
  62                 MODS_DIR.resolve(TEST_MODULE));
  63 
  64         assertTrue(compiled, "test module did not compile");
  65     }
  66 
  67 
  68     /**
  69      * Basic test of --limit-modules to limit which platform modules are observable.
  70      */
  71     public void testLimitingPlatformModules() throws Exception {
  72         int exitValue;
  73 
  74         // java --limit-modules java.base --list-modules
  75         exitValue = executeTestJava("--limit-modules", "java.base", "--list-modules")
  76             .outputTo(System.out)
  77             .errorTo(System.out)
  78             .shouldContain("java.base")
  79             .shouldNotContain("java.logging")
  80             .shouldNotContain("java.xml")
  81             .getExitValue();
  82 
  83         assertTrue(exitValue == 0);
  84 
  85 
  86         // java --limit-modules java.logging --list-modules
  87         exitValue = executeTestJava("--limit-modules", "java.logging", "--list-modules")
  88             .outputTo(System.out)
  89             .errorTo(System.out)
  90             .shouldContain("java.base")
  91             .shouldContain("java.logging")
  92             .shouldNotContain("java.xml")
  93             .getExitValue();
  94 
  95         assertTrue(exitValue == 0);
  96     }
  97 
  98 
  99     /**
 100      * Test --limit-modules with --add-modules
 101      */
 102     public void testWithAddMods() throws Exception {
 103         int exitValue;
 104 
 105         // java --limit-modules java.base --add-modules java.logging --list-modules
 106         exitValue = executeTestJava("--limit-modules", "java.base",
 107                                     "--add-modules", "java.logging",
 108                                     "--list-modules")
 109             .outputTo(System.out)
 110             .errorTo(System.out)
 111             .shouldContain("java.base")
 112             .shouldContain("java.logging")
 113             .shouldNotContain("java.xml")
 114             .getExitValue();
 115 
 116         assertTrue(exitValue == 0);
 117 
 118 
 119         // java --limit-modules java.base --add-modules java.sql --list-modules
 120         // This should fail because java.sql has dependences beyond java.base
 121         exitValue = executeTestJava("--limit-modules", "java.base",
 122                                     "--add-modules", "java.sql",
 123                                     "--list-modules")
 124             .outputTo(System.out)
 125             .errorTo(System.out)
 126             .getExitValue();
 127 
 128         assertTrue(exitValue != 0);
 129     }
 130 
 131 
 132     /**
 133      * Run class path application with --limit-modules
 134      */
 135     public void testUnnamedModule() throws Exception {
 136         String classpath = MODS_DIR.resolve(TEST_MODULE).toString();
 137 
 138         // java --limit-modules java.base -cp mods/$TESTMODULE ...
 139         int exitValue1
 140             = executeTestJava("--limit-modules", "java.base",
 141                               "-cp", classpath,
 142                               MAIN_CLASS)
 143                 .outputTo(System.out)
 144                 .errorTo(System.out)
 145                 .shouldContain("NoClassDefFoundError")
 146                 .getExitValue();
 147 
 148         assertTrue(exitValue1 != 0);
 149 
 150 
 151         // java --limit-modules java.base -cp mods/$TESTMODULE ...
 152         int exitValue2
 153             = executeTestJava("--limit-modules", "java.desktop",
 154                               "-cp", classpath,
 155                              MAIN_CLASS)
 156                 .outputTo(System.out)
 157                 .errorTo(System.out)
 158                 .getExitValue();
 159 
 160         assertTrue(exitValue2 == 0);
 161     }
 162 
 163 
 164     /**
 165      * Run named module with --limit-modules
 166      */
 167     public void testNamedModule() throws Exception {
 168 
 169         String modulepath = MODS_DIR.toString();
 170         String mid = TEST_MODULE + "/" + MAIN_CLASS;
 171 
 172         // java --limit-modules java.base --module-path mods -m $TESTMODULE/$MAINCLASS
 173         int exitValue = executeTestJava("--limit-modules", "java.base",
 174                                         "--module-path", modulepath,
 175                                         "-m", mid)
 176                 .outputTo(System.out)
 177                 .errorTo(System.out)
 178                 .getExitValue();
 179 
 180         assertTrue(exitValue != 0);
 181 
 182         // java --limit-modules java.desktop --module-path mods -m $TESTMODULE/$MAINCLASS
 183         exitValue = executeTestJava("--limit-modules", "java.desktop",
 184                                     "--module-path", modulepath,
 185                                     "-m", mid)
 186                 .outputTo(System.out)
 187                 .errorTo(System.out)
 188                 .getExitValue();
 189 
 190         assertTrue(exitValue == 0);
 191     }
 192 
 193 }