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 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))
  62                 .run()
  63                 .writeAll()
  64                 .getOutput(Task.OutputKind.DIRECT);
  65 
  66         if (!log.isEmpty()) {
  67             throw new Exception("unexpected output: " + log);
  68         }
  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()
 114                 .writeAll()
 115                 .getOutput(Task.OutputKind.DIRECT);
 116 
 117         if (!log.isEmpty()) {
 118             throw new Exception("unexpected output: " + log);
 119         }
 120 
 121         Path modulePath = base.resolve("module-path");
 122 
 123         Files.createDirectories(modulePath);
 124 
 125         Path apiJar = modulePath.resolve("test-api-1.0.jar");
 126 
 127         new JarTask(tb, apiJar)
 128           .baseDir(legacyClasses)
 129           .files("api/Api.class")
 130           .run();
 131 
 132         Path baseJar = base.resolve("base.jar");
 133 
 134         new JarTask(tb, baseJar)
 135           .baseDir(legacyClasses)
 136           .files("base/Base.class")
 137           .run();
 138 
 139         Path moduleSrc = base.resolve("module-src");
 140         Path m1 = moduleSrc.resolve("m1");
 141 
 142         Path classes = base.resolve("classes");
 143 
 144         Files.createDirectories(classes);
 145 
 146         tb.writeJavaFiles(m1,
 147                           "module m1 { requires test.api; }",
 148                           "package impl; public class Impl { public void e(api.Api api) { api.run(\"\"); } }");
 149 
 150         new JavacTask(tb)
 151                 .options("-modulesourcepath", moduleSrc.toString(), "-modulepath", modulePath.toString(), "-classpath", baseJar.toString())
 152                 .outdir(classes)
 153                 .files(findJavaFiles(moduleSrc))
 154                 .run()
 155                 .writeAll();
 156     }
 157 
 158     @Test
 159     public void testModuleInfoFromClassFileDependsOnAutomatic(Path base) throws Exception {
 160         Path automaticSrc = base.resolve("automaticSrc");
 161         tb.writeJavaFiles(automaticSrc, "package api; public class Api {}");
 162         Path automaticClasses = base.resolve("automaticClasses");
 163         tb.createDirectories(automaticClasses);
 164 
 165         String automaticLog = new JavacTask(tb)
 166                                 .outdir(automaticClasses)
 167                                 .files(findJavaFiles(automaticSrc))
 168                                 .run()
 169                                 .writeAll()
 170                                 .getOutput(Task.OutputKind.DIRECT);
 171 
 172         if (!automaticLog.isEmpty())
 173             throw new Exception("expected output not found: " + automaticLog);
 174 
 175         Path modulePath = base.resolve("module-path");
 176 
 177         Files.createDirectories(modulePath);
 178 
 179         Path automaticJar = modulePath.resolve("automatic-1.0.jar");
 180 
 181         new JarTask(tb, automaticJar)
 182           .baseDir(automaticClasses)
 183           .files("api/Api.class")
 184           .run();
 185 
 186         Path depSrc = base.resolve("depSrc");
 187         Path depClasses = base.resolve("depClasses");
 188 
 189         Files.createDirectories(depSrc);
 190         Files.createDirectories(depClasses);
 191 
 192         tb.writeJavaFiles(depSrc,
 193                           "module m1 { requires public automatic; }",
 194                           "package dep; public class Dep { api.Api api; }");
 195 
 196         new JavacTask(tb)
 197                 .options("-modulepath", modulePath.toString())
 198                 .outdir(depClasses)
 199                 .files(findJavaFiles(depSrc))
 200                 .run()
 201                 .writeAll();
 202 
 203         Path moduleJar = modulePath.resolve("m1.jar");
 204 
 205         new JarTask(tb, moduleJar)
 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 }