langtools/test/com/sun/javadoc/AccessSkipNav/AccessSkipNav.java
author jjg
Wed, 16 Apr 2014 16:17:09 -0700
changeset 23971 f5ff1f5a8dee
parent 21008 af0b915df7cc
child 24399 af1a0220d0fa
permissions -rw-r--r--
8031649: Clean up javadoc tests Reviewed-by: jjg Contributed-by: neil.toda@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
23971
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
     2
 * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
21008
af0b915df7cc 8025633: Fix javadoc to generate valid anchor names
bpatel
parents: 19909
diff changeset
    26
 * @bug 4638136 7198273 8025633
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary  Add ability to skip over nav bar for accessibility
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 * @author dkramer
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
 * @run main AccessSkipNav
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * Runs javadoc and runs regression tests on the resulting HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * It reads each file, complete with newlines, into a string to easily
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * find strings that contain newlines.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
public class AccessSkipNav {
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
23971
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
    45
    protected static final String NL = System.getProperty("line.separator");
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
    46
19909
784c68032a36 7198273: RFE : Javadoc Accessibility : Hyperlinks should contain text or an image with alt text
bpatel
parents: 7681
diff changeset
    47
    private static final String BUGID = "4638136 - 7198273";
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    private static final String BUGNAME = "AccessSkipNav";
23971
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
    49
    private static final String TMPDEST_DIR1 = "./docs1/";
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
    50
    private static final String TMPDEST_DIR2 = "./docs2/";
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    // Subtest number.  Needed because runResultsOnHTML is run twice,
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    // and subtestNum should increment across subtest runs.
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    public static int subtestNum = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    public static int numSubtestsPassed = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    // Entry point
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    public static void main(String[] args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        // Directory that contains source files that javadoc runs on
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        String srcdir = System.getProperty("test.src", ".");
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        // Test for all cases except the split index page
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        runJavadoc(new String[] {"-d", TMPDEST_DIR1,
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
                                 "-sourcepath", srcdir,
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
                                 "p1", "p2"});
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        runTestsOnHTML(testArray);
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        printSummary();
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    /** Run javadoc */
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    public static void runJavadoc(String[] javadocArgs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
            throw new Error("Javadoc failed to execute");
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     * Assign value for [ stringToFind, filename ]
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * NOTE: The standard doclet uses the same separator "\n" for all OS's
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    private static final String[][] testArray = {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
            // Testing only for the presence of the <a href> and <a name>
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    87
            // Top navbar <a href>
21008
af0b915df7cc 8025633: Fix javadoc to generate valid anchor names
bpatel
parents: 19909
diff changeset
    88
            { "<a href=\"#skip.navbar.top\" title=\"Skip navigation links\">Skip navigation links</a>",
23971
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
    89
                     TMPDEST_DIR1 + "p1/C1.html" },
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    91
            // Top navbar <a name>
23971
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
    92
            { "<a name=\"skip.navbar.top\">\n" +
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
    93
                      "<!--   -->\n" +
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
    94
                      "</a>",
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
    95
                     TMPDEST_DIR1 + "p1/C1.html" },
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    97
            // Bottom navbar <a href>
21008
af0b915df7cc 8025633: Fix javadoc to generate valid anchor names
bpatel
parents: 19909
diff changeset
    98
            { "<a href=\"#skip.navbar.bottom\" title=\"Skip navigation links\">Skip navigation links</a>",
23971
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
    99
                     TMPDEST_DIR1 + "p1/C1.html" },
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   101
            // Bottom navbar <a name>
23971
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
   102
            { "<a name=\"skip.navbar.bottom\">\n" +
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
   103
                      "<!--   -->\n" +
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
   104
                      "</a>",
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
   105
                     TMPDEST_DIR1 + "p1/C1.html" }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    public static void runTestsOnHTML(String[][] testArray) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        for (int i = 0; i < testArray.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            subtestNum += 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            // Read contents of file into a string
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            String fileString = readFileToString(testArray[i][1]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
            // Get string to find
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            String stringToFind = testArray[i][0];
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            // Find string in file's contents
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            if (findString(fileString, stringToFind) == -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                System.out.println("\nSub-test " + (subtestNum)
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                    + " for bug " + BUGID + " (" + BUGNAME + ") FAILED\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                    + "when searching for:\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                    + stringToFind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                numSubtestsPassed += 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                System.out.println("\nSub-test " + (subtestNum) + " passed:\n" + stringToFind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    public static void printSummary() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        if ( numSubtestsPassed == subtestNum ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            System.out.println("\nAll " + numSubtestsPassed + " subtests passed");
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            throw new Error("\n" + (subtestNum - numSubtestsPassed) + " of " + (subtestNum)
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                             + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    // Read the file into a String
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    public static String readFileToString(String filename) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            File file = new File(filename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            if ( !file.exists() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                System.out.println("\nFILE DOES NOT EXIST: " + filename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            BufferedReader in = new BufferedReader(new FileReader(file));
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            // Create an array of characters the size of the file
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            char[] allChars = new char[(int)file.length()];
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            // Read the characters into the allChars array
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            in.read(allChars, 0, (int)file.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            in.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
            // Convert to a string
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            String allCharsString = new String(allChars);
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            return allCharsString;
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        } catch (FileNotFoundException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            System.err.println(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            return "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            System.err.println(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            return "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
    public static int findString(String fileString, String stringToFind) {
23971
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 21008
diff changeset
   173
        return fileString.replace(NL, "\n").indexOf(stringToFind);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
}