1/* 2 * Copyright (c) 2015, 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 * @summary tests for "requires transitive" 27 * @library /tools/lib 28 * @modules 29 * jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.main 31 * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase 32 * @run main RequiresTransitiveTest 33 */ 34 35import java.nio.file.Files; 36import java.nio.file.Path; 37 38import toolbox.JavacTask; 39import toolbox.Task; 40import toolbox.ToolBox; 41 42public class RequiresTransitiveTest extends ModuleTestBase { 43 44 public static void main(String... args) throws Exception { 45 RequiresTransitiveTest t = new RequiresTransitiveTest(); 46 t.runTests(); 47 } 48 49 @Test 50 public void testJavaSE_OK(Path base) throws Exception { 51 Path src = base.resolve("src"); 52 tb.writeJavaFiles(src, 53 "module m { requires java.se; }", 54 "import java.awt.Frame;\n" // in java.se 55 + "class Test {\n" 56 + " Frame f;\n" 57 + "}"); 58 Path classes = base.resolve("classes"); 59 Files.createDirectories(classes); 60 61 new JavacTask(tb, Task.Mode.CMDLINE) 62 .files(findJavaFiles(src)) 63 .outdir(classes) 64 .run() 65 .writeAll(); 66 } 67 68 @Test 69 public void testJavaSE_Fail(Path base) throws Exception { 70 Path src = base.resolve("src"); 71 tb.writeJavaFiles(src, 72 "module m { requires java.se; }", 73 "import com.sun.source.tree.Tree;\n" // not in java.se (in jdk.compiler) 74 + "class Test {\n" 75 + " Tree t;\n" 76 + "}"); 77 Path classes = base.resolve("classes"); 78 Files.createDirectories(classes); 79 80 String log = new JavacTask(tb, Task.Mode.CMDLINE) 81 .options("-XDrawDiagnostics") 82 .files(findJavaFiles(src)) 83 .outdir(classes) 84 .run(Task.Expect.FAIL) 85 .writeAll() 86 .getOutput(Task.OutputKind.DIRECT); 87 88 if (!log.contains("Test.java:1:22: compiler.err.package.not.visible: com.sun.source.tree, (compiler.misc.not.def.access.does.not.read: m, com.sun.source.tree, jdk.compiler)")) 89 throw new Exception("expected output not found"); 90 } 91 92 @Test 93 public void testComplex_OK(Path base) throws Exception { 94 Path src = getComplexSrc(base, "", ""); 95 Path classes = base.resolve("classes"); 96 Files.createDirectories(classes); 97 98 new JavacTask(tb, Task.Mode.CMDLINE) 99 .options("--module-source-path", src.toString()) 100 .files(findJavaFiles(src)) 101 .outdir(classes) 102 .run() 103 .writeAll(); 104 } 105 106 @Test 107 public void testComplex_Fail(Path base) throws Exception { 108 Path src = getComplexSrc(base, 109 "import p5.C5; import p6.C6; import p7.C7;\n", 110 "C5 c5; C6 c6; C7 c7;\n"); 111 Path classes = base.resolve("classes"); 112 Files.createDirectories(classes); 113 114 String log = new JavacTask(tb, Task.Mode.CMDLINE) 115 .options("-XDrawDiagnostics", 116 "--module-source-path", src.toString()) 117 .files(findJavaFiles(src)) 118 .outdir(classes) 119 .run(Task.Expect.FAIL) 120 .writeAll() 121 .getOutput(Task.OutputKind.DIRECT); 122 123 String[] expect = { 124 "C1.java:5:8: compiler.err.package.not.visible: p5, (compiler.misc.not.def.access.does.not.read: m1x, p5, m5x)", 125 "C1.java:5:22: compiler.err.package.not.visible: p6, (compiler.misc.not.def.access.does.not.read: m1x, p6, m6x)", 126 "C1.java:5:36: compiler.err.package.not.visible: p7, (compiler.misc.not.def.access.does.not.read: m1x, p7, m7x)" 127 }; 128 129 for (String e: expect) { 130 if (!log.contains(e)) 131 throw new Exception("expected output not found: " + e); 132 } 133 } 134 135 /* 136 * Set up the following module graph 137 * m1x -> m2x => m3x => m4x -> m5x 138 * -> m6x => m7x 139 * where -> is requires, => is requires transitive 140 */ 141 Path getComplexSrc(Path base, String m1_extraImports, String m1_extraUses) throws Exception { 142 Path src = base.resolve("src"); 143 144 Path src_m1 = src.resolve("m1x"); 145 tb.writeJavaFiles(src_m1, 146 "module m1x { requires m2x; }", 147 "package p1;\n" 148 + "import p2.C2;\n" 149 + "import p3.C3;\n" 150 + "import p4.C4;\n" 151 + m1_extraImports 152 + "class C1 {\n" 153 + " C2 c2; C3 c3; C4 c4;\n" 154 + m1_extraUses 155 + "}\n"); 156 157 Path src_m2 = src.resolve("m2x"); 158 tb.writeJavaFiles(src_m2, 159 "module m2x {\n" 160 + " requires transitive m3x;\n" 161 + " requires m6x;\n" 162 + " exports p2;\n" 163 + "}", 164 "package p2;\n" 165 + "public class C2 { }\n"); 166 167 Path src_m3 = src.resolve("m3x"); 168 tb.writeJavaFiles(src_m3, 169 "module m3x { requires transitive m4x; exports p3; }", 170 "package p3;\n" 171 + "public class C3 { }\n"); 172 173 Path src_m4 = src.resolve("m4x"); 174 tb.writeJavaFiles(src_m4, 175 "module m4x { requires m5x; exports p4; }", 176 "package p4;\n" 177 + "public class C4 { }\n"); 178 179 Path src_m5 = src.resolve("m5x"); 180 tb.writeJavaFiles(src_m5, 181 "module m5x { exports p5; }", 182 "package p5;\n" 183 + "public class C5 { }\n"); 184 185 Path src_m6 = src.resolve("m6x"); 186 tb.writeJavaFiles(src_m6, 187 "module m6x { requires transitive m7x; exports p6; }", 188 "package p6;\n" 189 + "public class C6 { }\n"); 190 191 Path src_m7 = src.resolve("m7x"); 192 tb.writeJavaFiles(src_m7, 193 "module m7x { exports p7; }", 194 "package p7;\n" 195 + "public class C7 { }\n"); 196 197 return src; 198 } 199} 200