< prev index next >

test/tools/javac/modules/AutomaticModules.java

Print this page
rev 3492 : 8155026: javac grants implied readability to explicit modules
Summary: Ordinary named modules shouldn't be requires public for automatic modules
Reviewed-by: TBD


   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 automatic modules
  27  * @library /tools/lib
  28  * @modules

  29  *      jdk.compiler/com.sun.tools.javac.api
  30  *      jdk.compiler/com.sun.tools.javac.main
  31  * @build toolbox.ToolBox toolbox.JavacTask toolbox.JarTask ModuleTestBase
  32  * @run main AutomaticModules
  33  */
  34 
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;


  37 
  38 import toolbox.JarTask;
  39 import toolbox.JavacTask;
  40 import toolbox.Task;
  41 import toolbox.ToolBox;
  42 
  43 public class AutomaticModules extends ModuleTestBase {
  44 
  45     public static void main(String... args) throws Exception {
  46         AutomaticModules t = new AutomaticModules();
  47         t.runTests();
  48     }
  49 
  50     @Test
  51     public void testSimple(Path base) throws Exception {
  52         Path legacySrc = base.resolve("legacy-src");
  53         tb.writeJavaFiles(legacySrc,
  54                           "package api; import java.awt.event.ActionListener; public abstract class Api implements ActionListener {}");
  55         Path legacyClasses = base.resolve("legacy-classes");
  56         Files.createDirectories(legacyClasses);
  57 
  58         String log = new JavacTask(tb)
  59                 .options()
  60                 .outdir(legacyClasses)
  61                 .files(findJavaFiles(legacySrc))


  69 
  70         Path modulePath = base.resolve("module-path");
  71 
  72         Files.createDirectories(modulePath);
  73 
  74         Path jar = modulePath.resolve("test-api-1.0.jar");
  75 
  76         new JarTask(tb, jar)
  77           .baseDir(legacyClasses)
  78           .files("api/Api.class")
  79           .run();
  80 
  81         Path moduleSrc = base.resolve("module-src");
  82         Path m1 = moduleSrc.resolve("m1");
  83 
  84         Path classes = base.resolve("classes");
  85 
  86         Files.createDirectories(classes);
  87 
  88         tb.writeJavaFiles(m1,
  89                           "module m1 { requires test.api; }",
  90                           "package impl; public class Impl { public void e(api.Api api) { api.actionPerformed(null); } }");
  91 
  92         new JavacTask(tb)
  93                 .options("-modulesourcepath", moduleSrc.toString(), "-modulepath", modulePath.toString(), "-addmods", "java.desktop")
  94                 .outdir(classes)
  95                 .files(findJavaFiles(moduleSrc))
  96                 .run()
  97                 .writeAll();
  98     }
  99 
 100     @Test
 101     public void testUnnamedModule(Path base) throws Exception {
 102         Path legacySrc = base.resolve("legacy-src");
 103         tb.writeJavaFiles(legacySrc,
 104                           "package api; public abstract class Api { public void run(CharSequence str) { } private void run(base.Base base) { } }",
 105                           "package base; public interface Base { public void run(); }");
 106         Path legacyClasses = base.resolve("legacy-classes");
 107         Files.createDirectories(legacyClasses);
 108 
 109         String log = new JavacTask(tb)
 110                 .options()
 111                 .outdir(legacyClasses)
 112                 .files(findJavaFiles(legacySrc))
 113                 .run()


 206           .baseDir(depClasses)
 207           .files("module-info.class", "dep/Dep.class")
 208           .run();
 209 
 210         Path testSrc = base.resolve("testSrc");
 211         Path testClasses = base.resolve("testClasses");
 212 
 213         Files.createDirectories(testSrc);
 214         Files.createDirectories(testClasses);
 215 
 216         tb.writeJavaFiles(testSrc,
 217                           "module m2 { requires automatic; }",
 218                           "package test; public class Test { }");
 219 
 220         new JavacTask(tb)
 221                 .options("-modulepath", modulePath.toString())
 222                 .outdir(testClasses)
 223                 .files(findJavaFiles(testSrc))
 224                 .run()
 225                 .writeAll();

















































































 226     }
 227 }


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 8155026
  27  * @summary Test automatic modules
  28  * @library /tools/lib
  29  * @modules
  30  *      java.desktop
  31  *      jdk.compiler/com.sun.tools.javac.api
  32  *      jdk.compiler/com.sun.tools.javac.main
  33  * @build toolbox.ToolBox toolbox.JavacTask toolbox.JarTask ModuleTestBase
  34  * @run main AutomaticModules
  35  */
  36 
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.util.Arrays;
  40 import java.util.List;
  41 
  42 import toolbox.JarTask;
  43 import toolbox.JavacTask;
  44 import toolbox.Task;

  45 
  46 public class AutomaticModules extends ModuleTestBase {
  47 
  48     public static void main(String... args) throws Exception {
  49         AutomaticModules t = new AutomaticModules();
  50         t.runTests();
  51     }
  52 
  53     @Test
  54     public void testSimple(Path base) throws Exception {
  55         Path legacySrc = base.resolve("legacy-src");
  56         tb.writeJavaFiles(legacySrc,
  57                           "package api; import java.awt.event.ActionListener; public abstract class Api implements ActionListener {}");
  58         Path legacyClasses = base.resolve("legacy-classes");
  59         Files.createDirectories(legacyClasses);
  60 
  61         String log = new JavacTask(tb)
  62                 .options()
  63                 .outdir(legacyClasses)
  64                 .files(findJavaFiles(legacySrc))


  72 
  73         Path modulePath = base.resolve("module-path");
  74 
  75         Files.createDirectories(modulePath);
  76 
  77         Path jar = modulePath.resolve("test-api-1.0.jar");
  78 
  79         new JarTask(tb, jar)
  80           .baseDir(legacyClasses)
  81           .files("api/Api.class")
  82           .run();
  83 
  84         Path moduleSrc = base.resolve("module-src");
  85         Path m1 = moduleSrc.resolve("m1");
  86 
  87         Path classes = base.resolve("classes");
  88 
  89         Files.createDirectories(classes);
  90 
  91         tb.writeJavaFiles(m1,
  92                           "module m1 { requires test.api; requires java.desktop; }",
  93                           "package impl; public class Impl { public void e(api.Api api) { api.actionPerformed(null); } }");
  94 
  95         new JavacTask(tb)
  96                 .options("-modulesourcepath", moduleSrc.toString(), "-modulepath", modulePath.toString())
  97                 .outdir(classes)
  98                 .files(findJavaFiles(moduleSrc))
  99                 .run()
 100                 .writeAll();
 101     }
 102 
 103     @Test
 104     public void testUnnamedModule(Path base) throws Exception {
 105         Path legacySrc = base.resolve("legacy-src");
 106         tb.writeJavaFiles(legacySrc,
 107                           "package api; public abstract class Api { public void run(CharSequence str) { } private void run(base.Base base) { } }",
 108                           "package base; public interface Base { public void run(); }");
 109         Path legacyClasses = base.resolve("legacy-classes");
 110         Files.createDirectories(legacyClasses);
 111 
 112         String log = new JavacTask(tb)
 113                 .options()
 114                 .outdir(legacyClasses)
 115                 .files(findJavaFiles(legacySrc))
 116                 .run()


 209           .baseDir(depClasses)
 210           .files("module-info.class", "dep/Dep.class")
 211           .run();
 212 
 213         Path testSrc = base.resolve("testSrc");
 214         Path testClasses = base.resolve("testClasses");
 215 
 216         Files.createDirectories(testSrc);
 217         Files.createDirectories(testClasses);
 218 
 219         tb.writeJavaFiles(testSrc,
 220                           "module m2 { requires automatic; }",
 221                           "package test; public class Test { }");
 222 
 223         new JavacTask(tb)
 224                 .options("-modulepath", modulePath.toString())
 225                 .outdir(testClasses)
 226                 .files(findJavaFiles(testSrc))
 227                 .run()
 228                 .writeAll();
 229     }
 230 
 231     @Test
 232     public void testAutomaticAndNamedModules(Path base) throws Exception {
 233         Path modulePath = base.resolve("module-path");
 234 
 235         Files.createDirectories(modulePath);
 236 
 237         for (char c : new char[] {'A', 'B'}) {
 238             Path automaticSrc = base.resolve("automaticSrc" + c);
 239             tb.writeJavaFiles(automaticSrc, "package api" + c + "; public class Api {}");
 240             Path automaticClasses = base.resolve("automaticClasses" + c);
 241             tb.createDirectories(automaticClasses);
 242 
 243             String automaticLog = new JavacTask(tb)
 244                                     .outdir(automaticClasses)
 245                                     .files(findJavaFiles(automaticSrc))
 246                                     .run()
 247                                     .writeAll()
 248                                     .getOutput(Task.OutputKind.DIRECT);
 249 
 250             if (!automaticLog.isEmpty())
 251                 throw new Exception("expected output not found: " + automaticLog);
 252 
 253             Path automaticJar = modulePath.resolve("automatic" + c + "-1.0.jar");
 254 
 255             new JarTask(tb, automaticJar)
 256               .baseDir(automaticClasses)
 257               .files("api" + c + "/Api.class")
 258               .run();
 259         }
 260 
 261         Path moduleSrc = base.resolve("module-src");
 262 
 263         tb.writeJavaFiles(moduleSrc.resolve("m1"),
 264                           "module m1 { requires automaticA; }",
 265                           "package impl; public class Impl { apiA.Api a; apiB.Api b; m2.M2 m;}");
 266 
 267         tb.writeJavaFiles(moduleSrc.resolve("m2"),
 268                           "module m2 { exports m2; }",
 269                           "package m2; public class M2 { }");
 270 
 271         Path classes = base.resolve("classes");
 272 
 273         Files.createDirectories(classes);
 274 
 275         List<String> log = new JavacTask(tb)
 276                 .options("-modulesourcepath", moduleSrc.toString(),
 277                          "-modulepath", modulePath.toString(),
 278                          "-addmods", "automaticB",
 279                          "-XDrawDiagnostics")
 280                 .outdir(classes)
 281                 .files(findJavaFiles(moduleSrc))
 282                 .run(Task.Expect.FAIL)
 283                 .writeAll()
 284                 .getOutputLines(Task.OutputKind.DIRECT);
 285 
 286         List<String> expected = Arrays.asList("Impl.java:1:61: compiler.err.not.def.access.package.cant.access: m2.M2, m2",
 287                                               "1 error");
 288 
 289         if (!expected.equals(log)) {
 290             throw new Exception("expected output not found: " + log);
 291         }
 292 
 293         log = new JavacTask(tb)
 294                 .options("-modulesourcepath", moduleSrc.toString(),
 295                          "-modulepath", modulePath.toString(),
 296                          "-XDrawDiagnostics")
 297                 .outdir(classes)
 298                 .files(findJavaFiles(moduleSrc))
 299                 .run(Task.Expect.FAIL)
 300                 .writeAll()
 301                 .getOutputLines(Task.OutputKind.DIRECT);
 302 
 303         expected = Arrays.asList("Impl.java:1:51: compiler.err.doesnt.exist: apiB",
 304                                  "Impl.java:1:61: compiler.err.not.def.access.package.cant.access: m2.M2, m2",
 305                                  "2 errors");
 306 
 307         if (!expected.equals(log)) {
 308             throw new Exception("expected output not found: " + log);
 309         }
 310     }
 311 }
< prev index next >