1 /*
   2  * Copyright (c) 2013, 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 8025633 8025524 8081854 8187521 8182765
  27  * @summary Test for valid name attribute in HTML anchors.
  28  * @library /tools/lib ../../lib
  29  * @modules jdk.javadoc/jdk.javadoc.internal.tool
  30  * @build toolbox.ToolBox javadoc.tester.*
  31  * @run main TestAnchorNames
  32  */
  33 
  34 import java.io.IOException;
  35 import java.nio.file.Path;
  36 import java.nio.file.Paths;
  37 
  38 import javadoc.tester.JavadocTester;
  39 import toolbox.ToolBox;
  40 
  41 public class TestAnchorNames extends JavadocTester {
  42 
  43     public final ToolBox tb;
  44     public static void main(String... args) throws Exception {
  45         TestAnchorNames tester = new TestAnchorNames();
  46         tester.runTests(m -> new Object[] { Paths.get(m.getName()) });
  47     }
  48 
  49     public TestAnchorNames() {
  50         tb = new ToolBox();
  51     }
  52 
  53     @Test
  54     public void testHtml5(Path ignore) {
  55         javadoc("-d", "out-html5",
  56                 "-sourcepath", testSrc,
  57                 "-source", "8", //so that '_' can be used as an identifier
  58                 "-use",
  59                 "pkg1");
  60         checkExit(Exit.OK);
  61 
  62         // Test some section markers and links to these markers
  63         checkOutput("pkg1/RegClass.html", true,
  64                 "<a id=\"skip.navbar.top\">",
  65                 "<a href=\"#skip.navbar.top\" title=\"Skip navigation links\">",
  66                 "<a id=\"nested.class.summary\">",
  67                 "<a href=\"#nested.class.summary\">",
  68                 "<a id=\"method.summary\">",
  69                 "<a href=\"#method.summary\">",
  70                 "<a id=\"field.detail\">",
  71                 "<a href=\"#field.detail\">",
  72                 "<a id=\"constructor.detail\">",
  73                 "<a href=\"#constructor.detail\">");
  74 
  75         // Test some members and link to these members
  76         checkOutput("pkg1/RegClass.html", true,
  77                 //The marker for this appears in the serialized-form.html which we will
  78                 //test below
  79                 "<a href=\"../serialized-form.html#pkg1.RegClass\">");
  80 
  81         // Test some fields
  82         checkOutput("pkg1/RegClass.html", true,
  83                 "<a id=\"_\">",
  84                 "<a href=\"#_\">",
  85                 "<a id=\"_$\">",
  86                 "<a href=\"#_$\">",
  87                 "<a id=\"$_\">",
  88                 "<a href=\"#$_\">",
  89                 "<a id=\"$field\">",
  90                 "<a href=\"#$field\">",
  91                 "<a id=\"fieldInCla$$\">",
  92                 "<a href=\"#fieldInCla$$\">",
  93                 "<a id=\"S_$$$$$INT\">",
  94                 "<a href=\"#S_$$$$$INT\">",
  95                 "<a id=\"method$$\">",
  96                 "<a href=\"#method$$\">");
  97 
  98         checkOutput("pkg1/DeprMemClass.html", true,
  99                 "<a id=\"_field_In_Class\">",
 100                 "<a href=\"#_field_In_Class\">");
 101 
 102         // Test constructor
 103         checkOutput("pkg1/RegClass.html", true,
 104                 "<a id=\"&lt;init&gt;(java.lang.String,int)\">",
 105                 "<a href=\"#%3Cinit%3E(java.lang.String,int)\">");
 106 
 107         // Test some methods
 108         checkOutput("pkg1/RegClass.html", true,
 109                 "<a id=\"_methodInClass(java.lang.String)\">",
 110                 "<a href=\"#_methodInClass(java.lang.String)\">",
 111                 "<a id=\"method()\">",
 112                 "<a href=\"#method()\">",
 113                 "<a id=\"foo(java.util.Map)\">",
 114                 "<a href=\"#foo(java.util.Map)\">",
 115                 "<a id=\"methodInCla$s(java.lang.String[])\">",
 116                 "<a href=\"#methodInCla$s(java.lang.String%5B%5D)\">",
 117                 "<a id=\"_methodInClas$(java.lang.String,int)\">",
 118                 "<a href=\"#_methodInClas$(java.lang.String,int)\">",
 119                 "<a id=\"methodD(pkg1.RegClass.$A)\">",
 120                 "<a href=\"#methodD(pkg1.RegClass.$A)\">",
 121                 "<a id=\"methodD(pkg1.RegClass.D[])\">",
 122                 "<a href=\"#methodD(pkg1.RegClass.D%5B%5D)\">");
 123 
 124         checkOutput("pkg1/DeprMemClass.html", true,
 125                 "<a id=\"$method_In_Class()\">",
 126                 "<a href=\"#$method_In_Class()\">");
 127 
 128         // Test enum
 129         checkOutput("pkg1/RegClass.Te$t_Enum.html", true,
 130                 "<a id=\"$FLD2\">",
 131                 "<a href=\"#$FLD2\">");
 132 
 133         // Test nested class
 134         checkOutput("pkg1/RegClass._NestedClas$.html", true,
 135                 "<a id=\"&lt;init&gt;()\">",
 136                 "<a href=\"#%3Cinit%3E()\">");
 137 
 138         // Test class use page
 139         checkOutput("pkg1/class-use/DeprMemClass.html", true,
 140                 "<a href=\"../RegClass.html#d____mc\">");
 141 
 142         // Test deprecated list page
 143         checkOutput("deprecated-list.html", true,
 144                 "<a href=\"pkg1/DeprMemClass.html#_field_In_Class\">",
 145                 "<a href=\"pkg1/DeprMemClass.html#$method_In_Class()\">");
 146 
 147         // Test constant values page
 148         checkOutput("constant-values.html", true,
 149                 "<a href=\"pkg1/RegClass.html#S_$$$$$INT\">");
 150 
 151         // Test serialized form page
 152         checkOutput("serialized-form.html", true,
 153                 //This is the marker for the link that appears in the pkg1.RegClass.html page
 154                 "<a id=\"pkg1.RegClass\">");
 155 
 156         // Test member name index page
 157         checkOutput("index-all.html", true,
 158                 "<a id=\"I:$\">",
 159                 "<a href=\"#I:$\">$",
 160                 "<a href=\"#I:_\">_");
 161     }
 162 
 163     /**
 164      * The following test is somewhat simplistic, but it is useful
 165      * in conjunction with the W3C Validation Service at https://validator.w3.org/nu/#file
 166      * @param base A working directory for this method, in which some UTF-8 source files
 167      *      will be generated
 168      * @throws IOException if there is a problem generating the source files
 169      */
 170     @Test
 171     public void testNonAscii(Path base) throws IOException {
 172         Path src = base.resolve("src");
 173         tb.writeJavaFiles(src,
 174                 "package p; public class Def {\n"
 175                 + "    public int \u00e0\u00e9;\n"              // a`e'
 176                 + "    public void \u00c0\u00c9() { }\n"        // A`E'
 177                 + "    public int \u03b1\u03b2\u03b3;\n"        // alpha beta gamma
 178                 + "    public void \u0391\u0392\u0393() { }\n"  // ALPHA BETA GAMMA
 179                 + "}",
 180                 "package p; \n"
 181                 + "/**\n"
 182                 + " * {@link Def#\u00e0\u00e9 &agrave;&eacute;}<br>\n"
 183                 + " * {@link Def#\u00c0\u00c9() &Agrave;&Eacute;}<br>\n"
 184                 + " * {@link Def#\u03b1\u03b2\u03b3 &alpha;&beta;&gamma;}<br>\n"
 185                 + " * {@link Def#\u0391\u0392\u0393() &Alpha;&Beta;&Gamma;}<br>\n"
 186                 + " */\n"
 187                 + "public class Ref { }");
 188 
 189         javadoc("-d", "out-nonAscii",
 190                 "-sourcepath", src.toString(),
 191                 "-html5",
 192                 "-encoding", "utf-8",
 193                 "p");
 194         checkExit(Exit.OK);
 195 
 196         checkOutput("p/Def.html", true,
 197                 "<a id=\"\u00e0\u00e9\">",
 198                 "<a id=\"\u00c0\u00c9()\">",
 199                 "<a id=\"\u03b1\u03b2\u03b3\">",
 200                 "<a id=\"\u0391\u0392\u0393()\">");
 201 
 202         checkOutput("p/Ref.html", true,
 203                 "<a href=\"Def.html#%C3%A0%C3%A9\"><code>&agrave;&eacute;</code></a>",
 204                 "<a href=\"Def.html#%C3%80%C3%89()\"><code>&Agrave;&Eacute;</code></a>",
 205                 "<a href=\"Def.html#%CE%B1%CE%B2%CE%B3\"><code>&alpha;&beta;&gamma;</code></a>",
 206                 "<a href=\"Def.html#%CE%91%CE%92%CE%93()\"><code>&Alpha;&Beta;&Gamma;</code></a>");
 207 
 208     }
 209 }