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