make/jdk/src/classes/build/tools/module/ModuleInfoExtraTest.java
changeset 47216 71c04702a3d5
parent 42338 a60f280f803c
child 51339 554bb4e2d10d
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package build.tools.module;
       
    27 
       
    28 import java.io.BufferedWriter;
       
    29 import java.io.IOException;
       
    30 import java.io.PrintWriter;
       
    31 import java.nio.file.Files;
       
    32 import java.nio.file.Path;
       
    33 import java.nio.file.Paths;
       
    34 import java.util.Arrays;
       
    35 import java.util.Collections;
       
    36 import java.util.HashSet;
       
    37 import java.util.List;
       
    38 import java.util.Set;
       
    39 import build.tools.module.GenModuleInfoSource.Statement;
       
    40 
       
    41 /**
       
    42  * Sanity test for GenModuleInfoSource tool
       
    43  */
       
    44 public class ModuleInfoExtraTest {
       
    45     private static final Path DIR = Paths.get("test");
       
    46     public static void main(String... args) throws Exception {
       
    47         if (args.length != 0)
       
    48             GenModuleInfoSource.verbose = true;
       
    49 
       
    50         ModuleInfoExtraTest test = new ModuleInfoExtraTest("m", "m1", "m2", "m3");
       
    51         test.run();
       
    52     }
       
    53 
       
    54     String[] moduleInfo = new String[] {
       
    55         "exports p",
       
    56         "to",
       
    57         "   // comment",
       
    58         "   /* comment */ m1",
       
    59         ",",
       
    60         "m2,m3",
       
    61         "   ;",
       
    62         "exports q to m1;",
       
    63         "provides s with /* ",
       
    64         "  comment */ impl     ;    // comment",
       
    65         "provides s1",
       
    66         "    with  ",
       
    67         "    impl1, impl2;"
       
    68     };
       
    69 
       
    70     String[] moduleInfoExtra = new String[] {
       
    71         "exports q",
       
    72         "to",
       
    73         "   m2 // comment",
       
    74         "   /* comment */;",
       
    75         "   ;",
       
    76         "opens p.q ",
       
    77         "   to /* comment */ m3",
       
    78         "   , // m1",
       
    79         "   /* comment */, m4;",
       
    80         "provides s1 with impl3;"
       
    81     };
       
    82 
       
    83     String[] test1 = new String[] {
       
    84         "exports p1 to m1;",
       
    85         "exports p2"
       
    86     };
       
    87 
       
    88     String[] test2 = new String[] {
       
    89         "exports to m1;"
       
    90     };
       
    91 
       
    92     String[] test3 = new String[]{
       
    93         "exports p3 to m1;",
       
    94         "    m2, m3;"
       
    95     };
       
    96 
       
    97     String[] test4 = new String[]{
       
    98         "provides s with impl1;",   // typo ; should be ,
       
    99         "   impl2, impl3;"
       
   100     };
       
   101 
       
   102     String[] test5 = new String[]{
       
   103         "uses s3",
       
   104         "provides s3 with impl1,",
       
   105         "   impl2, impl3;"
       
   106     };
       
   107 
       
   108     final Builder builder;
       
   109     ModuleInfoExtraTest(String name, String... modules) {
       
   110         this.builder = new Builder(name).modules(modules);
       
   111     }
       
   112 
       
   113     void run() throws IOException {
       
   114         testModuleInfo();
       
   115         errorCases();
       
   116     }
       
   117 
       
   118 
       
   119     void testModuleInfo() throws IOException {
       
   120         GenModuleInfoSource source = builder.sourceFile(moduleInfo).build();
       
   121         Set<String> targetsP = new HashSet<>();
       
   122         targetsP.add("m1");
       
   123         targetsP.add("m2");
       
   124         targetsP.add("m3");
       
   125 
       
   126         Set<String> targetsQ = new HashSet<>();
       
   127         targetsQ.add("m1");
       
   128 
       
   129         Set<String> providerS = new HashSet<>();
       
   130         providerS.add("impl");
       
   131 
       
   132         Set<String> providerS1 = new HashSet<>();
       
   133         providerS1.add("impl1");
       
   134         providerS1.add("impl2");
       
   135 
       
   136         Set<String> opensPQ = new HashSet<>();
       
   137 
       
   138         check(source, targetsP, targetsQ, opensPQ, providerS, providerS1);
       
   139 
       
   140         // augment with extra
       
   141         Path file = DIR.resolve("extra");
       
   142         Files.write(file, Arrays.asList(moduleInfoExtra));
       
   143         source = builder.build(file);
       
   144 
       
   145         targetsQ.add("m2");
       
   146         providerS1.add("impl3");
       
   147 
       
   148         opensPQ.add("m3");
       
   149         check(source, targetsP, targetsQ, opensPQ, providerS, providerS1);
       
   150     }
       
   151 
       
   152     void check(GenModuleInfoSource source,
       
   153                Set<String> targetsP,
       
   154                Set<String> targetsQ,
       
   155                Set<String> opensPQ,
       
   156                Set<String> providerS,
       
   157                Set<String> providerS1) {
       
   158         source.moduleInfo.print(new PrintWriter(System.out, true));
       
   159         Statement export = source.moduleInfo.exports.get("p");
       
   160         if (!export.targets.equals(targetsP)) {
       
   161             throw new Error("unexpected: " + export);
       
   162         }
       
   163 
       
   164         export = source.moduleInfo.exports.get("q");
       
   165         if (!export.targets.equals(targetsQ)) {
       
   166             throw new Error("unexpected: " + export);
       
   167         }
       
   168 
       
   169         Statement provides = source.moduleInfo.provides.get("s");
       
   170         if (!provides.targets.equals(providerS)) {
       
   171             throw new Error("unexpected: " + provides);
       
   172         }
       
   173 
       
   174         provides = source.moduleInfo.provides.get("s1");
       
   175         if (!provides.targets.equals(providerS1)) {
       
   176             throw new Error("unexpected: " + provides);
       
   177         }
       
   178     }
       
   179 
       
   180 
       
   181 
       
   182     void errorCases() throws IOException {
       
   183         fail(test1);
       
   184         fail(test2);
       
   185         fail(test3);
       
   186         fail(test4);
       
   187         fail(test5);
       
   188     }
       
   189 
       
   190     void fail(String... extras) throws IOException {
       
   191         Path file = DIR.resolve("test1");
       
   192         Files.write(file, Arrays.asList(extras));
       
   193         try {
       
   194             builder.build(file);
       
   195         } catch (RuntimeException e) {
       
   196             if (!e.getMessage().matches("test/test1, line .* is malformed.*") &&
       
   197                 !e.getMessage().matches("test/test1, line .* missing keyword.*")) {
       
   198                 throw e;
       
   199             }
       
   200         }
       
   201     }
       
   202 
       
   203     static class Builder {
       
   204         final String moduleName;
       
   205         final Path sourceFile;
       
   206         final Set<String> modules = new HashSet<>();
       
   207         public Builder(String name) {
       
   208             this.moduleName = name;
       
   209             this.sourceFile = DIR.resolve(name).resolve("module-info.java");
       
   210         }
       
   211 
       
   212         public Builder modules(String... names) {
       
   213             Arrays.stream(names).forEach(modules::add);
       
   214             return this;
       
   215         }
       
   216 
       
   217         public Builder sourceFile(String... lines) throws IOException {
       
   218             Files.createDirectories(sourceFile.getParent());
       
   219             try (BufferedWriter bw = Files.newBufferedWriter(sourceFile);
       
   220                  PrintWriter writer = new PrintWriter(bw)) {
       
   221                 writer.format("module %s {%n", moduleName);
       
   222                 for (String l : lines) {
       
   223                     writer.println(l);
       
   224                 }
       
   225                 writer.println("}");
       
   226             }
       
   227             return this;
       
   228         }
       
   229 
       
   230         public GenModuleInfoSource build() throws IOException {
       
   231             return build(Collections.emptyList());
       
   232         }
       
   233 
       
   234         public GenModuleInfoSource build(Path extraFile) throws IOException {
       
   235             return build(Collections.singletonList(extraFile));
       
   236         }
       
   237 
       
   238         public GenModuleInfoSource build(List<Path> extraFiles) throws IOException {
       
   239             return new GenModuleInfoSource(sourceFile, extraFiles, modules);
       
   240         }
       
   241     }
       
   242 
       
   243 }