1 /*
2  * Copyright (c) 2019, 2020, 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  * @bug 8212233
27  * @summary The code being documented uses modules but the packages defined in $URL are in the unnamed module.
28  * @library /tools/lib ../../lib
29  * @modules
30  *      jdk.javadoc/jdk.javadoc.internal.tool
31  *      jdk.compiler/com.sun.tools.javac.api
32  *      jdk.compiler/com.sun.tools.javac.main
33  * @build javadoc.tester.* toolbox.JarTask toolbox.JavacTask toolbox.ModuleBuilder toolbox.ToolBox
34  * @run main TestLinkOptionWithAutomaticModule
35  */
36 
37 import java.io.IOException;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 
41 import toolbox.JarTask;
42 import toolbox.JavacTask;
43 import toolbox.ModuleBuilder;
44 import toolbox.ToolBox;
45 
46 import javadoc.tester.JavadocTester;
47 
48 public class TestLinkOptionWithAutomaticModule extends JavadocTester {
49 
main(String... args)50     public static void main(String... args) throws Exception {
51         TestLinkOptionWithAutomaticModule tester = new TestLinkOptionWithAutomaticModule();
52         tester.runTests(m -> new Object[]{ Path.of(m.getName()) });
53     }
54 
55     final ToolBox tb = new ToolBox();
56     private Path libJar;
57     private Path libAPI;
58 
TestLinkOptionWithAutomaticModule()59     TestLinkOptionWithAutomaticModule() throws IOException {
60         initLib();
61     }
62 
initLib()63     private void initLib() throws IOException {
64         // create library: write source, compile it, jar it
65         Path lib = Path.of("lib");
66         Path libSrc = lib.resolve("src");
67         tb.writeJavaFiles(libSrc, "package lib; public class LibClass { }");
68         Path libClasses = Files.createDirectories(lib.resolve("classes"));
69 
70         new JavacTask(tb)
71                 .outdir(libClasses)
72                 .files(tb.findJavaFiles(libSrc))
73                 .run()
74                 .writeAll();
75 
76         libJar = lib.resolve("MyLib.jar");
77         new JarTask(tb, libJar)
78                 .baseDir(libClasses)
79                 .files(".")
80                 .run();
81 
82         libAPI = lib.resolve("api");
83         javadoc("-d", libAPI.toString(),
84                 "-sourcepath", libSrc.toString(),
85                 "lib");
86         checkExit(Exit.OK);
87     }
88 
89     @Test
testLinkUnnamedToAutomaticModule(Path base)90     public void testLinkUnnamedToAutomaticModule(Path base) throws IOException {
91 
92         // create API referring to library
93         Path src = base.resolve("src");
94         tb.writeJavaFiles(src, "package p; public class MyClass extends lib.LibClass { }");
95 
96         // run javadoc with library as automatic module
97         Path api = base.resolve("api");
98         javadoc("-d", api.toString(),
99                 "-sourcepath", src.toString(),
100                 "--add-modules", "MyLib",
101                 "--module-path", libJar.toString(),
102                 "-linkoffline", "http://myWebsite", libAPI.toAbsolutePath().toString(),
103                 "p");
104         checkExit(Exit.OK);
105         checkOutput("p/MyClass.html", true,
106                 """
107                     extends <a href="http://myWebsite/lib/LibClass.html" title="class or interface i\
108                     n lib" class="external-link">LibClass</a>""");
109     }
110 
111     @Test
112     public void testLinkNamedToAutomaticModule(Path base) throws IOException {
113 
114         // create API referring to library
115         Path src = base.resolve("src");
116         new ModuleBuilder(tb, "my.module")
117                 .exports("p")
118                 .requires("MyLib")
119                 .classes("package p; public class MyClass extends lib.LibClass { }")
120                 .write(src);
121 
122         // run javadoc with library as automatic module
123         Path api = base.resolve("api");
124         javadoc("-d", api.toString(),
125                 "--module-source-path", src.toString(),
126                 "--module-path", libJar.toString(),
127                 "-linkoffline", "http://myWebsite", libAPI.toAbsolutePath().toString(),
128                 "--module", "my.module");
129         checkExit(Exit.OK);
130         checkOutput("my.module/p/MyClass.html", true,
131                 """
132                     extends <a href="http://myWebsite/lib/LibClass.html" title="class or interface i\
133                     n lib" class="external-link">LibClass</a>""");
134     }
135 
136     @Test
137     public void testLinkNamedToUnnamedModule(Path base) throws IOException {
138 
139         // create API referring to library
140         Path src = base.resolve("src");
141         new ModuleBuilder(tb, "my.module")
142                 .exports("p")
143                 .classes("package p; public class MyClass extends lib.LibClass { }")
144                 .write(src);
145 
146         // run javadoc with library as unnamed module
147         Path api = base.resolve("api");
148         javadoc("-d", api.toString(),
149                 "--module-source-path", src.toString(),
150                 "--add-reads", "my.module=ALL-UNNAMED",
151                 "--class-path", libJar.toString(),
152                 "-linkoffline", "http://myWebsite", libAPI.toAbsolutePath().toString(),
153                 "--module", "my.module");
154         checkExit(Exit.OK);
155         checkOutput("my.module/p/MyClass.html", true,
156                 """
157                     extends <a href="http://myWebsite/lib/LibClass.html" title="class or interface i\
158                     n lib" class="external-link">LibClass</a>""");
159     }
160 }
161