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  */
  23 
  24 /*
  25  * @test
  26  * @bug 4645058 4747738 4855054 8024756
  27  * @summary  Javascript IE load error when linked by -linkoffline
  28  *           Window title shouldn't change when loading left frames (javascript)
  29  * @author dkramer
  30  * @run main JavascriptWinTitle
  31  */
  32 
  33 
  34 import com.sun.javadoc.*;
  35 import java.util.*;
  36 import java.io.*;
  37 
  38 
  39 /**
  40  * Runs javadoc and runs regression tests on the resulting HTML.
  41  * It reads each file, complete with newlines, into a string to easily
  42  * find strings that contain newlines.
  43  */
  44 public class JavascriptWinTitle {
  45 
  46     private static final String BUGID = "4645058";
  47     private static final String BUGNAME = "JavascriptWinTitle";
  48     private static final String FS = System.getProperty("file.separator");
  49     private static final String PS = System.getProperty("path.separator");
  50     private static final String LS = System.getProperty("line.separator");
  51     private static final String TMPDEST_DIR1 = "." + FS + "docs1" + FS;
  52     private static final String TMPDEST_DIR2 = "." + FS + "docs2" + FS;
  53 
  54     // Subtest number.  Needed because runResultsOnHTML is run twice,
  55     // and subtestNum should increment across subtest runs.
  56     public static int subtestNum = 0;
  57     public static int numSubtestsPassed = 0;
  58 
  59     // Entry point
  60     public static void main(String[] args) {
  61 
  62         // Directory that contains source files that javadoc runs on
  63         String srcdir = System.getProperty("test.src", ".");
  64 
  65         // Test for all cases except the split index page
  66         runJavadoc(new String[] {"-d", TMPDEST_DIR1,
  67                                  "-doctitle", "Document Title",
  68                                  "-windowtitle", "Window Title",
  69                                  "-overview", (srcdir + FS + "overview.html"),
  70                                  "-linkoffline",
  71                                     "http://java.sun.com/j2se/1.4/docs/api", srcdir,
  72                                  "-sourcepath", srcdir,
  73                                  "p1", "p2"});
  74         runTestsOnHTML(testArray);
  75 
  76         printSummary();
  77     }
  78 
  79     /** Run javadoc */
  80     public static void runJavadoc(String[] javadocArgs) {
  81         if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {
  82             throw new Error("Javadoc failed to execute");
  83         }
  84     }
  85 
  86     /**
  87      * Assign value for [ stringToFind, filename ]
  88      * NOTE: The standard doclet uses the same separator "\n" for all OS's
  89      */
  90     private static final String[][] testArray = {
  91 
  92             // Test the javascript "type" attribute is present:
  93             {  "<script type=\"text/javascript\">",
  94                      TMPDEST_DIR1 + "overview-summary.html"  },
  95 
  96             // Test onload is absent:
  97             {  "<body>",
  98                      TMPDEST_DIR1 + "overview-summary.html"  },
  99 
 100             // Test onload is present:
 101             {  "<body>",
 102                      TMPDEST_DIR1 + FS + "p1" + FS + "package-summary.html"  },
 103 
 104             // Test that "onload" is not present in BODY tag:
 105             {   "<body>",
 106                      TMPDEST_DIR1 + "overview-frame.html"  },
 107 
 108             // Test that "onload" is not present in BODY tag:
 109             {   "<body>",
 110                      TMPDEST_DIR1 + "allclasses-frame.html"  },
 111 
 112             // Test that "onload" is not present in BODY tag:
 113             {   "<body>",
 114                      TMPDEST_DIR1 + FS + "p1" + FS + "package-frame.html"  },
 115 
 116             // Test that win title javascript is followed by NOSCRIPT code.
 117             {"<script type=\"text/javascript\"><!--" + LS +
 118             "    try {" + LS +
 119             "        if (location.href.indexOf('is-external=true') == -1) {" + LS +
 120             "            parent.document.title=\"C (Window Title)\";" + LS +
 121             "        }" + LS +
 122             "    }" + LS +
 123             "    catch(err) {" + LS +
 124             "    }" + LS + "//-->" + LS + "</script>",
 125              TMPDEST_DIR1 + FS + "p1" + FS + "C.html"
 126             }
 127 
 128         };
 129 
 130     public static void runTestsOnHTML(String[][] testArray) {
 131 
 132         for (int i = 0; i < testArray.length; i++) {
 133 
 134             subtestNum += 1;
 135 
 136             // Read contents of file into a string
 137             String fileString = readFileToString(testArray[i][1]);
 138 
 139             // Get string to find
 140             String stringToFind = testArray[i][0];
 141 
 142             // Find string in file's contents
 143             if (findString(fileString, stringToFind) == -1) {
 144                 System.out.println("\nSub-test " + (subtestNum)
 145                     + " for bug " + BUGID + " (" + BUGNAME + ") FAILED\n"
 146                     + "when searching for:\n"
 147                     + stringToFind);
 148             } else {
 149                 numSubtestsPassed += 1;
 150                 System.out.println("\nSub-test " + (subtestNum) + " passed:\n" + stringToFind);
 151             }
 152         }
 153     }
 154 
 155     public static void printSummary() {
 156         if ( numSubtestsPassed == subtestNum ) {
 157             System.out.println("\nAll " + numSubtestsPassed + " subtests passed");
 158         } else {
 159             throw new Error("\n" + (subtestNum - numSubtestsPassed) + " of " + (subtestNum)
 160                              + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");
 161         }
 162     }
 163 
 164     // Read the file into a String
 165     public static String readFileToString(String filename) {
 166         try {
 167             File file = new File(filename);
 168             if ( !file.exists() ) {
 169                 System.out.println("\nFILE DOES NOT EXIST: " + filename);
 170             }
 171             BufferedReader in = new BufferedReader(new FileReader(file));
 172 
 173             // Create an array of characters the size of the file
 174             char[] allChars = new char[(int)file.length()];
 175 
 176             // Read the characters into the allChars array
 177             in.read(allChars, 0, (int)file.length());
 178             in.close();
 179 
 180             // Convert to a string
 181             String allCharsString = new String(allChars);
 182 
 183             return allCharsString;
 184 
 185         } catch (FileNotFoundException e) {
 186             System.err.println(e);
 187             return "";
 188         } catch (IOException e) {
 189             System.err.println(e);
 190             return "";
 191         }
 192     }
 193 
 194     public static int findString(String fileString, String stringToFind) {
 195         return fileString.indexOf(stringToFind);
 196     }
 197 }