1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @summary tests for "requires transitive"
* @library /tools/lib
* @modules
* jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
* @run main RequiresTransitiveTest
*/
import java.nio.file.Files;
import java.nio.file.Path;
import toolbox.JavacTask;
import toolbox.Task;
import toolbox.ToolBox;
public class RequiresTransitiveTest extends ModuleTestBase {
public static void main(String... args) throws Exception {
RequiresTransitiveTest t = new RequiresTransitiveTest();
t.runTests();
}
@Test
public void testJavaSE_OK(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { requires java.se; }",
"import java.awt.Frame;\n" // in java.se
+ "class Test {\n"
+ " Frame f;\n"
+ "}");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
new JavacTask(tb, Task.Mode.CMDLINE)
.files(findJavaFiles(src))
.outdir(classes)
.run()
.writeAll();
}
@Test
public void testJavaSE_Fail(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { requires java.se; }",
"import com.sun.source.tree.Tree;\n" // not in java.se (in jdk.compiler)
+ "class Test {\n"
+ " Tree t;\n"
+ "}");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
String log = new JavacTask(tb, Task.Mode.CMDLINE)
.options("-XDrawDiagnostics")
.files(findJavaFiles(src))
.outdir(classes)
.run(Task.Expect.FAIL)
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
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)"))
throw new Exception("expected output not found");
}
@Test
public void testComplex_OK(Path base) throws Exception {
Path src = getComplexSrc(base, "", "");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
new JavacTask(tb, Task.Mode.CMDLINE)
.options("--module-source-path", src.toString())
.files(findJavaFiles(src))
.outdir(classes)
.run()
.writeAll();
}
@Test
public void testComplex_Fail(Path base) throws Exception {
Path src = getComplexSrc(base,
"import p5.C5; import p6.C6; import p7.C7;\n",
"C5 c5; C6 c6; C7 c7;\n");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
String log = new JavacTask(tb, Task.Mode.CMDLINE)
.options("-XDrawDiagnostics",
"--module-source-path", src.toString())
.files(findJavaFiles(src))
.outdir(classes)
.run(Task.Expect.FAIL)
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
String[] expect = {
"C1.java:5:8: compiler.err.package.not.visible: p5, (compiler.misc.not.def.access.does.not.read: m1x, p5, m5x)",
"C1.java:5:22: compiler.err.package.not.visible: p6, (compiler.misc.not.def.access.does.not.read: m1x, p6, m6x)",
"C1.java:5:36: compiler.err.package.not.visible: p7, (compiler.misc.not.def.access.does.not.read: m1x, p7, m7x)"
};
for (String e: expect) {
if (!log.contains(e))
throw new Exception("expected output not found: " + e);
}
}
/*
* Set up the following module graph
* m1x -> m2x => m3x => m4x -> m5x
* -> m6x => m7x
* where -> is requires, => is requires transitive
*/
Path getComplexSrc(Path base, String m1_extraImports, String m1_extraUses) throws Exception {
Path src = base.resolve("src");
Path src_m1 = src.resolve("m1x");
tb.writeJavaFiles(src_m1,
"module m1x { requires m2x; }",
"package p1;\n"
+ "import p2.C2;\n"
+ "import p3.C3;\n"
+ "import p4.C4;\n"
+ m1_extraImports
+ "class C1 {\n"
+ " C2 c2; C3 c3; C4 c4;\n"
+ m1_extraUses
+ "}\n");
Path src_m2 = src.resolve("m2x");
tb.writeJavaFiles(src_m2,
"module m2x {\n"
+ " requires transitive m3x;\n"
+ " requires m6x;\n"
+ " exports p2;\n"
+ "}",
"package p2;\n"
+ "public class C2 { }\n");
Path src_m3 = src.resolve("m3x");
tb.writeJavaFiles(src_m3,
"module m3x { requires transitive m4x; exports p3; }",
"package p3;\n"
+ "public class C3 { }\n");
Path src_m4 = src.resolve("m4x");
tb.writeJavaFiles(src_m4,
"module m4x { requires m5x; exports p4; }",
"package p4;\n"
+ "public class C4 { }\n");
Path src_m5 = src.resolve("m5x");
tb.writeJavaFiles(src_m5,
"module m5x { exports p5; }",
"package p5;\n"
+ "public class C5 { }\n");
Path src_m6 = src.resolve("m6x");
tb.writeJavaFiles(src_m6,
"module m6x { requires transitive m7x; exports p6; }",
"package p6;\n"
+ "public class C6 { }\n");
Path src_m7 = src.resolve("m7x");
tb.writeJavaFiles(src_m7,
"module m7x { exports p7; }",
"package p7;\n"
+ "public class C7 { }\n");
return src;
}
}
|