Wed, 08 Oct 2014 12:38:55 -0700
Added tag jdk8u40-b09 for changeset 8bb38a350722
1 /*
2 * Copyright (c) 2002, 2013, 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 */
24 /*
25 * @test
26 * @bug 4638136 7198273 8025633
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 - 7198273";
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 LS = System.getProperty("line.separator");
50 private static final String TMPDEST_DIR1 = "." + FS + "docs1" + FS;
51 private static final String TMPDEST_DIR2 = "." + FS + "docs2" + FS;
53 // Subtest number. Needed because runResultsOnHTML is run twice,
54 // and subtestNum should increment across subtest runs.
55 public static int subtestNum = 0;
56 public static int numSubtestsPassed = 0;
58 // Entry point
59 public static void main(String[] args) {
61 // Directory that contains source files that javadoc runs on
62 String srcdir = System.getProperty("test.src", ".");
64 // Test for all cases except the split index page
65 runJavadoc(new String[] {"-d", TMPDEST_DIR1,
66 "-sourcepath", srcdir,
67 "p1", "p2"});
68 runTestsOnHTML(testArray);
70 printSummary();
71 }
73 /** Run javadoc */
74 public static void runJavadoc(String[] javadocArgs) {
75 if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {
76 throw new Error("Javadoc failed to execute");
77 }
78 }
80 /**
81 * Assign value for [ stringToFind, filename ]
82 * NOTE: The standard doclet uses the same separator "\n" for all OS's
83 */
84 private static final String[][] testArray = {
86 // Testing only for the presence of the <a href> and <a name>
88 // Top navbar <a href>
89 { "<a href=\"#skip.navbar.top\" title=\"Skip navigation links\">Skip navigation links</a>",
90 TMPDEST_DIR1 + "p1" + FS + "C1.html" },
92 // Top navbar <a name>
93 { "<a name=\"skip.navbar.top\">" + LS +
94 "<!-- -->" + LS + "</a>",
95 TMPDEST_DIR1 + "p1" + FS + "C1.html" },
97 // Bottom navbar <a href>
98 { "<a href=\"#skip.navbar.bottom\" title=\"Skip navigation links\">Skip navigation links</a>",
99 TMPDEST_DIR1 + "p1" + FS + "C1.html" },
101 // Bottom navbar <a name>
102 { "<a name=\"skip.navbar.bottom\">" + LS +
103 "<!-- -->" + LS + "</a>",
104 TMPDEST_DIR1 + "p1" + FS + "C1.html" }
105 };
107 public static void runTestsOnHTML(String[][] testArray) {
109 for (int i = 0; i < testArray.length; i++) {
111 subtestNum += 1;
113 // Read contents of file into a string
114 String fileString = readFileToString(testArray[i][1]);
116 // Get string to find
117 String stringToFind = testArray[i][0];
119 // Find string in file's contents
120 if (findString(fileString, stringToFind) == -1) {
121 System.out.println("\nSub-test " + (subtestNum)
122 + " for bug " + BUGID + " (" + BUGNAME + ") FAILED\n"
123 + "when searching for:\n"
124 + stringToFind);
125 } else {
126 numSubtestsPassed += 1;
127 System.out.println("\nSub-test " + (subtestNum) + " passed:\n" + stringToFind);
128 }
129 }
130 }
132 public static void printSummary() {
133 if ( numSubtestsPassed == subtestNum ) {
134 System.out.println("\nAll " + numSubtestsPassed + " subtests passed");
135 } else {
136 throw new Error("\n" + (subtestNum - numSubtestsPassed) + " of " + (subtestNum)
137 + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");
138 }
139 }
141 // Read the file into a String
142 public static String readFileToString(String filename) {
143 try {
144 File file = new File(filename);
145 if ( !file.exists() ) {
146 System.out.println("\nFILE DOES NOT EXIST: " + filename);
147 }
148 BufferedReader in = new BufferedReader(new FileReader(file));
150 // Create an array of characters the size of the file
151 char[] allChars = new char[(int)file.length()];
153 // Read the characters into the allChars array
154 in.read(allChars, 0, (int)file.length());
155 in.close();
157 // Convert to a string
158 String allCharsString = new String(allChars);
160 return allCharsString;
162 } catch (FileNotFoundException e) {
163 System.err.println(e);
164 return "";
165 } catch (IOException e) {
166 System.err.println(e);
167 return "";
168 }
169 }
171 public static int findString(String fileString, String stringToFind) {
172 return fileString.indexOf(stringToFind);
173 }
174 }