langtools/test/tools/javac/modules/UpgradeModulePathTest.java
changeset 36526 3b41f1c69604
child 36778 e04318f39f92
equal deleted inserted replaced
36525:4caf88912b7f 36526:3b41f1c69604
       
     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 tests for -upgrademodulepath
       
    27  * @library /tools/lib
       
    28  * @modules
       
    29  *      jdk.compiler/com.sun.tools.javac.api
       
    30  *      jdk.compiler/com.sun.tools.javac.main
       
    31  *      jdk.jdeps/com.sun.tools.javap
       
    32  * @build ToolBox ModuleTestBase
       
    33  * @run main UpgradeModulePathTest
       
    34  */
       
    35 
       
    36 import java.io.File;
       
    37 import java.nio.file.Path;
       
    38 
       
    39 public class UpgradeModulePathTest extends ModuleTestBase {
       
    40 
       
    41     public static void main(String... args) throws Exception {
       
    42         UpgradeModulePathTest t = new UpgradeModulePathTest();
       
    43         t.runTests();
       
    44     }
       
    45 
       
    46     @Test
       
    47     void simpleUsage(Path base) throws Exception {
       
    48         final Path module = base.resolve("modules");
       
    49         new ModuleBuilder("m1")
       
    50                 .exports("pkg1")
       
    51                 .classes("package pkg1; public class E { }")
       
    52                 .build(module);
       
    53 
       
    54         final Path upgradeModule = base.resolve("upgradeModule");
       
    55         new ModuleBuilder("m1")
       
    56                 .exports("pkg2")
       
    57                 .classes("package pkg2; public class E { }")
       
    58                 .build(upgradeModule);
       
    59 
       
    60         Path src = base.resolve("src");
       
    61         tb.writeJavaFiles(src, "module m2 { requires m1; }",
       
    62                 "package p; class A { void main() { pkg2.E.class.getName(); } }");
       
    63 
       
    64         tb.new JavacTask(ToolBox.Mode.CMDLINE)
       
    65                 .options("-modulepath", module.toString(),
       
    66                         "-upgrademodulepath", upgradeModule.toString())
       
    67                 .files(findJavaFiles(src))
       
    68                 .run()
       
    69                 .writeAll();
       
    70     }
       
    71 
       
    72     @Test
       
    73     void onlyUpgradeModulePath(Path base) throws Exception {
       
    74         final Path module = base.resolve("modules");
       
    75         new ModuleBuilder("m1")
       
    76                 .exports("pkg1")
       
    77                 .classes("package pkg1; public class E { }")
       
    78                 .build(module);
       
    79 
       
    80         final Path upgradeModule = base.resolve("upgradeModule");
       
    81         new ModuleBuilder("m1")
       
    82                 .exports("pkg2")
       
    83                 .classes("package pkg2; public class E { }")
       
    84                 .build(upgradeModule);
       
    85 
       
    86         Path src = base.resolve("src");
       
    87         tb.writeJavaFiles(src, "module m2 { requires m1; }",
       
    88                 "package p; class A { void main() { pkg2.E.class.getName(); } }");
       
    89 
       
    90         tb.new JavacTask(ToolBox.Mode.CMDLINE)
       
    91                 .options("-upgrademodulepath", upgradeModule + File.pathSeparator + module)
       
    92                 .files(findJavaFiles(src))
       
    93                 .run()
       
    94                 .writeAll();
       
    95     }
       
    96 
       
    97     @Test
       
    98     void withModuleSourcePath(Path base) throws Exception {
       
    99         final Path module = base.resolve("modules");
       
   100         new ModuleBuilder("m1")
       
   101                 .exports("pkg1")
       
   102                 .classes("package pkg1; public class E { }")
       
   103                 .build(module);
       
   104 
       
   105         final Path upgradeModule = base.resolve("upgradeModule");
       
   106         new ModuleBuilder("m1")
       
   107                 .exports("pkg2")
       
   108                 .classes("package pkg2; public class E { }")
       
   109                 .build(upgradeModule);
       
   110 
       
   111         final Path s = base.resolve("source");
       
   112         tb.writeJavaFiles(s.resolve("m3"), "module m3 { }");
       
   113 
       
   114         final Path upgradeModule3 = base.resolve("upgradeModule");
       
   115         new ModuleBuilder("m3")
       
   116                 .exports("pkg3")
       
   117                 .classes("package pkg3; public class E { }")
       
   118                 .build(upgradeModule);
       
   119 
       
   120         Path src = base.resolve("src");
       
   121         tb.writeJavaFiles(src.resolve("m2"), "module m2 { requires m1; requires m3; }",
       
   122                 "package p; class A { void main() { pkg2.E.class.getName(); } }");
       
   123 
       
   124         tb.new JavacTask(ToolBox.Mode.CMDLINE)
       
   125                 .options("-modulepath", module.toString(),
       
   126                         "-modulesourcepath", src + File.pathSeparator + s,
       
   127                         "-upgrademodulepath", upgradeModule + File.pathSeparator + upgradeModule3)
       
   128                 .outdir(module)
       
   129                 .files(findJavaFiles(src))
       
   130                 .run()
       
   131                 .writeAll();
       
   132     }
       
   133 
       
   134     @Test
       
   135     void sameUpgradeAndModulePath(Path base) throws Exception {
       
   136         final Path module = base.resolve("modules");
       
   137         new ModuleBuilder("m1")
       
   138                 .exports("pkg1")
       
   139                 .classes("package pkg1; public class E { }")
       
   140                 .build(module);
       
   141 
       
   142         final Path upgradeModule = base.resolve("upgradeModule");
       
   143         new ModuleBuilder("m1")
       
   144                 .exports("pkg2")
       
   145                 .classes("package pkg2; public class E { }")
       
   146                 .build(upgradeModule);
       
   147 
       
   148         Path src = base.resolve("src");
       
   149         tb.writeJavaFiles(src, "module m2 { requires m1; }",
       
   150                 "package p; class A { void main() { pkg2.E.class.getName(); } }");
       
   151 
       
   152         tb.new JavacTask(ToolBox.Mode.CMDLINE)
       
   153                 .options("-modulepath", upgradeModule + File.pathSeparator + module,
       
   154                         "-upgrademodulepath", upgradeModule.toString())
       
   155                 .files(findJavaFiles(src))
       
   156                 .run()
       
   157                 .writeAll();
       
   158     }
       
   159 
       
   160     @Test
       
   161     void dummyFileInUpgradeModulePath(Path base) throws Exception {
       
   162         final Path module = base.resolve("modules");
       
   163         new ModuleBuilder("m1")
       
   164                 .exports("pkg1")
       
   165                 .classes("package pkg1; public class E { }")
       
   166                 .build(module);
       
   167 
       
   168         Path dummy = base.resolve("dummy.txt");
       
   169         tb.writeFile(dummy, "");
       
   170 
       
   171         Path src = base.resolve("src");
       
   172         tb.writeJavaFiles(src, "module m2 { requires m1; }",
       
   173                 "package p; class A { void main() { pkg2.E.class.getName(); } }");
       
   174 
       
   175         String output = tb.new JavacTask(ToolBox.Mode.CMDLINE)
       
   176                 .options("-XDrawDiagnostics",
       
   177                         "-modulepath", module.toString(),
       
   178                         "-upgrademodulepath", dummy.toString())
       
   179                 .files(findJavaFiles(src))
       
   180                 .run(ToolBox.Expect.FAIL)
       
   181                 .writeAll()
       
   182                 .getOutput(ToolBox.OutputKind.DIRECT);
       
   183         if (!output.contains("compiler.err.illegal.argument.for.option: -upgrademodulepath, " + dummy)) {
       
   184             throw new Exception("Expected output was not found");
       
   185         }
       
   186     }
       
   187 
       
   188     @Test
       
   189     void severalUpgradeModules(Path base) throws Exception {
       
   190         final Path module = base.resolve("modules");
       
   191         new ModuleBuilder("m1")
       
   192                 .exports("pkg1")
       
   193                 .classes("package pkg1; public class A { }")
       
   194                 .build(module);
       
   195 
       
   196         new ModuleBuilder("m2")
       
   197                 .exports("pkg2")
       
   198                 .classes("package pkg2; public class B { }")
       
   199                 .build(module);
       
   200 
       
   201         Path upgradeModule = base.resolve("upgradeModule");
       
   202         new ModuleBuilder("m2")
       
   203                 .exports("pkg2")
       
   204                 .classes("package pkg2; public class BC { }")
       
   205                 .build(upgradeModule);
       
   206         new ModuleBuilder("m3")
       
   207                 .exports("pkg3")
       
   208                 .classes("package pkg3; public class DC { }")
       
   209                 .build(upgradeModule);
       
   210 
       
   211         Path src = base.resolve("src");
       
   212         tb.writeJavaFiles(src, "module m4 { requires m1; requires m2; requires m3; }",
       
   213                 "package p; class A { void main() { pkg1.A.class.getName(); pkg2.BC.class.getName(); pkg3.DC.class.getName(); } }");
       
   214 
       
   215         tb.new JavacTask(ToolBox.Mode.CMDLINE)
       
   216                 .options("-modulepath", module.toString(),
       
   217                         "-upgrademodulepath", upgradeModule.toString())
       
   218                 .files(findJavaFiles(src))
       
   219                 .run()
       
   220                 .writeAll();
       
   221 
       
   222         Path src2 = base.resolve("src2");
       
   223         tb.writeJavaFiles(src2, "module m4 { requires m1; }",
       
   224                 "package p; class A { void main() { pkg2.B.class.getName(); } }");
       
   225 
       
   226         String log = tb.new JavacTask(ToolBox.Mode.CMDLINE)
       
   227                 .options("-XDrawDiagnostics",
       
   228                         "-modulepath", module.toString(),
       
   229                         "-upgrademodulepath", upgradeModule.toString())
       
   230                 .files(findJavaFiles(src2))
       
   231                 .run(ToolBox.Expect.FAIL)
       
   232                 .writeAll()
       
   233                 .getOutput(ToolBox.OutputKind.DIRECT);
       
   234         if (!log.contains("compiler.err.doesnt.exist: pkg2")) {
       
   235             throw new Exception("Expected output was not found");
       
   236         }
       
   237     }
       
   238 
       
   239     @Test
       
   240     void severalUpgradeModulePathsLastWin(Path base) throws Exception {
       
   241         final Path module = base.resolve("modules");
       
   242         new ModuleBuilder("m1")
       
   243                 .exports("pkg1")
       
   244                 .classes("package pkg1; public class E { }")
       
   245                 .build(module);
       
   246 
       
   247         final Path upgradeModule1 = base.resolve("upgradeModule1");
       
   248         new ModuleBuilder("m1")
       
   249                 .exports("pkg2")
       
   250                 .classes("package pkg2; public class EC1 { }")
       
   251                 .build(upgradeModule1);
       
   252 
       
   253         final Path upgradeModule2 = base.resolve("upgradeModule2");
       
   254         new ModuleBuilder("m1")
       
   255                 .exports("pkg2")
       
   256                 .classes("package pkg2; public class EC2 { }")
       
   257                 .build(upgradeModule2);
       
   258 
       
   259         Path src = base.resolve("src");
       
   260         tb.writeJavaFiles(src, "module m2 { requires m1; }",
       
   261                 "package p; class A { void main() { pkg2.EC2.class.getName(); } }");
       
   262 
       
   263         tb.new JavacTask(ToolBox.Mode.CMDLINE)
       
   264                 .options("-modulepath", module.toString(),
       
   265                         "-upgrademodulepath", upgradeModule1.toString(),
       
   266                         "-upgrademodulepath", upgradeModule2.toString())
       
   267                 .files(findJavaFiles(src))
       
   268                 .run()
       
   269                 .writeAll();
       
   270 
       
   271         Path src2 = base.resolve("src2");
       
   272         tb.writeJavaFiles(src2, "module m2 { requires m1; }",
       
   273                 "package p; class A { void main() { pkg2.EC1.class.getName(); } }");
       
   274 
       
   275         final String log = tb.new JavacTask(ToolBox.Mode.CMDLINE)
       
   276                 .options("-XDrawDiagnostics",
       
   277                         "-modulepath", module.toString(),
       
   278                         "-upgrademodulepath", upgradeModule1.toString(),
       
   279                         "-upgrademodulepath", upgradeModule2.toString())
       
   280                 .files(findJavaFiles(src2))
       
   281                 .run(ToolBox.Expect.FAIL)
       
   282                 .writeAll()
       
   283                 .getOutput(ToolBox.OutputKind.DIRECT);
       
   284 
       
   285         if (!log.contains("compiler.err.cant.resolve.location: kindname.class, EC1, , , (compiler.misc.location: kindname.package, pkg2, null)")) {
       
   286             throw new Exception("Expected output was not found");
       
   287         }
       
   288     }
       
   289 }