Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/com/sun/javadoc/JavascriptWinTitle/JavascriptWinTitle.java
48068 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 4645058 4747738 4855054 802475626* @summary Javascript IE load error when linked by -linkoffline27* Window title shouldn't change when loading left frames (javascript)28* @author dkramer29* @run main JavascriptWinTitle30*/313233import com.sun.javadoc.*;34import java.util.*;35import java.io.*;363738/**39* Runs javadoc and runs regression tests on the resulting HTML.40* It reads each file, complete with newlines, into a string to easily41* find strings that contain newlines.42*/43public class JavascriptWinTitle {4445private static final String BUGID = "4645058";46private static final String BUGNAME = "JavascriptWinTitle";47private static final String FS = System.getProperty("file.separator");48private static final String PS = System.getProperty("path.separator");49private static final String LS = System.getProperty("line.separator");50private static final String TMPDEST_DIR1 = "." + FS + "docs1" + FS;51private static final String TMPDEST_DIR2 = "." + FS + "docs2" + FS;5253// Subtest number. Needed because runResultsOnHTML is run twice,54// and subtestNum should increment across subtest runs.55public static int subtestNum = 0;56public static int numSubtestsPassed = 0;5758// Entry point59public static void main(String[] args) {6061// Directory that contains source files that javadoc runs on62String srcdir = System.getProperty("test.src", ".");6364// Test for all cases except the split index page65runJavadoc(new String[] {"-d", TMPDEST_DIR1,66"-doctitle", "Document Title",67"-windowtitle", "Window Title",68"-overview", (srcdir + FS + "overview.html"),69"-linkoffline",70"http://java.sun.com/j2se/1.4/docs/api", srcdir,71"-sourcepath", srcdir,72"p1", "p2"});73runTestsOnHTML(testArray);7475printSummary();76}7778/** Run javadoc */79public static void runJavadoc(String[] javadocArgs) {80if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {81throw new Error("Javadoc failed to execute");82}83}8485/**86* Assign value for [ stringToFind, filename ]87* NOTE: The standard doclet uses the same separator "\n" for all OS's88*/89private static final String[][] testArray = {9091// Test the javascript "type" attribute is present:92{ "<script type=\"text/javascript\">",93TMPDEST_DIR1 + "overview-summary.html" },9495// Test onload is absent:96{ "<body>",97TMPDEST_DIR1 + "overview-summary.html" },9899// Test onload is present:100{ "<body>",101TMPDEST_DIR1 + FS + "p1" + FS + "package-summary.html" },102103// Test that "onload" is not present in BODY tag:104{ "<body>",105TMPDEST_DIR1 + "overview-frame.html" },106107// Test that "onload" is not present in BODY tag:108{ "<body>",109TMPDEST_DIR1 + "allclasses-frame.html" },110111// Test that "onload" is not present in BODY tag:112{ "<body>",113TMPDEST_DIR1 + FS + "p1" + FS + "package-frame.html" },114115// Test that win title javascript is followed by NOSCRIPT code.116{"<script type=\"text/javascript\"><!--" + LS +117" try {" + LS +118" if (location.href.indexOf('is-external=true') == -1) {" + LS +119" parent.document.title=\"C (Window Title)\";" + LS +120" }" + LS +121" }" + LS +122" catch(err) {" + LS +123" }" + LS + "//-->" + LS + "</script>",124TMPDEST_DIR1 + FS + "p1" + FS + "C.html"125}126127};128129public static void runTestsOnHTML(String[][] testArray) {130131for (int i = 0; i < testArray.length; i++) {132133subtestNum += 1;134135// Read contents of file into a string136String fileString = readFileToString(testArray[i][1]);137138// Get string to find139String stringToFind = testArray[i][0];140141// Find string in file's contents142if (findString(fileString, stringToFind) == -1) {143System.out.println("\nSub-test " + (subtestNum)144+ " for bug " + BUGID + " (" + BUGNAME + ") FAILED\n"145+ "when searching for:\n"146+ stringToFind);147} else {148numSubtestsPassed += 1;149System.out.println("\nSub-test " + (subtestNum) + " passed:\n" + stringToFind);150}151}152}153154public static void printSummary() {155if ( numSubtestsPassed == subtestNum ) {156System.out.println("\nAll " + numSubtestsPassed + " subtests passed");157} else {158throw new Error("\n" + (subtestNum - numSubtestsPassed) + " of " + (subtestNum)159+ " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");160}161}162163// Read the file into a String164public static String readFileToString(String filename) {165try {166File file = new File(filename);167if ( !file.exists() ) {168System.out.println("\nFILE DOES NOT EXIST: " + filename);169}170BufferedReader in = new BufferedReader(new FileReader(file));171172// Create an array of characters the size of the file173char[] allChars = new char[(int)file.length()];174175// Read the characters into the allChars array176in.read(allChars, 0, (int)file.length());177in.close();178179// Convert to a string180String allCharsString = new String(allChars);181182return allCharsString;183184} catch (FileNotFoundException e) {185System.err.println(e);186return "";187} catch (IOException e) {188System.err.println(e);189return "";190}191}192193public static int findString(String fileString, String stringToFind) {194return fileString.indexOf(stringToFind);195}196}197198199