Sat, 01 Dec 2007 00:00:00 +0000
Initial load
1 /*
2 * Copyright 2002 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
24 /*
25 * @test
26 * @bug 4638136
27 * @summary Add ability to skip over nav bar for accessibility
28 * @author dkramer
29 * @run main AccessSkipNav
30 */
33 import com.sun.javadoc.*;
34 import java.util.*;
35 import java.io.*;
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 AccessSkipNav {
45 private static final String BUGID = "4638136";
46 private static final String BUGNAME = "AccessSkipNav";
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 TMPDEST_DIR1 = "." + FS + "docs1" + FS;
50 private static final String TMPDEST_DIR2 = "." + FS + "docs2" + FS;
52 // Subtest number. Needed because runResultsOnHTML is run twice,
53 // and subtestNum should increment across subtest runs.
54 public static int subtestNum = 0;
55 public static int numSubtestsPassed = 0;
57 // Entry point
58 public static void main(String[] args) {
60 // Directory that contains source files that javadoc runs on
61 String srcdir = System.getProperty("test.src", ".");
63 // Test for all cases except the split index page
64 runJavadoc(new String[] {"-d", TMPDEST_DIR1,
65 "-sourcepath", srcdir,
66 "p1", "p2"});
67 runTestsOnHTML(testArray);
69 printSummary();
70 }
72 /** Run javadoc */
73 public static void runJavadoc(String[] javadocArgs) {
74 if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {
75 throw new Error("Javadoc failed to execute");
76 }
77 }
79 /**
80 * Assign value for [ stringToFind, filename ]
81 * NOTE: The standard doclet uses the same separator "\n" for all OS's
82 */
83 private static final String[][] testArray = {
85 // Testing only for the presence of the <a href> and <a name>
87 // Top navbar <A HREF>
88 { "<A HREF=\"#skip-navbar_top\" title=\"Skip navigation links\"></A>",
89 TMPDEST_DIR1 + "p1" + FS + "C1.html" },
91 // Top navbar <A NAME>
92 { "<A NAME=\"skip-navbar_top\"></A>",
93 TMPDEST_DIR1 + "p1" + FS + "C1.html" },
95 // Bottom navbar <A HREF>
96 { "<A HREF=\"#skip-navbar_bottom\" title=\"Skip navigation links\"></A>",
97 TMPDEST_DIR1 + "p1" + FS + "C1.html" },
99 // Bottom navbar <A NAME>
100 { "<A NAME=\"skip-navbar_bottom\"></A>",
101 TMPDEST_DIR1 + "p1" + FS + "C1.html" }
102 };
104 public static void runTestsOnHTML(String[][] testArray) {
106 for (int i = 0; i < testArray.length; i++) {
108 subtestNum += 1;
110 // Read contents of file into a string
111 String fileString = readFileToString(testArray[i][1]);
113 // Get string to find
114 String stringToFind = testArray[i][0];
116 // Find string in file's contents
117 if (findString(fileString, stringToFind) == -1) {
118 System.out.println("\nSub-test " + (subtestNum)
119 + " for bug " + BUGID + " (" + BUGNAME + ") FAILED\n"
120 + "when searching for:\n"
121 + stringToFind);
122 } else {
123 numSubtestsPassed += 1;
124 System.out.println("\nSub-test " + (subtestNum) + " passed:\n" + stringToFind);
125 }
126 }
127 }
129 public static void printSummary() {
130 if ( numSubtestsPassed == subtestNum ) {
131 System.out.println("\nAll " + numSubtestsPassed + " subtests passed");
132 } else {
133 throw new Error("\n" + (subtestNum - numSubtestsPassed) + " of " + (subtestNum)
134 + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");
135 }
136 }
138 // Read the file into a String
139 public static String readFileToString(String filename) {
140 try {
141 File file = new File(filename);
142 if ( !file.exists() ) {
143 System.out.println("\nFILE DOES NOT EXIST: " + filename);
144 }
145 BufferedReader in = new BufferedReader(new FileReader(file));
147 // Create an array of characters the size of the file
148 char[] allChars = new char[(int)file.length()];
150 // Read the characters into the allChars array
151 in.read(allChars, 0, (int)file.length());
152 in.close();
154 // Convert to a string
155 String allCharsString = new String(allChars);
157 return allCharsString;
159 } catch (FileNotFoundException e) {
160 System.err.println(e);
161 return "";
162 } catch (IOException e) {
163 System.err.println(e);
164 return "";
165 }
166 }
168 public static int findString(String fileString, String stringToFind) {
169 return fileString.indexOf(stringToFind);
170 }
171 }