make/jdk/src/classes/build/tools/module/ModuleInfoExtraTest.java
author ihse
Mon, 03 Dec 2018 18:46:10 +0100
changeset 52804 28094715ae71
parent 51499 fdd768b9865e
permissions -rw-r--r--
8214718: Update missing copyright year in build system Reviewed-by: erikj, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     1
/*
52804
28094715ae71 8214718: Update missing copyright year in build system
ihse
parents: 51499
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     4
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    10
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    15
 * accompanied this code).
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    16
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    20
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    23
 * questions.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    24
 */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    25
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    26
package build.tools.module;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    27
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    28
import java.io.BufferedWriter;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    29
import java.io.IOException;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    30
import java.io.PrintWriter;
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    31
import java.io.UncheckedIOException;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    32
import java.nio.file.Files;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    33
import java.nio.file.Path;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    34
import java.nio.file.Paths;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    35
import java.util.Arrays;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    36
import java.util.Collections;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    37
import java.util.HashSet;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    38
import java.util.List;
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    39
import java.util.Map;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    40
import java.util.Set;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    41
import build.tools.module.GenModuleInfoSource.Statement;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    42
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    43
/**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    44
 * Sanity test for GenModuleInfoSource tool
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    45
 */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    46
public class ModuleInfoExtraTest {
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    47
    private static final Path DIR = Paths.get("gen-module-info-test");
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    48
    private static boolean verbose = false;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    49
    public static void main(String... args) throws Exception {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    50
        if (args.length != 0)
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    51
            verbose = true;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    52
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    53
        GenModuleInfoSource.verbose = verbose;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    54
        ModuleInfoExtraTest test = new ModuleInfoExtraTest("m", "m1", "m2", "m3");
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    55
        test.testModuleInfo();
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    56
        test.errorCases();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    57
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    58
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    59
    String[] moduleInfo = new String[] {
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    60
        "module m {",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    61
        "    requires m1;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    62
        "    requires transitive m2;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    63
        "    exports p",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    64
        "    to",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    65
        "               // comment ... ",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    66
        "    /* comment */ m1",
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    67
        ",",
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    68
        "       m2,m3",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    69
        "  ;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    70
        "    exports q to m1;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    71
        "    provides s with /*   ",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    72
        "    comment */ impl     ;    // comment",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    73
        "    provides s1",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    74
        "       with  ",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    75
        "       impl1, impl2;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    76
        "}"
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    77
    };
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    78
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    79
    String[] moduleInfoExtra = new String[] {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    80
        "exports q",
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    81
        "to",
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    82
        "   m2 // comment",
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    83
        "   /* comment */;",
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    84
        "   ;",
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    85
        "opens p.q ",
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    86
        "   to /* comment */ m3",
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    87
        "   , // m1",
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    88
        "   /* comment */ m4; uses p.I",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
    89
        ";   provides s1 with impl3;"
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    90
    };
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    91
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    92
    final Builder builder;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    93
    ModuleInfoExtraTest(String name, String... modules) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    94
        this.builder = new Builder(name).modules(modules);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    95
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    96
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    97
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    98
    void testModuleInfo() throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    99
        GenModuleInfoSource source = builder.sourceFile(moduleInfo).build();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   100
        Set<String> targetsP = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   101
        targetsP.add("m1");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   102
        targetsP.add("m2");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   103
        targetsP.add("m3");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   104
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   105
        Set<String> targetsQ = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   106
        targetsQ.add("m1");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   107
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   108
        Set<String> providerS = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   109
        providerS.add("impl");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   110
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   111
        Set<String> providerS1 = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   112
        providerS1.add("impl1");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   113
        providerS1.add("impl2");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   114
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   115
        Set<String> opensPQ = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   116
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   117
        check(source, targetsP, targetsQ, opensPQ, providerS, providerS1);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   118
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   119
        // augment with extra
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   120
        Path file = DIR.resolve("extra");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   121
        Files.write(file, Arrays.asList(moduleInfoExtra));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   122
        source = builder.build(file);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   123
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   124
        targetsQ.add("m2");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   125
        providerS1.add("impl3");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   126
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   127
        opensPQ.add("m3");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   128
        check(source, targetsP, targetsQ, opensPQ, providerS, providerS1);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   129
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   130
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   131
    void check(GenModuleInfoSource source,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   132
               Set<String> targetsP,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   133
               Set<String> targetsQ,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   134
               Set<String> opensPQ,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   135
               Set<String> providerS,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   136
               Set<String> providerS1) {
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   137
        if (verbose)
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   138
            source.moduleInfo.print(new PrintWriter(System.out, true));
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   139
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   140
        Statement export = source.moduleInfo.exports.get("p");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   141
        if (!export.targets.equals(targetsP)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   142
            throw new Error("unexpected: " + export);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   143
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   144
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   145
        export = source.moduleInfo.exports.get("q");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   146
        if (!export.targets.equals(targetsQ)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   147
            throw new Error("unexpected: " + export);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   148
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   149
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   150
        Statement provides = source.moduleInfo.provides.get("s");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   151
        if (!provides.targets.equals(providerS)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   152
            throw new Error("unexpected: " + provides);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   153
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   154
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   155
        provides = source.moduleInfo.provides.get("s1");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   156
        if (!provides.targets.equals(providerS1)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   157
            throw new Error("unexpected: " + provides);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   158
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   159
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   160
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   161
    final Map<String[], String> badModuleInfos = Map.of(
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   162
        new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   163
            "module x {",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   164
            "   exports p1 to ",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   165
            "           m1",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   166
            "}"
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   167
        },                      ".*, line .*, missing semicolon.*",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   168
        new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   169
            "module x ",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   170
            "   exports p1;"
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   171
        },                      ".*, line .*, missing \\{.*",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   172
        new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   173
            "module x {",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   174
            "   requires m1;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   175
            "   requires",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   176
            "}"
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   177
        },                      ".*, line .*, <identifier> missing.*",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   178
        new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   179
            "module x {",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   180
            "   requires transitive m1",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   181
            "}"
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   182
        },                      ".*, line .*, missing semicolon.*",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   183
        new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   184
            "module x {",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   185
            "   exports p1 to m1;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   186
            "   exports p1 to m2;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   187
            "}"
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   188
        },                      ".*, line .*, multiple exports p1.*"
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   189
    );
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   190
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   191
    final Map<String[], String> badExtraFiles = Map.of(
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   192
            new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   193
                "requires m2;"     // not allowed
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   194
            },                      ".*, line .*, cannot declare requires .*",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   195
            new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   196
                "exports p1 to m1;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   197
                "exports p2"            // missing semicolon
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   198
            },                      ".*, line .*, reach end of file.*",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   199
            new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   200
                "exports to m1;"        // missing <identifier>
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   201
            },                      ".*, line .*, <identifier> missing.*",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   202
            new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   203
                "exports p3 to m1;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   204
                "    m2, m3;"           // missing keyword
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   205
            },                      ".*, line .*, missing keyword.*",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   206
            new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   207
                "provides s with impl1;",   // typo ; should be ,
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   208
                "   impl2, impl3;"
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   209
            },                      ".*, line .*, missing keyword.*",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   210
            new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   211
                "uses s3",                  // missing semicolon
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   212
                "provides s3 with impl1,",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   213
                "   impl2, impl3;"
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   214
            },                      ".*, line .*, missing semicolon.*",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   215
            new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   216
                "opens p1 to m1,, m2;"     // missing identifier
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   217
            },                      ".*, line .*, <identifier> missing.*"
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   218
    );
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   219
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   220
    final Map<String[], String> duplicates = Map.of(
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   221
            new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   222
                "   exports p1 to m1, m2;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   223
                "   exports p1 to m3;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   224
            },                      ".*, line .*, multiple exports p1.*",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   225
            new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   226
                "   opens p1 to m1, m2;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   227
                "   exports p1 to m3;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   228
                "   opens p1 to m3;"
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   229
            },                      ".*, line .*, multiple opens p1.*",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   230
            new String[] {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   231
                "   uses s;",
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   232
                "   uses s;"
51499
fdd768b9865e 8167314: Enable the check to detect duplicate provides in in GenModuleInfoSource
mchung
parents: 51339
diff changeset
   233
            },                      ".*, line .*, multiple uses s.*",
fdd768b9865e 8167314: Enable the check to detect duplicate provides in in GenModuleInfoSource
mchung
parents: 51339
diff changeset
   234
            new String[] {
fdd768b9865e 8167314: Enable the check to detect duplicate provides in in GenModuleInfoSource
mchung
parents: 51339
diff changeset
   235
                "   provides s with impl1;",
fdd768b9865e 8167314: Enable the check to detect duplicate provides in in GenModuleInfoSource
mchung
parents: 51339
diff changeset
   236
                "   provides s with impl2, impl3;"
fdd768b9865e 8167314: Enable the check to detect duplicate provides in in GenModuleInfoSource
mchung
parents: 51339
diff changeset
   237
            },                      ".*, line .*, multiple provides s.*"
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   238
    );
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   239
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   240
    void errorCases() {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   241
        badModuleInfos.entrySet().stream().forEach(e -> badModuleInfoFile(e.getKey(), e.getValue()));
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   242
        badExtraFiles.entrySet().stream().forEach(e -> badExtraFile(e.getKey(), e.getValue()));
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   243
        duplicates.entrySet().stream().forEach(e -> badExtraFile(e.getKey(), e.getValue()));
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   244
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   245
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   246
    void badModuleInfoFile(String[] lines, String regex)  {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   247
        Builder builder = new Builder("x").modules("m1", "m2", "m3");
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   248
        try {
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   249
            GenModuleInfoSource source = builder.sourceFile(lines).build();
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   250
            throw new RuntimeException("Expected error: " + Arrays.toString(lines));
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   251
        } catch (IOException e) {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   252
            throw new UncheckedIOException(e);
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   253
        } catch (Error e) {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   254
            if (!e.getMessage().matches(regex)) {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   255
                throw e;
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   256
            }
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   257
        }
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   258
    }
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   259
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   260
    void badExtraFile(String[] extras, String regex)  {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   261
        Path file = DIR.resolve("test1");
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   262
        try {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   263
            Files.write(file, Arrays.asList(extras));
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   264
            builder.build(file);
51339
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   265
            Files.deleteIfExists(file);
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   266
            throw new RuntimeException("Expected error: " + Arrays.toString(extras));
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   267
        } catch (IOException e) {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   268
            throw new UncheckedIOException(e);
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   269
        } catch (Error e) {
554bb4e2d10d 8202941: GenModuleInfoSource build tool does not detect missing semicolons
mchung
parents: 47216
diff changeset
   270
            if (!e.getMessage().matches(regex)) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   271
                throw e;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   272
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   273
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   274
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   275
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   276
    static class Builder {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   277
        final String moduleName;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   278
        final Path sourceFile;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   279
        final Set<String> modules = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   280
        public Builder(String name) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   281
            this.moduleName = name;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   282
            this.sourceFile = DIR.resolve(name).resolve("module-info.java");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   283
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   284
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   285
        public Builder modules(String... names) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   286
            Arrays.stream(names).forEach(modules::add);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   287
            return this;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   288
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   289
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   290
        public Builder sourceFile(String... lines) throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   291
            Files.createDirectories(sourceFile.getParent());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   292
            try (BufferedWriter bw = Files.newBufferedWriter(sourceFile);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   293
                 PrintWriter writer = new PrintWriter(bw)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   294
                for (String l : lines) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   295
                    writer.println(l);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   296
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   297
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   298
            return this;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   299
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   300
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   301
        public GenModuleInfoSource build() throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   302
            return build(Collections.emptyList());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   303
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   304
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   305
        public GenModuleInfoSource build(Path extraFile) throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   306
            return build(Collections.singletonList(extraFile));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   307
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   308
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   309
        public GenModuleInfoSource build(List<Path> extraFiles) throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   310
            return new GenModuleInfoSource(sourceFile, extraFiles, modules);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   311
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   312
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   313
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   314
}