1 /*
   2  * Copyright (c) 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary Test the --limit-modules option
  27  * @library /tools/lib
  28  * @modules jdk.compiler/com.sun.tools.javac.api
  29  *          jdk.compiler/com.sun.tools.javac.main
  30  * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
  31  * @run main LimitModulesTest
  32  */
  33 
  34 
  35 import java.nio.file.Path;
  36 
  37 import toolbox.JavacTask;
  38 import toolbox.Task;
  39 import toolbox.ToolBox;
  40 
  41 public class LimitModulesTest extends ModuleTestBase {
  42     public static void main(String... args) throws Exception {
  43         new LimitModulesTest().runTests();
  44     }
  45 
  46     @Test
  47     public void testEmpty(Path base) throws Exception {
  48         Path src = base.resolve("src");
  49         tb.writeJavaFiles(src, "class Dummy { }");
  50         Path classes = base.resolve("classes");
  51         tb.createDirectories(classes);
  52 
  53         String log = new JavacTask(tb, Task.Mode.CMDLINE)
  54                 .options("--module-source-path", src.toString(),
  55                          "--limit-modules", "")
  56                 .outdir(classes)
  57                 .files(findJavaFiles(src))
  58                 .run(Task.Expect.FAIL)
  59                 .writeAll()
  60                 .getOutput(Task.OutputKind.DIRECT);
  61 
  62         if (!log.contains("javac: no value for --limit-modules option"))
  63             throw new Exception("expected output not found");
  64 
  65         log = new JavacTask(tb, Task.Mode.CMDLINE)
  66                 .options("--module-source-path", src.toString(),
  67                          "--limit-modules=")
  68                 .outdir(classes)
  69                 .files(findJavaFiles(src))
  70                 .run(Task.Expect.FAIL)
  71                 .writeAll()
  72                 .getOutput(Task.OutputKind.DIRECT);
  73 
  74         if (!log.contains("javac: no value for --limit-modules option"))
  75             throw new Exception("expected output not found");
  76     }
  77 
  78     @Test
  79     public void testEmptyItem(Path base) throws Exception {
  80         Path src = base.resolve("src");
  81         Path src_m1 = src.resolve("m1");
  82         tb.writeJavaFiles(src_m1,
  83                           "module m1 { }");
  84         Path src_m2 = src.resolve("m2");
  85         tb.writeJavaFiles(src_m2,
  86                           "module m2 { }");
  87         Path classes = base.resolve("classes");
  88         tb.createDirectories(classes);
  89 
  90         new JavacTask(tb)
  91                 .options("--module-source-path", src.toString(),
  92                          "--limit-modules", ",m1")
  93                 .outdir(classes)
  94                 .files(findJavaFiles(src))
  95                 .run()
  96                 .writeAll();
  97 
  98         new JavacTask(tb)
  99                 .options("--module-source-path", src.toString(),
 100                          "--limit-modules", "m1,,m2")
 101                 .outdir(classes)
 102                 .files(findJavaFiles(src))
 103                 .run()
 104                 .writeAll();
 105 
 106         new JavacTask(tb)
 107                 .options("--module-source-path", src.toString(),
 108                          "--limit-modules", "m1,")
 109                 .outdir(classes)
 110                 .files(findJavaFiles(src))
 111                 .run()
 112                 .writeAll();
 113     }
 114 
 115     @Test
 116     public void testEmptyList(Path base) throws Exception {
 117         Path src = base.resolve("src");
 118         tb.writeJavaFiles(src, "class Dummy { }");
 119         Path classes = base.resolve("classes");
 120         tb.createDirectories(classes);
 121 
 122         String log = new JavacTask(tb, Task.Mode.CMDLINE)
 123                 .options("--module-source-path", src.toString(),
 124                          "--limit-modules", ",")
 125                 .outdir(classes)
 126                 .files(findJavaFiles(src))
 127                 .run(Task.Expect.FAIL)
 128                 .writeAll()
 129                 .getOutput(Task.OutputKind.DIRECT);
 130 
 131         if (!log.contains("javac: bad value for --limit-modules option"))
 132             throw new Exception("expected output not found");
 133     }
 134 
 135     @Test
 136     public void testInvalidName(Path base) throws Exception {
 137         Path src = base.resolve("src");
 138         tb.writeJavaFiles(src, "class Dummy { }");
 139         Path classes = base.resolve("classes");
 140         tb.createDirectories(classes);
 141 
 142         String log = new JavacTask(tb)
 143                 .options("-XDrawDiagnostics",
 144                          "--limit-modules", "BadModule!")
 145                 .outdir(classes)
 146                 .files(findJavaFiles(src))
 147                 .run(Task.Expect.FAIL)
 148                 .writeAll()
 149                 .getOutput(Task.OutputKind.DIRECT);
 150 
 151         if (!log.contains("- compiler.err.bad.name.for.option: --limit-modules, BadModule!"))
 152             throw new Exception("expected output not found");
 153     }
 154 
 155     @Test
 156     public void testLastOneWins(Path base) throws Exception {
 157         Path src = base.resolve("src");
 158         tb.writeJavaFiles(src,
 159                           "package p; class C { com.sun.tools.javac.Main main; }");
 160         Path classes = base.resolve("classes");
 161         tb.createDirectories(classes);
 162 
 163         System.err.println("case 1:");
 164         new JavacTask(tb)
 165                 .options("-XDrawDiagnostics",
 166                          "--limit-modules", "java.base",
 167                          "--limit-modules", "jdk.compiler")
 168                 .outdir(classes)
 169                 .files(findJavaFiles(src))
 170                 .run()
 171                 .writeAll();
 172 
 173         System.err.println("case 2:");
 174         String log = new JavacTask(tb)
 175                 .options("-XDrawDiagnostics",
 176                          "--limit-modules", "jdk.compiler",
 177                          "--limit-modules", "java.base")
 178                 .outdir(classes)
 179                 .files(findJavaFiles(src))
 180                 .run(Task.Expect.FAIL)
 181                 .writeAll()
 182                 .getOutput(Task.OutputKind.DIRECT);
 183 
 184         if (!log.contains("C.java:1:41: compiler.err.doesnt.exist: com.sun.tools.javac"))
 185             throw new Exception("expected output not found");
 186     }
 187 }
 188