1 /*
   2  * Copyright (c) 2002, 2016, 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  */
  23 
  24 /*
  25  * @test
  26  * @bug 4524350 4662945 4633447
  27  * @summary stddoclet: {@docRoot} inserts an extra trailing "/"
  28  * @author dkramer
  29  * @library ../lib
  30  * @modules jdk.javadoc
  31  * @build JavadocTester
  32  * @run main DocRootSlash
  33  */
  34 
  35 import java.util.regex.*;
  36 
  37 /**
  38  * Runs javadoc and runs regression tests on the resulting HTML.
  39  * It reads each file, complete with newlines, into a string to easily
  40  * find strings that contain newlines.
  41  */
  42 public class DocRootSlash extends JavadocTester {
  43 
  44     public static void main(String... args) throws Exception {
  45         DocRootSlash tester = new DocRootSlash();
  46         tester.runTests();
  47     }
  48 
  49     @Test
  50     void test() {
  51         // Directory that contains source files that javadoc runs on
  52         String srcdir = System.getProperty("test.src", ".");
  53 
  54         javadoc("-d", "out",
  55                 "-Xdoclint:none",
  56                 "-overview", (srcdir + "/overview.html"),
  57                 "-header", "<A HREF=\"{@docroot}/package-list\">{&#064;docroot}</A> <A HREF=\"{@docRoot}/help-doc\">{&#064;docRoot}</A>",
  58                 "-sourcepath", srcdir,
  59                 "p1", "p2");
  60 
  61         checkFiles(
  62                 "p1/C1.html",
  63                 "p1/package-summary.html",
  64                 "overview-summary.html");
  65 
  66         // Bug 4633447: Special test for overview-frame.html
  67         // Find two strings in file "overview-frame.html"
  68         checkOutput("overview-frame.html", true,
  69                 "<A HREF=\"./package-list\">",
  70                 "<A HREF=\"./help-doc\">");
  71     }
  72 
  73     void checkFiles(String... filenameArray) {
  74         int count = 0;
  75 
  76         for (String f : filenameArray) {
  77             // Read contents of file into a string
  78             String fileString = readFile(f);
  79             System.out.println("\nSub-tests for file: " + f + " --------------");
  80             // Loop over all tests in a single file
  81             for ( int j = 0; j < 11; j++ ) {
  82 
  83                 // Compare actual to expected string for a single subtest
  84                 compareActualToExpected(++count, fileString);
  85             }
  86         }
  87     }
  88 
  89     /**
  90      * Regular expression pattern matching code
  91      *
  92      * Prefix Pattern:
  93      * flag   (?i)            (case insensitive, so "a href" == "A HREF" and all combinations)
  94      * group1 (
  95      *          <a or <A
  96      *          \\s+          (one or more whitespace characters)
  97      *          href or HREF
  98      *          \"            (double quote)
  99      *        )
 100      * group2 ([^\"]*)        (link reference -- characters that don't include a quote)
 101      * group3 (\".*?>)        (" target="frameName">)
 102      * group4 (.*?)           (label - zero or more characters)
 103      * group5 (</a>)          (end tag)
 104      */
 105     private static final String prefix = "(?i)(<a\\s+href=";    // <a href=     (start group1)
 106     private static final String ref1   = "\")([^\"]*)(\".*?>)"; // doublequotes (end group1, group2, group3)
 107     private static final String ref2   = ")(\\S+?)([^<>]*>)";   // no quotes    (end group1, group2, group3)
 108     private static final String label  = "(.*?)";               // text label   (group4)
 109     private static final String end    = "(</a>)";              // </a>         (group5)
 110 
 111     /**
 112      * Compares the actual string to the expected string in the specified string
 113      * @param str   String to search through
 114      */
 115     void compareActualToExpected(int count, String str) {
 116         checking("comparison for " + str);
 117 
 118         // Pattern must be compiled each run because numTestsRun is incremented
 119         Pattern actualLinkPattern1 =
 120             Pattern.compile("Sub-test " + count + " Actual: " + prefix + ref1, Pattern.DOTALL);
 121         Pattern expectLinkPattern1 =
 122             Pattern.compile("Sub-test " + count + " Expect: " + prefix + ref1, Pattern.DOTALL);
 123         // Pattern linkPattern2 = Pattern.compile(prefix + ref2 + label + end, Pattern.DOTALL);
 124 
 125         Matcher actualLinkMatcher1 = actualLinkPattern1.matcher(str);
 126         Matcher expectLinkMatcher1 = expectLinkPattern1.matcher(str);
 127         if (expectLinkMatcher1.find() && actualLinkMatcher1.find()) {
 128             String expectRef = expectLinkMatcher1.group(2);
 129             String actualRef = actualLinkMatcher1.group(2);
 130             if (actualRef.equals(expectRef)) {
 131                 passed(expectRef);
 132                 // System.out.println("pattern:   " + actualLinkPattern1.pattern());
 133                 // System.out.println("actualRef: " + actualRef);
 134                 // System.out.println("group0:    " + actualLinkMatcher1.group());
 135                 // System.out.println("group1:    " + actualLinkMatcher1.group(1));
 136                 // System.out.println("group2:    " + actualLinkMatcher1.group(2));
 137                 // System.out.println("group3:    " + actualLinkMatcher1.group(3));
 138                 // System.exit(0);
 139             } else {
 140                 failed("\n"
 141                         + "Actual: \"" + actualRef + "\"\n"
 142                         + "Expect: \"" + expectRef + "\"");
 143             }
 144         } else {
 145             failed("Didn't find <A HREF> that fits the pattern: "
 146                     + expectLinkPattern1.pattern());
 147         }
 148     }
 149 }