test/tools/javadoc/TestScriptInComment.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 3315
6f0746b6de9f
child 3845
735048c9f2d6
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

aefimov@3315 1 /*
aefimov@3315 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
aefimov@3315 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aefimov@3315 4 *
aefimov@3315 5 * This code is free software; you can redistribute it and/or modify it
aefimov@3315 6 * under the terms of the GNU General Public License version 2 only, as
aefimov@3315 7 * published by the Free Software Foundation. Oracle designates this
aefimov@3315 8 * particular file as subject to the "Classpath" exception as provided
aefimov@3315 9 * by Oracle in the LICENSE file that accompanied this code.
aefimov@3315 10 *
aefimov@3315 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aefimov@3315 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aefimov@3315 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aefimov@3315 14 * version 2 for more details (a copy is included in the LICENSE file that
aefimov@3315 15 * accompanied this code).
aefimov@3315 16 *
aefimov@3315 17 * You should have received a copy of the GNU General Public License version
aefimov@3315 18 * 2 along with this work; if not, write to the Free Software Foundation,
aefimov@3315 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aefimov@3315 20 *
aefimov@3315 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aefimov@3315 22 * or visit www.oracle.com if you need additional information or have any
aefimov@3315 23 * questions.
aefimov@3315 24 */
aefimov@3315 25
aefimov@3315 26 /**
aefimov@3315 27 * @test
aefimov@3315 28 * @bug 8138725
aefimov@3315 29 * @summary test --allow-script-in-comments
aefimov@3315 30 * @run main TestScriptInComment
aefimov@3315 31 */
aefimov@3315 32
aefimov@3315 33 import java.io.File;
aefimov@3315 34 import java.io.FileWriter;
aefimov@3315 35 import java.io.IOException;
aefimov@3315 36 import java.io.PrintStream;
aefimov@3315 37 import java.io.PrintWriter;
aefimov@3315 38 import java.io.StringWriter;
aefimov@3315 39 import java.util.ArrayList;
aefimov@3315 40 import java.util.Arrays;
aefimov@3315 41 import java.util.Collections;
aefimov@3315 42 import java.util.List;
aefimov@3315 43 import java.util.regex.Matcher;
aefimov@3315 44 import java.util.regex.Pattern;
aefimov@3315 45
aefimov@3315 46 /**
aefimov@3315 47 * Combo-style test, exercising combinations of different HTML fragments that may contain
aefimov@3315 48 * JavaScript, different places to place those fragments, and whether or not to allow the use
aefimov@3315 49 * of JavaScript.
aefimov@3315 50 */
aefimov@3315 51 public class TestScriptInComment {
aefimov@3315 52 public static void main(String... args) throws Exception {
aefimov@3315 53 new TestScriptInComment().run();
aefimov@3315 54 }
aefimov@3315 55
aefimov@3315 56 /**
aefimov@3315 57 * Representative samples of different fragments of HTML that may contain JavaScript.
aefimov@3315 58 * To facilitate checking the output manually in a browser, the text "#ALERT" will be
aefimov@3315 59 * replaced by a JavaScript call of "alert(msg)", using a message string that is specific
aefimov@3315 60 * to the test case.
aefimov@3315 61 */
aefimov@3315 62 enum Comment {
aefimov@3315 63 LC("<script>#ALERT</script>", true), // script tag in Lower Case
aefimov@3315 64 UC("<SCRIPT>#ALERT</script>", true), // script tag in Upper Case
aefimov@3315 65 WS("< script >#ALERT</script>", false, "-Xdoclint:none"), // script tag with invalid white space
aefimov@3315 66 SA("<script src=\"file\"> #ALERT </script>", true), // script tag with an attribute
aefimov@3315 67 ON("<a onclick='#ALERT'>x</a>", true), // event handler attribute
aefimov@3315 68 URI("<a href='javascript:#ALERT'>x</a>", true); // javadcript URI
aefimov@3315 69
aefimov@3315 70 /**
aefimov@3315 71 * Creates an HTML fragment to be injected into a template.
aefimov@3315 72 * @param text the HTML fragment to put into a doc comment or option.
aefimov@3315 73 * @param hasScript whether or not this fragment does contain legal JavaScript
aefimov@3315 74 * @param opts any additional options to be specified when javadoc is run
aefimov@3315 75 */
aefimov@3315 76 Comment(String text, boolean hasScript, String... opts) {
aefimov@3315 77 this.text = text;
aefimov@3315 78 this.hasScript = hasScript;
aefimov@3315 79 this.opts = Arrays.asList(opts);
aefimov@3315 80 }
aefimov@3315 81
aefimov@3315 82 final String text;
aefimov@3315 83 final boolean hasScript;
aefimov@3315 84 final List<String> opts;
aefimov@3315 85 };
aefimov@3315 86
aefimov@3315 87 /**
aefimov@3315 88 * Representative samples of positions in which javadoc may find JavaScript.
aefimov@3315 89 * Each template contains a series of strings, which are written to files or inferred as options.
aefimov@3315 90 * The first source file implies a corresponding output file which should not be written
aefimov@3315 91 * if the comment contains JavaScript and JavaScript is not allowed.
aefimov@3315 92 */
aefimov@3315 93 enum Template {
aefimov@3315 94 OVR("<html><body> overview #COMMENT </body></html>", "package p; public class C { }"),
aefimov@3315 95 PKGINFO("#COMMENT package p;", "package p; public class C { }"),
aefimov@3315 96 PKGHTML("<html><body>#COMMENT package p;</body></html>", "package p; public class C { }"),
aefimov@3315 97 CLS("package p; #COMMENT public class C { }"),
aefimov@3315 98 CON("package p; public class C { #COMMENT public C() { } }"),
aefimov@3315 99 FLD("package p; public class C { #COMMENT public int f; }"),
aefimov@3315 100 MTH("package p; public class C { #COMMENT public void m() { } }"),
aefimov@3315 101 TOP("-top", "lorem #COMMENT ipsum", "package p; public class C { }"),
aefimov@3315 102 HDR("-header", "lorem #COMMENT ipsum", "package p; public class C { }"),
aefimov@3315 103 FTR("-footer", "lorem #COMMENT ipsum", "package p; public class C { }"),
aefimov@3315 104 BTM("-bottom", "lorem #COMMENT ipsum", "package p; public class C { }"),
aefimov@3315 105 DTTL("-doctitle", "lorem #COMMENT ipsum", "package p; public class C { }"),
aefimov@3315 106 PHDR("-packagesheader", "lorem #COMMENT ipsum", "package p; public class C { }");
aefimov@3315 107
aefimov@3315 108 Template(String... args) {
aefimov@3315 109 opts = new ArrayList<String>();
aefimov@3315 110 sources = new ArrayList<String>();
aefimov@3315 111 int i = 0;
aefimov@3315 112 while (args[i].startsWith("-")) {
aefimov@3315 113 // all options being tested have a single argument that follow the option
aefimov@3315 114 opts.add(args[i++]);
aefimov@3315 115 opts.add(args[i++]);
aefimov@3315 116 }
aefimov@3315 117 while(i < args.length) {
aefimov@3315 118 sources.add(args[i++]);
aefimov@3315 119 }
aefimov@3315 120 }
aefimov@3315 121
aefimov@3315 122 // groups: 1 <html> or not; 2: package name; 3: class name
aefimov@3315 123 private final Pattern pat =
aefimov@3315 124 Pattern.compile("(?i)(<html>)?.*?(?:package ([a-z]+);.*?(?:class ([a-z]+).*)?)?");
aefimov@3315 125
aefimov@3315 126 /**
aefimov@3315 127 * Infer the file in which to write the given source.
aefimov@3315 128 * @param dir the base source directory
aefimov@3315 129 * @param src the source text
aefimov@3315 130 * @return the file in which the source should be written
aefimov@3315 131 */
aefimov@3315 132 File getSrcFile(File srcDir, String src) {
aefimov@3315 133 String f;
aefimov@3315 134 Matcher m = pat.matcher(src);
aefimov@3315 135 if (!m.matches())
aefimov@3315 136 throw new Error("match failed");
aefimov@3315 137 if (m.group(3) != null) {
aefimov@3315 138 f = m.group(2) + "/" + m.group(3) + ".java";
aefimov@3315 139 } else if (m.group(2) != null) {
aefimov@3315 140 f = m.group(2) + "/" + (m.group(1) == null ? "package-info.java" : "package.html");
aefimov@3315 141 } else {
aefimov@3315 142 f = "overview.html";
aefimov@3315 143 }
aefimov@3315 144 return new File(srcDir, f);
aefimov@3315 145 }
aefimov@3315 146
aefimov@3315 147 /**
aefimov@3315 148 * Get the options to give to javadoc.
aefimov@3315 149 * @param srcDir the srcDir to use -overview is needed
aefimov@3315 150 * @return
aefimov@3315 151 */
aefimov@3315 152 List<String> getOpts(File srcDir) {
aefimov@3315 153 if (!opts.isEmpty()) {
aefimov@3315 154 return opts;
aefimov@3315 155 } else if (sources.get(0).contains("overview")) {
aefimov@3315 156 return Arrays.asList("-overview", getSrcFile(srcDir, sources.get(0)).getPath());
aefimov@3315 157 } else {
aefimov@3315 158 return Collections.emptyList();
aefimov@3315 159 }
aefimov@3315 160 }
aefimov@3315 161
aefimov@3315 162 /**
aefimov@3315 163 * Gets the output file corresponding to the first source file.
aefimov@3315 164 * This file should not be written if the comment contains JavaScript and JavaScripot is
aefimov@3315 165 * not allowed.
aefimov@3315 166 * @param dir the base output directory
aefimov@3315 167 * @return the output file
aefimov@3315 168 */
aefimov@3315 169 File getOutFile(File outDir) {
aefimov@3315 170 String f;
aefimov@3315 171 Matcher m = pat.matcher(sources.get(0));
aefimov@3315 172 if (!m.matches())
aefimov@3315 173 throw new Error("match failed");
aefimov@3315 174 if (m.group(3) != null) {
aefimov@3315 175 f = m.group(2) + "/" + m.group(3) + ".html";
aefimov@3315 176 } else if (m.group(2) != null) {
aefimov@3315 177 f = m.group(2) + "/package-summary.html";
aefimov@3315 178 } else {
aefimov@3315 179 f = "overview-summary.html";
aefimov@3315 180 }
aefimov@3315 181 return new File(outDir, f);
aefimov@3315 182 }
aefimov@3315 183
aefimov@3315 184 final List<String> opts;
aefimov@3315 185 final List<String> sources;
aefimov@3315 186 };
aefimov@3315 187
aefimov@3315 188 enum Option {
aefimov@3315 189 OFF(null),
aefimov@3315 190 ON("--allow-script-in-comments");
aefimov@3315 191
aefimov@3315 192 Option(String text) {
aefimov@3315 193 this.text = text;
aefimov@3315 194 }
aefimov@3315 195
aefimov@3315 196 final String text;
aefimov@3315 197 };
aefimov@3315 198
aefimov@3315 199 private PrintStream out = System.err;
aefimov@3315 200
aefimov@3315 201 public void run() throws Exception {
aefimov@3315 202 int count = 0;
aefimov@3315 203 for (Template template: Template.values()) {
aefimov@3315 204 for (Comment comment: Comment.values()) {
aefimov@3315 205 for (Option option: Option.values()) {
aefimov@3315 206 if (test(template, comment, option)) {
aefimov@3315 207 count++;
aefimov@3315 208 }
aefimov@3315 209 }
aefimov@3315 210 }
aefimov@3315 211 }
aefimov@3315 212
aefimov@3315 213 out.println(count + " test cases run");
aefimov@3315 214 if (errors > 0) {
aefimov@3315 215 throw new Exception(errors + " errors occurred");
aefimov@3315 216 }
aefimov@3315 217 }
aefimov@3315 218
aefimov@3315 219 boolean test(Template template, Comment comment, Option option) throws IOException {
aefimov@3315 220 if (option == Option.ON && !comment.hasScript) {
aefimov@3315 221 // skip --allowScriptInComments if comment does not contain JavaScript
aefimov@3315 222 return false;
aefimov@3315 223 }
aefimov@3315 224
aefimov@3315 225 String test = template + "-" + comment + "-" + option;
aefimov@3315 226 out.println("Test: " + test);
aefimov@3315 227
aefimov@3315 228 File dir = new File(test);
aefimov@3315 229 dir.mkdirs();
aefimov@3315 230 File srcDir = new File(dir, "src");
aefimov@3315 231 File outDir = new File(dir, "out");
aefimov@3315 232
aefimov@3315 233 String alert = "alert(\"" + test + "\");";
aefimov@3315 234 for (String src: template.sources) {
aefimov@3315 235 writeFile(template.getSrcFile(srcDir, src),
aefimov@3315 236 src.replace("#COMMENT",
aefimov@3315 237 "/** " + comment.text.replace("#ALERT", alert) + " **/"));
aefimov@3315 238 }
aefimov@3315 239
aefimov@3315 240 List<String> opts = new ArrayList<String>();
aefimov@3315 241 opts.add("-sourcepath");
aefimov@3315 242 opts.add(srcDir.getPath());
aefimov@3315 243 opts.add("-d");
aefimov@3315 244 opts.add(outDir.getPath());
aefimov@3315 245 if (option.text != null)
aefimov@3315 246 opts.add(option.text);
aefimov@3315 247 for (String opt: template.getOpts(srcDir)) {
aefimov@3315 248 opts.add(opt.replace("#COMMENT", comment.text.replace("#ALERT", alert)));
aefimov@3315 249 }
aefimov@3315 250 opts.addAll(comment.opts);
aefimov@3315 251 opts.add("-noindex"); // index not required; save time/space writing files
aefimov@3315 252 opts.add("p");
aefimov@3315 253
aefimov@3315 254 StringWriter sw = new StringWriter();
aefimov@3315 255 PrintWriter pw = new PrintWriter(sw);
aefimov@3315 256 int rc = javadoc(opts, pw);
aefimov@3315 257 pw.close();
aefimov@3315 258 String log = sw.toString();
aefimov@3315 259 writeFile(new File(dir, "log.txt"), log);
aefimov@3315 260
aefimov@3315 261 out.println("opts: " + opts);
aefimov@3315 262 out.println(" rc: " + rc);
aefimov@3315 263 out.println(" log:");
aefimov@3315 264 out.println(log);
aefimov@3315 265
aefimov@3315 266 String ERROR = "Use --allow-script-in-comment";
aefimov@3315 267 File outFile = template.getOutFile(outDir);
aefimov@3315 268
aefimov@3315 269 boolean expectErrors = comment.hasScript && (option == Option.OFF);
aefimov@3315 270
aefimov@3315 271 if (expectErrors) {
aefimov@3315 272 check(rc != 0, "unexpected exit code: " + rc);
aefimov@3315 273 check(log.contains(ERROR), "expected error message not found");
aefimov@3315 274 check(!outFile.exists(), "output file found unexpectedly");
aefimov@3315 275 } else {
aefimov@3315 276 check(rc == 0, "unexpected exit code: " + rc);
aefimov@3315 277 check(!log.contains(ERROR), "error message found");
aefimov@3315 278 check(outFile.exists(), "output file not found");
aefimov@3315 279 }
aefimov@3315 280
aefimov@3315 281 out.println();
aefimov@3315 282 return true;
aefimov@3315 283 }
aefimov@3315 284
aefimov@3315 285 int javadoc(List<String> opts, PrintWriter pw) {
aefimov@3315 286 return com.sun.tools.javadoc.Main.execute("javadoc", pw, pw, pw,
aefimov@3315 287 "com.sun.tools.doclets.standard.Standard", opts.toArray(new String[opts.size()]));
aefimov@3315 288 }
aefimov@3315 289
aefimov@3315 290 File writeFile(File f, String text) throws IOException {
aefimov@3315 291 f.getParentFile().mkdirs();
aefimov@3315 292 FileWriter fw = new FileWriter(f);
aefimov@3315 293 try {
aefimov@3315 294 fw.write(text);
aefimov@3315 295 } finally {
aefimov@3315 296 fw.close();
aefimov@3315 297 }
aefimov@3315 298 return f;
aefimov@3315 299 }
aefimov@3315 300
aefimov@3315 301 void check(boolean cond, String errMessage) {
aefimov@3315 302 if (!cond) {
aefimov@3315 303 error(errMessage);
aefimov@3315 304 }
aefimov@3315 305 }
aefimov@3315 306
aefimov@3315 307 void error(String message) {
aefimov@3315 308 out.println("Error: " + message);
aefimov@3315 309 errors++;
aefimov@3315 310 }
aefimov@3315 311
aefimov@3315 312 int errors = 0;
aefimov@3315 313 }
aefimov@3315 314

mercurial