Path: blob/master/test/langtools/jdk/javadoc/doclet/testDocFiles/TestDocFiles.java
40971 views
/*1* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8008949 8234051 824569626* @summary doclet crashes if HTML files in module doc-files directories27* @library /tools/lib ../../lib28* @modules jdk.javadoc/jdk.javadoc.internal.tool29* @build toolbox.ToolBox javadoc.tester.*30* @run main TestDocFiles31*/3233import java.io.IOException;34import java.nio.file.Path;35import java.util.ArrayList;36import java.util.List;3738import toolbox.ToolBox;39import javadoc.tester.JavadocTester;4041public class TestDocFiles extends JavadocTester {4243public static void main(String... args) throws Exception {44TestDocFiles tester = new TestDocFiles();45tester.runTests(m -> new Object[] { Path.of(m.getName()) });46}4748ToolBox tb = new ToolBox();4950/**51* Check doc-files support for a package that is not in a module.52* @param base the base directory for scratch files53* @throws IOException if an exception occurs54*/55@Test56public void testPackage(Path base) throws IOException {57Path src = base.resolve("src");5859// write the skeletal Java files60tb.writeJavaFiles(src,61"package p; public class C { }\n");6263// write the doc files for the package64Path pkgDocFiles = src.resolve("p").resolve("doc-files");65tb.writeFile(pkgDocFiles.resolve("pkg-file.txt"),66"package text file\n");67tb.writeFile(pkgDocFiles.resolve("pkg-file.html"),68"""69<html>70<head><title>Package HTML file</title></head>71<body><h1>Package HTML file</h1>File content</body>72</html>73""");7475javadoc("-d", base.resolve("out").toString(),76"--source-path", src.toString(),77"p");78checkExit(Exit.OK);7980checkOutput("p/doc-files/pkg-file.txt", true,81"package text file");82checkOutput("p/doc-files/pkg-file.html", true,83"Package HTML file");84}8586/**87* Check doc-files support for a module and a package that is in a module.88* @param base the base directory for scratch files89* @throws IOException if an exception occurs90*/91@Test92public void testModules(Path base) throws IOException {93Path src = base.resolve("src");9495// write the skeletal Java files96tb.writeJavaFiles(src,97"module m { exports p; }\n",98"package p; public class C { }\n");99100// write the doc files for the module101Path mdlDocFiles = src.resolve("doc-files");102tb.writeFile(mdlDocFiles.resolve("mdl-file.txt"),103"module text file\n");104tb.writeFile(mdlDocFiles.resolve("mdl-file.html"),105"""106<html>107<head><title>Module HTML file</title></head>108<body><h1>Module HTML file</h1>File content</body>109</html>110""");111112// write the doc files for a package in the module113Path pkgDocFiles = src.resolve("p").resolve("doc-files");114tb.writeFile(pkgDocFiles.resolve("pkg-file.txt"),115"package text file\n");116tb.writeFile(pkgDocFiles.resolve("pkg-file.html"),117"""118<html>119<head><title>Package HTML file</title></head>120<body><h1>Package HTML file</h1>File content</body>121</html>122""");123124javadoc("-d", base.resolve("out").toString(),125"--source-path", src.toString(),126"--module", "m");127checkExit(Exit.OK);128129checkOutput("m/doc-files/mdl-file.txt", true,130"module text file");131checkOutput("m/doc-files/mdl-file.html", true,132"Module HTML file");133checkOutput("m/p/doc-files/pkg-file.txt", true,134"package text file");135checkOutput("m/p/doc-files/pkg-file.html", true,136"Package HTML file");137}138139@Test140public void testBadFiles(Path base) throws IOException {141Path src = base.resolve("src");142143tb.writeJavaFiles(src,144"package p; public class C { }");145Path df = src.resolve("p").resolve("doc-files");146147// note that '?' may be an illegal filename character on some systems (e.g. Windows)148List<String> cases = List.of("valid", "#bad#", "#bad", "bad#bad", "bad?bad");149List<Path> files = new ArrayList<>();150for (String s : cases) {151try {152Path f = df.resolve(s);153tb.writeFile(f, "dummy contents");154files.add(f);155} catch (Throwable t) {156out.println("Cannot write doc-file " + s);157}158}159160javadoc("-d", base.resolve("out").toString(),161"--source-path", src.toString(),162"p");163checkExit(Exit.OK);164165// only check for those files166for (Path f : files) {167if (f.getFileName().toString().contains("valid")) {168checkOutput("p/doc-files/" + f.getFileName(), true,169"dummy contents");170} else {171// be careful handing file separator characters in the message172checkOutput(Output.OUT, true,173"warning: File " + f + " not copied: invalid name");174}175}176}177}178179180