Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/com/sun/javadoc/DocRootSlash/DocRootSlash.java
48425 views
/*1* Copyright (c) 2002, 2013, 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 463344726* @summary stddoclet: {@docRoot} inserts an extra trailing "/"27* @author dkramer28* @run main DocRootSlash29*/3031import com.sun.javadoc.*;32import java.util.*;33import java.io.*;34import java.nio.*;35import java.util.regex.*;3637/**38* Runs javadoc and runs regression tests on the resulting HTML.39* It reads each file, complete with newlines, into a string to easily40* find strings that contain newlines.41*/42public class DocRootSlash43{44private static final String BUGID = "4524350, 4662945, or 4633447";45private static final String BUGNAME = "DocRootSlash";46private static final String FS = System.getProperty("file.separator");47private static final String PS = System.getProperty("path.separator");48private static final String TMPDIR_STRING1 = "." + FS + "docs1" + FS;4950// Test number. Needed because runResultsOnHTMLFile is run twice, and subtestNum51// should increment across test runs.52public static int subtestNum = 0;53public static int numOfSubtestsPassed = 0;5455// Entry point56public static void main(String[] args) {5758// Directory that contains source files that javadoc runs on59String srcdir = System.getProperty("test.src", ".");6061runJavadoc(new String[] {"-d", TMPDIR_STRING1,62"-Xdoclint:none",63"-overview", (srcdir + FS + "overview.html"),64"-header", "<A HREF=\"{@docroot}/package-list\">{@docroot}</A> <A HREF=\"{@docRoot}/help-doc\">{@docRoot}</A>",65"-sourcepath", srcdir,66"p1", "p2"});67runTestsOnHTMLFiles(filenameArray);6869printSummary();70}7172/** Run javadoc */73public static void runJavadoc(String[] javadocArgs) {74if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {75throw new Error("Javadoc failed to execute");76}77}7879/** The array of filenames to test */80private static final String[] filenameArray = {81TMPDIR_STRING1 + "p1" + FS + "C1.html" ,82TMPDIR_STRING1 + "p1" + FS + "package-summary.html",83TMPDIR_STRING1 + "overview-summary.html"84};8586public static void runTestsOnHTMLFiles(String[] filenameArray) {87String fileString;8889// Bugs 4524350 466294590for (int i = 0; i < filenameArray.length; i++ ) {9192// Read contents of file (whose filename is in filenames) into a string93fileString = readFileToString(filenameArray[i]);9495System.out.println("\nSub-tests for file: " + filenameArray[i]96+ " --------------");9798// Loop over all tests in a single file99for ( int j = 0; j < 11; j++ ) {100subtestNum += 1;101102// Compare actual to expected string for a single subtest103compareActualToExpected(fileString);104}105}106107// Bug 4633447: Special test for overview-frame.html108// Find two strings in file "overview-frame.html"109String filename = TMPDIR_STRING1 + "overview-frame.html";110fileString = readFileToString(filename);111112// Find first string <A HREF="./package-list"> in overview-frame.html113subtestNum += 1;114String stringToFind = "<A HREF=\"./package-list\">";115String result;116if ( fileString.indexOf(stringToFind) == -1 ) {117result = "FAILED";118} else {119result = "succeeded";120numOfSubtestsPassed += 1;121}122System.out.println("\nSub-test " + (subtestNum)123+ " for bug " + BUGID + " (" + BUGNAME + ") " + result + "\n"124+ "when searching for:\n"125+ stringToFind + "\n"126+ "in file " + filename);127128// Find second string <A HREF="./help-doc"> in overview-frame.html129subtestNum += 1;130stringToFind = "<A HREF=\"./help-doc\">";131if ( fileString.indexOf(stringToFind) == -1 ) {132result = "FAILED";133} else {134result = "succeeded";135numOfSubtestsPassed += 1;136}137System.out.println("\nSub-test " + (subtestNum)138+ " for bug " + BUGID + " (" + BUGNAME + ") " + result + "\n"139+ "when searching for:\n"140+ stringToFind + "\n"141+ "in file " + filename);142}143144public static void printSummary() {145System.out.println("");146if ( numOfSubtestsPassed == subtestNum ) {147System.out.println("\nAll " + numOfSubtestsPassed + " subtests passed");148} else {149throw new Error("\n" + (subtestNum - numOfSubtestsPassed) + " of " + (subtestNum)150+ " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");151}152}153154// Read the contents of the file into a String155public static String readFileToString(String filename) {156try {157File file = new File(filename);158if ( !file.exists() ) {159System.out.println("\nFILE DOES NOT EXIST: " + filename);160}161162BufferedReader in = new BufferedReader(new FileReader(file));163164// Create an array of characters the size of the file165char[] allChars = new char[(int)file.length()];166167// Read the characters into the allChars array168in.read(allChars, 0, (int)file.length());169in.close();170171// Convert to a string172String allCharsString = new String(allChars);173174return allCharsString;175} catch (FileNotFoundException e) {176System.err.println(e);177return "";178} catch (IOException e) {179System.err.println(e);180return "";181}182}183184/**185* Regular expression pattern matching code adapted from Eric's186* /java/pubs/dev/linkfix/src/LinkFix.java187*188* Prefix Pattern:189* flag (?i) (case insensitive, so "a href" == "A HREF" and all combinations)190* group1 (191* <a or <A192* \\s+ (one or more whitespace characters)193* href or HREF194* \" (double quote)195* )196* group2 ([^\"]*) (link reference -- characters that don't include a quote)197* group3 (\".*?>) (" target="frameName">)198* group4 (.*?) (label - zero or more characters)199* group5 (</a>) (end tag)200*/201static String prefix = "(?i)(<a\\s+href="; // <a href= (start group1)202static String ref1 = "\")([^\"]*)(\".*?>)"; // doublequotes (end group1, group2, group3)203static String ref2 = ")(\\S+?)([^<>]*>)"; // no quotes (end group1, group2, group3)204static String label = "(.*?)"; // text label (group4)205static String end = "(</a>)"; // </a> (group5)206207/**208* Compares the actual string to the expected string in the specified string209* str String to search through210*/211static void compareActualToExpected(String str) {212// Pattern must be compiled each run because subtestNum is incremented213Pattern actualLinkPattern1 =214Pattern.compile("Sub-test " + subtestNum + " Actual: " + prefix + ref1, Pattern.DOTALL);215Pattern expectLinkPattern1 =216Pattern.compile("Sub-test " + subtestNum + " Expect: " + prefix + ref1, Pattern.DOTALL);217// Pattern linkPattern2 = Pattern.compile(prefix + ref2 + label + end, Pattern.DOTALL);218219CharBuffer charBuffer = CharBuffer.wrap(str);220Matcher actualLinkMatcher1 = actualLinkPattern1.matcher(charBuffer);221Matcher expectLinkMatcher1 = expectLinkPattern1.matcher(charBuffer);222String result;223if ( expectLinkMatcher1.find() && actualLinkMatcher1.find() ) {224String expectRef = expectLinkMatcher1.group(2);225String actualRef = actualLinkMatcher1.group(2);226if ( actualRef.equals(expectRef) ) {227result = "succeeded";228numOfSubtestsPassed += 1;229// System.out.println("pattern: " + actualLinkPattern1.pattern());230// System.out.println("actualRef: " + actualRef);231// System.out.println("group0: " + actualLinkMatcher1.group());232// System.out.println("group1: " + actualLinkMatcher1.group(1));233// System.out.println("group2: " + actualLinkMatcher1.group(2));234// System.out.println("group3: " + actualLinkMatcher1.group(3));235// System.exit(0);236} else {237result = "FAILED";238}239System.out.println("\nSub-test " + (subtestNum)240+ " for bug " + BUGID + " (" + BUGNAME + ") " + result + "\n"241+ "Actual: \"" + actualRef + "\"" + "\n"242+ "Expect: \"" + expectRef + "\"");243} else {244System.out.println("Didn't find <A HREF> that fits the pattern: "245+ expectLinkPattern1.pattern() );246}247}248}249250251