AccessSkipNav.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 4638136 7198273 8025633 27 * @summary Add ability to skip over nav bar for accessibility 28 * @author dkramer 29 * @run main AccessSkipNav 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 AccessSkipNav { 44 45 protected static final String NL = System.getProperty("line.separator"); 46 47 private static final String BUGID = "4638136 - 7198273"; 48 private static final String BUGNAME = "AccessSkipNav"; 49 private static final String TMPDEST_DIR1 = "./docs1/"; 50 private static final String TMPDEST_DIR2 = "./docs2/"; 51 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; 56 57 // Entry point 58 public static void main(String[] args) { 59 60 // Directory that contains source files that javadoc runs on 61 String srcdir = System.getProperty("test.src", "."); 62 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); 68 69 printSummary(); 70 } 71 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 } 78 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 = { 84 85 // Testing only for the presence of the <a href> and <a name> 86 87 // Top navbar <a href> 88 { "<a href=\"#skip.navbar.top\" title=\"Skip navigation links\">Skip navigation links</a>", 89 TMPDEST_DIR1 + "p1/C1.html" }, 90 91 // Top navbar <a name> 92 { "<a name=\"skip.navbar.top\">\n" + 93 "<!-- -->\n" + 94 "</a>", 95 TMPDEST_DIR1 + "p1/C1.html" }, 96 97 // Bottom navbar <a href> 98 { "<a href=\"#skip.navbar.bottom\" title=\"Skip navigation links\">Skip navigation links</a>", 99 TMPDEST_DIR1 + "p1/C1.html" }, 100 101 // Bottom navbar <a name> 102 { "<a name=\"skip.navbar.bottom\">\n" + 103 "<!-- -->\n" + 104 "</a>", 105 TMPDEST_DIR1 + "p1/C1.html" } 106 }; 107 108 public static void runTestsOnHTML(String[][] testArray) { 109 110 for (int i = 0; i < testArray.length; i++) { 111 112 subtestNum += 1; 113 114 // Read contents of file into a string 115 String fileString = readFileToString(testArray[i][1]); 116 117 // Get string to find 118 String stringToFind = testArray[i][0]; 119 120 // Find string in file's contents 121 if (findString(fileString, stringToFind) == -1) { 122 System.out.println("\nSub-test " + (subtestNum) 123 + " for bug " + BUGID + " (" + BUGNAME + ") FAILED\n" 124 + "when searching for:\n" 125 + stringToFind); 126 } else { 127 numSubtestsPassed += 1; 128 System.out.println("\nSub-test " + (subtestNum) + " passed:\n" + stringToFind); 129 } 130 } 131 } 132 133 public static void printSummary() { 134 if ( numSubtestsPassed == subtestNum ) { 135 System.out.println("\nAll " + numSubtestsPassed + " subtests passed"); 136 } else { 137 throw new Error("\n" + (subtestNum - numSubtestsPassed) + " of " + (subtestNum) 138 + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n"); 139 } 140 } 141 142 // Read the file into a String 143 public static String readFileToString(String filename) { 144 try { 145 File file = new File(filename); 146 if ( !file.exists() ) { 147 System.out.println("\nFILE DOES NOT EXIST: " + filename); 148 } 149 BufferedReader in = new BufferedReader(new FileReader(file)); 150 151 // Create an array of characters the size of the file 152 char[] allChars = new char[(int)file.length()]; 153 154 // Read the characters into the allChars array 155 in.read(allChars, 0, (int)file.length()); 156 in.close(); 157 158 // Convert to a string 159 String allCharsString = new String(allChars); 160 161 return allCharsString; 162 163 } catch (FileNotFoundException e) { 164 System.err.println(e); 165 return ""; 166 } catch (IOException e) { 167 System.err.println(e); 168 return ""; 169 } 170 } 171 172 public static int findString(String fileString, String stringToFind) { 173 return fileString.replace(NL, "\n").indexOf(stringToFind); 174 } 175} 176