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