langtools/test/com/sun/javadoc/ValidHtml/ValidHtml.java
changeset 24399 af1a0220d0fa
parent 23971 f5ff1f5a8dee
child 29429 964498d4dcec
equal deleted inserted replaced
24398:601a611d0aee 24399:af1a0220d0fa
    28  *           Displays unnecessary horizontal scroll bars.
    28  *           Displays unnecessary horizontal scroll bars.
    29  *           Missing whitespace in DOCTYPE declaration
    29  *           Missing whitespace in DOCTYPE declaration
    30  *           <NOFRAMES> not allowed outside <FRAMESET> element
    30  *           <NOFRAMES> not allowed outside <FRAMESET> element
    31  *           HTML table tags inserted in wrong place in pakcage use page
    31  *           HTML table tags inserted in wrong place in pakcage use page
    32  * @author dkramer
    32  * @author dkramer
       
    33  * @library ../lib
       
    34  * @build JavadocTester
    33  * @run main ValidHtml
    35  * @run main ValidHtml
    34  */
    36  */
    35 
    37 
    36 import com.sun.javadoc.*;
    38 public class ValidHtml extends JavadocTester {
    37 import java.util.*;
       
    38 import java.io.*;
       
    39 
    39 
    40 /**
    40     public static void main(String... args) throws Exception {
    41  * Runs javadoc and runs regression tests on the resulting HTML.
    41         ValidHtml tester = new ValidHtml();
    42  * It reads each file, complete with newlines, into a string to easily
    42         tester.runTests();
    43  * find strings that contain newlines.
    43     }
    44  */
       
    45 public class ValidHtml {
       
    46 
    44 
    47     protected static final String NL = System.getProperty("line.separator");
    45     @Test
    48 
    46     void test() {
    49     private static final String BUGID = "4275630";
       
    50     private static final String BUGNAME = "ValidHtml";
       
    51     private static final String TMPDEST_DIR1 = "./docs1/";
       
    52     private static final String TMPDEST_DIR2 = "./docs2/";
       
    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
    47         // Test for all cases except the split index page
    66         runJavadoc(new String[]{"-d", TMPDEST_DIR1,
    48         javadoc("-d", "out",
    67                     "-doctitle", "Document Title",
    49                     "-doctitle", "Document Title",
    68                     "-windowtitle", "Window Title",
    50                     "-windowtitle", "Window Title",
    69                     "-use",
    51                     "-use",
    70                     "-overview", (srcdir + "/overview.html"),
    52                     "-overview", testSrc("overview.html"),
    71                     "-sourcepath", srcdir,
    53                     "-sourcepath", testSrc,
    72                     "p1", "p2"
    54                     "p1", "p2");
    73                 });
    55         checkExit(Exit.OK);
    74         runTestsOnHTML(testArray);
       
    75 
    56 
    76         printSummary();
    57         // Test the proper DOCTYPE element are present:
       
    58         checkOutput("index.html",              true, FRAMESET);
       
    59         checkOutput("overview-summary.html",   true, LOOSE);
       
    60         checkOutput("p1/package-summary.html", true, LOOSE);
       
    61         checkOutput("p1/C.html",               true, LOOSE);
       
    62         checkOutput("overview-frame.html",     true, LOOSE);
       
    63         checkOutput("allclasses-frame.html",   true, LOOSE);
       
    64         checkOutput("p1/package-frame.html",   true, LOOSE);
       
    65 
       
    66         // Test that <NOFRAMES> is inside <FRAMESET> element:
       
    67         checkOutput("index.html", true,
       
    68                 "</noframes>\n"
       
    69                 + "</frameset>");
       
    70 
       
    71         // Test the table elements are in the correct order:
       
    72         checkOutput("p1/package-use.html", true,
       
    73                 "</td>\n"
       
    74                 + "</tr>");
    77     }
    75     }
    78 
    76 
    79     /** Run javadoc */
    77     private static final String FRAMESET =
    80     public static void runJavadoc(String[] javadocArgs) {
    78             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" \"http://www.w3.org/TR/html4/frameset.dtd\">";
    81         if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {
    79     private static final String LOOSE =
    82             throw new Error("Javadoc failed to execute");
    80             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
    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         // Test the proper DOCTYPE element is present:
       
    92         {
       
    93             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" \"http://www.w3.org/TR/html4/frameset.dtd\">",
       
    94             TMPDEST_DIR1 + "index.html"
       
    95         },
       
    96         // Test the proper DOCTYPE element is present:
       
    97         {
       
    98             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">",
       
    99             TMPDEST_DIR1 + "overview-summary.html"
       
   100         },
       
   101         // Test the proper DOCTYPE element is present:
       
   102         {
       
   103             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">",
       
   104             TMPDEST_DIR1 + "p1/package-summary.html"
       
   105         },
       
   106         // Test the proper DOCTYPE element is present:
       
   107         {
       
   108             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">",
       
   109             TMPDEST_DIR1 + "p1/C.html"
       
   110         },
       
   111         // Test the proper DOCTYPE element is present:
       
   112         {
       
   113             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">",
       
   114             TMPDEST_DIR1 + "overview-frame.html"
       
   115         },
       
   116         // Test the proper DOCTYPE element is present:
       
   117         {
       
   118             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">",
       
   119             TMPDEST_DIR1 + "allclasses-frame.html"
       
   120         },
       
   121         // Test the proper DOCTYPE element is present:
       
   122         {
       
   123             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">",
       
   124             TMPDEST_DIR1 + "p1/package-frame.html"
       
   125         },
       
   126         // Test that <NOFRAMES> is inside <FRAMESET> element:
       
   127         {
       
   128             "</noframes>\n" +
       
   129             "</frameset>",
       
   130             TMPDEST_DIR1 + "index.html"
       
   131         },
       
   132         // Test the table elements are in the correct order:
       
   133         {
       
   134             "</td>\n" +
       
   135             "</tr>",
       
   136             TMPDEST_DIR1 + "/p1/package-use.html"
       
   137         }
       
   138     };
       
   139 
       
   140     public static void runTestsOnHTML(String[][] testArray) {
       
   141 
       
   142         for (int i = 0; i < testArray.length; i++) {
       
   143 
       
   144             subtestNum += 1;
       
   145 
       
   146             // Read contents of file into a string
       
   147             String fileString = readFileToString(testArray[i][1]);
       
   148 
       
   149             // Get string to find
       
   150             String stringToFind = testArray[i][0];
       
   151 
       
   152             // Find string in file's contents
       
   153             if (findString(fileString, stringToFind) == -1) {
       
   154                 System.out.println("\nSub-test " + (subtestNum) + " for bug " + BUGID + " (" + BUGNAME + ") FAILED\n" + "when searching for:\n" + stringToFind);
       
   155             } else {
       
   156                 numSubtestsPassed += 1;
       
   157                 System.out.println("\nSub-test " + (subtestNum) + " passed:\n" + stringToFind);
       
   158             }
       
   159         }
       
   160     }
       
   161 
       
   162     public static void printSummary() {
       
   163         if (numSubtestsPassed == subtestNum) {
       
   164             System.out.println("\nAll " + numSubtestsPassed + " subtests passed");
       
   165         } else {
       
   166             throw new Error("\n" + (subtestNum - numSubtestsPassed) + " of " + (subtestNum) + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");
       
   167         }
       
   168     }
       
   169 
       
   170     // Read the file into a String
       
   171     public static String readFileToString(String filename) {
       
   172         try {
       
   173             File file = new File(filename);
       
   174             if (!file.exists()) {
       
   175                 System.out.println("\nFILE DOES NOT EXIST: " + filename);
       
   176             }
       
   177             BufferedReader in = new BufferedReader(new FileReader(file));
       
   178 
       
   179             // Create an array of characters the size of the file
       
   180             char[] allChars = new char[(int) file.length()];
       
   181 
       
   182             // Read the characters into the allChars array
       
   183             in.read(allChars, 0, (int) file.length());
       
   184             in.close();
       
   185 
       
   186             // Convert to a string
       
   187             String allCharsString = new String(allChars);
       
   188 
       
   189             return allCharsString;
       
   190 
       
   191         } catch (FileNotFoundException e) {
       
   192             System.err.println(e);
       
   193             return "";
       
   194         } catch (IOException e) {
       
   195             System.err.println(e);
       
   196             return "";
       
   197         }
       
   198     }
       
   199 
       
   200     public static int findString(String fileString, String stringToFind) {
       
   201         return fileString.replace(NL, "\n").indexOf(stringToFind);
       
   202     }
       
   203 }
    81 }