TestLinkOption.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2002, 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 * @bug 4720957 5020118 8026567 8038976
27 * @summary Test to make sure that -link and -linkoffline link to
28 * right files, and URLs with and without trailing slash are accepted.
29 * @author jamieh
30 * @library ../lib
31 * @modules jdk.javadoc/jdk.javadoc.internal.tool
32 * @build JavadocTester
33 * @run main TestLinkOption
34 */
35
36import java.io.File;
37
38public class TestLinkOption extends JavadocTester {
39    /**
40     * The entry point of the test.
41     * @param args the array of command line arguments.
42     */
43    public static void main(String... args) throws Exception {
44        TestLinkOption tester = new TestLinkOption();
45        tester.runTests();
46    }
47
48    // The following test runs javadoc multiple times; it is important that the
49    // first one is run first, since the subsequent runs refer to the output
50    // it generates. Therefore we run everything serially in a single @Test
51    // method and not in independent @Test methods.
52    @Test
53    void test() {
54        String mylib = "mylib";
55        String[] javacArgs = {
56            "-d", mylib, testSrc + "/extra/StringBuilder.java"
57        };
58        com.sun.tools.javac.Main.compile(javacArgs);
59
60        // Generate the documentation using -linkoffline and a URL as the first parameter.
61        String out1 = "out1";
62        String url = "http://acme.com/jdk/";
63        javadoc("-d", out1,
64                "-classpath", mylib,
65                "-sourcepath", testSrc,
66                "-linkoffline", url, testSrc + "/jdk",
67                "-package",
68                "pkg", "mylib.lang");
69        checkExit(Exit.OK);
70
71        checkOutput("pkg/C.html", true,
72                "<a href=\"" + url + "java/lang/String.html?is-external=true\" "
73                + "title=\"class or interface in java.lang\"><code>Link to String Class</code></a>",
74                //Make sure the parameters are indented properly when the -link option is used.
75                "(int&nbsp;p1,\n"
76                + "      int&nbsp;p2,\n"
77                + "      int&nbsp;p3)",
78                "(int&nbsp;p1,\n"
79                + "      int&nbsp;p2,\n"
80                + "      <a href=\"" + url + "java/lang/Object.html?is-external=true\" title=\"class or interface in java.lang\">"
81                + "Object</a>&nbsp;p3)");
82
83        checkOutput("pkg/B.html", true,
84                "<div class=\"block\">A method with html tag the method "
85                + "<a href=\"" + url + "java/lang/ClassLoader.html?is-external=true#getSystemClassLoader--\""
86                + " title=\"class or interface in java.lang\"><code><tt>getSystemClassLoader()</tt>"
87                + "</code></a> as the parent class loader.</div>",
88                "<div class=\"block\">is equivalent to invoking <code>"
89                + "<a href=\"../pkg/B.html#createTempFile-java.lang.String-java.lang.String-java.io.File-\">"
90                + "<code>createTempFile(prefix,&nbsp;suffix,&nbsp;null)</code></a></code>.</div>",
91                "<a href=\"" + url + "java/lang/String.html?is-external=true\" "
92                + "title=\"class or interface in java.lang\">Link-Plain to String Class</a>",
93                "<code><tt>getSystemClassLoader()</tt></code>",
94                "<code>createTempFile(prefix,&nbsp;suffix,&nbsp;null)</code>",
95                "<dd><a href=\"http://www.ietf.org/rfc/rfc2279.txt\"><i>RFC&nbsp;2279: UTF-8, a\n" +
96                " transformation format of ISO 10646</i></a>, <br><a " +
97                "href=\"http://www.ietf.org/rfc/rfc2373.txt\"><i>RFC&nbsp;2373: IPv6 Addressing\n" +
98                " Architecture</i></a>, <br><a href=\"http://www.ietf.org/rfc/rfc2396.txt\">" +
99                "<i>RFC&nbsp;2396: Uniform\n" +
100                " Resource Identifiers (URI): Generic Syntax</i></a>, " +
101                "<br><a href=\"http://www.ietf.org/rfc/rfc2732.txt\"><i>RFC&nbsp;2732: Format for\n" +
102                " Literal IPv6 Addresses in URLs</i></a>, <br><a href=\"URISyntaxException.html\">" +
103                "URISyntaxException</a></dd>\n" +
104                "</dl>");
105
106        checkOutput("mylib/lang/StringBuilderChild.html", true,
107                "<pre>public abstract class <span class=\"typeNameLabel\">StringBuilderChild</span>\n"
108                + "extends <a href=\"" + url + "java/lang/Object.html?is-external=true\" "
109                + "title=\"class or interface in java.lang\">Object</a></pre>"
110        );
111
112        // Generate the documentation using -linkoffline and a relative path as the first parameter.
113        // We will try linking to the docs generated in test 1 with a relative path.
114        String out2 = "out2";
115        javadoc("-d", out2,
116                "-sourcepath", testSrc,
117                "-linkoffline", "../" + out1, out1,
118                "-package",
119                "pkg2");
120        checkExit(Exit.OK);
121        checkOutput("pkg2/C2.html", true,
122            "This is a link to <a href=\"../../" + out1 + "/pkg/C.html?is-external=true\" " +
123            "title=\"class or interface in pkg\"><code>Class C</code></a>."
124        );
125
126        String out3 = "out3";
127        javadoc(createArguments(out3, out1, true));  // with trailing slash
128        checkExit(Exit.OK);
129
130        String out4 = "out4";
131        javadoc(createArguments(out4, out1, false)); // without trailing slash
132        checkExit(Exit.OK);
133        // Note: the following test is very weak, and will fail if ever the text
134        // of the message is changed. We should have a separate test to verify
135        // this is the text that is given when there is a problem with a URL
136        checkOutput(Output.OUT, false,
137                "warning - Error fetching URL");
138    }
139
140    /*
141     * Create the documentation using the -link option, vary the behavior with
142     * both trailing and no trailing slash. We are only interested in ensuring
143     * that the command executes with no errors or related warnings.
144     */
145    static String[] createArguments(String outDir, String packageDir, boolean withTrailingSlash) {
146        String packagePath = new File(packageDir).getAbsolutePath();
147        if (withTrailingSlash) {
148            // add the trailing slash, if it is not present!
149            if (!packagePath.endsWith(FS)) {
150                packagePath = packagePath + FS;
151            }
152        } else {
153            // remove the trailing slash, if it is present!
154            if (packagePath.endsWith(FS)) {
155                packagePath = packagePath.substring(0, packagePath.length() - 1);
156            }
157        }
158        String args[] = {
159            "-d", outDir,
160            "-sourcepath", testSrc,
161            "-link", "file:///" + packagePath,
162            "-package",
163            "pkg2"
164        };
165        System.out.println("packagePath: " + packagePath);
166        return args;
167    }
168}
169