< prev index next >

test/tools/javac/modules/XModuleTest.java

Print this page
rev 3947 : imported patch xmodule-to-patch-module


   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 multi-module mode compilation
  27  * @library /tools/lib
  28  * @modules
  29  *      jdk.compiler/com.sun.tools.javac.api
  30  *      jdk.compiler/com.sun.tools.javac.code
  31  *      jdk.compiler/com.sun.tools.javac.main
  32  *      jdk.compiler/com.sun.tools.javac.processing
  33  * @build toolbox.ToolBox toolbox.JavacTask toolbox.ModuleBuilder ModuleTestBase
  34  * @run main XModuleTest
  35  */
  36 

  37 import java.nio.file.Path;
  38 import java.util.Arrays;
  39 import java.util.List;
  40 import java.util.Set;
  41 
  42 import javax.annotation.processing.AbstractProcessor;
  43 import javax.annotation.processing.RoundEnvironment;
  44 import javax.annotation.processing.SupportedAnnotationTypes;
  45 import javax.lang.model.SourceVersion;
  46 import javax.lang.model.element.ModuleElement;
  47 import javax.lang.model.element.TypeElement;
  48 import javax.lang.model.util.Elements;
  49 
  50 import com.sun.tools.javac.code.Symtab;
  51 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
  52 import toolbox.JavacTask;
  53 import toolbox.ModuleBuilder;
  54 import toolbox.Task;
  55 import toolbox.Task.Expect;
  56 
  57 public class XModuleTest extends ModuleTestBase {
  58 
  59     public static void main(String... args) throws Exception {
  60         new XModuleTest().runTests();
  61     }
  62 
  63     @Test
  64     public void testCorrectXModule(Path base) throws Exception {
  65         //note: avoiding use of java.base, as that gets special handling on some places:
  66         Path src = base.resolve("src");
  67         tb.writeJavaFiles(src, "package javax.lang.model.element; public interface Extra extends Element { }");
  68         Path classes = base.resolve("classes");
  69         tb.createDirectories(classes);
  70 
  71         String log = new JavacTask(tb)
  72                 .options("-Xmodule:java.compiler")
  73                 .outdir(classes)
  74                 .files(findJavaFiles(src))
  75                 .run()
  76                 .writeAll()
  77                 .getOutput(Task.OutputKind.DIRECT);
  78 
  79         if (!log.isEmpty())
  80             throw new Exception("expected output not found: " + log);
  81     }
  82 
  83     @Test
  84     public void testSourcePath(Path base) throws Exception {
  85         //note: avoiding use of java.base, as that gets special handling on some places:
  86         Path src = base.resolve("src");
  87         tb.writeJavaFiles(src, "package javax.lang.model.element; public interface Extra extends Element, Other { }", "package javax.lang.model.element; interface Other { }");



  88         Path classes = base.resolve("classes");
  89         tb.createDirectories(classes);
  90 
  91         String log = new JavacTask(tb)
  92                 .options("-Xmodule:java.compiler", "-sourcepath", src.toString())


  93                 .outdir(classes)
  94                 .files(src.resolve("javax/lang/model/element/Extra.java"))
  95                 .run()
  96                 .writeAll()
  97                 .getOutput(Task.OutputKind.DIRECT);
  98 
  99         if (!log.isEmpty())
 100             throw new Exception("expected output not found: " + log);
















































































































 101     }
 102 
 103     @Test
 104     public void testClassPath(Path base) throws Exception {
 105         Path cpSrc = base.resolve("cpSrc");
 106         tb.writeJavaFiles(cpSrc, "package p; public interface Other { }");
 107         Path cpClasses = base.resolve("cpClasses");
 108         tb.createDirectories(cpClasses);
 109 
 110         String cpLog = new JavacTask(tb)
 111                 .outdir(cpClasses)
 112                 .files(findJavaFiles(cpSrc))
 113                 .run()
 114                 .writeAll()
 115                 .getOutput(Task.OutputKind.DIRECT);
 116 
 117         if (!cpLog.isEmpty())
 118             throw new Exception("expected output not found: " + cpLog);
 119 
 120         Path src = base.resolve("src");
 121         //note: avoiding use of java.base, as that gets special handling on some places:
 122         tb.writeJavaFiles(src, "package javax.lang.model.element; public interface Extra extends Element, p.Other { }");
 123         Path classes = base.resolve("classes");
 124         tb.createDirectories(classes);
 125 
 126         List<String> log = new JavacTask(tb)
 127                 .options("-Xmodule:java.compiler",
 128                          "--class-path", cpClasses.toString(),
 129                          "-XDrawDiagnostics")
 130                 .outdir(classes)
 131                 .files(src.resolve("javax/lang/model/element/Extra.java"))
 132                 .run(Expect.FAIL)
 133                 .writeAll()
 134                 .getOutputLines(Task.OutputKind.DIRECT);
 135 
 136         List<String> expectedOut = Arrays.asList(
 137                 "Extra.java:1:76: compiler.err.doesnt.exist: p",
 138                 "1 error"
 139         );
 140 
 141         if (!expectedOut.equals(log))
 142             throw new Exception("expected output not found: " + log);
 143     }
 144 
 145     @Test
 146     public void testNoModuleInfoOnSourcePath(Path base) throws Exception {
 147         //note: avoiding use of java.base, as that gets special handling on some places:
 148         Path src = base.resolve("src");
 149         tb.writeJavaFiles(src,
 150                           "module java.compiler {}",
 151                           "package javax.lang.model.element; public interface Extra { }");



 152         Path classes = base.resolve("classes");
 153         tb.createDirectories(classes);
 154 
 155         List<String> log = new JavacTask(tb)
 156                 .options("-XDrawDiagnostics", "-Xmodule:java.compiler")

 157                 .outdir(classes)
 158                 .files(findJavaFiles(src))
 159                 .run(Task.Expect.FAIL)
 160                 .writeAll()
 161                 .getOutputLines(Task.OutputKind.DIRECT);
 162 
 163         List<String> expected = Arrays.asList("Extra.java:1:1: compiler.err.module-info.with.xmodule.sourcepath",
 164                                               "1 error");
 165 
 166         if (!expected.equals(log))
 167             throw new Exception("expected output not found: " + log);
 168     }
 169 
 170     @Test
 171     public void testNoModuleInfoInClassOutput(Path base) throws Exception {
 172         //note: avoiding use of java.base, as that gets special handling on some places:
 173         Path srcMod = base.resolve("src-mod");
 174         tb.writeJavaFiles(srcMod,
 175                           "module mod {}");
 176         Path classes = base.resolve("classes");
 177         tb.createDirectories(classes);
 178 
 179         String logMod = new JavacTask(tb)
 180                 .options()
 181                 .outdir(classes)
 182                 .files(findJavaFiles(srcMod))
 183                 .run()
 184                 .writeAll()
 185                 .getOutput(Task.OutputKind.DIRECT);
 186 
 187         if (!logMod.isEmpty())
 188             throw new Exception("unexpected output found: " + logMod);
 189 
 190         Path src = base.resolve("src");
 191         tb.writeJavaFiles(src,
 192                           "package javax.lang.model.element; public interface Extra { }");
 193         tb.createDirectories(classes);
 194 
 195         List<String> log = new JavacTask(tb)
 196                 .options("-XDrawDiagnostics", "-Xmodule:java.compiler")

 197                 .outdir(classes)
 198                 .files(findJavaFiles(src))
 199                 .run(Task.Expect.FAIL)
 200                 .writeAll()
 201                 .getOutputLines(Task.OutputKind.DIRECT);
 202 
 203         List<String> expected = Arrays.asList("Extra.java:1:1: compiler.err.module-info.with.xmodule.classpath",
 204                                               "1 error");
 205 
 206         if (!expected.equals(log))
 207             throw new Exception("expected output not found: " + log);
 208     }
 209 
 210     @Test
 211     public void testModuleSourcePathXModule(Path base) throws Exception {
 212         //note: avoiding use of java.base, as that gets special handling on some places:
 213         Path src = base.resolve("src");
 214         tb.writeJavaFiles(src, "package javax.lang.model.element; public interface Extra extends Element { }");
 215         Path classes = base.resolve("classes");
 216         tb.createDirectories(classes);
 217 
 218         List<String> log = new JavacTask(tb)
 219                 .options("-XDrawDiagnostics", "-Xmodule:java.compiler", "--module-source-path", src.toString())
 220                 .outdir(classes)
 221                 .files(findJavaFiles(src))
 222                 .run(Task.Expect.FAIL)
 223                 .writeAll()
 224                 .getOutputLines(Task.OutputKind.DIRECT);
 225 
 226         List<String> expected = Arrays.asList("- compiler.err.xmodule.no.module.sourcepath");
 227 
 228         if (!expected.equals(log))
 229             throw new Exception("expected output not found: " + log);
 230     }
 231 
 232     @Test
 233     public void testXModuleTooMany(Path base) throws Exception {
 234         //note: avoiding use of java.base, as that gets special handling on some places:
 235         Path src = base.resolve("src");
 236         tb.writeJavaFiles(src, "package javax.lang.model.element; public interface Extra extends Element { }");
 237         Path classes = base.resolve("classes");
 238         tb.createDirectories(classes);
 239 
 240         List<String> log = new JavacTask(tb, Task.Mode.CMDLINE)
 241                 .options("-XDrawDiagnostics", "-Xmodule:java.compiler", "-Xmodule:java.compiler")
 242                 .outdir(classes)
 243                 .files(findJavaFiles(src))
 244                 .run(Task.Expect.FAIL)
 245                 .writeAll()
 246                 .getOutputLines(Task.OutputKind.DIRECT);
 247 
 248         List<String> expected = Arrays.asList("javac: option -Xmodule: can only be specified once",
 249                                               "Usage: javac <options> <source files>",
 250                                               "use --help for a list of possible options");
 251 
 252         if (!expected.equals(log))
 253             throw new Exception("expected output not found: " + log);
 254     }
 255 
 256     @Test
 257     public void testWithModulePath(Path base) throws Exception {
 258         Path modSrc = base.resolve("modSrc");
 259         Path modules = base.resolve("modules");
 260         new ModuleBuilder(tb, "m1")
 261                 .classes("package pkg1; public interface E { }")
 262                 .build(modSrc, modules);
 263 
 264         Path src = base.resolve("src");
 265         tb.writeJavaFiles(src, "package p; interface A extends pkg1.E { }");
 266 
 267         new JavacTask(tb, Task.Mode.CMDLINE)
 268                 .options("--module-path", modules.toString(),
 269                         "-Xmodule:m1")
 270                 .files(findJavaFiles(src))
 271                 .run()
 272                 .writeAll();
 273 
 274         //checks module bounds still exist
 275         new ModuleBuilder(tb, "m2")
 276                 .classes("package pkg2; public interface D { }")
 277                 .build(modSrc, modules);
 278 
 279         Path src2 = base.resolve("src2");
 280         tb.writeJavaFiles(src2, "package p; interface A extends pkg2.D { }");
 281 
 282         List<String> log = new JavacTask(tb, Task.Mode.CMDLINE)
 283                 .options("-XDrawDiagnostics",
 284                         "--module-path", modules.toString(),
 285                         "-Xmodule:m1")
 286                 .files(findJavaFiles(src2))
 287                 .run(Task.Expect.FAIL)
 288                 .writeAll()
 289                 .getOutputLines(Task.OutputKind.DIRECT);
 290 
 291         List<String> expected = Arrays.asList("A.java:1:32: compiler.err.package.not.visible: pkg2, (compiler.misc.not.def.access.does.not.read: m1, pkg2, m2)",
 292                 "1 error");
 293 
 294         if (!expected.equals(log))
 295             throw new Exception("expected output not found: " + log);
 296     }
 297 
 298     @Test
 299     public void testWithUpgradeModulePath(Path base) throws Exception {
 300         Path modSrc = base.resolve("modSrc");
 301         Path modules = base.resolve("modules");
 302         new ModuleBuilder(tb, "m1")
 303                 .classes("package pkg1; public interface E { }")
 304                 .build(modSrc, modules);
 305 
 306         Path upgrSrc = base.resolve("upgradeSrc");
 307         Path upgrade = base.resolve("upgrade");
 308         new ModuleBuilder(tb, "m1")
 309                 .classes("package pkg1; public interface D { }")
 310                 .build(upgrSrc, upgrade);
 311 
 312         Path src = base.resolve("src");
 313         tb.writeJavaFiles(src, "package p; interface A extends pkg1.D { }");
 314 
 315         new JavacTask(tb, Task.Mode.CMDLINE)
 316                 .options("--module-path", modules.toString(),
 317                         "--upgrade-module-path", upgrade.toString(),
 318                         "-Xmodule:m1")
 319                 .files(findJavaFiles(src))
 320                 .run()
 321                 .writeAll();
 322     }
 323 
 324     @Test
 325     public void testUnnamedIsolation(Path base) throws Exception {
 326         //note: avoiding use of java.base, as that gets special handling on some places:
 327         Path sourcePath = base.resolve("source-path");
 328         tb.writeJavaFiles(sourcePath, "package src; public class Src {}");
 329 
 330         Path classPathSrc = base.resolve("class-path-src");
 331         tb.writeJavaFiles(classPathSrc, "package cp; public class CP { }");
 332         Path classPath = base.resolve("classPath");
 333         tb.createDirectories(classPath);
 334 
 335         String cpLog = new JavacTask(tb)
 336                 .outdir(classPath)
 337                 .files(findJavaFiles(classPathSrc))
 338                 .run()


 348                           "package m; public class M {}");
 349         Path modulePath = base.resolve("modulePath");
 350         tb.createDirectories(modulePath.resolve("m"));
 351 
 352         String modLog = new JavacTask(tb)
 353                 .outdir(modulePath.resolve("m"))
 354                 .files(findJavaFiles(modulePathSrc))
 355                 .run()
 356                 .writeAll()
 357                 .getOutput(Task.OutputKind.DIRECT);
 358 
 359         if (!modLog.isEmpty())
 360             throw new Exception("expected output not found: " + modLog);
 361 
 362         Path src = base.resolve("src");
 363         tb.writeJavaFiles(src, "package m; public class Extra { }");
 364         Path classes = base.resolve("classes");
 365         tb.createDirectories(classes);
 366 
 367         String log = new JavacTask(tb)
 368                 .options("-Xmodule:m",
 369                          "--class-path", classPath.toString(),
 370                          "--source-path", sourcePath.toString(),
 371                          "--module-path", modulePath.toString(),
 372                          "--processor-path", System.getProperty("test.classes"),
 373                          "-XDaccessInternalAPI=true",
 374                          "-processor", CheckModuleContentProcessing.class.getName())
 375                 .outdir(classes)
 376                 .files(findJavaFiles(sourcePath))
 377                 .run()
 378                 .writeAll()
 379                 .getOutput(Task.OutputKind.DIRECT);
 380 
 381         if (!log.isEmpty())
 382             throw new Exception("expected output not found: " + log);
 383     }
 384 
 385     @SupportedAnnotationTypes("*")
 386     public static final class CheckModuleContentProcessing extends AbstractProcessor {
 387 
 388         @Override


 402         }
 403 
 404         @Override
 405         public SourceVersion getSupportedSourceVersion() {
 406             return SourceVersion.latest();
 407         }
 408 
 409         private static void assertNonNull(String msg, Object val) {
 410             if (val == null) {
 411                 throw new AssertionError(msg);
 412             }
 413         }
 414 
 415         private static void assertNull(String msg, Object val) {
 416             if (val != null) {
 417                 throw new AssertionError(msg);
 418             }
 419         }
 420     }
 421 







 422 }


   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 8173777
  27  * @summary tests for multi-module mode compilation
  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 XModuleTest
  36  */
  37 
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.util.Arrays;
  41 import java.util.List;
  42 import java.util.Set;
  43 
  44 import javax.annotation.processing.AbstractProcessor;
  45 import javax.annotation.processing.RoundEnvironment;
  46 import javax.annotation.processing.SupportedAnnotationTypes;
  47 import javax.lang.model.SourceVersion;
  48 import javax.lang.model.element.ModuleElement;
  49 import javax.lang.model.element.TypeElement;
  50 import javax.lang.model.util.Elements;
  51 
  52 import com.sun.tools.javac.code.Symtab;
  53 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
  54 import toolbox.JavacTask;
  55 import toolbox.ModuleBuilder;
  56 import toolbox.Task;
  57 import toolbox.Task.Expect;
  58 
  59 public class XModuleTest extends ModuleTestBase {
  60 
  61     public static void main(String... args) throws Exception {
  62         new XModuleTest().runTests();
  63     }
  64 
  65     @Test
  66     public void testCorrectXModule(Path base) throws Exception {
  67         //note: avoiding use of java.base, as that gets special handling on some places:
  68         Path src = base.resolve("src");
  69         tb.writeJavaFiles(src, "package javax.lang.model.element; public interface Extra extends Element { }");
  70         Path classes = base.resolve("classes");
  71         tb.createDirectories(classes);
  72 
  73         String log = new JavacTask(tb)
  74                 .options("--patch-module", "java.compiler=" + src.toString())
  75                 .outdir(classes)
  76                 .files(findJavaFiles(src))
  77                 .run()
  78                 .writeAll()
  79                 .getOutput(Task.OutputKind.DIRECT);
  80 
  81         if (!log.isEmpty())
  82             throw new Exception("expected output not found: " + log);
  83     }
  84 
  85     @Test
  86     public void testCorrectXModuleMultiModule(Path base) throws Exception {
  87         //note: avoiding use of java.base, as that gets special handling on some places:
  88         Path src = base.resolve("src");
  89         Path m1 = src.resolve("m1");
  90         tb.writeJavaFiles(m1, "package javax.lang.model.element; public interface Extra extends Element { }");
  91         Path m2 = src.resolve("m2");
  92         tb.writeJavaFiles(m2, "package com.sun.source.tree; public interface Extra extends Tree { }");
  93         Path classes = base.resolve("classes");
  94         tb.createDirectories(classes);
  95 
  96         String log = new JavacTask(tb)
  97                 .options("--patch-module", "java.compiler=" + m1.toString(),
  98                          "--patch-module", "jdk.compiler=" + m2.toString(),
  99                          "--module-source-path", "dummy")
 100                 .outdir(classes)
 101                 .files(findJavaFiles(src))
 102                 .run()
 103                 .writeAll()
 104                 .getOutput(Task.OutputKind.DIRECT);
 105 
 106         if (!log.isEmpty())
 107             throw new Exception("expected output not found: " + log);
 108 
 109         checkFileExists(classes, "java.compiler/javax/lang/model/element/Extra.class");
 110         checkFileExists(classes, "jdk.compiler/com/sun/source/tree/Extra.class");
 111     }
 112 
 113     @Test
 114     public void testCorrectXModuleMultiModule2(Path base) throws Exception {
 115         //note: avoiding use of java.base, as that gets special handling on some places:
 116         Path src = base.resolve("src");
 117         Path m1 = src.resolve("m1");
 118         tb.writeJavaFiles(m1,
 119                           "package javax.lang.model.element; public interface Extra extends Element { }");
 120         Path m2 = src.resolve("m2");
 121         tb.writeJavaFiles(m2,
 122                           "package com.sun.source.tree; public interface Extra extends Tree { }");
 123         Path msp = base.resolve("msp");
 124         Path m3 = msp.resolve("m3x");
 125         tb.writeJavaFiles(m3,
 126                           "module m3x { }",
 127                           "package m3; public class Test { }");
 128         Path m4 = msp.resolve("m4x");
 129         tb.writeJavaFiles(m4,
 130                           "module m4x { }",
 131                           "package m4; public class Test { }");
 132         Path classes = base.resolve("classes");
 133         tb.createDirectories(classes);
 134 
 135         String log = new JavacTask(tb)
 136                 .options("--patch-module", "java.compiler=" + m1.toString(),
 137                          "--patch-module", "jdk.compiler=" + m2.toString(),
 138                          "--module-source-path", msp.toString())
 139                 .outdir(classes)
 140                 .files(findJavaFiles(src, msp))
 141                 .run()
 142                 .writeAll()
 143                 .getOutput(Task.OutputKind.DIRECT);
 144 
 145         if (!log.isEmpty())
 146             throw new Exception("expected output not found: " + log);
 147 
 148         checkFileExists(classes, "java.compiler/javax/lang/model/element/Extra.class");
 149         checkFileExists(classes, "jdk.compiler/com/sun/source/tree/Extra.class");
 150         checkFileExists(classes, "m3x/m3/Test.class");
 151         checkFileExists(classes, "m4x/m4/Test.class");
 152     }
 153 
 154     @Test
 155     public void testPatchModuleModuleSourcePathConflict(Path base) throws Exception {
 156         //note: avoiding use of java.base, as that gets special handling on some places:
 157         Path src = base.resolve("src");
 158         Path m1 = src.resolve("m1x");
 159         tb.writeJavaFiles(m1,
 160                           "module m1x { }",
 161                           "package m1; public class Test { }");
 162         Path m2 = src.resolve("m2x");
 163         tb.writeJavaFiles(m2,
 164                           "module m2x { }",
 165                           "package m2; public class Test { }");
 166         Path classes = base.resolve("classes");
 167         tb.createDirectories(classes);
 168 
 169         List<String> log = new JavacTask(tb)
 170                 .options("--patch-module", "m1x=" + m1.toString(),
 171                          "--patch-module", "m2x=" + m2.toString(),
 172                          "--module-source-path", src.toString(),
 173                          "-XDrawDiagnostics")
 174                 .outdir(classes)
 175                 .files(findJavaFiles(src.resolve("m1x").resolve("m1"),
 176                                      src.resolve("m2x").resolve("m2")))
 177                 .run(Expect.FAIL)
 178                 .writeAll()
 179                 .getOutputLines(Task.OutputKind.DIRECT);
 180 
 181         List<String> expectedOut = Arrays.asList(
 182                 "Test.java:1:1: compiler.err.file.patched.and.msp",
 183                 "Test.java:1:1: compiler.err.file.patched.and.msp",
 184                 "module-info.java:1:1: compiler.err.file.patched.and.msp",
 185                 "- compiler.err.cant.access: m2x.module-info, (compiler.misc.cant.resolve.modules)",
 186                 "4 errors"
 187         );
 188 
 189         if (!expectedOut.equals(log))
 190             throw new Exception("expected output not found: " + log);
 191     }
 192 
 193     @Test
 194     public void testSourcePath(Path base) throws Exception {
 195         //note: avoiding use of java.base, as that gets special handling on some places:
 196         Path src = base.resolve("src");
 197         tb.writeJavaFiles(src, "package javax.lang.model.element; public interface Extra extends Element, Other { }");
 198         Path srcPath = base.resolve("src-path");
 199         tb.writeJavaFiles(srcPath, "package javax.lang.model.element; interface Other { }");
 200         Path classes = base.resolve("classes");
 201         tb.createDirectories(classes);
 202 
 203         List<String> log = new JavacTask(tb)
 204                 .options("--patch-module", "java.compiler=" + src.toString(),
 205                          "-sourcepath", srcPath.toString(),
 206                          "-XDrawDiagnostics")
 207                 .outdir(classes)
 208                 .files(src.resolve("javax/lang/model/element/Extra.java"))
 209                 .run(Expect.FAIL)
 210                 .writeAll()
 211                 .getOutputLines(Task.OutputKind.DIRECT);
 212 
 213         List<String> expectedOut = Arrays.asList(
 214                 "Extra.java:1:75: compiler.err.cant.resolve: kindname.class, Other, , ",
 215                 "1 error"
 216         );
 217 
 218         if (!expectedOut.equals(log))
 219             throw new Exception("expected output not found: " + log);
 220     }
 221 
 222     @Test
 223     public void testClassPath(Path base) throws Exception {
 224         Path cpSrc = base.resolve("cpSrc");
 225         tb.writeJavaFiles(cpSrc, "package p; public interface Other { }");
 226         Path cpClasses = base.resolve("cpClasses");
 227         tb.createDirectories(cpClasses);
 228 
 229         String cpLog = new JavacTask(tb)
 230                 .outdir(cpClasses)
 231                 .files(findJavaFiles(cpSrc))
 232                 .run()
 233                 .writeAll()
 234                 .getOutput(Task.OutputKind.DIRECT);
 235 
 236         if (!cpLog.isEmpty())
 237             throw new Exception("expected output not found: " + cpLog);
 238 
 239         Path src = base.resolve("src");
 240         //note: avoiding use of java.base, as that gets special handling on some places:
 241         tb.writeJavaFiles(src, "package javax.lang.model.element; public interface Extra extends Element, p.Other { }");
 242         Path classes = base.resolve("classes");
 243         tb.createDirectories(classes);
 244 
 245         List<String> log = new JavacTask(tb)
 246                 .options("--patch-module", "java.compiler=" + src.toString(),
 247                          "--class-path", cpClasses.toString(),
 248                          "-XDrawDiagnostics")
 249                 .outdir(classes)
 250                 .files(src.resolve("javax/lang/model/element/Extra.java"))
 251                 .run(Expect.FAIL)
 252                 .writeAll()
 253                 .getOutputLines(Task.OutputKind.DIRECT);
 254 
 255         List<String> expectedOut = Arrays.asList(
 256                 "Extra.java:1:76: compiler.err.doesnt.exist: p",
 257                 "1 error"
 258         );
 259 
 260         if (!expectedOut.equals(log))
 261             throw new Exception("expected output not found: " + log);
 262     }
 263 
 264     @Test
 265     public void testNoModuleInfoOnSourcePath(Path base) throws Exception {
 266         //note: avoiding use of java.base, as that gets special handling on some places:
 267         Path src = base.resolve("src");
 268         tb.writeJavaFiles(src,

 269                           "package javax.lang.model.element; public interface Extra { }");
 270         Path srcPath = base.resolve("src-path");
 271         tb.writeJavaFiles(src,
 272                           "module java.compiler {}");
 273         Path classes = base.resolve("classes");
 274         tb.createDirectories(classes);
 275 
 276         List<String> log = new JavacTask(tb)
 277                 .options("-XDrawDiagnostics",
 278                          "--patch-module", "java.compiler=" + src.toString())
 279                 .outdir(classes)
 280                 .files(findJavaFiles(src))
 281                 .run(Task.Expect.FAIL)
 282                 .writeAll()
 283                 .getOutputLines(Task.OutputKind.DIRECT);
 284 
 285         List<String> expected = Arrays.asList("Extra.java:1:1: compiler.err.module-info.with.patched.module.sourcepath",
 286                                               "1 error");
 287 
 288         if (!expected.equals(log))
 289             throw new Exception("expected output not found: " + log);
 290     }
 291 
 292     @Test
 293     public void testNoModuleInfoInClassOutput(Path base) throws Exception {
 294         //note: avoiding use of java.base, as that gets special handling on some places:
 295         Path srcMod = base.resolve("src-mod");
 296         tb.writeJavaFiles(srcMod,
 297                           "module mod {}");
 298         Path classes = base.resolve("classes");
 299         tb.createDirectories(classes);
 300 
 301         String logMod = new JavacTask(tb)
 302                 .options()
 303                 .outdir(classes)
 304                 .files(findJavaFiles(srcMod))
 305                 .run()
 306                 .writeAll()
 307                 .getOutput(Task.OutputKind.DIRECT);
 308 
 309         if (!logMod.isEmpty())
 310             throw new Exception("unexpected output found: " + logMod);
 311 
 312         Path src = base.resolve("src");
 313         tb.writeJavaFiles(src,
 314                           "package javax.lang.model.element; public interface Extra { }");
 315         tb.createDirectories(classes);
 316 
 317         List<String> log = new JavacTask(tb)
 318                 .options("-XDrawDiagnostics",
 319                          "--patch-module", "java.compiler=" + src.toString())
 320                 .outdir(classes)
 321                 .files(findJavaFiles(src))
 322                 .run(Task.Expect.FAIL)
 323                 .writeAll()
 324                 .getOutputLines(Task.OutputKind.DIRECT);
 325 
 326         List<String> expected = Arrays.asList("Extra.java:1:1: compiler.err.module-info.with.patched.module.classpath",
 327                                               "1 error");
 328 
 329         if (!expected.equals(log))
 330             throw new Exception("expected output not found: " + log);
 331     }
 332 
 333     @Test














































 334     public void testWithModulePath(Path base) throws Exception {
 335         Path modSrc = base.resolve("modSrc");
 336         Path modules = base.resolve("modules");
 337         new ModuleBuilder(tb, "m1")
 338                 .classes("package pkg1; public interface E { }")
 339                 .build(modSrc, modules);
 340 
 341         Path src = base.resolve("src");
 342         tb.writeJavaFiles(src, "package p; interface A extends pkg1.E { }");
 343 
 344         new JavacTask(tb, Task.Mode.CMDLINE)
 345                 .options("--module-path", modules.toString(),
 346                         "--patch-module", "m1=" + src.toString())
 347                 .files(findJavaFiles(src))
 348                 .run()
 349                 .writeAll();
 350 
 351         //checks module bounds still exist
 352         new ModuleBuilder(tb, "m2")
 353                 .classes("package pkg2; public interface D { }")
 354                 .build(modSrc, modules);
 355 
 356         Path src2 = base.resolve("src2");
 357         tb.writeJavaFiles(src2, "package p; interface A extends pkg2.D { }");
 358 
 359         List<String> log = new JavacTask(tb, Task.Mode.CMDLINE)
 360                 .options("-XDrawDiagnostics",
 361                         "--module-path", modules.toString(),
 362                         "--patch-module", "m1=" + src2.toString())
 363                 .files(findJavaFiles(src2))
 364                 .run(Task.Expect.FAIL)
 365                 .writeAll()
 366                 .getOutputLines(Task.OutputKind.DIRECT);
 367 
 368         List<String> expected = Arrays.asList("A.java:1:32: compiler.err.package.not.visible: pkg2, (compiler.misc.not.def.access.does.not.read: m1, pkg2, m2)",
 369                 "1 error");
 370 
 371         if (!expected.equals(log))
 372             throw new Exception("expected output not found: " + log);
 373     }
 374 
 375     @Test
 376     public void testWithUpgradeModulePath(Path base) throws Exception {
 377         Path modSrc = base.resolve("modSrc");
 378         Path modules = base.resolve("modules");
 379         new ModuleBuilder(tb, "m1")
 380                 .classes("package pkg1; public interface E { }")
 381                 .build(modSrc, modules);
 382 
 383         Path upgrSrc = base.resolve("upgradeSrc");
 384         Path upgrade = base.resolve("upgrade");
 385         new ModuleBuilder(tb, "m1")
 386                 .classes("package pkg1; public interface D { }")
 387                 .build(upgrSrc, upgrade);
 388 
 389         Path src = base.resolve("src");
 390         tb.writeJavaFiles(src, "package p; interface A extends pkg1.D { }");
 391 
 392         new JavacTask(tb, Task.Mode.CMDLINE)
 393                 .options("--module-path", modules.toString(),
 394                         "--upgrade-module-path", upgrade.toString(),
 395                         "--patch-module", "m1=" + src.toString())
 396                 .files(findJavaFiles(src))
 397                 .run()
 398                 .writeAll();
 399     }
 400 
 401     @Test
 402     public void testUnnamedIsolation(Path base) throws Exception {
 403         //note: avoiding use of java.base, as that gets special handling on some places:
 404         Path sourcePath = base.resolve("source-path");
 405         tb.writeJavaFiles(sourcePath, "package src; public class Src {}");
 406 
 407         Path classPathSrc = base.resolve("class-path-src");
 408         tb.writeJavaFiles(classPathSrc, "package cp; public class CP { }");
 409         Path classPath = base.resolve("classPath");
 410         tb.createDirectories(classPath);
 411 
 412         String cpLog = new JavacTask(tb)
 413                 .outdir(classPath)
 414                 .files(findJavaFiles(classPathSrc))
 415                 .run()


 425                           "package m; public class M {}");
 426         Path modulePath = base.resolve("modulePath");
 427         tb.createDirectories(modulePath.resolve("m"));
 428 
 429         String modLog = new JavacTask(tb)
 430                 .outdir(modulePath.resolve("m"))
 431                 .files(findJavaFiles(modulePathSrc))
 432                 .run()
 433                 .writeAll()
 434                 .getOutput(Task.OutputKind.DIRECT);
 435 
 436         if (!modLog.isEmpty())
 437             throw new Exception("expected output not found: " + modLog);
 438 
 439         Path src = base.resolve("src");
 440         tb.writeJavaFiles(src, "package m; public class Extra { }");
 441         Path classes = base.resolve("classes");
 442         tb.createDirectories(classes);
 443 
 444         String log = new JavacTask(tb)
 445                 .options("--patch-module", "m=" + sourcePath.toString(),
 446                          "--class-path", classPath.toString(),
 447                          "--source-path", sourcePath.toString(),
 448                          "--module-path", modulePath.toString(),
 449                          "--processor-path", System.getProperty("test.classes"),
 450                          "-XDaccessInternalAPI=true",
 451                          "-processor", CheckModuleContentProcessing.class.getName())
 452                 .outdir(classes)
 453                 .files(findJavaFiles(sourcePath))
 454                 .run()
 455                 .writeAll()
 456                 .getOutput(Task.OutputKind.DIRECT);
 457 
 458         if (!log.isEmpty())
 459             throw new Exception("expected output not found: " + log);
 460     }
 461 
 462     @SupportedAnnotationTypes("*")
 463     public static final class CheckModuleContentProcessing extends AbstractProcessor {
 464 
 465         @Override


 479         }
 480 
 481         @Override
 482         public SourceVersion getSupportedSourceVersion() {
 483             return SourceVersion.latest();
 484         }
 485 
 486         private static void assertNonNull(String msg, Object val) {
 487             if (val == null) {
 488                 throw new AssertionError(msg);
 489             }
 490         }
 491 
 492         private static void assertNull(String msg, Object val) {
 493             if (val != null) {
 494                 throw new AssertionError(msg);
 495             }
 496         }
 497     }
 498 
 499     private void checkFileExists(Path dir, String path) {
 500         Path toCheck = dir.resolve(path.replace("/", dir.getFileSystem().getSeparator()));
 501 
 502         if (!Files.exists(toCheck)) {
 503             throw new AssertionError(toCheck.toString() + " does not exist!");
 504         }
 505     }
 506 }
< prev index next >