< prev index next >

langtools/test/tools/javac/modules/EdgeCases.java

Print this page




  56 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
  57 
  58 import toolbox.JarTask;
  59 import toolbox.JavacTask;
  60 import toolbox.Task;
  61 
  62 public class EdgeCases extends ModuleTestBase {
  63 
  64     public static void main(String... args) throws Exception {
  65         new EdgeCases().runTests();
  66     }
  67 
  68     @Test
  69     public void testAddExportUndefinedModule(Path base) throws Exception {
  70         Path src = base.resolve("src");
  71         tb.writeJavaFiles(src, "package test; import undef.Any; public class Test {}");
  72         Path classes = base.resolve("classes");
  73         tb.createDirectories(classes);
  74 
  75         List<String> log = new JavacTask(tb)
  76                 .options("-XaddExports:undef/undef=ALL-UNNAMED", "-XDrawDiagnostics")
  77                 .outdir(classes)
  78                 .files(findJavaFiles(src))
  79                 .run(Task.Expect.FAIL)
  80                 .writeAll()
  81                 .getOutputLines(Task.OutputKind.DIRECT);
  82 
  83         List<String> expected = Arrays.asList("- compiler.err.cant.find.module: undef",
  84                                               "Test.java:1:27: compiler.err.doesnt.exist: undef",
  85                                               "2 errors");
  86 
  87         if (!expected.equals(log))
  88             throw new Exception("expected output not found: " + log);
  89     }
  90 
  91     @Test
  92     public void testModuleSymbolOutterMostClass(Path base) throws Exception {
  93         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  94         try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
  95             Path moduleSrc = base.resolve("module-src");
  96             Path m1 = moduleSrc.resolve("m1");


 150     @Test
 151     public void testModuleImplicitModuleBoundaries(Path base) throws Exception {
 152         Path src = base.resolve("src");
 153         Path src_m1 = src.resolve("m1");
 154         tb.writeJavaFiles(src_m1,
 155                           "module m1 { exports api1; }",
 156                           "package api1; public class Api1 { public void call() { } }");
 157         Path src_m2 = src.resolve("m2");
 158         tb.writeJavaFiles(src_m2,
 159                           "module m2 { requires m1; exports api2; }",
 160                           "package api2; public class Api2 { public static api1.Api1 get() { return null; } }");
 161         Path src_m3 = src.resolve("m3");
 162         tb.writeJavaFiles(src_m3,
 163                           "module m3 { requires m2; }",
 164                           "package test; public class Test { { api2.Api2.get().call(); api2.Api2.get().toString(); } }");
 165         Path classes = base.resolve("classes");
 166         tb.createDirectories(classes);
 167 
 168         String log = new JavacTask(tb)
 169                 .options("-XDrawDiagnostics",
 170                          "-modulesourcepath", src.toString())
 171                 .outdir(classes)
 172                 .files(findJavaFiles(src))
 173                 .run(Task.Expect.FAIL)
 174                 .writeAll()
 175                 .getOutput(Task.OutputKind.DIRECT);
 176 
 177         if (!log.contains("Test.java:1:52: compiler.err.not.def.access.class.intf.cant.access: call(), api1.Api1") ||
 178             !log.contains("Test.java:1:76: compiler.err.not.def.access.class.intf.cant.access: toString(), java.lang.Object"))
 179             throw new Exception("expected output not found");
 180     }
 181 
 182     @Test
 183     public void testAssignClassToAutomaticModule(Path base) throws Exception {
 184         //check that if a ClassSymbol belongs to an automatic module, it is properly assigned and not
 185         //duplicated when being accessed through a classfile.
 186         Path automaticSrc = base.resolve("automaticSrc");
 187         tb.writeJavaFiles(automaticSrc, "package api1; public class Api1 {}");
 188         Path automaticClasses = base.resolve("automaticClasses");
 189         tb.createDirectories(automaticClasses);
 190 


 205         Path automaticJar = modulePath.resolve("m1-1.0.jar");
 206 
 207         new JarTask(tb, automaticJar)
 208           .baseDir(automaticClasses)
 209           .files("api1/Api1.class")
 210           .run();
 211 
 212         Path src = base.resolve("src");
 213         Path src_m2 = src.resolve("m2");
 214         tb.writeJavaFiles(src_m2,
 215                           "module m2 { requires m1; exports api2; }",
 216                           "package api2; public class Api2 { public static api1.Api1 get() { return null; } }");
 217         Path src_m3 = src.resolve("m3");
 218         tb.writeJavaFiles(src_m3,
 219                           "module m3 { requires m1; requires m2; }",
 220                           "package test; public class Test { { api2.Api2.get(); api1.Api1 a1; } }");
 221         Path classes = base.resolve("classes");
 222         tb.createDirectories(classes);
 223 
 224         new JavacTask(tb)
 225                 .options("-modulepath", modulePath.toString(),
 226                          "-modulesourcepath", src.toString())
 227                 .outdir(classes)
 228                 .files(findJavaFiles(src_m2))
 229                 .run()
 230                 .writeAll();
 231 
 232         new JavacTask(tb)
 233                 .options("-modulepath", modulePath.toString(),
 234                          "-modulesourcepath", src.toString())
 235                 .outdir(classes)
 236                 .files(findJavaFiles(src_m3))
 237                 .run()
 238                 .writeAll();
 239     }
 240 
 241     @Test
 242     public void testEmptyImplicitModuleInfo(Path base) throws Exception {
 243         Path src = base.resolve("src");
 244         Path src_m1 = src.resolve("m1");
 245         Files.createDirectories(src_m1);
 246         try (Writer w = Files.newBufferedWriter(src_m1.resolve("module-info.java"))) {}
 247         tb.writeJavaFiles(src_m1,
 248                           "package test; public class Test {}");
 249         Path classes = base.resolve("classes");
 250         tb.createDirectories(classes);
 251 
 252         new JavacTask(tb)
 253                 .options("-sourcepath", src_m1.toString(),
 254                          "-XDrawDiagnostics")
 255                 .outdir(classes)
 256                 .files(findJavaFiles(src_m1.resolve("test")))
 257                 .run(Task.Expect.FAIL)
 258                 .writeAll();
 259 
 260         tb.writeJavaFiles(src_m1,
 261                           "module m1 {}");
 262 
 263         new JavacTask(tb)
 264                 .options("-sourcepath", src_m1.toString())
 265                 .outdir(classes)
 266                 .files(findJavaFiles(src_m1.resolve("test")))
 267                 .run()
 268                 .writeAll();
 269 
 270     }
 271 
 272     @Test
 273     public void testClassPackageClash(Path base) throws Exception {
 274         Path src = base.resolve("src");
 275         Path src_m1 = src.resolve("m1");
 276         tb.writeJavaFiles(src_m1,
 277                           "module m1 { exports test.m1; }",
 278                           "package test.m1;\n" +
 279                           "public class Test {}\n");
 280         Path src_m2 = src.resolve("m2");
 281         tb.writeJavaFiles(src_m2,
 282                           "module m2 { requires m1; }",
 283                           "package test;\n" +
 284                           "public class m1 {}\n");
 285         Path classes = base.resolve("classes");
 286         tb.createDirectories(classes);
 287 
 288         List<String> log = new JavacTask(tb)
 289                 .options("-modulesourcepath", src.toString(),
 290                          "-XDrawDiagnostics")
 291                 .outdir(classes)
 292                 .files(findJavaFiles(src))
 293                 .run(Task.Expect.FAIL)
 294                 .writeAll()
 295                 .getOutputLines(Task.OutputKind.DIRECT);
 296 
 297         List<String> expected = Arrays.asList(
 298             "m1.java:2:8: compiler.err.clash.with.pkg.of.same.name: kindname.class, test.m1",
 299             "1 error"
 300         );
 301 
 302         if (!expected.equals(log)) {
 303             throw new IllegalStateException(log.toString());
 304         }
 305     }
 306 
 307 }


  56 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
  57 
  58 import toolbox.JarTask;
  59 import toolbox.JavacTask;
  60 import toolbox.Task;
  61 
  62 public class EdgeCases extends ModuleTestBase {
  63 
  64     public static void main(String... args) throws Exception {
  65         new EdgeCases().runTests();
  66     }
  67 
  68     @Test
  69     public void testAddExportUndefinedModule(Path base) throws Exception {
  70         Path src = base.resolve("src");
  71         tb.writeJavaFiles(src, "package test; import undef.Any; public class Test {}");
  72         Path classes = base.resolve("classes");
  73         tb.createDirectories(classes);
  74 
  75         List<String> log = new JavacTask(tb)
  76                 .options("--add-exports", "undef/undef=ALL-UNNAMED", "-XDrawDiagnostics")
  77                 .outdir(classes)
  78                 .files(findJavaFiles(src))
  79                 .run(Task.Expect.FAIL)
  80                 .writeAll()
  81                 .getOutputLines(Task.OutputKind.DIRECT);
  82 
  83         List<String> expected = Arrays.asList("- compiler.err.cant.find.module: undef",
  84                                               "Test.java:1:27: compiler.err.doesnt.exist: undef",
  85                                               "2 errors");
  86 
  87         if (!expected.equals(log))
  88             throw new Exception("expected output not found: " + log);
  89     }
  90 
  91     @Test
  92     public void testModuleSymbolOutterMostClass(Path base) throws Exception {
  93         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  94         try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
  95             Path moduleSrc = base.resolve("module-src");
  96             Path m1 = moduleSrc.resolve("m1");


 150     @Test
 151     public void testModuleImplicitModuleBoundaries(Path base) throws Exception {
 152         Path src = base.resolve("src");
 153         Path src_m1 = src.resolve("m1");
 154         tb.writeJavaFiles(src_m1,
 155                           "module m1 { exports api1; }",
 156                           "package api1; public class Api1 { public void call() { } }");
 157         Path src_m2 = src.resolve("m2");
 158         tb.writeJavaFiles(src_m2,
 159                           "module m2 { requires m1; exports api2; }",
 160                           "package api2; public class Api2 { public static api1.Api1 get() { return null; } }");
 161         Path src_m3 = src.resolve("m3");
 162         tb.writeJavaFiles(src_m3,
 163                           "module m3 { requires m2; }",
 164                           "package test; public class Test { { api2.Api2.get().call(); api2.Api2.get().toString(); } }");
 165         Path classes = base.resolve("classes");
 166         tb.createDirectories(classes);
 167 
 168         String log = new JavacTask(tb)
 169                 .options("-XDrawDiagnostics",
 170                          "--module-source-path", src.toString())
 171                 .outdir(classes)
 172                 .files(findJavaFiles(src))
 173                 .run(Task.Expect.FAIL)
 174                 .writeAll()
 175                 .getOutput(Task.OutputKind.DIRECT);
 176 
 177         if (!log.contains("Test.java:1:52: compiler.err.not.def.access.class.intf.cant.access: call(), api1.Api1") ||
 178             !log.contains("Test.java:1:76: compiler.err.not.def.access.class.intf.cant.access: toString(), java.lang.Object"))
 179             throw new Exception("expected output not found");
 180     }
 181 
 182     @Test
 183     public void testAssignClassToAutomaticModule(Path base) throws Exception {
 184         //check that if a ClassSymbol belongs to an automatic module, it is properly assigned and not
 185         //duplicated when being accessed through a classfile.
 186         Path automaticSrc = base.resolve("automaticSrc");
 187         tb.writeJavaFiles(automaticSrc, "package api1; public class Api1 {}");
 188         Path automaticClasses = base.resolve("automaticClasses");
 189         tb.createDirectories(automaticClasses);
 190 


 205         Path automaticJar = modulePath.resolve("m1-1.0.jar");
 206 
 207         new JarTask(tb, automaticJar)
 208           .baseDir(automaticClasses)
 209           .files("api1/Api1.class")
 210           .run();
 211 
 212         Path src = base.resolve("src");
 213         Path src_m2 = src.resolve("m2");
 214         tb.writeJavaFiles(src_m2,
 215                           "module m2 { requires m1; exports api2; }",
 216                           "package api2; public class Api2 { public static api1.Api1 get() { return null; } }");
 217         Path src_m3 = src.resolve("m3");
 218         tb.writeJavaFiles(src_m3,
 219                           "module m3 { requires m1; requires m2; }",
 220                           "package test; public class Test { { api2.Api2.get(); api1.Api1 a1; } }");
 221         Path classes = base.resolve("classes");
 222         tb.createDirectories(classes);
 223 
 224         new JavacTask(tb)
 225                 .options("--module-path", modulePath.toString(),
 226                          "--module-source-path", src.toString())
 227                 .outdir(classes)
 228                 .files(findJavaFiles(src_m2))
 229                 .run()
 230                 .writeAll();
 231 
 232         new JavacTask(tb)
 233                 .options("--module-path", modulePath.toString(),
 234                          "--module-source-path", src.toString())
 235                 .outdir(classes)
 236                 .files(findJavaFiles(src_m3))
 237                 .run()
 238                 .writeAll();
 239     }
 240 
 241     @Test
 242     public void testEmptyImplicitModuleInfo(Path base) throws Exception {
 243         Path src = base.resolve("src");
 244         Path src_m1 = src.resolve("m1");
 245         Files.createDirectories(src_m1);
 246         try (Writer w = Files.newBufferedWriter(src_m1.resolve("module-info.java"))) {}
 247         tb.writeJavaFiles(src_m1,
 248                           "package test; public class Test {}");
 249         Path classes = base.resolve("classes");
 250         tb.createDirectories(classes);
 251 
 252         new JavacTask(tb)
 253                 .options("--source-path", src_m1.toString(),
 254                          "-XDrawDiagnostics")
 255                 .outdir(classes)
 256                 .files(findJavaFiles(src_m1.resolve("test")))
 257                 .run(Task.Expect.FAIL)
 258                 .writeAll();
 259 
 260         tb.writeJavaFiles(src_m1,
 261                           "module m1 {}");
 262 
 263         new JavacTask(tb)
 264                 .options("--source-path", src_m1.toString())
 265                 .outdir(classes)
 266                 .files(findJavaFiles(src_m1.resolve("test")))
 267                 .run()
 268                 .writeAll();
 269 
 270     }
 271 
 272     @Test
 273     public void testClassPackageClash(Path base) throws Exception {
 274         Path src = base.resolve("src");
 275         Path src_m1 = src.resolve("m1");
 276         tb.writeJavaFiles(src_m1,
 277                           "module m1 { exports test.m1; }",
 278                           "package test.m1;\n" +
 279                           "public class Test {}\n");
 280         Path src_m2 = src.resolve("m2");
 281         tb.writeJavaFiles(src_m2,
 282                           "module m2 { requires m1; }",
 283                           "package test;\n" +
 284                           "public class m1 {}\n");
 285         Path classes = base.resolve("classes");
 286         tb.createDirectories(classes);
 287 
 288         List<String> log = new JavacTask(tb)
 289                 .options("--module-source-path", src.toString(),
 290                          "-XDrawDiagnostics")
 291                 .outdir(classes)
 292                 .files(findJavaFiles(src))
 293                 .run(Task.Expect.FAIL)
 294                 .writeAll()
 295                 .getOutputLines(Task.OutputKind.DIRECT);
 296 
 297         List<String> expected = Arrays.asList(
 298             "m1.java:2:8: compiler.err.clash.with.pkg.of.same.name: kindname.class, test.m1",
 299             "1 error"
 300         );
 301 
 302         if (!expected.equals(log)) {
 303             throw new IllegalStateException(log.toString());
 304         }
 305     }
 306 
 307 }
< prev index next >