TestLinkOption.java revision 3233:b5d08bc0d224
1/* 2 * Copyright (c) 2002, 2015, 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 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 // Generate the documentation using -linkoffline and a URL as the first parameter. 55 String out1 = "out1"; 56 String url = "http://java.sun.com/j2se/1.4/docs/api/"; 57 javadoc("-d", out1, 58 "-sourcepath", testSrc, 59 "-linkoffline", url, testSrc, 60 "-package", 61 "pkg", "java.lang"); 62 checkExit(Exit.OK); 63 64 checkOutput("pkg/C.html", true, 65 "<a href=\"" + url + "java/lang/String.html?is-external=true\" " 66 + "title=\"class or interface in java.lang\"><code>Link to String Class</code></a>", 67 //Make sure the parameters are indented properly when the -link option is used. 68 "(int p1,\n" 69 + " int p2,\n" 70 + " int p3)", 71 "(int p1,\n" 72 + " int p2,\n" 73 + " <a href=\"" + url + "java/lang/Object.html?is-external=true\" title=\"class or interface in java.lang\">" 74 + "Object</a> p3)"); 75 76 checkOutput("pkg/B.html", true, 77 "<div class=\"block\">A method with html tag the method " 78 + "<a href=\"http://java.sun.com/j2se/1.4/docs/api/java/lang/ClassLoader.html?is-external=true#getSystemClassLoader--\"" 79 + " title=\"class or interface in java.lang\"><code><tt>getSystemClassLoader()</tt>" 80 + "</code></a> as the parent class loader.</div>", 81 "<div class=\"block\">is equivalent to invoking <code>" 82 + "<a href=\"../pkg/B.html#createTempFile-java.lang.String-java.lang.String-java.io.File-\">" 83 + "<code>createTempFile(prefix, suffix, null)</code></a></code>.</div>", 84 "<a href=\"" + url + "java/lang/String.html?is-external=true\" " 85 + "title=\"class or interface in java.lang\">Link-Plain to String Class</a>", 86 "<code><tt>getSystemClassLoader()</tt></code>", 87 "<code>createTempFile(prefix, suffix, null)</code>", 88 "<dd><a href=\"http://www.ietf.org/rfc/rfc2279.txt\"><i>RFC 2279: UTF-8, a\n" + 89 " transformation format of ISO 10646</i></a>, <br><a " + 90 "href=\"http://www.ietf.org/rfc/rfc2373.txt\"><i>RFC 2373: IPv6 Addressing\n" + 91 " Architecture</i></a>, <br><a href=\"http://www.ietf.org/rfc/rfc2396.txt\">" + 92 "<i>RFC 2396: Uniform\n" + 93 " Resource Identifiers (URI): Generic Syntax</i></a>, " + 94 "<br><a href=\"http://www.ietf.org/rfc/rfc2732.txt\"><i>RFC 2732: Format for\n" + 95 " Literal IPv6 Addresses in URLs</i></a>, <br><a href=\"URISyntaxException.html\">" + 96 "URISyntaxException</a></dd>\n" + 97 "</dl>"); 98 99 checkOutput("java/lang/StringBuilderChild.html", true, 100 "<pre>public abstract class <span class=\"typeNameLabel\">StringBuilderChild</span>\n" 101 + "extends <a href=\"" + url + "java/lang/Object.html?is-external=true\" " 102 + "title=\"class or interface in java.lang\">Object</a></pre>" 103 ); 104 105 // Generate the documentation using -linkoffline and a relative path as the first parameter. 106 // We will try linking to the docs generated in test 1 with a relative path. 107 String out2 = "out2"; 108 javadoc("-d", out2, 109 "-sourcepath", testSrc, 110 "-linkoffline", "../" + out1, out1, 111 "-package", 112 "pkg2"); 113 checkExit(Exit.OK); 114 checkOutput("pkg2/C2.html", true, 115 "This is a link to <a href=\"../../" + out1 + "/pkg/C.html?is-external=true\" " + 116 "title=\"class or interface in pkg\"><code>Class C</code></a>." 117 ); 118 119 String out3 = "out3"; 120 javadoc(createArguments(out3, out1, true)); // with trailing slash 121 checkExit(Exit.OK); 122 123 String out4 = "out4"; 124 javadoc(createArguments(out4, out1, false)); // without trailing slash 125 checkExit(Exit.OK); 126 // Note: the following test is very weak, and will fail if ever the test 127 // of the message is changed. We should have a separate test to verify 128 // this is the text that is given when there is a problem with a URL 129 checkOutput(Output.OUT, false, 130 "warning - Error fetching URL"); 131 } 132 133 /* 134 * Create the documentation using the -link option, vary the behavior with 135 * both trailing and no trailing slash. We are only interested in ensuring 136 * that the command executes with no errors or related warnings. 137 */ 138 static String[] createArguments(String outDir, String packageDir, boolean withTrailingSlash) { 139 String packagePath = new File(packageDir).getAbsolutePath(); 140 if (withTrailingSlash) { 141 // add the trailing slash, if it is not present! 142 if (!packagePath.endsWith(FS)) { 143 packagePath = packagePath + FS; 144 } 145 } else { 146 // remove the trailing slash, if it is present! 147 if (packagePath.endsWith(FS)) { 148 packagePath = packagePath.substring(0, packagePath.length() - 1); 149 } 150 } 151 String args[] = { 152 "-d", outDir, 153 "-sourcepath", testSrc, 154 "-link", "file:///" + packagePath, 155 "-package", 156 "pkg2" 157 }; 158 System.out.println("packagePath: " + packagePath); 159 return args; 160 } 161} 162