langtools/test/tools/doclint/DocLintTester.java
author bpatel
Thu, 24 Oct 2013 11:22:50 -0700
changeset 21500 475e59d3b40c
parent 19121 c626ed0c6ab0
child 29957 7740f9657f56
permissions -rw-r--r--
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
     1
/*
15370
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
     4
 *
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
     8
 *
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    13
 * accompanied this code).
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    14
 *
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    18
 *
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    21
 * questions.
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    22
 */
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    23
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    24
import java.io.File;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    25
import java.util.ArrayList;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    26
import java.util.List;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    27
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    28
import com.sun.tools.doclint.DocLint;
15370
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    29
import com.sun.tools.doclint.DocLint.BadArgs;
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    30
import java.io.BufferedReader;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    31
import java.io.FileReader;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    32
import java.io.IOException;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    33
import java.io.PrintWriter;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    34
import java.io.Reader;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    35
import java.io.StringWriter;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    36
import java.util.regex.Matcher;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    37
import java.util.regex.Pattern;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    38
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    39
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    40
public class DocLintTester {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    41
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    42
    public static void main(String... args) throws Exception {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    43
        new DocLintTester().run(args);
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    44
    }
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    45
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    46
    public void run(String... args) throws Exception {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    47
        String testSrc = System.getProperty("test.src");
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    48
15370
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    49
        boolean badArgs = false;
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    50
        File refFile = null;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    51
        List<String> opts = new ArrayList<String>();
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    52
        List<File> files = new ArrayList<File>();
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    53
        for (int i = 0; i < args.length; i++) {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    54
            String arg = args[i];
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    55
            if (arg.equals("-ref")) {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    56
                refFile = new File(testSrc, args[++i]);
15370
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    57
            } else if (arg.equals("-badargs")) {
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    58
                badArgs = true;
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    59
            } else if (arg.startsWith("-Xmsgs")) {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    60
                opts.add(arg);
21500
475e59d3b40c 8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents: 19121
diff changeset
    61
            } else if (arg.startsWith("-XcustomTags")) {
475e59d3b40c 8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents: 19121
diff changeset
    62
                opts.add(arg);
15370
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    63
            } else if (arg.startsWith("-")) {
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    64
                opts.add(arg);
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    65
                if (i < args.length - 1 && !args[i+1].startsWith("-"))
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    66
                    opts.add(args[++i]);
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    67
            } else
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    68
                files.add(new File(testSrc, arg));
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    69
        }
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    70
15370
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    71
        check(opts, files, badArgs, refFile);
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    72
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    73
        if (errors > 0)
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    74
            throw new Exception(errors + " errors occurred");
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    75
    }
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    76
15370
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    77
    void check(List<String> opts, List<File> files, boolean expectBadArgs, File refFile) throws Exception {
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    78
        List<String> args = new ArrayList<String>();
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    79
        args.addAll(opts);
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    80
        for (File file: files)
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    81
            args.add(file.getPath());
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    82
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    83
        StringWriter sw = new StringWriter();
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    84
        PrintWriter pw = new PrintWriter(sw);
15370
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    85
        try {
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    86
            new DocLint().run(pw, args.toArray(new String[args.size()]));
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    87
            if (expectBadArgs)
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    88
                error("expected exception not thrown");
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    89
        } catch (BadArgs e) {
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    90
            if (!expectBadArgs)
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    91
                error("unexpected exception caught: " + e);
c1c9a9e11b26 8006263: Supplementary test cases needed for doclint
jjg
parents: 15032
diff changeset
    92
        }
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    93
        pw.flush();
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    94
        String out = normalizeNewlines(removeFileNames(sw.toString())).trim();
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    95
        if (out != null)
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    96
            System.err.println("Output:\n" + out);
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    97
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    98
        if (refFile == null) {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
    99
            if (!out.isEmpty())
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   100
                error("unexpected output");
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   101
        } else {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   102
            String expect = readFile(refFile);
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   103
            if (!expect.equals(out)) {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   104
                error("expected output not found");
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   105
                System.err.println("EXPECT>>" + expect + "<<");
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   106
                System.err.println(" FOUND>>" + out    + "<<");
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   107
            }
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   108
        }
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   109
    }
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   110
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   111
    String readFile(File file) throws IOException {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   112
        StringBuilder sb = new StringBuilder();
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   113
        Reader in = new BufferedReader(new FileReader(file));
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   114
        try {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   115
            char[] buf = new char[1024];
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   116
            int n;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   117
            while ((n = in.read(buf)) != -1)
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   118
                sb.append(buf, 0, n);
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   119
        } finally {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   120
            in.close();
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   121
        }
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   122
        return sb.toString().trim();
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   123
    }
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   124
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   125
    private static final Pattern dirFileLine = Pattern.compile(
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   126
            "(?m)"                          // multi-line mode
15032
985785b3a715 8005195: Doclint regression tests fail on windows
jjg
parents: 14952
diff changeset
   127
            + "^(.*?)"                      // directory part of file name
19121
c626ed0c6ab0 8020664: doclint gives incorrect warnings on normal package statements
jjg
parents: 15370
diff changeset
   128
            + "([-A-Za-z0-9.]+:[0-9]+:)");  // file name and line number
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   129
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   130
    String removeFileNames(String s) {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   131
        Matcher m = dirFileLine.matcher(s);
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   132
        StringBuffer sb = new StringBuffer();
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   133
        while (m.find()) {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   134
            m.appendReplacement(sb, "$2");
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   135
        }
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   136
        m.appendTail(sb);
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   137
        return sb.toString();
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   138
    }
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   139
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   140
    private static final String nl = System.getProperty("line.separator");
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   141
    String normalizeNewlines(String s) {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   142
        return (nl.equals("\n") ? s : s.replace(nl, "\n"));
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   143
    }
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   144
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   145
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   146
    void error(String msg) {
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   147
        System.err.println("Error: " + msg);
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   148
        errors++;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   149
    }
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   150
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   151
    int errors;
d0022ae20516 8004832: Add new doclint package
jjg
parents:
diff changeset
   152
}