AccessAsciiArt.java revision 2365:6207608205b8
1/*
2 * Copyright (c) 2002, 2014, 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 4706779 4956908
27 * @summary  Add text equivalent of class tree ASCII art for accessibility
28 * @author dkramer
29 * @run main AccessAsciiArt
30 */
31
32
33import com.sun.javadoc.*;
34import java.util.*;
35import java.io.*;
36
37
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 */
43public class AccessAsciiArt {
44
45    private static final String BUGID = "4706779-4956908";
46    private static final String BUGNAME = "AccessAsciiArt";
47    private static final String TMPDEST_DIR1 = "./docs1/";
48    private static final String TMPDEST_DIR2 = "./docs2/";
49
50    // Subtest number.  Needed because runResultsOnHTML is run twice,
51    // and subtestNum should increment across subtest runs.
52    public static int subtestNum = 0;
53    public static int numSubtestsPassed = 0;
54
55    // Entry point
56    public static void main(String[] args) {
57
58        // Directory that contains source files that javadoc runs on
59        String srcdir = System.getProperty("test.src", ".");
60
61        // Test for all cases except the split index page
62        runJavadoc(new String[] {"-d", TMPDEST_DIR1,
63                                 "-sourcepath", srcdir,
64                                 "p1", "p1.subpkg"});
65        runTestsOnHTML(testArray);
66
67        printSummary();
68    }
69
70    /** Run javadoc */
71    public static void runJavadoc(String[] javadocArgs) {
72        if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {
73            throw new Error("Javadoc failed to execute");
74        }
75    }
76
77    /**
78     * Assign value for [ stringToFind, filename ]
79     * NOTE: The standard doclet uses the same separator "\n" for all OS's
80     */
81    private static final String[][] testArray = {
82
83            // Test the top line of the class tree
84            {
85"<li><a href=\"../../p1/C.html\" title=\"class in p1\">p1.C</a></li>",
86                     TMPDEST_DIR1 + "p1/subpkg/SSC.html" },
87
88            // Test the second line of the class tree
89            {
90"<li><a href=\"../../p1/SC.html\" title=\"class in p1\">p1.SC</a></li>",
91                     TMPDEST_DIR1 + "p1/subpkg/SSC.html" },
92
93            // Test the third line of the class tree
94            {
95"<li>p1.subpkg.SSC</li>",
96                     TMPDEST_DIR1 + "p1/subpkg/SSC.html" },
97
98        };
99
100    public static void runTestsOnHTML(String[][] testArray) {
101
102        for (int i = 0; i < testArray.length; i++) {
103
104            subtestNum += 1;
105
106            // Read contents of file into a string
107            String fileString = readFileToString(testArray[i][1]);
108
109            // Get string to find
110            String stringToFind = testArray[i][0];
111
112            // Find string in file's contents
113            if (findString(fileString, stringToFind) == -1) {
114                System.out.println("\nSub-test " + (subtestNum)
115                    + " for bug " + BUGID + " (" + BUGNAME + ") FAILED\n"
116                    + "when searching for:\n"
117                    + stringToFind);
118            } else {
119                numSubtestsPassed += 1;
120                System.out.println("\nSub-test " + (subtestNum) + " passed:\n" + stringToFind);
121            }
122        }
123    }
124
125    public static void printSummary() {
126        if ( numSubtestsPassed == subtestNum ) {
127            System.out.println("\nAll " + numSubtestsPassed + " subtests passed");
128        } else {
129            throw new Error("\n" + (subtestNum - numSubtestsPassed) + " of " + (subtestNum)
130                             + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");
131        }
132    }
133
134    // Read the file into a String
135    public static String readFileToString(String filename) {
136        try {
137            File file = new File(filename);
138            if ( !file.exists() ) {
139                System.out.println("\nFILE DOES NOT EXIST: " + filename);
140            }
141            BufferedReader in = new BufferedReader(new FileReader(file));
142
143            // Create an array of characters the size of the file
144            char[] allChars = new char[(int)file.length()];
145
146            // Read the characters into the allChars array
147            in.read(allChars, 0, (int)file.length());
148            in.close();
149
150            // Convert to a string
151            String allCharsString = new String(allChars);
152
153            return allCharsString;
154
155        } catch (FileNotFoundException e) {
156            System.err.println(e);
157            return "";
158        } catch (IOException e) {
159            System.err.println(e);
160            return "";
161        }
162    }
163
164    public static int findString(String fileString, String stringToFind) {
165        return fileString.indexOf(stringToFind);
166    }
167}
168