test/com/sun/javadoc/DocRootSlash/DocRootSlash.java

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 554
9d9f26857129
permissions
-rw-r--r--

Initial load

     1 /*
     2  * Copyright 2002 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 4524350 4662945 4633447
    27  * @summary stddoclet: {@docRoot} inserts an extra trailing "/"
    28  * @author dkramer
    29  * @run main DocRootSlash
    30  */
    32 import com.sun.javadoc.*;
    33 import java.util.*;
    34 import java.io.*;
    35 import java.nio.*;
    36 import java.util.regex.*;
    38 /**
    39  * Runs javadoc and runs regression tests on the resulting HTML.
    40  * It reads each file, complete with newlines, into a string to easily
    41  * find strings that contain newlines.
    42  */
    43 public class DocRootSlash
    44 {
    45     private static final String BUGID = "4524350, 4662945, or 4633447";
    46     private static final String BUGNAME = "DocRootSlash";
    47     private static final String FS = System.getProperty("file.separator");
    48     private static final String PS = System.getProperty("path.separator");
    49     private static final String TMPDIR_STRING1 = "." + FS + "docs1" + FS;
    51     // Test number.  Needed because runResultsOnHTMLFile is run twice, and subtestNum
    52     // should increment across test runs.
    53     public static int subtestNum = 0;
    54     public static int numOfSubtestsPassed = 0;
    56     // Entry point
    57     public static void main(String[] args) {
    59         // Directory that contains source files that javadoc runs on
    60         String srcdir = System.getProperty("test.src", ".");
    62         runJavadoc(new String[] {"-d", TMPDIR_STRING1,
    63                                  "-overview", (srcdir + FS + "overview.html"),
    64                                  "-header", "<A HREF=\"{@docroot}/package-list\">{&#064;docroot}</A> <A HREF=\"{@docRoot}/help-doc\">{&#064;docRoot}</A>",
    65                                  "-sourcepath", srcdir,
    66                                  "p1", "p2"});
    67         runTestsOnHTMLFiles(filenameArray);
    69         printSummary();
    70     }
    72     /** Run javadoc */
    73     public static void runJavadoc(String[] javadocArgs) {
    74         if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {
    75             throw new Error("Javadoc failed to execute");
    76         }
    77     }
    79     /**  The array of filenames to test */
    80     private static final String[] filenameArray = {
    81         TMPDIR_STRING1 + "p1" + FS + "C1.html" ,
    82         TMPDIR_STRING1 + "p1" + FS + "package-summary.html",
    83         TMPDIR_STRING1 + "overview-summary.html"
    84     };
    86     public static void runTestsOnHTMLFiles(String[] filenameArray) {
    87         String fileString;
    89         // Bugs 4524350 4662945
    90         for (int i = 0; i < filenameArray.length; i++ ) {
    92             // Read contents of file (whose filename is in filenames) into a string
    93             fileString = readFileToString(filenameArray[i]);
    95             System.out.println("\nSub-tests for file: " + filenameArray[i]
    96                                 + " --------------");
    98             // Loop over all tests in a single file
    99             for ( int j = 0; j < 11; j++ ) {
   100                 subtestNum += 1;
   102                 // Compare actual to expected string for a single subtest
   103                 compareActualToExpected(fileString);
   104             }
   105         }
   107         // Bug 4633447: Special test for overview-frame.html
   108         // Find two strings in file "overview-frame.html"
   109         String filename = TMPDIR_STRING1 + "overview-frame.html";
   110         fileString = readFileToString(filename);
   112         // Find first string <A HREF="./package-list"> in overview-frame.html
   113         subtestNum += 1;
   114         String stringToFind = "<A HREF=\"./package-list\">";
   115         String result;
   116         if ( fileString.indexOf(stringToFind) == -1 ) {
   117             result = "FAILED";
   118         } else {
   119             result = "succeeded";
   120             numOfSubtestsPassed += 1;
   121         }
   122         System.out.println("\nSub-test " + (subtestNum)
   123             + " for bug " + BUGID + " (" + BUGNAME + ") " + result + "\n"
   124             + "when searching for:\n"
   125             + stringToFind + "\n"
   126             + "in file " + filename);
   128         // Find second string <A HREF="./help-doc"> in overview-frame.html
   129         subtestNum += 1;
   130         stringToFind = "<A HREF=\"./help-doc\">";
   131         if ( fileString.indexOf(stringToFind) == -1 ) {
   132             result = "FAILED";
   133         } else {
   134             result = "succeeded";
   135             numOfSubtestsPassed += 1;
   136         }
   137         System.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     }
   144     public static void printSummary() {
   145         System.out.println("");
   146         if ( numOfSubtestsPassed == subtestNum ) {
   147             System.out.println("\nAll " + numOfSubtestsPassed + " subtests passed");
   148         } else {
   149             throw new Error("\n" + (subtestNum - numOfSubtestsPassed) + " of " + (subtestNum)
   150                             + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");
   151         }
   152     }
   154     // Read the contents of the file into a String
   155     public static String readFileToString(String filename) {
   156         try {
   157             File file = new File(filename);
   158             if ( !file.exists() ) {
   159                 System.out.println("\nFILE DOES NOT EXIST: " + filename);
   160             }
   162             BufferedReader in = new BufferedReader(new FileReader(file));
   164             // Create an array of characters the size of the file
   165             char[] allChars = new char[(int)file.length()];
   167             // Read the characters into the allChars array
   168             in.read(allChars, 0, (int)file.length());
   169             in.close();
   171             // Convert to a string
   172             String allCharsString = new String(allChars);
   174             return allCharsString;
   175         } catch (FileNotFoundException e) {
   176             System.err.println(e);
   177             return "";
   178         } catch (IOException e) {
   179             System.err.println(e);
   180             return "";
   181         }
   182     }
   184     /**
   185      * Regular expression pattern matching code adapted from Eric's
   186      * /java/pubs/dev/linkfix/src/LinkFix.java
   187      *
   188      * Prefix Pattern:
   189      * flag   (?i)            (case insensitive, so "a href" == "A HREF" and all combinations)
   190      * group1 (
   191      *          <a or <A
   192      *          \\s+          (one or more whitespace characters)
   193      *          href or HREF
   194      *          \"            (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      */
   201     static String prefix = "(?i)(<a\\s+href=";    // <a href=     (start group1)
   202     static String ref1   = "\")([^\"]*)(\".*?>)"; // doublequotes (end group1, group2, group3)
   203     static String ref2   = ")(\\S+?)([^<>]*>)";   // no quotes    (end group1, group2, group3)
   204     static String label  = "(.*?)";               // text label   (group4)
   205     static String end    = "(</a>)";              // </a>         (group5)
   207     /**
   208      * Compares the actual string to the expected string in the specified string
   209      * str   String to search through
   210      */
   211     static void compareActualToExpected(String str) {
   212         // Pattern must be compiled each run because subtestNum is incremented
   213         Pattern actualLinkPattern1 =
   214             Pattern.compile("Sub-test " + subtestNum + " Actual: " + prefix + ref1, Pattern.DOTALL);
   215         Pattern expectLinkPattern1 =
   216             Pattern.compile("Sub-test " + subtestNum + " Expect: " + prefix + ref1, Pattern.DOTALL);
   217         // Pattern linkPattern2 = Pattern.compile(prefix + ref2 + label + end, Pattern.DOTALL);
   219         CharBuffer charBuffer = CharBuffer.wrap(str);
   220         Matcher actualLinkMatcher1 = actualLinkPattern1.matcher(charBuffer);
   221         Matcher expectLinkMatcher1 = expectLinkPattern1.matcher(charBuffer);
   222         String result;
   223         if ( expectLinkMatcher1.find() && actualLinkMatcher1.find() ) {
   224             String expectRef = expectLinkMatcher1.group(2);
   225             String actualRef = actualLinkMatcher1.group(2);
   226             if ( actualRef.equals(expectRef) ) {
   227                 result = "succeeded";
   228                 numOfSubtestsPassed += 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 {
   237                 result = "FAILED";
   238             }
   239             System.out.println("\nSub-test " + (subtestNum)
   240                 + " for bug " + BUGID + " (" + BUGNAME + ") " + result + "\n"
   241                 + "Actual: \"" + actualRef + "\"" + "\n"
   242                 + "Expect: \"" + expectRef + "\"");
   243         } else {
   244             System.out.println("Didn't find <A HREF> that fits the pattern: "
   245                   + expectLinkPattern1.pattern() );
   246         }
   247     }
   248 }

mercurial