1 /*
2  * Copyright (c) 2002, 2012, 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 4720849
27  * @summary  com.sun.tools.doclets.standard.Standard contains hard-coded version number
28  * @author dkramer
29  * @run main VersionNumber
30  */
31 
32 
33 import com.sun.javadoc.*;
34 import java.util.*;
35 import java.io.*;
36 
37 
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 VersionNumber {
44 
45     private static final String BUGID = "4720849";
46     private static final String BUGNAME = "VersionNumber";
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;
52 
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;
57 
58     // Entry point
main(String[] args)59     public static void main(String[] args) {
60 
61         // Directory that contains source files that javadoc runs on
62         String srcdir = System.getProperty("test.src", ".");
63 
64         // Test for all cases except the split index page
65         runJavadoc(new String[] {"-d", TMPDEST_DIR1,
66                                  "p1"});
67         runTestsOnHTML(testArray);
68 
69         printSummary();
70     }
71 
72     /** Run javadoc */
runJavadoc(String[] javadocArgs)73     public static void runJavadoc(String[] javadocArgs) {
74         if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) {
75             throw new Error("Javadoc failed to execute");
76         }
77     }
78 
79     /**
80      * Assign value for [ stringToFind, filename ]
81      * NOTE: The standard doclet uses the same separator "\n" for all OS's
82      */
83     private static final String[][] testArray = {
84 
85             // Test the proper DOCTYPE element is present:
86             {
87                  "<!-- Generated by javadoc (",
88                      TMPDEST_DIR1 + "p1" + FS + "C.html"  },
89 
90         };
91 
runTestsOnHTML(String[][] testArray)92     public static void runTestsOnHTML(String[][] testArray) {
93 
94         for (int i = 0; i < testArray.length; i++) {
95 
96             subtestNum += 1;
97 
98             // Read contents of file into a string
99             String fileString = readFileToString(testArray[i][1]);
100 
101             // Get string to find
102             String stringToFind = testArray[i][0];
103 
104             // Find string in file's contents
105             if (findString(fileString, stringToFind) == -1) {
106                 System.out.println("\nSub-test " + (subtestNum)
107                     + " for bug " + BUGID + " (" + BUGNAME + ") FAILED\n"
108                     + "when searching for:\n"
109                     + stringToFind);
110             } else {
111                 numSubtestsPassed += 1;
112                 System.out.println("\nSub-test " + (subtestNum) + " passed:\n" + stringToFind);
113             }
114         }
115     }
116 
printSummary()117     public static void printSummary() {
118         if ( numSubtestsPassed == subtestNum ) {
119             System.out.println("\nAll " + numSubtestsPassed + " subtests passed");
120         } else {
121             throw new Error("\n" + (subtestNum - numSubtestsPassed) + " of " + (subtestNum)
122                              + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n");
123         }
124     }
125 
126     // Read the file into a String
readFileToString(String filename)127     public static String readFileToString(String filename) {
128         try {
129             File file = new File(filename);
130             if ( !file.exists() ) {
131                 System.out.println("\nFILE DOES NOT EXIST: " + filename);
132             }
133             BufferedReader in = new BufferedReader(new FileReader(file));
134 
135             // Create an array of characters the size of the file
136             char[] allChars = new char[(int)file.length()];
137 
138             // Read the characters into the allChars array
139             in.read(allChars, 0, (int)file.length());
140             in.close();
141 
142             // Convert to a string
143             String allCharsString = new String(allChars);
144 
145             return allCharsString;
146 
147         } catch (FileNotFoundException e) {
148             System.err.println(e);
149             return "";
150         } catch (IOException e) {
151             System.err.println(e);
152             return "";
153         }
154     }
155 
findString(String fileString, String stringToFind)156     public static int findString(String fileString, String stringToFind) {
157         return fileString.indexOf(stringToFind);
158     }
159 }
160