WindowTitles.java revision 2365:6207608205b8
1/*
2 * Copyright (c) 2002, 2014, 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 4530730
27 * @summary stddoclet: With frames off, window titles have "()" appended
28 * @author dkramer
29 * @run main WindowTitles
30 */
31
32
33import com.sun.javadoc.*;
34import java.util.*;
35import java.io.*;
36
37// If needing regular expression pattern matching,
38// see /java/pubs/dev/linkfix/src/LinkFix.java
39
40/**
41 * Runs javadoc and runs regression tests on the resulting HTML.
42 * It reads each file, complete with newlines, into a string to easily
43 * find strings that contain newlines.
44 */
45public class WindowTitles
46{
47    private static final String BUGID = "4530730";
48    private static final String BUGNAME = "WindowTitles";
49    private static final String TMPDIR_STRING1 = "./docs1/";
50    private static final String TMPDIR_STRING2 = "./docs2/";
51
52    // Subtest number.  Needed because runResultsOnHTML is run twice, and subtestNum
53    // should increment across subtest runs.
54    public static int subtestNum = 0;
55    public static int numSubtestsPassed = 0;
56
57    // Entry point
58    public static void main(String[] args) {
59
60        // Directory that contains source files that javadoc runs on
61        String srcdir = System.getProperty("test.src", ".");
62
63        // Test for all cases except the split index page
64        runJavadoc(new String[] {"-d", TMPDIR_STRING1,
65                                 "-use",
66                                 "-sourcepath", srcdir,
67                                 "p1", "p2"});
68        runTestsOnHTML(testArray);
69
70        // Test only for the split-index case (and run on only one package)
71        System.out.println("");  // blank line
72        runJavadoc(new String[] {"-d", TMPDIR_STRING2,
73                                 "-splitindex",
74                                 "-sourcepath", System.getProperty("test.src", "."),
75                                 "p1"});
76        runTestsOnHTML(testSplitIndexArray);
77
78        printSummary();
79    }
80
81    /** Run javadoc */
82    public static void runJavadoc(String[] javadocArgs) {
83        if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {
84            throw new Error("Javadoc failed to execute");
85        }
86    }
87
88    /**
89     * Assign value for [ stringToFind, filename ]
90     * NOTE: The standard doclet uses platform-specific line separators ("\n")
91     */
92    private static final String[][] testArray = {
93
94            { "<title>Overview</title>",
95                    TMPDIR_STRING1 + "overview-summary.html"                  },
96
97            { "<title>Class Hierarchy</title>",
98                    TMPDIR_STRING1 + "overview-tree.html"                     },
99
100            { "<title>Overview List</title>",
101                    TMPDIR_STRING1 + "overview-frame.html"                    },
102
103            { "<title>p1</title>",
104                    TMPDIR_STRING1 + "p1/package-summary.html"       },
105
106            { "<title>p1</title>",
107                    TMPDIR_STRING1 + "p1/package-frame.html"         },
108
109            { "<title>p1 Class Hierarchy</title>",
110                    TMPDIR_STRING1 + "p1/package-tree.html"          },
111
112            { "<title>Uses of Package p1</title>",
113                    TMPDIR_STRING1 + "p1/package-use.html"           },
114
115            { "<title>C1</title>",
116                    TMPDIR_STRING1 + "p1/C1.html"                    },
117
118            { "<title>All Classes</title>",
119                    TMPDIR_STRING1 + "allclasses-frame.html"                  },
120
121            { "<title>All Classes</title>",
122                    TMPDIR_STRING1 + "allclasses-noframe.html"                },
123
124            { "<title>Constant Field Values</title>",
125                    TMPDIR_STRING1 + "constant-values.html"                   },
126
127            { "<title>Deprecated List</title>",
128                    TMPDIR_STRING1 + "deprecated-list.html"                   },
129
130            { "<title>Serialized Form</title>",
131                    TMPDIR_STRING1 + "serialized-form.html"                   },
132
133            { "<title>API Help</title>",
134                    TMPDIR_STRING1 + "help-doc.html"                          },
135
136            { "<title>Index</title>",
137                    TMPDIR_STRING1 + "index-all.html"                         },
138
139            { "<title>Uses of Class p1.C1</title>",
140                    TMPDIR_STRING1 + "p1/class-use/C1.html" },
141        };
142
143    /**
144     * Assign value for [ stringToFind, filename ] for split index page
145     */
146    private static final String[][] testSplitIndexArray = {
147            { "<title>C-Index</title>",
148                    TMPDIR_STRING2 + "index-files/index-1.html"       },
149        };
150
151    public static void runTestsOnHTML(String[][] testArray) {
152
153        for (int i = 0; i < testArray.length; i++) {
154
155            subtestNum += 1;
156
157            // Read contents of file into a string
158            String fileString = readFileToString(testArray[i][1]);
159
160            // Get string to find
161            String stringToFind = testArray[i][0];
162
163            // Find string in file's contents
164            if (findString(fileString, stringToFind) == -1) {
165                System.out.println("\nSub-test " + (subtestNum)
166                    + " for bug " + BUGID + " (" + BUGNAME + ") FAILED\n"
167                    + "when searching for:\n"
168                    + stringToFind);
169            } else {
170                numSubtestsPassed += 1;
171                System.out.println("\nSub-test " + (subtestNum) + " passed:\n" + stringToFind);
172            }
173        }
174    }
175
176    public static void printSummary() {
177        if ( numSubtestsPassed == subtestNum ) {
178            System.out.println("\nAll " + numSubtestsPassed + " subtests passed");
179        } else {
180            throw new Error("\n" + (subtestNum - numSubtestsPassed) + " of " + (subtestNum)
181                             + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");
182        }
183    }
184
185    // Read the file into a String
186    public static String readFileToString(String filename) {
187        try {
188            File file = new File(filename);
189            if ( !file.exists() ) {
190                System.out.println("\nFILE DOES NOT EXIST: " + filename);
191            }
192            BufferedReader in = new BufferedReader(new FileReader(file));
193
194            // Create an array of characters the size of the file
195            char[] allChars = new char[(int)file.length()];
196
197            // Read the characters into the allChars array
198            in.read(allChars, 0, (int)file.length());
199            in.close();
200
201            // Convert to a string
202            String allCharsString = new String(allChars);
203
204            return allCharsString;
205
206        } catch (FileNotFoundException e) {
207            System.err.println(e);
208            return "";
209        } catch (IOException e) {
210            System.err.println(e);
211            return "";
212        }
213    }
214
215    public static int findString(String fileString, String stringToFind) {
216        return fileString.indexOf(stringToFind);
217    }
218}
219