test/tools/doclint/tool/PathsTest.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

     1 /*
     2  * Copyright (c) 2013, 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 /*
    25  * @test
    26  * @bug 8006263
    27  * @summary Supplementary test cases needed for doclint
    28  */
    30 import com.sun.tools.doclint.DocLint;
    31 import com.sun.tools.doclint.DocLint.BadArgs;
    32 import java.io.File;
    33 import java.io.FileWriter;
    34 import java.io.IOException;
    35 import java.io.PrintWriter;
    36 import java.io.StringWriter;
    37 import java.util.regex.Pattern;
    39 public class PathsTest {
    40     public static void main(String... args) throws Exception {
    41         new PathsTest().run();
    42     }
    44     void run() throws Exception {
    45         String PS = File.pathSeparator;
    46         writeFile("src1/p/A.java",
    47                 "package p; public class A { }");
    48         compile("-d", "classes1", "src1/p/A.java");
    50         writeFile("src2/q/B.java",
    51                 "package q; public class B extends p.A { }");
    52         compile("-d", "classes2", "-classpath", "classes1", "src2/q/B.java");
    54         writeFile("src/Test.java",
    55                 "/** &0; */ class Test extends q.B { }");
    57         test("src/Test.java", "-sourcepath", "src1" + PS + "src2");
    58         test("src/Test.java", "-classpath", "classes1" + PS + "classes2");
    59         String sysBootClassPath = System.getProperty("sun.boot.class.path");
    60         test("src/Test.java", "-bootclasspath",
    61                 sysBootClassPath + PS + "classes1" + PS + "classes2");
    63         if (errors > 0)
    64             throw new Exception(errors + " errors found");
    65     }
    67     Pattern pkgNotFound = Pattern.compile("package [a-z]+ does not exist");
    68     Pattern badHtmlEntity = Pattern.compile("bad HTML entity");
    70     void test(String file, String pathOpt, String path) throws BadArgs, IOException {
    71         System.err.println("test " + pathOpt);
    72         String out1 = doclint("-Xmsgs", file);
    73         if (!pkgNotFound.matcher(out1).find())
    74             error("message not found: " + pkgNotFound);
    76         String out2 = doclint("-Xmsgs", pathOpt, path, file);
    77         if (pkgNotFound.matcher(out2).find())
    78             error("unexpected message found: " + pkgNotFound);
    79         if (!badHtmlEntity.matcher(out1).find())
    80             error("message not found: " + badHtmlEntity);
    82         try {
    83             doclint("-Xmsgs", pathOpt);
    84             error("expected exception not thrown");
    85         } catch (BadArgs e) {
    86             System.err.println(e);
    87         }
    88     }
    90     void compile(String... args) {
    91         for (int i = 0; i < args.length; i++) {
    92             if (args[i].equals("-d")) {
    93                 new File(args[++i]).mkdirs();
    94                 break;
    95             }
    96         }
    98         StringWriter sw = new StringWriter();
    99         PrintWriter pw = new PrintWriter(sw);
   100         int rc = com.sun.tools.javac.Main.compile(args, pw);
   101         pw.close();
   102         String out = sw.toString();
   103         if (!out.isEmpty())
   104             System.err.println(out);
   105         if (rc != 0)
   106             error("compilation failed: rc=" + rc);
   107     }
   109     String doclint(String... args) throws BadArgs, IOException {
   110         StringWriter sw = new StringWriter();
   111         PrintWriter pw = new PrintWriter(sw);
   112         DocLint dl = new DocLint();
   113         dl.run(pw, args);
   114         pw.close();
   115         String out = sw.toString();
   116         if (!out.isEmpty())
   117             System.err.println(out);
   118         return out;
   119     }
   121     File writeFile(String path, String body) throws IOException {
   122         File f = new File(path);
   123         f.getParentFile().mkdirs();
   124         try (FileWriter fw = new FileWriter(path)) {
   125             fw.write(body);
   126         }
   127         return f;
   128     }
   130     void error(String msg) {
   131         System.err.println("Error: " + msg);
   132         errors++;
   133     }
   135     int errors;
   136 }

mercurial