0N/A/*
553N/A * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4524350 4662945 4633447
0N/A * @summary stddoclet: {@docRoot} inserts an extra trailing "/"
0N/A * @author dkramer
0N/A * @run main DocRootSlash
0N/A */
0N/A
0N/Aimport com.sun.javadoc.*;
0N/Aimport java.util.*;
0N/Aimport java.io.*;
0N/Aimport java.nio.*;
0N/Aimport java.util.regex.*;
0N/A
0N/A/**
0N/A * Runs javadoc and runs regression tests on the resulting HTML.
0N/A * It reads each file, complete with newlines, into a string to easily
0N/A * find strings that contain newlines.
0N/A */
0N/Apublic class DocRootSlash
0N/A{
0N/A private static final String BUGID = "4524350, 4662945, or 4633447";
0N/A private static final String BUGNAME = "DocRootSlash";
0N/A private static final String FS = System.getProperty("file.separator");
0N/A private static final String PS = System.getProperty("path.separator");
0N/A private static final String TMPDIR_STRING1 = "." + FS + "docs1" + FS;
0N/A
0N/A // Test number. Needed because runResultsOnHTMLFile is run twice, and subtestNum
0N/A // should increment across test runs.
0N/A public static int subtestNum = 0;
0N/A public static int numOfSubtestsPassed = 0;
0N/A
0N/A // Entry point
0N/A public static void main(String[] args) {
0N/A
0N/A // Directory that contains source files that javadoc runs on
0N/A String srcdir = System.getProperty("test.src", ".");
0N/A
0N/A runJavadoc(new String[] {"-d", TMPDIR_STRING1,
0N/A "-overview", (srcdir + FS + "overview.html"),
0N/A "-header", "<A HREF=\"{@docroot}/package-list\">{&#064;docroot}</A> <A HREF=\"{@docRoot}/help-doc\">{&#064;docRoot}</A>",
0N/A "-sourcepath", srcdir,
0N/A "p1", "p2"});
0N/A runTestsOnHTMLFiles(filenameArray);
0N/A
0N/A printSummary();
0N/A }
0N/A
0N/A /** Run javadoc */
0N/A public static void runJavadoc(String[] javadocArgs) {
0N/A if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {
0N/A throw new Error("Javadoc failed to execute");
0N/A }
0N/A }
0N/A
0N/A /** The array of filenames to test */
0N/A private static final String[] filenameArray = {
0N/A TMPDIR_STRING1 + "p1" + FS + "C1.html" ,
0N/A TMPDIR_STRING1 + "p1" + FS + "package-summary.html",
0N/A TMPDIR_STRING1 + "overview-summary.html"
0N/A };
0N/A
0N/A public static void runTestsOnHTMLFiles(String[] filenameArray) {
0N/A String fileString;
0N/A
0N/A // Bugs 4524350 4662945
0N/A for (int i = 0; i < filenameArray.length; i++ ) {
0N/A
0N/A // Read contents of file (whose filename is in filenames) into a string
0N/A fileString = readFileToString(filenameArray[i]);
0N/A
0N/A System.out.println("\nSub-tests for file: " + filenameArray[i]
0N/A + " --------------");
0N/A
0N/A // Loop over all tests in a single file
0N/A for ( int j = 0; j < 11; j++ ) {
0N/A subtestNum += 1;
0N/A
0N/A // Compare actual to expected string for a single subtest
0N/A compareActualToExpected(fileString);
0N/A }
0N/A }
0N/A
0N/A // Bug 4633447: Special test for overview-frame.html
0N/A // Find two strings in file "overview-frame.html"
0N/A String filename = TMPDIR_STRING1 + "overview-frame.html";
0N/A fileString = readFileToString(filename);
0N/A
0N/A // Find first string <A HREF="./package-list"> in overview-frame.html
0N/A subtestNum += 1;
0N/A String stringToFind = "<A HREF=\"./package-list\">";
0N/A String result;
0N/A if ( fileString.indexOf(stringToFind) == -1 ) {
0N/A result = "FAILED";
0N/A } else {
0N/A result = "succeeded";
0N/A numOfSubtestsPassed += 1;
0N/A }
0N/A System.out.println("\nSub-test " + (subtestNum)
0N/A + " for bug " + BUGID + " (" + BUGNAME + ") " + result + "\n"
0N/A + "when searching for:\n"
0N/A + stringToFind + "\n"
0N/A + "in file " + filename);
0N/A
0N/A // Find second string <A HREF="./help-doc"> in overview-frame.html
0N/A subtestNum += 1;
0N/A stringToFind = "<A HREF=\"./help-doc\">";
0N/A if ( fileString.indexOf(stringToFind) == -1 ) {
0N/A result = "FAILED";
0N/A } else {
0N/A result = "succeeded";
0N/A numOfSubtestsPassed += 1;
0N/A }
0N/A System.out.println("\nSub-test " + (subtestNum)
0N/A + " for bug " + BUGID + " (" + BUGNAME + ") " + result + "\n"
0N/A + "when searching for:\n"
0N/A + stringToFind + "\n"
0N/A + "in file " + filename);
0N/A }
0N/A
0N/A public static void printSummary() {
0N/A System.out.println("");
0N/A if ( numOfSubtestsPassed == subtestNum ) {
0N/A System.out.println("\nAll " + numOfSubtestsPassed + " subtests passed");
0N/A } else {
0N/A throw new Error("\n" + (subtestNum - numOfSubtestsPassed) + " of " + (subtestNum)
0N/A + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");
0N/A }
0N/A }
0N/A
0N/A // Read the contents of the file into a String
0N/A public static String readFileToString(String filename) {
0N/A try {
0N/A File file = new File(filename);
0N/A if ( !file.exists() ) {
0N/A System.out.println("\nFILE DOES NOT EXIST: " + filename);
0N/A }
0N/A
0N/A BufferedReader in = new BufferedReader(new FileReader(file));
0N/A
0N/A // Create an array of characters the size of the file
0N/A char[] allChars = new char[(int)file.length()];
0N/A
0N/A // Read the characters into the allChars array
0N/A in.read(allChars, 0, (int)file.length());
0N/A in.close();
0N/A
0N/A // Convert to a string
0N/A String allCharsString = new String(allChars);
0N/A
0N/A return allCharsString;
0N/A } catch (FileNotFoundException e) {
0N/A System.err.println(e);
0N/A return "";
0N/A } catch (IOException e) {
0N/A System.err.println(e);
0N/A return "";
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Regular expression pattern matching code adapted from Eric's
0N/A * /java/pubs/dev/linkfix/src/LinkFix.java
0N/A *
0N/A * Prefix Pattern:
0N/A * flag (?i) (case insensitive, so "a href" == "A HREF" and all combinations)
0N/A * group1 (
0N/A * <a or <A
0N/A * \\s+ (one or more whitespace characters)
0N/A * href or HREF
0N/A * \" (double quote)
0N/A * )
0N/A * group2 ([^\"]*) (link reference -- characters that don't include a quote)
0N/A * group3 (\".*?>) (" target="frameName">)
0N/A * group4 (.*?) (label - zero or more characters)
0N/A * group5 (</a>) (end tag)
0N/A */
0N/A static String prefix = "(?i)(<a\\s+href="; // <a href= (start group1)
0N/A static String ref1 = "\")([^\"]*)(\".*?>)"; // doublequotes (end group1, group2, group3)
0N/A static String ref2 = ")(\\S+?)([^<>]*>)"; // no quotes (end group1, group2, group3)
0N/A static String label = "(.*?)"; // text label (group4)
0N/A static String end = "(</a>)"; // </a> (group5)
0N/A
0N/A /**
0N/A * Compares the actual string to the expected string in the specified string
0N/A * str String to search through
0N/A */
0N/A static void compareActualToExpected(String str) {
0N/A // Pattern must be compiled each run because subtestNum is incremented
0N/A Pattern actualLinkPattern1 =
0N/A Pattern.compile("Sub-test " + subtestNum + " Actual: " + prefix + ref1, Pattern.DOTALL);
0N/A Pattern expectLinkPattern1 =
0N/A Pattern.compile("Sub-test " + subtestNum + " Expect: " + prefix + ref1, Pattern.DOTALL);
0N/A // Pattern linkPattern2 = Pattern.compile(prefix + ref2 + label + end, Pattern.DOTALL);
0N/A
0N/A CharBuffer charBuffer = CharBuffer.wrap(str);
0N/A Matcher actualLinkMatcher1 = actualLinkPattern1.matcher(charBuffer);
0N/A Matcher expectLinkMatcher1 = expectLinkPattern1.matcher(charBuffer);
0N/A String result;
0N/A if ( expectLinkMatcher1.find() && actualLinkMatcher1.find() ) {
0N/A String expectRef = expectLinkMatcher1.group(2);
0N/A String actualRef = actualLinkMatcher1.group(2);
0N/A if ( actualRef.equals(expectRef) ) {
0N/A result = "succeeded";
0N/A numOfSubtestsPassed += 1;
0N/A // System.out.println("pattern: " + actualLinkPattern1.pattern());
0N/A // System.out.println("actualRef: " + actualRef);
0N/A // System.out.println("group0: " + actualLinkMatcher1.group());
0N/A // System.out.println("group1: " + actualLinkMatcher1.group(1));
0N/A // System.out.println("group2: " + actualLinkMatcher1.group(2));
0N/A // System.out.println("group3: " + actualLinkMatcher1.group(3));
0N/A // System.exit(0);
0N/A } else {
0N/A result = "FAILED";
0N/A }
0N/A System.out.println("\nSub-test " + (subtestNum)
0N/A + " for bug " + BUGID + " (" + BUGNAME + ") " + result + "\n"
0N/A + "Actual: \"" + actualRef + "\"" + "\n"
0N/A + "Expect: \"" + expectRef + "\"");
0N/A } else {
0N/A System.out.println("Didn't find <A HREF> that fits the pattern: "
0N/A + expectLinkPattern1.pattern() );
0N/A }
0N/A }
0N/A}