1 /*
   2  * Copyright (c) 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 8235414
  27  * @summary Module level doc-files show "unnamed package" as holder
  28  * @library /tools/lib ../../lib
  29  * @modules jdk.javadoc/jdk.javadoc.internal.tool
  30  * @build javadoc.tester.* toolbox.ToolBox
  31  * @run main TestIndexInDocFiles
  32  */
  33 
  34 
  35 import java.io.IOException;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 
  39 import toolbox.ToolBox;
  40 
  41 import javadoc.tester.JavadocTester;
  42 
  43 public class TestIndexInDocFiles extends JavadocTester {
  44 
  45     final ToolBox tb;
  46 
  47     public static void main(String... args) throws Exception {
  48         TestIndexInDocFiles tester = new TestIndexInDocFiles();
  49         tester.runTests(m -> new Object[] { Paths.get(m.getName()) });
  50     }
  51 
  52     TestIndexInDocFiles() {
  53         tb = new ToolBox();
  54     }
  55 
  56     /**
  57      * Test support for index tag and system properties in package level doc-files.
  58      * @param base the base directory for scratch files
  59      * @throws IOException if an exception occurs
  60      */
  61     @Test
  62     public void testPackageDocFiles(Path base) throws IOException {
  63         Path src = base.resolve("src");
  64 
  65         // write the skeletal Java files
  66         tb.writeJavaFiles(src,
  67                 "public class A { }\n",
  68                 "package p.q; public class C { }\n");
  69 
  70         // write the top level (unnamed package) doc file
  71         Path topLevelDocFiles = src.resolve("doc-files");
  72         tb.writeFile(topLevelDocFiles.resolve("top-level-file.html"),
  73                 "<html>\n"
  74                 + "<head><title>Top level HTML file</title></head>\n"
  75                 + "<body><h1>Package HTML file</h1>\n"
  76                 + "{@index top-level-index additional info}\n"
  77                 + "{@systemProperty top.level.property}\n"
  78                 + "File content</body>\n"
  79                 + "</html>\n");
  80 
  81         // write the (named) package level doc file
  82         Path pkgDocFiles = src.resolve("p").resolve("q").resolve("doc-files");
  83         tb.writeFile(pkgDocFiles.resolve("package-file.html"),
  84                 "<html>\n"
  85                 + "<head><title>Package HTML file</title></head>\n"
  86                 + "<body><h1>Package HTML file</h1>\n"
  87                 + "{@index package-index additional info}\n"
  88                 + "{@systemProperty package.property}\n"
  89                 + "File content</body>\n"
  90                 + "</html>\n");
  91 
  92         javadoc("-d", base.resolve("out").toString(),
  93                 "--source-path", src.toString(),
  94                 src.resolve("A.java").toString(), "p.q");
  95         checkExit(Exit.OK);
  96 
  97         checkOutput("doc-files/top-level-file.html", true,
  98                 "<h1>Package HTML file</h1>\n"
  99                 + "<a id=\"top-level-index\" class=\"searchTagResult\">top-level-index</a>\n"
 100                 + "<code><a id=\"top.level.property\" class=\"searchTagResult\">top.level.property</a></code>\n");
 101         checkOutput("p/q/doc-files/package-file.html", true,
 102                 "<h1>Package HTML file</h1>\n"
 103                 + "<a id=\"package-index\" class=\"searchTagResult\">package-index</a>\n"
 104                 + "<code><a id=\"package.property\" class=\"searchTagResult\">package.property</a></code>\n");
 105         checkOutput("tag-search-index.js", true,
 106                 "{\"l\":\"package-index\",\"h\":\"package p.q\",\"d\":\"additional info\","
 107                 + "\"u\":\"p/q/doc-files/package-file.html#package-index\"}",
 108                 "{\"l\":\"package.property\",\"h\":\"package p.q\",\"d\":\"System Property\","
 109                 + "\"u\":\"p/q/doc-files/package-file.html#package.property\"}",
 110                 "{\"l\":\"top-level-index\",\"h\":\"unnamed package\",\"d\":\"additional info\","
 111                 + "\"u\":\"doc-files/top-level-file.html#top-level-index\"}",
 112                 "{\"l\":\"top.level.property\",\"h\":\"unnamed package\",\"d\":\"System Property\","
 113                 + "\"u\":\"doc-files/top-level-file.html#top.level.property\"}");
 114     }
 115 
 116     /**
 117      * Test support for index tags and system properties in module and package level doc-files.
 118      * @param base the base directory for scratch files
 119      * @throws IOException if an exception occurs
 120      */
 121     @Test
 122     public void testModuleDocFiles(Path base) throws IOException {
 123         Path src = base.resolve("src");
 124 
 125         // write the skeletal Java files
 126         tb.writeJavaFiles(src,
 127                 "module m.n { exports p.q; }\n",
 128                 "public class A { }\n",
 129                 "package p.q; public class C { }\n");
 130 
 131         // write the doc files for the module
 132         Path mdlDocFiles = src.resolve("doc-files");
 133         tb.writeFile(mdlDocFiles.resolve("module-file.html"),
 134                 "<html>\n"
 135                 + "<head><title>Module HTML file</title></head>\n"
 136                 + "<body><h1>Module HTML file</h1>\n"
 137                 + "{@index module-index additional info}\n"
 138                 + "{@systemProperty module.property}\n"
 139                 + "File content</body>\n"
 140                 + "</html>\n");
 141 
 142         // write the doc files for a package in the module
 143         Path pkgDocFiles = src.resolve("p").resolve("q").resolve("doc-files");
 144         tb.writeFile(pkgDocFiles.resolve("package-file.html"),
 145                 "<html>\n"
 146                 + "<head><title>Package HTML file</title></head>\n"
 147                 + "<body><h1>Package HTML file</h1>\n"
 148                 + "{@index package-index additional info}\n"
 149                 + "{@systemProperty package.property}\n"
 150                 + "File content</body>\n"
 151                 + "</html>\n");
 152 
 153         javadoc("-d", base.resolve("out").toString(),
 154                 "--source-path", src.toString(),
 155                 "--module", "m.n");
 156         checkExit(Exit.OK);
 157 
 158         checkOutput("m.n/doc-files/module-file.html", true,
 159                 "<h1>Module HTML file</h1>\n"
 160                 + "<a id=\"module-index\" class=\"searchTagResult\">module-index</a>\n"
 161                 + "<code><a id=\"module.property\" class=\"searchTagResult\">module.property</a></code>\n");
 162         checkOutput("m.n/p/q/doc-files/package-file.html", true,
 163                 "<h1>Package HTML file</h1>\n"
 164                 + "<a id=\"package-index\" class=\"searchTagResult\">package-index</a>\n"
 165                 + "<code><a id=\"package.property\" class=\"searchTagResult\">package.property</a></code>\n");
 166         checkOutput("tag-search-index.js", true,
 167                 "{\"l\":\"module-index\",\"h\":\"module m.n\",\"d\":\"additional info\","
 168                 + "\"u\":\"m.n/doc-files/module-file.html#module-index\"}",
 169                 "{\"l\":\"package-index\",\"h\":\"package p.q\",\"d\":\"additional info\","
 170                 + "\"u\":\"m.n/p/q/doc-files/package-file.html#package-index\"}",
 171                 "{\"l\":\"module.property\",\"h\":\"module m.n\",\"d\":\"System Property\","
 172                 + "\"u\":\"m.n/doc-files/module-file.html#module.property\"}",
 173                 "{\"l\":\"package.property\",\"h\":\"package p.q\",\"d\":\"System Property\","
 174                 + "\"u\":\"m.n/p/q/doc-files/package-file.html#package.property\"}");
 175     }
 176 }