test/com/sun/javadoc/AccessH1/AccessH1.java

Wed, 08 Oct 2014 14:16:40 -0700

author
asaha
date
Wed, 08 Oct 2014 14:16:40 -0700
changeset 2586
f5e5ca7505e2
parent 2225
b8ebde062692
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

     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 4636667 7052425 8016549
    27  * @summary  Use <H1, <H2>, and <H3> in proper sequence for accessibility
    28  * @author dkramer
    29  * @run main AccessH1
    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 AccessH1 {
    45     private static final String BUGID = "4636667-7052425";
    46     private static final String BUGNAME = "AccessH1";
    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                                  "-doctitle", "Document Title",
    67                                  "-sourcepath", srcdir,
    68                                  "p1", "p2"});
    69         runTestsOnHTML(testArray);
    71         printSummary();
    72     }
    74     /** Run javadoc */
    75     public static void runJavadoc(String[] javadocArgs) {
    76         if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {
    77             throw new Error("Javadoc failed to execute");
    78         }
    79     }
    81     /**
    82      * Assign value for [ stringToFind, filename ]
    83      * NOTE: The standard doclet uses the same separator "\n" for all OS's
    84      */
    85     private static final String[][] testArray = {
    86         // Test the style sheet
    87         {
    88             "h1 {" + LS + "    font-size:20px;" + LS +
    89             "}",
    90             TMPDEST_DIR1 + "stylesheet.css"
    91         },
    92         // Test the doc title in the overview page
    93         {
    94             "<h1 class=\"title\">Document Title</h1>",
    95             TMPDEST_DIR1 + "overview-summary.html"
    96         }
    97     };
    99     public static void runTestsOnHTML(String[][] testArray) {
   101         for (int i = 0; i < testArray.length; i++) {
   103             subtestNum += 1;
   105             // Read contents of file into a string
   106             String fileString = readFileToString(testArray[i][1]);
   108             // Get string to find
   109             String stringToFind = testArray[i][0];
   111             // Find string in file's contents
   112             if (findString(fileString, stringToFind) == -1) {
   113                 System.out.println("\nSub-test " + (subtestNum)
   114                     + " for bug " + BUGID + " (" + BUGNAME + ") FAILED\n"
   115                     + "when searching for:\n"
   116                     + stringToFind);
   117             } else {
   118                 numSubtestsPassed += 1;
   119                 System.out.println("\nSub-test " + (subtestNum) + " passed:\n" + stringToFind);
   120             }
   121         }
   122     }
   124     public static void printSummary() {
   125         if ( numSubtestsPassed == subtestNum ) {
   126             System.out.println("\nAll " + numSubtestsPassed + " subtests passed");
   127         } else {
   128             throw new Error("\n" + (subtestNum - numSubtestsPassed) + " of " + (subtestNum)
   129                              + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");
   130         }
   131     }
   133     // Read the file into a String
   134     public static String readFileToString(String filename) {
   135         try {
   136             File file = new File(filename);
   137             if ( !file.exists() ) {
   138                 System.out.println("\nFILE DOES NOT EXIST: " + filename);
   139             }
   140             BufferedReader in = new BufferedReader(new FileReader(file));
   142             // Create an array of characters the size of the file
   143             char[] allChars = new char[(int)file.length()];
   145             // Read the characters into the allChars array
   146             in.read(allChars, 0, (int)file.length());
   147             in.close();
   149             // Convert to a string
   150             String allCharsString = new String(allChars);
   152             return allCharsString;
   154         } catch (FileNotFoundException e) {
   155             System.err.println(e);
   156             return "";
   157         } catch (IOException e) {
   158             System.err.println(e);
   159             return "";
   160         }
   161     }
   163     public static int findString(String fileString, String stringToFind) {
   164         return fileString.indexOf(stringToFind);
   165     }
   166 }

mercurial