test/tools/doclint/DocLintTester.java

Mon, 14 Jan 2013 19:52:36 +0100

author
jfranck
date
Mon, 14 Jan 2013 19:52:36 +0100
changeset 1491
9f42a06a49c0
parent 1474
383bc0fbd759
child 1506
4a3cfc970c6f
permissions
-rw-r--r--

7193719: Support repeating annotations in javax.lang.model
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 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  */
    24 import java.io.File;
    25 import java.util.ArrayList;
    26 import java.util.List;
    28 import com.sun.tools.doclint.DocLint;
    29 import java.io.BufferedReader;
    30 import java.io.FileReader;
    31 import java.io.IOException;
    32 import java.io.PrintWriter;
    33 import java.io.Reader;
    34 import java.io.StringWriter;
    35 import java.util.regex.Matcher;
    36 import java.util.regex.Pattern;
    39 public class DocLintTester {
    41     public static void main(String... args) throws Exception {
    42         new DocLintTester().run(args);
    43     }
    45     public void run(String... args) throws Exception {
    46         String testSrc = System.getProperty("test.src");
    48         File refFile = null;
    49         List<String> opts = new ArrayList<String>();
    50         List<File> files = new ArrayList<File>();
    51         for (int i = 0; i < args.length; i++) {
    52             String arg = args[i];
    53             if (arg.equals("-ref")) {
    54                 refFile = new File(testSrc, args[++i]);
    55             } else if (arg.startsWith("-Xmsgs")) {
    56                 opts.add(arg);
    57             } else
    58                 files.add(new File(testSrc, arg));
    59         }
    61         check(opts, files, refFile);
    63         if (errors > 0)
    64             throw new Exception(errors + " errors occurred");
    65     }
    67     void check(List<String> opts, List<File> files, File refFile) throws Exception {
    68         List<String> args = new ArrayList<String>();
    69         args.addAll(opts);
    70         for (File file: files)
    71             args.add(file.getPath());
    73         StringWriter sw = new StringWriter();
    74         PrintWriter pw = new PrintWriter(sw);
    75         new DocLint().run(pw, args.toArray(new String[args.size()]));
    76         pw.flush();
    77         String out = normalizeNewlines(removeFileNames(sw.toString())).trim();
    78         if (out != null)
    79             System.err.println("Output:\n" + out);
    81         if (refFile == null) {
    82             if (!out.isEmpty())
    83                 error("unexpected output");
    84         } else {
    85             String expect = readFile(refFile);
    86             if (!expect.equals(out)) {
    87                 error("expected output not found");
    88                 System.err.println("EXPECT>>" + expect + "<<");
    89                 System.err.println(" FOUND>>" + out    + "<<");
    90             }
    91         }
    92     }
    94     String readFile(File file) throws IOException {
    95         StringBuilder sb = new StringBuilder();
    96         Reader in = new BufferedReader(new FileReader(file));
    97         try {
    98             char[] buf = new char[1024];
    99             int n;
   100             while ((n = in.read(buf)) != -1)
   101                 sb.append(buf, 0, n);
   102         } finally {
   103             in.close();
   104         }
   105         return sb.toString().trim();
   106     }
   108     private static final Pattern dirFileLine = Pattern.compile(
   109             "(?m)"                          // multi-line mode
   110             + "^(.*?)"                      // directory part of file name
   111             + "([A-Za-z0-9.]+:[0-9]+:)");   // file name and line number
   113     String removeFileNames(String s) {
   114         Matcher m = dirFileLine.matcher(s);
   115         StringBuffer sb = new StringBuffer();
   116         while (m.find()) {
   117             m.appendReplacement(sb, "$2");
   118         }
   119         m.appendTail(sb);
   120         return sb.toString();
   121     }
   123     private static final String nl = System.getProperty("line.separator");
   124     String normalizeNewlines(String s) {
   125         return (nl.equals("\n") ? s : s.replace(nl, "\n"));
   126     }
   129     void error(String msg) {
   130         System.err.println("Error: " + msg);
   131         errors++;
   132     }
   134     int errors;
   135 }

mercurial