test/com/sun/javadoc/ValidHtml/ValidHtml.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/com/sun/javadoc/ValidHtml/ValidHtml.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,202 @@
     1.4 +/*
     1.5 + * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 4275630 4749453 4625400 4753048 4415270
    1.30 + * @summary  Generated HTML is invalid with frameset DTD.
    1.31 + *           Displays unnecessary horizontal scroll bars.
    1.32 + *           Missing whitespace in DOCTYPE declaration
    1.33 + *           <NOFRAMES> not allowed outside <FRAMESET> element
    1.34 + *           HTML table tags inserted in wrong place in pakcage use page
    1.35 + * @author dkramer
    1.36 + * @run main ValidHtml
    1.37 + */
    1.38 +
    1.39 +import com.sun.javadoc.*;
    1.40 +import java.util.*;
    1.41 +import java.io.*;
    1.42 +
    1.43 +/**
    1.44 + * Runs javadoc and runs regression tests on the resulting HTML.
    1.45 + * It reads each file, complete with newlines, into a string to easily
    1.46 + * find strings that contain newlines.
    1.47 + */
    1.48 +public class ValidHtml {
    1.49 +
    1.50 +    private static final String BUGID = "4275630";
    1.51 +    private static final String BUGNAME = "ValidHtml";
    1.52 +    private static final String FS = System.getProperty("file.separator");
    1.53 +    private static final String PS = System.getProperty("path.separator");
    1.54 +    private static final String LS = System.getProperty("line.separator");
    1.55 +    private static final String TMPDEST_DIR1 = "." + FS + "docs1" + FS;
    1.56 +    private static final String TMPDEST_DIR2 = "." + FS + "docs2" + FS;
    1.57 +
    1.58 +    // Subtest number.  Needed because runResultsOnHTML is run twice,
    1.59 +    // and subtestNum should increment across subtest runs.
    1.60 +    public static int subtestNum = 0;
    1.61 +    public static int numSubtestsPassed = 0;
    1.62 +
    1.63 +    // Entry point
    1.64 +    public static void main(String[] args) {
    1.65 +
    1.66 +        // Directory that contains source files that javadoc runs on
    1.67 +        String srcdir = System.getProperty("test.src", ".");
    1.68 +
    1.69 +        // Test for all cases except the split index page
    1.70 +        runJavadoc(new String[]{"-d", TMPDEST_DIR1,
    1.71 +                    "-doctitle", "Document Title",
    1.72 +                    "-windowtitle", "Window Title",
    1.73 +                    "-use",
    1.74 +                    "-overview", (srcdir + FS + "overview.html"),
    1.75 +                    "-sourcepath", srcdir,
    1.76 +                    "p1", "p2"
    1.77 +                });
    1.78 +        runTestsOnHTML(testArray);
    1.79 +
    1.80 +        printSummary();
    1.81 +    }
    1.82 +
    1.83 +    /** Run javadoc */
    1.84 +    public static void runJavadoc(String[] javadocArgs) {
    1.85 +        if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {
    1.86 +            throw new Error("Javadoc failed to execute");
    1.87 +        }
    1.88 +    }
    1.89 +
    1.90 +    /**
    1.91 +     * Assign value for [ stringToFind, filename ]
    1.92 +     * NOTE: The standard doclet uses the same separator "\n" for all OS's
    1.93 +     */
    1.94 +    private static final String[][] testArray = {
    1.95 +        // Test the proper DOCTYPE element is present:
    1.96 +        {
    1.97 +            "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" \"http://www.w3.org/TR/html4/frameset.dtd\">",
    1.98 +            TMPDEST_DIR1 + "index.html"
    1.99 +        },
   1.100 +        // Test the proper DOCTYPE element is present:
   1.101 +        {
   1.102 +            "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">",
   1.103 +            TMPDEST_DIR1 + "overview-summary.html"
   1.104 +        },
   1.105 +        // Test the proper DOCTYPE element is present:
   1.106 +        {
   1.107 +            "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">",
   1.108 +            TMPDEST_DIR1 + "p1" + FS + "package-summary.html"
   1.109 +        },
   1.110 +        // Test the proper DOCTYPE element is present:
   1.111 +        {
   1.112 +            "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">",
   1.113 +            TMPDEST_DIR1 + "p1" + FS + "C.html"
   1.114 +        },
   1.115 +        // Test the proper DOCTYPE element is present:
   1.116 +        {
   1.117 +            "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">",
   1.118 +            TMPDEST_DIR1 + "overview-frame.html"
   1.119 +        },
   1.120 +        // Test the proper DOCTYPE element is present:
   1.121 +        {
   1.122 +            "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">",
   1.123 +            TMPDEST_DIR1 + "allclasses-frame.html"
   1.124 +        },
   1.125 +        // Test the proper DOCTYPE element is present:
   1.126 +        {
   1.127 +            "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">",
   1.128 +            TMPDEST_DIR1 + "p1" + FS + "package-frame.html"
   1.129 +        },
   1.130 +        // Test that <NOFRAMES> is inside <FRAMESET> element:
   1.131 +        {
   1.132 +            "</noframes>" + LS + "</frameset>",
   1.133 +            TMPDEST_DIR1 + "index.html"
   1.134 +        },
   1.135 +        // Test the table elements are in the correct order:
   1.136 +        {
   1.137 +            "</td>" + LS + "</tr>",
   1.138 +            TMPDEST_DIR1 + FS + "p1" + FS + "package-use.html"
   1.139 +        }
   1.140 +    };
   1.141 +
   1.142 +    public static void runTestsOnHTML(String[][] testArray) {
   1.143 +
   1.144 +        for (int i = 0; i < testArray.length; i++) {
   1.145 +
   1.146 +            subtestNum += 1;
   1.147 +
   1.148 +            // Read contents of file into a string
   1.149 +            String fileString = readFileToString(testArray[i][1]);
   1.150 +
   1.151 +            // Get string to find
   1.152 +            String stringToFind = testArray[i][0];
   1.153 +
   1.154 +            // Find string in file's contents
   1.155 +            if (findString(fileString, stringToFind) == -1) {
   1.156 +                System.out.println("\nSub-test " + (subtestNum) + " for bug " + BUGID + " (" + BUGNAME + ") FAILED\n" + "when searching for:\n" + stringToFind);
   1.157 +            } else {
   1.158 +                numSubtestsPassed += 1;
   1.159 +                System.out.println("\nSub-test " + (subtestNum) + " passed:\n" + stringToFind);
   1.160 +            }
   1.161 +        }
   1.162 +    }
   1.163 +
   1.164 +    public static void printSummary() {
   1.165 +        if (numSubtestsPassed == subtestNum) {
   1.166 +            System.out.println("\nAll " + numSubtestsPassed + " subtests passed");
   1.167 +        } else {
   1.168 +            throw new Error("\n" + (subtestNum - numSubtestsPassed) + " of " + (subtestNum) + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");
   1.169 +        }
   1.170 +    }
   1.171 +
   1.172 +    // Read the file into a String
   1.173 +    public static String readFileToString(String filename) {
   1.174 +        try {
   1.175 +            File file = new File(filename);
   1.176 +            if (!file.exists()) {
   1.177 +                System.out.println("\nFILE DOES NOT EXIST: " + filename);
   1.178 +            }
   1.179 +            BufferedReader in = new BufferedReader(new FileReader(file));
   1.180 +
   1.181 +            // Create an array of characters the size of the file
   1.182 +            char[] allChars = new char[(int) file.length()];
   1.183 +
   1.184 +            // Read the characters into the allChars array
   1.185 +            in.read(allChars, 0, (int) file.length());
   1.186 +            in.close();
   1.187 +
   1.188 +            // Convert to a string
   1.189 +            String allCharsString = new String(allChars);
   1.190 +
   1.191 +            return allCharsString;
   1.192 +
   1.193 +        } catch (FileNotFoundException e) {
   1.194 +            System.err.println(e);
   1.195 +            return "";
   1.196 +        } catch (IOException e) {
   1.197 +            System.err.println(e);
   1.198 +            return "";
   1.199 +        }
   1.200 +    }
   1.201 +
   1.202 +    public static int findString(String fileString, String stringToFind) {
   1.203 +        return fileString.indexOf(stringToFind);
   1.204 +    }
   1.205 +}

mercurial