langtools/test/tools/javac/modules/EdgeCases.java
author jjg
Thu, 31 Mar 2016 15:20:50 -0700
changeset 36778 e04318f39f92
parent 36526 3b41f1c69604
child 37744 bf4fd5e022c5
permissions -rw-r--r--
8152897: refactor ToolBox to allow reduced documented dependencies Reviewed-by: vromero
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    23
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/*
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @summary tests for multi-module mode compilation
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @library /tools/lib
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @modules
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 *      jdk.compiler/com.sun.tools.javac.api
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 *      jdk.compiler/com.sun.tools.javac.code
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 *      jdk.compiler/com.sun.tools.javac.main
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    32
 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask ModuleTestBase
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 * @run main EdgeCases
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    35
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.io.Writer;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.file.Files;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.nio.file.Path;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.nio.file.Paths;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.Arrays;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.HashSet;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.List;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.Objects;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.Set;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    45
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import javax.lang.model.element.Element;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import javax.tools.JavaCompiler;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import javax.tools.JavaFileObject;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import javax.tools.StandardJavaFileManager;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import javax.tools.ToolProvider;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    51
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import com.sun.source.tree.CompilationUnitTree;
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    53
//import com.sun.source.util.JavacTask; // conflicts with toolbox.JavacTask
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import com.sun.tools.javac.api.JavacTaskImpl;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    56
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    57
import toolbox.JarTask;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    58
import toolbox.JavacTask;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    59
import toolbox.Task;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    60
import toolbox.ToolBox;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    61
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    62
public class EdgeCases extends ModuleTestBase {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    63
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    public static void main(String... args) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        new EdgeCases().runTests();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    67
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    @Test
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    69
    void testAddExportUndefinedModule(Path base) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        Path src = base.resolve("src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        tb.writeJavaFiles(src, "package test; import undef.Any; public class Test {}");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        tb.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    74
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    75
        List<String> log = new JavacTask(tb)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    76
                .options("-XaddExports:undef/undef=ALL-UNNAMED", "-XDrawDiagnostics")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    77
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    78
                .files(findJavaFiles(src))
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    79
                .run(Task.Expect.FAIL)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    80
                .writeAll()
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    81
                .getOutputLines(Task.OutputKind.DIRECT);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    82
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        List<String> expected = Arrays.asList("- compiler.err.cant.find.module: undef",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                                              "Test.java:1:27: compiler.err.doesnt.exist: undef",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                                              "2 errors");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    86
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        if (!expected.equals(log))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            throw new Exception("expected output not found: " + log);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    89
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    90
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    91
    @Test
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    void testModuleSymbolOutterMostClass(Path base) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    95
            Path moduleSrc = base.resolve("module-src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    96
            Path m1 = moduleSrc.resolve("m1");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    97
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    98
            tb.writeJavaFiles(m1, "module m1 { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    99
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(moduleSrc));
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   101
            com.sun.source.util.JavacTask task =
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   102
                (com.sun.source.util.JavacTask) compiler.getTask(null, fm, null, null, null, files);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   103
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            task.analyze();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   105
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   106
            ModuleSymbol msym = (ModuleSymbol) task.getElements().getModuleElement("m1");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   107
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   108
            msym.outermostClass();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   110
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   111
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   112
    @Test
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   113
    void testParseEnterAnalyze(Path base) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   116
            Path moduleSrc = base.resolve("module-src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            Path m1 = moduleSrc.resolve("m1");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   118
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            tb.writeJavaFiles(m1, "module m1 { }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                                  "package p;",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                                  "package p; class T { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   122
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(moduleSrc));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   125
            List<String> options = Arrays.asList("-d", classes.toString(), "-Xpkginfo:always");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   126
            JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, fm, null, options, null, files);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   127
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            Iterable<? extends CompilationUnitTree> parsed = task.parse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            Iterable<? extends Element> entered = task.enter(parsed);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   130
            Iterable<? extends Element> analyzed = task.analyze(entered);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   131
            Iterable<? extends JavaFileObject> generatedFiles = task.generate(analyzed);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   132
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            Set<String> generated = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   134
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            for (JavaFileObject jfo : generatedFiles) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   136
                generated.add(jfo.getName());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   137
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   138
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   139
            Set<String> expected = new HashSet<>(
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                    Arrays.asList(Paths.get("testParseEnterAnalyze", "classes", "p", "package-info.class").toString(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   141
                                  Paths.get("testParseEnterAnalyze", "classes", "module-info.class").toString(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                                  Paths.get("testParseEnterAnalyze", "classes", "p", "T.class").toString())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   143
            );
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   144
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   145
            if (!Objects.equals(expected, generated))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                throw new AssertionError("Incorrect generated files: " + generated);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   148
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   149
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   150
    @Test
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   151
    void testModuleImplicitModuleBoundaries(Path base) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        Path src = base.resolve("src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        Path src_m1 = src.resolve("m1");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        tb.writeJavaFiles(src_m1,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   155
                          "module m1 { exports api1; }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   156
                          "package api1; public class Api1 { public void call() { } }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        Path src_m2 = src.resolve("m2");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        tb.writeJavaFiles(src_m2,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   159
                          "module m2 { requires m1; exports api2; }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   160
                          "package api2; public class Api2 { public static api1.Api1 get() { return null; } }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        Path src_m3 = src.resolve("m3");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        tb.writeJavaFiles(src_m3,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                          "module m3 { requires m2; }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   164
                          "package test; public class Test { { api2.Api2.get().call(); api2.Api2.get().toString(); } }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        tb.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   167
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   168
        String log = new JavacTask(tb)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   169
                .options("-XDrawDiagnostics",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   170
                         "-modulesourcepath", src.toString())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   171
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   172
                .files(findJavaFiles(src))
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   173
                .run(Task.Expect.FAIL)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   174
                .writeAll()
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   175
                .getOutput(Task.OutputKind.DIRECT);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   176
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   177
        if (!log.contains("Test.java:1:52: compiler.err.not.def.access.class.intf.cant.access: call(), api1.Api1") ||
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   178
            !log.contains("Test.java:1:76: compiler.err.not.def.access.class.intf.cant.access: toString(), java.lang.Object"))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   179
            throw new Exception("expected output not found");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   180
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   181
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   182
    @Test
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   183
    void testAssignClassToAutomaticModule(Path base) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   184
        //check that if a ClassSymbol belongs to an automatic module, it is properly assigned and not
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   185
        //duplicated when being accessed through a classfile.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        Path automaticSrc = base.resolve("automaticSrc");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   187
        tb.writeJavaFiles(automaticSrc, "package api1; public class Api1 {}");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   188
        Path automaticClasses = base.resolve("automaticClasses");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   189
        tb.createDirectories(automaticClasses);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   190
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   191
        String automaticLog = new JavacTask(tb)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   192
                                .outdir(automaticClasses)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   193
                                .files(findJavaFiles(automaticSrc))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   194
                                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   195
                                .writeAll()
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   196
                                .getOutput(Task.OutputKind.DIRECT);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   197
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   198
        if (!automaticLog.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   199
            throw new Exception("expected output not found: " + automaticLog);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   200
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   201
        Path modulePath = base.resolve("module-path");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   202
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   203
        Files.createDirectories(modulePath);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   204
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   205
        Path automaticJar = modulePath.resolve("m1-1.0.jar");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   206
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   207
        new JarTask(tb, automaticJar)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   208
          .baseDir(automaticClasses)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   209
          .files("api1/Api1.class")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   210
          .run();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   211
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   212
        Path src = base.resolve("src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   213
        Path src_m2 = src.resolve("m2");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   214
        tb.writeJavaFiles(src_m2,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   215
                          "module m2 { requires m1; exports api2; }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   216
                          "package api2; public class Api2 { public static api1.Api1 get() { return null; } }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        Path src_m3 = src.resolve("m3");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   218
        tb.writeJavaFiles(src_m3,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   219
                          "module m3 { requires m1; requires m2; }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   220
                          "package test; public class Test { { api2.Api2.get(); api1.Api1 a1; } }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   221
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   222
        tb.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   223
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   224
        new JavacTask(tb)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   225
                .options("-modulepath", modulePath.toString(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   226
                         "-modulesourcepath", src.toString())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   227
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   228
                .files(findJavaFiles(src_m2))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   229
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   230
                .writeAll();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   231
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   232
        new JavacTask(tb)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   233
                .options("-modulepath", modulePath.toString(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                         "-modulesourcepath", src.toString())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   235
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   236
                .files(findJavaFiles(src_m3))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   237
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   238
                .writeAll();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   239
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   240
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   241
    @Test
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   242
    void testEmptyImplicitModuleInfo(Path base) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   243
        Path src = base.resolve("src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   244
        Path src_m1 = src.resolve("m1");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   245
        Files.createDirectories(src_m1);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   246
        try (Writer w = Files.newBufferedWriter(src_m1.resolve("module-info.java"))) {}
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   247
        tb.writeJavaFiles(src_m1,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   248
                          "package test; public class Test {}");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   249
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   250
        tb.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   251
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   252
        new JavacTask(tb)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                .options("-sourcepath", src_m1.toString(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   254
                         "-XDrawDiagnostics")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   255
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   256
                .files(findJavaFiles(src_m1.resolve("test")))
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   257
                .run(Task.Expect.FAIL)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   258
                .writeAll();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   259
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   260
        tb.writeJavaFiles(src_m1,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   261
                          "module m1 {}");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   262
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   263
        new JavacTask(tb)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   264
                .options("-sourcepath", src_m1.toString())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   265
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   266
                .files(findJavaFiles(src_m1.resolve("test")))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   267
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   268
                .writeAll();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   269
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   270
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   271
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   272
}