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 subpackage issues
  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 ModuleTestBase
  32  * @run main SubpackageTest
  33  */
  34 
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.util.ArrayList;
  38 import java.util.Arrays;
  39 import java.util.Collections;
  40 import java.util.List;
  41 import java.util.stream.Collectors;
  42 
  43 import toolbox.JavacTask;
  44 import toolbox.Task;
  45 import toolbox.ToolBox;
  46 
  47 public class SubpackageTest extends ModuleTestBase {
  48 
  49     public static void main(String... args) throws Exception {
  50         SubpackageTest t = new SubpackageTest();
  51         t.runTests();
  52     }
  53 
  54     @Test // based on JDK-8075435
  55     public void testUnnamedModule(Path base) throws Exception {
  56         Path libsrc = base.resolve("lib/src");
  57         tb.writeJavaFiles(libsrc,
  58             "package p; public class E extends Error { }");
  59         Path libclasses = base.resolve("lib/classes");
  60         Files.createDirectories(libclasses);
  61         new JavacTask(tb)
  62                 .outdir(libclasses)
  63                 .files(findJavaFiles(libsrc))
  64                 .run()
  65                 .writeAll();
  66 
  67         Path src = base.resolve("src");
  68         tb.writeJavaFiles(src,
  69             "package p.q;\n"
  70             + "import p.E;\n"
  71             + "class Test {\n"
  72             + "  void m() { throw new E(); }\n"
  73             + "}");
  74         Path classes = base.resolve("classes");
  75         Files.createDirectories(classes);
  76 
  77         new JavacTask(tb)
  78                 .classpath(libclasses)
  79                 .outdir(classes)
  80                 .files(findJavaFiles(src))
  81                 .run()
  82                 .writeAll();
  83     }
  84 
  85     @Test
  86     public void testSimpleMulti(Path base) throws Exception {
  87         Path src = base.resolve("src");
  88         tb.writeJavaFiles(src.resolve("mp"),
  89                 "module mp { exports p; }",
  90                 "package p; public class C1 { }");
  91         tb.writeJavaFiles(src.resolve("mpq"),
  92                 "module mpq { exports p.q; }",
  93                 "package p.q; public class C2 { }");
  94         tb.writeJavaFiles(src.resolve("mpqr"),
  95                 "module mpqr { exports p.q.r; }",
  96                 "package p.q.r; public class C3 { }");
  97         tb.writeJavaFiles(src.resolve("m"),
  98                 "module m {"
  99                 + "  requires mp;\n"
 100                 + "  requires mpq;\n"
 101                 + "  requires mpqr;\n"
 102                 + "}",
 103                 "package x;\n"
 104                 + "class C {\n"
 105                 + "  p.C1 c1;\n"
 106                 + "  p.q.C2 c2;\n"
 107                 + "  p.q.r.C3 c3;\n"
 108                 + "}");
 109         Path modules = base.resolve("modules");
 110         Files.createDirectories(modules);
 111 
 112         new JavacTask(tb)
 113                 .options("--module-source-path", src.toString())
 114                 .outdir(modules)
 115                 .files(findJavaFiles(src))
 116                 .run()
 117                 .writeAll();
 118     }
 119 
 120 }