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