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

Wed, 08 Oct 2014 14:16:40 -0700

author
asaha
date
Wed, 08 Oct 2014 14:16:40 -0700
changeset 2586
f5e5ca7505e2
parent 1493
d54b4a091450
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright (c) 2002, 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * 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                                  "-Xdoclint:none",
    64                                  "-overview", (srcdir + FS + "overview.html"),
    65                                  "-header", "<A HREF=\"{@docroot}/package-list\">{&#064;docroot}</A> <A HREF=\"{@docRoot}/help-doc\">{&#064;docRoot}</A>",
    66                                  "-sourcepath", srcdir,
    67                                  "p1", "p2"});
    68         runTestsOnHTMLFiles(filenameArray);
    70         printSummary();
    71     }
    73     /** Run javadoc */
    74     public static void runJavadoc(String[] javadocArgs) {
    75         if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {
    76             throw new Error("Javadoc failed to execute");
    77         }
    78     }
    80     /**  The array of filenames to test */
    81     private static final String[] filenameArray = {
    82         TMPDIR_STRING1 + "p1" + FS + "C1.html" ,
    83         TMPDIR_STRING1 + "p1" + FS + "package-summary.html",
    84         TMPDIR_STRING1 + "overview-summary.html"
    85     };
    87     public static void runTestsOnHTMLFiles(String[] filenameArray) {
    88         String fileString;
    90         // Bugs 4524350 4662945
    91         for (int i = 0; i < filenameArray.length; i++ ) {
    93             // Read contents of file (whose filename is in filenames) into a string
    94             fileString = readFileToString(filenameArray[i]);
    96             System.out.println("\nSub-tests for file: " + filenameArray[i]
    97                                 + " --------------");
    99             // Loop over all tests in a single file
   100             for ( int j = 0; j < 11; j++ ) {
   101                 subtestNum += 1;
   103                 // Compare actual to expected string for a single subtest
   104                 compareActualToExpected(fileString);
   105             }
   106         }
   108         // Bug 4633447: Special test for overview-frame.html
   109         // Find two strings in file "overview-frame.html"
   110         String filename = TMPDIR_STRING1 + "overview-frame.html";
   111         fileString = readFileToString(filename);
   113         // Find first string <A HREF="./package-list"> in overview-frame.html
   114         subtestNum += 1;
   115         String stringToFind = "<A HREF=\"./package-list\">";
   116         String result;
   117         if ( fileString.indexOf(stringToFind) == -1 ) {
   118             result = "FAILED";
   119         } else {
   120             result = "succeeded";
   121             numOfSubtestsPassed += 1;
   122         }
   123         System.out.println("\nSub-test " + (subtestNum)
   124             + " for bug " + BUGID + " (" + BUGNAME + ") " + result + "\n"
   125             + "when searching for:\n"
   126             + stringToFind + "\n"
   127             + "in file " + filename);
   129         // Find second string <A HREF="./help-doc"> in overview-frame.html
   130         subtestNum += 1;
   131         stringToFind = "<A HREF=\"./help-doc\">";
   132         if ( fileString.indexOf(stringToFind) == -1 ) {
   133             result = "FAILED";
   134         } else {
   135             result = "succeeded";
   136             numOfSubtestsPassed += 1;
   137         }
   138         System.out.println("\nSub-test " + (subtestNum)
   139             + " for bug " + BUGID + " (" + BUGNAME + ") " + result + "\n"
   140             + "when searching for:\n"
   141             + stringToFind + "\n"
   142             + "in file " + filename);
   143     }
   145     public static void printSummary() {
   146         System.out.println("");
   147         if ( numOfSubtestsPassed == subtestNum ) {
   148             System.out.println("\nAll " + numOfSubtestsPassed + " subtests passed");
   149         } else {
   150             throw new Error("\n" + (subtestNum - numOfSubtestsPassed) + " of " + (subtestNum)
   151                             + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");
   152         }
   153     }
   155     // Read the contents of the file into a String
   156     public static String readFileToString(String filename) {
   157         try {
   158             File file = new File(filename);
   159             if ( !file.exists() ) {
   160                 System.out.println("\nFILE DOES NOT EXIST: " + filename);
   161             }
   163             BufferedReader in = new BufferedReader(new FileReader(file));
   165             // Create an array of characters the size of the file
   166             char[] allChars = new char[(int)file.length()];
   168             // Read the characters into the allChars array
   169             in.read(allChars, 0, (int)file.length());
   170             in.close();
   172             // Convert to a string
   173             String allCharsString = new String(allChars);
   175             return allCharsString;
   176         } catch (FileNotFoundException e) {
   177             System.err.println(e);
   178             return "";
   179         } catch (IOException e) {
   180             System.err.println(e);
   181             return "";
   182         }
   183     }
   185     /**
   186      * Regular expression pattern matching code adapted from Eric's
   187      * /java/pubs/dev/linkfix/src/LinkFix.java
   188      *
   189      * Prefix Pattern:
   190      * flag   (?i)            (case insensitive, so "a href" == "A HREF" and all combinations)
   191      * group1 (
   192      *          <a or <A
   193      *          \\s+          (one or more whitespace characters)
   194      *          href or HREF
   195      *          \"            (double quote)
   196      *        )
   197      * group2 ([^\"]*)        (link reference -- characters that don't include a quote)
   198      * group3 (\".*?>)        (" target="frameName">)
   199      * group4 (.*?)           (label - zero or more characters)
   200      * group5 (</a>)          (end tag)
   201      */
   202     static String prefix = "(?i)(<a\\s+href=";    // <a href=     (start group1)
   203     static String ref1   = "\")([^\"]*)(\".*?>)"; // doublequotes (end group1, group2, group3)
   204     static String ref2   = ")(\\S+?)([^<>]*>)";   // no quotes    (end group1, group2, group3)
   205     static String label  = "(.*?)";               // text label   (group4)
   206     static String end    = "(</a>)";              // </a>         (group5)
   208     /**
   209      * Compares the actual string to the expected string in the specified string
   210      * str   String to search through
   211      */
   212     static void compareActualToExpected(String str) {
   213         // Pattern must be compiled each run because subtestNum is incremented
   214         Pattern actualLinkPattern1 =
   215             Pattern.compile("Sub-test " + subtestNum + " Actual: " + prefix + ref1, Pattern.DOTALL);
   216         Pattern expectLinkPattern1 =
   217             Pattern.compile("Sub-test " + subtestNum + " Expect: " + prefix + ref1, Pattern.DOTALL);
   218         // Pattern linkPattern2 = Pattern.compile(prefix + ref2 + label + end, Pattern.DOTALL);
   220         CharBuffer charBuffer = CharBuffer.wrap(str);
   221         Matcher actualLinkMatcher1 = actualLinkPattern1.matcher(charBuffer);
   222         Matcher expectLinkMatcher1 = expectLinkPattern1.matcher(charBuffer);
   223         String result;
   224         if ( expectLinkMatcher1.find() && actualLinkMatcher1.find() ) {
   225             String expectRef = expectLinkMatcher1.group(2);
   226             String actualRef = actualLinkMatcher1.group(2);
   227             if ( actualRef.equals(expectRef) ) {
   228                 result = "succeeded";
   229                 numOfSubtestsPassed += 1;
   230                 // System.out.println("pattern:   " + actualLinkPattern1.pattern());
   231                 // System.out.println("actualRef: " + actualRef);
   232                 // System.out.println("group0:    " + actualLinkMatcher1.group());
   233                 // System.out.println("group1:    " + actualLinkMatcher1.group(1));
   234                 // System.out.println("group2:    " + actualLinkMatcher1.group(2));
   235                 // System.out.println("group3:    " + actualLinkMatcher1.group(3));
   236                 // System.exit(0);
   237             } else {
   238                 result = "FAILED";
   239             }
   240             System.out.println("\nSub-test " + (subtestNum)
   241                 + " for bug " + BUGID + " (" + BUGNAME + ") " + result + "\n"
   242                 + "Actual: \"" + actualRef + "\"" + "\n"
   243                 + "Expect: \"" + expectRef + "\"");
   244         } else {
   245             System.out.println("Didn't find <A HREF> that fits the pattern: "
   246                   + expectLinkPattern1.pattern() );
   247         }
   248     }
   249 }

mercurial