MissingModuleTest.java revision 3944:7bb9b0d10b22
1/* 2 * Copyright (c) 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 8172240 27 * @summary javac should not need the transitive closure to compile a module 28 * @library /tools/lib 29 * @modules 30 * jdk.compiler/com.sun.tools.javac.api 31 * jdk.compiler/com.sun.tools.javac.code 32 * jdk.compiler/com.sun.tools.javac.main 33 * jdk.compiler/com.sun.tools.javac.processing 34 * @build toolbox.ToolBox toolbox.JavacTask toolbox.ModuleBuilder ModuleTestBase 35 * @run main MissingModuleTest 36 */ 37 38import java.nio.file.Files; 39import java.nio.file.Path; 40import java.util.Arrays; 41import java.util.List; 42 43import toolbox.JavacTask; 44import toolbox.Task; 45import toolbox.Task.Expect; 46 47public class MissingModuleTest extends ModuleTestBase { 48 49 public static void main(String... args) throws Exception { 50 new MissingModuleTest().runTests(); 51 } 52 53 @Test 54 public void testMissingNotNeeded(Path base) throws Exception { 55 doTest(base, "m1x", new String[0]); 56 } 57 58 @Test 59 public void testMissingNeededTransitive(Path base) throws Exception { 60 doTest(base, "m2x", "- compiler.err.module.not.found: m2x", "1 error"); 61 } 62 63 @Test 64 public void testMissingNeededDirect(Path base) throws Exception { 65 doTest(base, "m3x", "module-info.java:1:24: compiler.err.module.not.found: m3x", "1 error"); 66 } 67 68 @Test 69 public void testMultipleErrors(Path base) throws Exception { 70 doTest(base, "m4x", "module-info.java:1:38: compiler.err.module.not.found: m4x", "1 error"); 71 } 72 73 private void doTest(Path base, String toDelete, String... errors) throws Exception { 74 //note: avoiding use of java.base, as that gets special handling on some places: 75 Path srcMP = base.resolve("src-mp"); 76 Path m1x = srcMP.resolve("m1x"); 77 tb.writeJavaFiles(m1x, 78 "module m1x { exports api1; }", 79 "package api1; public interface Api { }"); 80 Path m2x = srcMP.resolve("m2x"); 81 tb.writeJavaFiles(m2x, 82 "module m2x { requires m1x; exports api2; }", 83 "package api2; public interface Api { }"); 84 Path m3x = srcMP.resolve("m3x"); 85 tb.writeJavaFiles(m3x, 86 "module m3x { requires transitive m2x; }"); 87 Path m4x = srcMP.resolve("m4x"); 88 tb.writeJavaFiles(m4x, 89 "module m4x { requires transitive m2x; }"); 90 Path m5x = srcMP.resolve("m5x"); 91 tb.writeJavaFiles(m5x, 92 "module m5x { requires transitive m4x; }"); 93 Path classesMP = base.resolve("classes-mp"); 94 tb.createDirectories(classesMP); 95 96 new JavacTask(tb) 97 .options("--module-source-path", srcMP.toString()) 98 .outdir(classesMP) 99 .files(findJavaFiles(srcMP)) 100 .run() 101 .writeAll(); 102 103 tb.cleanDirectory(classesMP.resolve(toDelete)); 104 Files.delete(classesMP.resolve(toDelete)); 105 106 Path src = base.resolve("src"); 107 tb.writeJavaFiles(src, 108 "module test { requires m3x; requires m4x; requires m5x; } "); 109 Path classes = base.resolve("classes"); 110 tb.createDirectories(classes); 111 112 List<String> log = new JavacTask(tb) 113 .options("--module-path", classesMP.toString(), 114 "-XDrawDiagnostics") 115 .outdir(classes) 116 .files(findJavaFiles(src)) 117 .run(errors.length > 0 ? Expect.FAIL : Expect.SUCCESS) 118 .writeAll() 119 .getOutputLines(Task.OutputKind.DIRECT); 120 121 if (errors.length == 0) { 122 errors = new String[] {""}; 123 } 124 125 if (!log.equals(Arrays.asList(errors))) 126 throw new Exception("expected output not found: " + log); 127 } 128 129} 130