DocRootSlash.java revision 3233:b5d08bc0d224
127074Ssteve/*
227074Ssteve * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
31592Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41592Srgrimes *
51592Srgrimes * This code is free software; you can redistribute it and/or modify it
61592Srgrimes * under the terms of the GNU General Public License version 2 only, as
71592Srgrimes * published by the Free Software Foundation.
81592Srgrimes *
91592Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101592Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111592Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121592Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131592Srgrimes * accompanied this code).
141592Srgrimes *
151592Srgrimes * You should have received a copy of the GNU General Public License version
161592Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171592Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181592Srgrimes *
191592Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
201592Srgrimes * or visit www.oracle.com if you need additional information or have any
211592Srgrimes * questions.
221592Srgrimes */
231592Srgrimes
241592Srgrimes/*
251592Srgrimes * @test
261592Srgrimes * @bug 4524350 4662945 4633447
271592Srgrimes * @summary stddoclet: {@docRoot} inserts an extra trailing "/"
281592Srgrimes * @author dkramer
291592Srgrimes * @library ../lib
301592Srgrimes * @modules jdk.javadoc
311592Srgrimes * @build JavadocTester
321592Srgrimes * @run main DocRootSlash
331592Srgrimes */
341592Srgrimes
351592Srgrimesimport java.util.regex.*;
361592Srgrimes
371592Srgrimes/**
381592Srgrimes * Runs javadoc and runs regression tests on the resulting HTML.
391592Srgrimes * It reads each file, complete with newlines, into a string to easily
401592Srgrimes * find strings that contain newlines.
411592Srgrimes */
421592Srgrimespublic class DocRootSlash extends JavadocTester {
4327074Ssteve
441592Srgrimes    public static void main(String... args) throws Exception {
4527074Ssteve        DocRootSlash tester = new DocRootSlash();
461592Srgrimes        tester.runTests();
471592Srgrimes    }
481592Srgrimes
491592Srgrimes    @Test
5027074Ssteve    void test() {
5127074Ssteve        // Directory that contains source files that javadoc runs on
521592Srgrimes        String srcdir = System.getProperty("test.src", ".");
531592Srgrimes
541592Srgrimes        javadoc("-d", "out",
551592Srgrimes                "-Xdoclint:none",
561592Srgrimes                "-overview", (srcdir + "/overview.html"),
571592Srgrimes                "-header", "<A HREF=\"{@docroot}/package-list\">{&#064;docroot}</A> <A HREF=\"{@docRoot}/help-doc\">{&#064;docRoot}</A>",
581592Srgrimes                "-sourcepath", srcdir,
591592Srgrimes                "p1", "p2");
601592Srgrimes
611592Srgrimes        checkFiles(
621592Srgrimes                "p1/C1.html",
631592Srgrimes                "p1/package-summary.html",
641592Srgrimes                "overview-summary.html");
651592Srgrimes
661592Srgrimes        // Bug 4633447: Special test for overview-frame.html
671592Srgrimes        // Find two strings in file "overview-frame.html"
681592Srgrimes        checkOutput("overview-frame.html", true,
691592Srgrimes                "<A HREF=\"./package-list\">",
701592Srgrimes                "<A HREF=\"./help-doc\">");
711592Srgrimes    }
721592Srgrimes
731592Srgrimes    void checkFiles(String... filenameArray) {
741592Srgrimes        int count = 0;
751592Srgrimes
761592Srgrimes        for (String f : filenameArray) {
771592Srgrimes            // Read contents of file into a string
781592Srgrimes            String fileString = readFile(f);
791592Srgrimes            System.out.println("\nSub-tests for file: " + f + " --------------");
801592Srgrimes            // Loop over all tests in a single file
811592Srgrimes            for ( int j = 0; j < 11; j++ ) {
821592Srgrimes
831592Srgrimes                // Compare actual to expected string for a single subtest
841592Srgrimes                compareActualToExpected(++count, fileString);
851592Srgrimes            }
861592Srgrimes        }
871592Srgrimes    }
881592Srgrimes
891592Srgrimes    /**
9027074Ssteve     * Regular expression pattern matching code
911592Srgrimes     *
921592Srgrimes     * Prefix Pattern:
931592Srgrimes     * flag   (?i)            (case insensitive, so "a href" == "A HREF" and all combinations)
941592Srgrimes     * group1 (
951592Srgrimes     *          <a or <A
961592Srgrimes     *          \\s+          (one or more whitespace characters)
971592Srgrimes     *          href or HREF
981592Srgrimes     *          \"            (double quote)
991592Srgrimes     *        )
1001592Srgrimes     * group2 ([^\"]*)        (link reference -- characters that don't include a quote)
1011592Srgrimes     * group3 (\".*?>)        (" target="frameName">)
1021592Srgrimes     * group4 (.*?)           (label - zero or more characters)
1031592Srgrimes     * group5 (</a>)          (end tag)
1041592Srgrimes     */
1051592Srgrimes    private static final String prefix = "(?i)(<a\\s+href=";    // <a href=     (start group1)
1061592Srgrimes    private static final String ref1   = "\")([^\"]*)(\".*?>)"; // doublequotes (end group1, group2, group3)
1071592Srgrimes    private static final String ref2   = ")(\\S+?)([^<>]*>)";   // no quotes    (end group1, group2, group3)
1081592Srgrimes    private static final String label  = "(.*?)";               // text label   (group4)
1091592Srgrimes    private static final String end    = "(</a>)";              // </a>         (group5)
1101592Srgrimes
1111592Srgrimes    /**
1121592Srgrimes     * Compares the actual string to the expected string in the specified string
1131592Srgrimes     * @param str   String to search through
1141592Srgrimes     */
1151592Srgrimes    void compareActualToExpected(int count, String str) {
1161592Srgrimes        checking("comparison for " + str);
1171592Srgrimes
11827074Ssteve        // Pattern must be compiled each run because numTestsRun is incremented
1191592Srgrimes        Pattern actualLinkPattern1 =
1201592Srgrimes            Pattern.compile("Sub-test " + count + " Actual: " + prefix + ref1, Pattern.DOTALL);
1211592Srgrimes        Pattern expectLinkPattern1 =
12227074Ssteve            Pattern.compile("Sub-test " + count + " Expect: " + prefix + ref1, Pattern.DOTALL);
1231592Srgrimes        // Pattern linkPattern2 = Pattern.compile(prefix + ref2 + label + end, Pattern.DOTALL);
1241592Srgrimes
1251592Srgrimes        Matcher actualLinkMatcher1 = actualLinkPattern1.matcher(str);
1261592Srgrimes        Matcher expectLinkMatcher1 = expectLinkPattern1.matcher(str);
1271592Srgrimes        if (expectLinkMatcher1.find() && actualLinkMatcher1.find()) {
1281592Srgrimes            String expectRef = expectLinkMatcher1.group(2);
1291592Srgrimes            String actualRef = actualLinkMatcher1.group(2);
1301592Srgrimes            if (actualRef.equals(expectRef)) {
1311592Srgrimes                passed(expectRef);
13227074Ssteve                // System.out.println("pattern:   " + actualLinkPattern1.pattern());
1331592Srgrimes                // System.out.println("actualRef: " + actualRef);
1341592Srgrimes                // System.out.println("group0:    " + actualLinkMatcher1.group());
1351592Srgrimes                // System.out.println("group1:    " + actualLinkMatcher1.group(1));
1361592Srgrimes                // System.out.println("group2:    " + actualLinkMatcher1.group(2));
1371592Srgrimes                // System.out.println("group3:    " + actualLinkMatcher1.group(3));
1381592Srgrimes                // System.exit(0);
1391592Srgrimes            } else {
14027074Ssteve                failed("\n"
14127074Ssteve                        + "Actual: \"" + actualRef + "\"\n"
1421592Srgrimes                        + "Expect: \"" + expectRef + "\"");
1431592Srgrimes            }
1441592Srgrimes        } else {
1451592Srgrimes            failed("Didn't find <A HREF> that fits the pattern: "
1461592Srgrimes                    + expectLinkPattern1.pattern());
1471592Srgrimes        }
1481592Srgrimes    }
1491592Srgrimes}
1501592Srgrimes