Path: blob/master/test/langtools/jdk/javadoc/doclet/DocRootSlash/DocRootSlash.java
40971 views
/*1* Copyright (c) 2002, 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 4524350 4662945 4633447 8196202 826197626* @summary stddoclet: {@docRoot} inserts an extra trailing "/"27* @library ../../lib28* @modules jdk.javadoc/jdk.javadoc.internal.tool29* @build javadoc.tester.*30* @run main DocRootSlash31*/3233import java.util.regex.*;3435/**36* Runs javadoc and runs regression tests on the resulting HTML.37* It reads each file, complete with newlines, into a string to easily38* find strings that contain newlines.39*/40import javadoc.tester.JavadocTester;4142public class DocRootSlash extends JavadocTester {4344public static void main(String... args) throws Exception {45DocRootSlash tester = new DocRootSlash();46tester.runTests();47}4849@Test50public void test() {51// Directory that contains source files that javadoc runs on52String srcdir = System.getProperty("test.src", ".");535455javadoc("-d", "out",56"-Xdoclint:none",57"-overview", (srcdir + "/overview.html"),58"-header", """59<A HREF="{@docroot}/element-list">{@docroot}</A> <A HREF="{@docRoot}/help-doc.html">{@docRoot}</A>""",60"-sourcepath", srcdir,61"p1", "p2");6263checkFiles(64"p1/C1.html",65"p1/package-summary.html",66"index.html");67}6869void checkFiles(String... filenameArray) {70int count = 0;7172for (String f : filenameArray) {73// Read contents of file into a string74String fileString = readFile(f);75System.out.println("\nSub-tests for file: " + f + " --------------");76// Loop over all tests in a single file77for ( int j = 0; j < 7; j++ ) {7879// Compare actual to expected string for a single subtest80compareActualToExpected(++count, fileString);81}82}83}8485/**86* Regular expression pattern matching code87*88* Prefix Pattern:89* flag (?i) (case insensitive, so "a href" == "A HREF" and all combinations)90* group1 (91* <a or <A92* \\s+ (one or more whitespace characters)93* href or HREF94* \" (double quote)95* )96* group2 ([^\"]*) (link reference -- characters that don't include a quote)97* group3 (\".*?>) (" target="frameName">)98* group4 (.*?) (label - zero or more characters)99* group5 (</a>) (end tag)100*/101private static final String prefix = "(?i)(<a\\s+href="; // <a href= (start group1)102private static final String ref1 = "\")([^\"]*)(\".*?>)"; // doublequotes (end group1, group2, group3)103104/**105* Compares the actual string to the expected string in the specified string106* @param str String to search through107*/108void compareActualToExpected(int count, String str) {109checking("comparison for " + str);110111// Pattern must be compiled each run because numTestsRun is incremented112Pattern actualLinkPattern1 =113Pattern.compile("Sub-test " + count + " Actual: " + prefix + ref1, Pattern.DOTALL);114Pattern expectLinkPattern1 =115Pattern.compile("Sub-test " + count + " Expect: " + prefix + ref1, Pattern.DOTALL);116// Pattern linkPattern2 = Pattern.compile(prefix + ref2 + label + end, Pattern.DOTALL);117118Matcher actualLinkMatcher1 = actualLinkPattern1.matcher(str);119Matcher expectLinkMatcher1 = expectLinkPattern1.matcher(str);120if (expectLinkMatcher1.find() && actualLinkMatcher1.find()) {121String expectRef = expectLinkMatcher1.group(2);122String actualRef = actualLinkMatcher1.group(2);123if (actualRef.equals(expectRef)) {124passed(expectRef);125// System.out.println("pattern: " + actualLinkPattern1.pattern());126// System.out.println("actualRef: " + actualRef);127// System.out.println("group0: " + actualLinkMatcher1.group());128// System.out.println("group1: " + actualLinkMatcher1.group(1));129// System.out.println("group2: " + actualLinkMatcher1.group(2));130// System.out.println("group3: " + actualLinkMatcher1.group(3));131// System.exit(0);132} else {133failed("\n"134+ "Actual: \"" + actualRef + "\"\n"135+ "Expect: \"" + expectRef + "\"");136}137} else {138failed("Didn't find <A HREF> that fits the pattern: "139+ expectLinkPattern1.pattern());140}141}142}143144145