AddModulesTest.java revision 3999:ae88ea1b7649
1/* 2 * Copyright (c) 2015, 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 * @bug 8167975 8173596 27 * @summary Test the --add-modules option 28 * @library /tools/lib 29 * @modules jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.main 31 * @build toolbox.Assert toolbox.ToolBox toolbox.JavacTask ModuleTestBase 32 * @run main AddModulesTest 33 */ 34 35 36import java.nio.file.Path; 37import java.util.Arrays; 38 39import javax.tools.JavaCompiler; 40import javax.tools.JavaCompiler.CompilationTask; 41import javax.tools.JavaFileObject; 42import javax.tools.StandardJavaFileManager; 43import javax.tools.StandardLocation; 44import javax.tools.ToolProvider; 45 46import toolbox.Assert; 47import toolbox.JavacTask; 48import toolbox.Task; 49 50public class AddModulesTest extends ModuleTestBase { 51 public static void main(String... args) throws Exception { 52 new AddModulesTest().runTests(); 53 } 54 55 @Test 56 public void testEmpty(Path base) throws Exception { 57 Path src = base.resolve("src"); 58 tb.writeJavaFiles(src, "class Dummy { }"); 59 Path classes = base.resolve("classes"); 60 tb.createDirectories(classes); 61 62 testEmpty(src, classes, "--add-modules", ""); 63 testEmpty(src, classes, "--add-modules="); 64 } 65 66 private void testEmpty(Path src, Path classes, String... options) throws Exception { 67 String log = new JavacTask(tb, Task.Mode.CMDLINE) 68 .options(options) 69 .outdir(classes) 70 .files(findJavaFiles(src)) 71 .run(Task.Expect.FAIL) 72 .writeAll() 73 .getOutput(Task.OutputKind.DIRECT); 74 75 checkOutputContains(log, 76 "javac: no value for --add-modules option"); 77 } 78 79 @Test 80 public void testEmptyItem(Path base) throws Exception { 81 Path src = base.resolve("src"); 82 Path src_m1 = src.resolve("m1x"); 83 tb.writeJavaFiles(src_m1, 84 "module m1x { }"); 85 Path src_m2 = src.resolve("m2x"); 86 tb.writeJavaFiles(src_m2, 87 "module m2x { }"); 88 Path classes = base.resolve("classes"); 89 tb.createDirectories(classes); 90 91 testEmptyItem(src, classes, ",m1x"); 92 testEmptyItem(src, classes, "m1x,,m2x"); 93 testEmptyItem(src, classes, "m1x,"); 94 } 95 96 private void testEmptyItem(Path src, Path classes, String option) throws Exception { 97 new JavacTask(tb) 98 .options("--module-source-path", src.toString(), 99 "--add-modules", option) 100 .outdir(classes) 101 .files(findJavaFiles(src)) 102 .run() 103 .writeAll(); 104 } 105 106 @Test 107 public void testEmptyList(Path base) throws Exception { 108 Path src = base.resolve("src"); 109 tb.writeJavaFiles(src, "class Dummy { }"); 110 Path classes = base.resolve("classes"); 111 tb.createDirectories(classes); 112 113 String log = new JavacTask(tb, Task.Mode.CMDLINE) 114 .options("--module-source-path", src.toString(), 115 "--add-modules", ",") 116 .outdir(classes) 117 .files(findJavaFiles(src)) 118 .run(Task.Expect.FAIL) 119 .writeAll() 120 .getOutput(Task.OutputKind.DIRECT); 121 122 checkOutputContains(log, 123 "javac: bad value for --add-modules option"); 124 } 125 126 @Test 127 public void testInvalidName(Path base) throws Exception { 128 Path src = base.resolve("src"); 129 tb.writeJavaFiles(src, "class Dummy { }"); 130 Path classes = base.resolve("classes"); 131 tb.createDirectories(classes); 132 133 String log = new JavacTask(tb) 134 .options("-XDrawDiagnostics", 135 "--add-modules", "BadModule!") 136 .outdir(classes) 137 .files(findJavaFiles(src)) 138 .run(Task.Expect.FAIL) 139 .writeAll() 140 .getOutput(Task.OutputKind.DIRECT); 141 142 checkOutputContains(log, 143 "- compiler.err.bad.name.for.option: --add-modules, BadModule!"); 144 } 145 146 @Test 147 public void testUnknownName(Path base) throws Exception { 148 Path src = base.resolve("src"); 149 tb.writeJavaFiles(src, "class Dummy { }"); 150 Path classes = base.resolve("classes"); 151 tb.createDirectories(classes); 152 153 String log = new JavacTask(tb) 154 .options("-XDrawDiagnostics", 155 "--add-modules", "DoesNotExist") 156 .outdir(classes) 157 .files(findJavaFiles(src)) 158 .run(Task.Expect.FAIL) 159 .writeAll() 160 .getOutput(Task.OutputKind.DIRECT); 161 162 checkOutputContains(log, 163 "- compiler.err.module.not.found: DoesNotExist"); 164 } 165 166 @Test 167 public void testDuplicate(Path base) throws Exception { 168 Path src = base.resolve("src"); 169 170 // setup a utility module 171 Path src_m1 = src.resolve("m1x"); 172 tb.writeJavaFiles(src_m1, 173 "module m1x { exports p1; }", 174 "package p1; public class C1 { }"); 175 Path modules = base.resolve("modules"); 176 tb.createDirectories(modules); 177 178 new JavacTask(tb) 179 .options("--module-source-path", src.toString()) 180 .outdir(modules) 181 .files(findJavaFiles(src)) 182 .run() 183 .writeAll(); 184 185 // now test access to the module 186 Path src2 = base.resolve("src2"); 187 tb.writeJavaFiles(src2, 188 "class Dummy { p1.C1 c1; }"); 189 Path classes = base.resolve("classes"); 190 tb.createDirectories(classes); 191 192 new JavacTask(tb) 193 .options("--module-path", modules.toString(), 194 "--add-modules", "m1x,m1x") 195 .outdir(classes) 196 .files(findJavaFiles(src2)) 197 .run() 198 .writeAll(); 199 } 200 201 @Test 202 public void testRepeatable(Path base) throws Exception { 203 Path src = base.resolve("src"); 204 205 // setup some utility modules 206 Path src_m1 = src.resolve("m1x"); 207 tb.writeJavaFiles(src_m1, 208 "module m1x { exports p1; }", 209 "package p1; public class C1 { }"); 210 Path src_m2 = src.resolve("m2x"); 211 tb.writeJavaFiles(src_m2, 212 "module m2x { exports p2; }", 213 "package p2; public class C2 { }"); 214 Path modules = base.resolve("modules"); 215 tb.createDirectories(modules); 216 217 new JavacTask(tb) 218 .options("--module-source-path", src.toString()) 219 .outdir(modules) 220 .files(findJavaFiles(src)) 221 .run() 222 .writeAll(); 223 224 // now test access to the modules 225 Path src2 = base.resolve("src2"); 226 tb.writeJavaFiles(src2, 227 "class Dummy { p1.C1 c1; p2.C2 c2; }"); 228 Path classes = base.resolve("classes"); 229 tb.createDirectories(classes); 230 231 new JavacTask(tb) 232 .options("--module-path", modules.toString(), 233 "--add-modules", "m1x", 234 "--add-modules", "m2x") 235 .outdir(classes) 236 .files(findJavaFiles(src2)) 237 .run() 238 .writeAll(); 239 } 240 241 @Test 242 public void testAddModulesAPI(Path base) throws Exception { 243 Path src = base.resolve("src"); 244 245 // setup some utility modules 246 Path src_m1 = src.resolve("m1x"); 247 tb.writeJavaFiles(src_m1, 248 "module m1x { exports p1; }", 249 "package p1; public class C1 { }"); 250 Path src_m2 = src.resolve("m2x"); 251 tb.writeJavaFiles(src_m2, 252 "module m2x { exports p2; }", 253 "package p2; public class C2 { }"); 254 Path modules = base.resolve("modules"); 255 tb.createDirectories(modules); 256 257 new JavacTask(tb) 258 .options("--module-source-path", src.toString()) 259 .outdir(modules) 260 .files(findJavaFiles(src)) 261 .run() 262 .writeAll(); 263 264 // now test access to the modules 265 Path src2 = base.resolve("src2"); 266 tb.writeJavaFiles(src2, 267 "class Dummy { p1.C1 c1; p2.C2 c2; }"); 268 Path classes = base.resolve("classes"); 269 tb.createDirectories(classes); 270 271 JavaCompiler c = ToolProvider.getSystemJavaCompiler(); 272 try (StandardJavaFileManager fm = c.getStandardFileManager(null, null, null)) { 273 fm.setLocationFromPaths(StandardLocation.MODULE_PATH, Arrays.asList(modules)); 274 fm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Arrays.asList(classes)); 275 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(src2)); 276 CompilationTask t = c.getTask(null, fm, null, null, null, files); 277 t.addModules(Arrays.asList("m1x", "m2x")); 278 Assert.check(t.call()); 279 } 280 } 281} 282 283