Wed, 28 Nov 2012 14:07:26 -0800
Merge
duke@1 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. |
duke@1 | 8 | * |
duke@1 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 13 | * accompanied this code). |
duke@1 | 14 | * |
duke@1 | 15 | * You should have received a copy of the GNU General Public License version |
duke@1 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 18 | * |
ohair@554 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 20 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 21 | * questions. |
duke@1 | 22 | */ |
duke@1 | 23 | |
duke@1 | 24 | /* |
duke@1 | 25 | * @test |
duke@1 | 26 | * @bug 4524350 4662945 4633447 |
duke@1 | 27 | * @summary stddoclet: {@docRoot} inserts an extra trailing "/" |
duke@1 | 28 | * @author dkramer |
duke@1 | 29 | * @run main DocRootSlash |
duke@1 | 30 | */ |
duke@1 | 31 | |
duke@1 | 32 | import com.sun.javadoc.*; |
duke@1 | 33 | import java.util.*; |
duke@1 | 34 | import java.io.*; |
duke@1 | 35 | import java.nio.*; |
duke@1 | 36 | import java.util.regex.*; |
duke@1 | 37 | |
duke@1 | 38 | /** |
duke@1 | 39 | * Runs javadoc and runs regression tests on the resulting HTML. |
duke@1 | 40 | * It reads each file, complete with newlines, into a string to easily |
duke@1 | 41 | * find strings that contain newlines. |
duke@1 | 42 | */ |
duke@1 | 43 | public class DocRootSlash |
duke@1 | 44 | { |
duke@1 | 45 | private static final String BUGID = "4524350, 4662945, or 4633447"; |
duke@1 | 46 | private static final String BUGNAME = "DocRootSlash"; |
duke@1 | 47 | private static final String FS = System.getProperty("file.separator"); |
duke@1 | 48 | private static final String PS = System.getProperty("path.separator"); |
duke@1 | 49 | private static final String TMPDIR_STRING1 = "." + FS + "docs1" + FS; |
duke@1 | 50 | |
duke@1 | 51 | // Test number. Needed because runResultsOnHTMLFile is run twice, and subtestNum |
duke@1 | 52 | // should increment across test runs. |
duke@1 | 53 | public static int subtestNum = 0; |
duke@1 | 54 | public static int numOfSubtestsPassed = 0; |
duke@1 | 55 | |
duke@1 | 56 | // Entry point |
duke@1 | 57 | public static void main(String[] args) { |
duke@1 | 58 | |
duke@1 | 59 | // Directory that contains source files that javadoc runs on |
duke@1 | 60 | String srcdir = System.getProperty("test.src", "."); |
duke@1 | 61 | |
duke@1 | 62 | runJavadoc(new String[] {"-d", TMPDIR_STRING1, |
duke@1 | 63 | "-overview", (srcdir + FS + "overview.html"), |
duke@1 | 64 | "-header", "<A HREF=\"{@docroot}/package-list\">{@docroot}</A> <A HREF=\"{@docRoot}/help-doc\">{@docRoot}</A>", |
duke@1 | 65 | "-sourcepath", srcdir, |
duke@1 | 66 | "p1", "p2"}); |
duke@1 | 67 | runTestsOnHTMLFiles(filenameArray); |
duke@1 | 68 | |
duke@1 | 69 | printSummary(); |
duke@1 | 70 | } |
duke@1 | 71 | |
duke@1 | 72 | /** Run javadoc */ |
duke@1 | 73 | public static void runJavadoc(String[] javadocArgs) { |
duke@1 | 74 | if (com.sun.tools.javadoc.Main.execute(javadocArgs) != 0) { |
duke@1 | 75 | throw new Error("Javadoc failed to execute"); |
duke@1 | 76 | } |
duke@1 | 77 | } |
duke@1 | 78 | |
duke@1 | 79 | /** The array of filenames to test */ |
duke@1 | 80 | private static final String[] filenameArray = { |
duke@1 | 81 | TMPDIR_STRING1 + "p1" + FS + "C1.html" , |
duke@1 | 82 | TMPDIR_STRING1 + "p1" + FS + "package-summary.html", |
duke@1 | 83 | TMPDIR_STRING1 + "overview-summary.html" |
duke@1 | 84 | }; |
duke@1 | 85 | |
duke@1 | 86 | public static void runTestsOnHTMLFiles(String[] filenameArray) { |
duke@1 | 87 | String fileString; |
duke@1 | 88 | |
duke@1 | 89 | // Bugs 4524350 4662945 |
duke@1 | 90 | for (int i = 0; i < filenameArray.length; i++ ) { |
duke@1 | 91 | |
duke@1 | 92 | // Read contents of file (whose filename is in filenames) into a string |
duke@1 | 93 | fileString = readFileToString(filenameArray[i]); |
duke@1 | 94 | |
duke@1 | 95 | System.out.println("\nSub-tests for file: " + filenameArray[i] |
duke@1 | 96 | + " --------------"); |
duke@1 | 97 | |
duke@1 | 98 | // Loop over all tests in a single file |
duke@1 | 99 | for ( int j = 0; j < 11; j++ ) { |
duke@1 | 100 | subtestNum += 1; |
duke@1 | 101 | |
duke@1 | 102 | // Compare actual to expected string for a single subtest |
duke@1 | 103 | compareActualToExpected(fileString); |
duke@1 | 104 | } |
duke@1 | 105 | } |
duke@1 | 106 | |
duke@1 | 107 | // Bug 4633447: Special test for overview-frame.html |
duke@1 | 108 | // Find two strings in file "overview-frame.html" |
duke@1 | 109 | String filename = TMPDIR_STRING1 + "overview-frame.html"; |
duke@1 | 110 | fileString = readFileToString(filename); |
duke@1 | 111 | |
duke@1 | 112 | // Find first string <A HREF="./package-list"> in overview-frame.html |
duke@1 | 113 | subtestNum += 1; |
duke@1 | 114 | String stringToFind = "<A HREF=\"./package-list\">"; |
duke@1 | 115 | String result; |
duke@1 | 116 | if ( fileString.indexOf(stringToFind) == -1 ) { |
duke@1 | 117 | result = "FAILED"; |
duke@1 | 118 | } else { |
duke@1 | 119 | result = "succeeded"; |
duke@1 | 120 | numOfSubtestsPassed += 1; |
duke@1 | 121 | } |
duke@1 | 122 | System.out.println("\nSub-test " + (subtestNum) |
duke@1 | 123 | + " for bug " + BUGID + " (" + BUGNAME + ") " + result + "\n" |
duke@1 | 124 | + "when searching for:\n" |
duke@1 | 125 | + stringToFind + "\n" |
duke@1 | 126 | + "in file " + filename); |
duke@1 | 127 | |
duke@1 | 128 | // Find second string <A HREF="./help-doc"> in overview-frame.html |
duke@1 | 129 | subtestNum += 1; |
duke@1 | 130 | stringToFind = "<A HREF=\"./help-doc\">"; |
duke@1 | 131 | if ( fileString.indexOf(stringToFind) == -1 ) { |
duke@1 | 132 | result = "FAILED"; |
duke@1 | 133 | } else { |
duke@1 | 134 | result = "succeeded"; |
duke@1 | 135 | numOfSubtestsPassed += 1; |
duke@1 | 136 | } |
duke@1 | 137 | System.out.println("\nSub-test " + (subtestNum) |
duke@1 | 138 | + " for bug " + BUGID + " (" + BUGNAME + ") " + result + "\n" |
duke@1 | 139 | + "when searching for:\n" |
duke@1 | 140 | + stringToFind + "\n" |
duke@1 | 141 | + "in file " + filename); |
duke@1 | 142 | } |
duke@1 | 143 | |
duke@1 | 144 | public static void printSummary() { |
duke@1 | 145 | System.out.println(""); |
duke@1 | 146 | if ( numOfSubtestsPassed == subtestNum ) { |
duke@1 | 147 | System.out.println("\nAll " + numOfSubtestsPassed + " subtests passed"); |
duke@1 | 148 | } else { |
duke@1 | 149 | throw new Error("\n" + (subtestNum - numOfSubtestsPassed) + " of " + (subtestNum) |
duke@1 | 150 | + " subtests failed for bug " + BUGID + " (" + BUGNAME + ")\n"); |
duke@1 | 151 | } |
duke@1 | 152 | } |
duke@1 | 153 | |
duke@1 | 154 | // Read the contents of the file into a String |
duke@1 | 155 | public static String readFileToString(String filename) { |
duke@1 | 156 | try { |
duke@1 | 157 | File file = new File(filename); |
duke@1 | 158 | if ( !file.exists() ) { |
duke@1 | 159 | System.out.println("\nFILE DOES NOT EXIST: " + filename); |
duke@1 | 160 | } |
duke@1 | 161 | |
duke@1 | 162 | BufferedReader in = new BufferedReader(new FileReader(file)); |
duke@1 | 163 | |
duke@1 | 164 | // Create an array of characters the size of the file |
duke@1 | 165 | char[] allChars = new char[(int)file.length()]; |
duke@1 | 166 | |
duke@1 | 167 | // Read the characters into the allChars array |
duke@1 | 168 | in.read(allChars, 0, (int)file.length()); |
duke@1 | 169 | in.close(); |
duke@1 | 170 | |
duke@1 | 171 | // Convert to a string |
duke@1 | 172 | String allCharsString = new String(allChars); |
duke@1 | 173 | |
duke@1 | 174 | return allCharsString; |
duke@1 | 175 | } catch (FileNotFoundException e) { |
duke@1 | 176 | System.err.println(e); |
duke@1 | 177 | return ""; |
duke@1 | 178 | } catch (IOException e) { |
duke@1 | 179 | System.err.println(e); |
duke@1 | 180 | return ""; |
duke@1 | 181 | } |
duke@1 | 182 | } |
duke@1 | 183 | |
duke@1 | 184 | /** |
duke@1 | 185 | * Regular expression pattern matching code adapted from Eric's |
duke@1 | 186 | * /java/pubs/dev/linkfix/src/LinkFix.java |
duke@1 | 187 | * |
duke@1 | 188 | * Prefix Pattern: |
duke@1 | 189 | * flag (?i) (case insensitive, so "a href" == "A HREF" and all combinations) |
duke@1 | 190 | * group1 ( |
duke@1 | 191 | * <a or <A |
duke@1 | 192 | * \\s+ (one or more whitespace characters) |
duke@1 | 193 | * href or HREF |
duke@1 | 194 | * \" (double quote) |
duke@1 | 195 | * ) |
duke@1 | 196 | * group2 ([^\"]*) (link reference -- characters that don't include a quote) |
duke@1 | 197 | * group3 (\".*?>) (" target="frameName">) |
duke@1 | 198 | * group4 (.*?) (label - zero or more characters) |
duke@1 | 199 | * group5 (</a>) (end tag) |
duke@1 | 200 | */ |
duke@1 | 201 | static String prefix = "(?i)(<a\\s+href="; // <a href= (start group1) |
duke@1 | 202 | static String ref1 = "\")([^\"]*)(\".*?>)"; // doublequotes (end group1, group2, group3) |
duke@1 | 203 | static String ref2 = ")(\\S+?)([^<>]*>)"; // no quotes (end group1, group2, group3) |
duke@1 | 204 | static String label = "(.*?)"; // text label (group4) |
duke@1 | 205 | static String end = "(</a>)"; // </a> (group5) |
duke@1 | 206 | |
duke@1 | 207 | /** |
duke@1 | 208 | * Compares the actual string to the expected string in the specified string |
duke@1 | 209 | * str String to search through |
duke@1 | 210 | */ |
duke@1 | 211 | static void compareActualToExpected(String str) { |
duke@1 | 212 | // Pattern must be compiled each run because subtestNum is incremented |
duke@1 | 213 | Pattern actualLinkPattern1 = |
duke@1 | 214 | Pattern.compile("Sub-test " + subtestNum + " Actual: " + prefix + ref1, Pattern.DOTALL); |
duke@1 | 215 | Pattern expectLinkPattern1 = |
duke@1 | 216 | Pattern.compile("Sub-test " + subtestNum + " Expect: " + prefix + ref1, Pattern.DOTALL); |
duke@1 | 217 | // Pattern linkPattern2 = Pattern.compile(prefix + ref2 + label + end, Pattern.DOTALL); |
duke@1 | 218 | |
duke@1 | 219 | CharBuffer charBuffer = CharBuffer.wrap(str); |
duke@1 | 220 | Matcher actualLinkMatcher1 = actualLinkPattern1.matcher(charBuffer); |
duke@1 | 221 | Matcher expectLinkMatcher1 = expectLinkPattern1.matcher(charBuffer); |
duke@1 | 222 | String result; |
duke@1 | 223 | if ( expectLinkMatcher1.find() && actualLinkMatcher1.find() ) { |
duke@1 | 224 | String expectRef = expectLinkMatcher1.group(2); |
duke@1 | 225 | String actualRef = actualLinkMatcher1.group(2); |
duke@1 | 226 | if ( actualRef.equals(expectRef) ) { |
duke@1 | 227 | result = "succeeded"; |
duke@1 | 228 | numOfSubtestsPassed += 1; |
duke@1 | 229 | // System.out.println("pattern: " + actualLinkPattern1.pattern()); |
duke@1 | 230 | // System.out.println("actualRef: " + actualRef); |
duke@1 | 231 | // System.out.println("group0: " + actualLinkMatcher1.group()); |
duke@1 | 232 | // System.out.println("group1: " + actualLinkMatcher1.group(1)); |
duke@1 | 233 | // System.out.println("group2: " + actualLinkMatcher1.group(2)); |
duke@1 | 234 | // System.out.println("group3: " + actualLinkMatcher1.group(3)); |
duke@1 | 235 | // System.exit(0); |
duke@1 | 236 | } else { |
duke@1 | 237 | result = "FAILED"; |
duke@1 | 238 | } |
duke@1 | 239 | System.out.println("\nSub-test " + (subtestNum) |
duke@1 | 240 | + " for bug " + BUGID + " (" + BUGNAME + ") " + result + "\n" |
duke@1 | 241 | + "Actual: \"" + actualRef + "\"" + "\n" |
duke@1 | 242 | + "Expect: \"" + expectRef + "\""); |
duke@1 | 243 | } else { |
duke@1 | 244 | System.out.println("Didn't find <A HREF> that fits the pattern: " |
duke@1 | 245 | + expectLinkPattern1.pattern() ); |
duke@1 | 246 | } |
duke@1 | 247 | } |
duke@1 | 248 | } |