Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/com/sun/javadoc/AccessSkipNav/AccessSkipNav.java
48527 views
/*1* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4638136 7198273 802563326* @summary Add ability to skip over nav bar for accessibility27* @author dkramer28* @run main AccessSkipNav29*/303132import com.sun.javadoc.*;33import java.util.*;34import java.io.*;353637/**38* Runs javadoc and runs regression tests on the resulting HTML.39* It reads each file, complete with newlines, into a string to easily40* find strings that contain newlines.41*/42public class AccessSkipNav {4344private static final String BUGID = "4638136 - 7198273";45private static final String BUGNAME = "AccessSkipNav";46private static final String FS = System.getProperty("file.separator");47private static final String PS = System.getProperty("path.separator");48private static final String LS = System.getProperty("line.separator");49private static final String TMPDEST_DIR1 = "." + FS + "docs1" + FS;50private static final String TMPDEST_DIR2 = "." + FS + "docs2" + FS;5152// Subtest number. Needed because runResultsOnHTML is run twice,53// and subtestNum should increment across subtest runs.54public static int subtestNum = 0;55public static int numSubtestsPassed = 0;5657// Entry point58public static void main(String[] args) {5960// Directory that contains source files that javadoc runs on61String srcdir = System.getProperty("test.src", ".");6263// Test for all cases except the split index page64runJavadoc(new String[] {"-d", TMPDEST_DIR1,65"-sourcepath", srcdir,66"p1", "p2"});67runTestsOnHTML(testArray);6869printSummary();70}7172/** Run javadoc */73public static void runJavadoc(String[] javadocArgs) {74if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {75throw new Error("Javadoc failed to execute");76}77}7879/**80* Assign value for [ stringToFind, filename ]81* NOTE: The standard doclet uses the same separator "\n" for all OS's82*/83private static final String[][] testArray = {8485// Testing only for the presence of the <a href> and <a name>8687// Top navbar <a href>88{ "<a href=\"#skip.navbar.top\" title=\"Skip navigation links\">Skip navigation links</a>",89TMPDEST_DIR1 + "p1" + FS + "C1.html" },9091// Top navbar <a name>92{ "<a name=\"skip.navbar.top\">" + LS +93"<!-- -->" + LS + "</a>",94TMPDEST_DIR1 + "p1" + FS + "C1.html" },9596// Bottom navbar <a href>97{ "<a href=\"#skip.navbar.bottom\" title=\"Skip navigation links\">Skip navigation links</a>",98TMPDEST_DIR1 + "p1" + FS + "C1.html" },99100// Bottom navbar <a name>101{ "<a name=\"skip.navbar.bottom\">" + LS +102"<!-- -->" + LS + "</a>",103TMPDEST_DIR1 + "p1" + FS + "C1.html" }104};105106public static void runTestsOnHTML(String[][] testArray) {107108for (int i = 0; i < testArray.length; i++) {109110subtestNum += 1;111112// Read contents of file into a string113String fileString = readFileToString(testArray[i][1]);114115// Get string to find116String stringToFind = testArray[i][0];117118// Find string in file's contents119if (findString(fileString, stringToFind) == -1) {120System.out.println("\nSub-test " + (subtestNum)121+ " for bug " + BUGID + " (" + BUGNAME + ") FAILED\n"122+ "when searching for:\n"123+ stringToFind);124} else {125numSubtestsPassed += 1;126System.out.println("\nSub-test " + (subtestNum) + " passed:\n" + stringToFind);127}128}129}130131public static void printSummary() {132if ( numSubtestsPassed == subtestNum ) {133System.out.println("\nAll " + numSubtestsPassed + " subtests passed");134} else {135throw new Error("\n" + (subtestNum - numSubtestsPassed) + " of " + (subtestNum)136+ " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");137}138}139140// Read the file into a String141public static String readFileToString(String filename) {142try {143File file = new File(filename);144if ( !file.exists() ) {145System.out.println("\nFILE DOES NOT EXIST: " + filename);146}147BufferedReader in = new BufferedReader(new FileReader(file));148149// Create an array of characters the size of the file150char[] allChars = new char[(int)file.length()];151152// Read the characters into the allChars array153in.read(allChars, 0, (int)file.length());154in.close();155156// Convert to a string157String allCharsString = new String(allChars);158159return allCharsString;160161} catch (FileNotFoundException e) {162System.err.println(e);163return "";164} catch (IOException e) {165System.err.println(e);166return "";167}168}169170public static int findString(String fileString, String stringToFind) {171return fileString.indexOf(stringToFind);172}173}174175176