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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
* 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 test module/package conflicts
* @library /tools/lib
* @modules
* jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* @build toolbox.ToolBox toolbox.JavacTask toolbox.ModuleBuilder ModuleTestBase
* @run main PackageConflictTest
*/
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import toolbox.JavacTask;
import toolbox.ModuleBuilder;
import toolbox.Task;
public class PackageConflictTest extends ModuleTestBase {
public static void main(String... args) throws Exception {
PackageConflictTest t = new PackageConflictTest();
t.runTests();
}
@Test
public void testSimple(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"package java.util; public class MyList { }");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
String log = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!log.contains("MyList.java:1:1: compiler.err.package.in.other.module: java.base"))
throw new Exception("expected output not found");
}
@Test
public void testDisjoint(Path base) throws Exception {
Path m1 = base.resolve("m1x");
Path m2 = base.resolve("m2x");
tb.writeJavaFiles(m1,
"module m1x { }",
"package test; public class A { }");
tb.writeJavaFiles(m2,
"module m2x { }",
"package test; public class B { }");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
new JavacTask(tb)
.options("-Werror", "--module-source-path", base.toString())
.outdir(classes)
.files(findJavaFiles(base))
.run()
.writeAll();
}
@Test
public void testConflictInDependencies(Path base) throws Exception {
Path m1 = base.resolve("m1x");
Path m2 = base.resolve("m2x");
Path m3 = base.resolve("m3x");
tb.writeJavaFiles(m1,
"module m1x { exports test; }",
"package test; public class A { }");
tb.writeJavaFiles(m2,
"module m2x { exports test; }",
"package test; public class B { }");
tb.writeJavaFiles(m3,
"module m3x { requires m1x; requires m2x; }",
"package impl; public class Impl { }");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
List<String> log = new JavacTask(tb)
.options("-XDrawDiagnostics", "--module-source-path", base.toString())
.outdir(classes)
.files(findJavaFiles(base))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected =
Arrays.asList("module-info.java:1:1: compiler.err.package.clash.from.requires: m3x, test, m1x, m2x",
"1 error");
if (!expected.equals(log)) {
throw new AssertionError("Unexpected output: " + log);
}
}
@Test
public void testSimple2(Path base) throws Exception {
Path modSrc = base.resolve("modSrc");
Path modules = base.resolve("modules");
new ModuleBuilder(tb, "N")
.exports("pack")
.classes("package pack; public class A { }")
.build(modules);
new ModuleBuilder(tb, "M")
.requires("N")
.classes("package pack; public class B { pack.A f; }")
.write(modSrc);
String log = new JavacTask(tb)
.options("-XDrawDiagnostics", "-p", modules.toString())
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(modSrc.resolve("M")))
.run(Task.Expect.FAIL)
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!log.contains("B.java:1:1: compiler.err.package.in.other.module: N"))
throw new Exception("expected output not found");
}
@Test
public void testPrivateConflict(Path base) throws Exception {
Path modSrc = base.resolve("modSrc");
new ModuleBuilder(tb, "N")
.exports("publ")
.classes("package pack; public class A { }")
.classes("package publ; public class B { }")
.write(modSrc);
new ModuleBuilder(tb, "M")
.requires("N")
.classes("package pack; public class C { publ.B b; }")
.write(modSrc);
String log = new JavacTask(tb)
.options("-XDrawDiagnostics", "--module-source-path", modSrc.toString())
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(modSrc))
.run(Task.Expect.SUCCESS)
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!log.contains(""))
throw new Exception("unexpected output not found");
}
@Test
public void testPrivateConflictOnModulePath(Path base) throws Exception {
Path modSrc = base.resolve("modSrc");
Path modules = base.resolve("modules");
new ModuleBuilder(tb, "N")
.exports("publ")
.classes("package pack; public class A { }")
.classes("package publ; public class B { }")
.build(modules);
new ModuleBuilder(tb, "M")
.requires("N")
.classes("package pack; public class C { publ.B b; }")
.write(modSrc);
String log = new JavacTask(tb)
.options("-XDrawDiagnostics", "-p", modules.toString())
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(modSrc.resolve("M")))
.run(Task.Expect.SUCCESS)
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!log.contains(""))
throw new Exception("expected output not found");
}
@Test
public void testRequiresConflictExports(Path base) throws Exception {
Path modSrc = base.resolve("modSrc");
Path modules = base.resolve("modules");
new ModuleBuilder(tb, "M")
.exports("pack")
.classes("package pack; public class A { }")
.build(modules);
new ModuleBuilder(tb, "N")
.exports("pack")
.classes("package pack; public class B { }")
.build(modules);
new ModuleBuilder(tb, "K")
.requires("M")
.requires("N")
.classes("package pkg; public class C { pack.A a; pack.B b; }")
.write(modSrc);
List<String> log = new JavacTask(tb)
.options("-XDrawDiagnostics", "-p", modules.toString())
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(modSrc.resolve("K")))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected =
Arrays.asList("module-info.java:1:1: compiler.err.package.clash.from.requires: K, pack, M, N",
"1 error");
if (!log.containsAll(expected))
throw new Exception("expected output not found");
}
@Test
public void testQualifiedExportsToDifferentModules(Path base) throws Exception {
Path modSrc = base.resolve("modSrc");
new ModuleBuilder(tb, "U").write(modSrc);
new ModuleBuilder(tb, "M")
.exports("pkg to U")
.classes("package pkg; public class A { public static boolean flagM; }")
.write(modSrc);
new ModuleBuilder(tb, "N")
.exports("pkg to K")
.classes("package pkg; public class A { public static boolean flagN; }")
.write(modSrc);
ModuleBuilder moduleK = new ModuleBuilder(tb, "K");
moduleK.requires("M")
.requires("N")
.classes("package p; public class DependsOnN { boolean f = pkg.A.flagN; } ")
.write(modSrc);
new JavacTask(tb)
.options("--module-source-path", modSrc.toString())
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(modSrc.resolve("K")))
.run(Task.Expect.SUCCESS)
.writeAll();
//negative case
moduleK.classes("package pkg; public class DuplicatePackage { } ")
.classes("package p; public class DependsOnM { boolean f = pkg.A.flagM; } ")
.write(modSrc);
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics",
"--module-source-path", modSrc.toString())
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(modSrc.resolve("K")))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList(
"DependsOnM.java:1:55: compiler.err.cant.resolve.location: kindname.variable, flagM, , , (compiler.misc.location: kindname.class, pkg.A, null)");
if (!output.containsAll(expected)) {
throw new Exception("expected output not found");
}
}
}
|