test/tools/doclint/tool/PathsTest.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/doclint/tool/PathsTest.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,136 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8006263
    1.30 + * @summary Supplementary test cases needed for doclint
    1.31 + */
    1.32 +
    1.33 +import com.sun.tools.doclint.DocLint;
    1.34 +import com.sun.tools.doclint.DocLint.BadArgs;
    1.35 +import java.io.File;
    1.36 +import java.io.FileWriter;
    1.37 +import java.io.IOException;
    1.38 +import java.io.PrintWriter;
    1.39 +import java.io.StringWriter;
    1.40 +import java.util.regex.Pattern;
    1.41 +
    1.42 +public class PathsTest {
    1.43 +    public static void main(String... args) throws Exception {
    1.44 +        new PathsTest().run();
    1.45 +    }
    1.46 +
    1.47 +    void run() throws Exception {
    1.48 +        String PS = File.pathSeparator;
    1.49 +        writeFile("src1/p/A.java",
    1.50 +                "package p; public class A { }");
    1.51 +        compile("-d", "classes1", "src1/p/A.java");
    1.52 +
    1.53 +        writeFile("src2/q/B.java",
    1.54 +                "package q; public class B extends p.A { }");
    1.55 +        compile("-d", "classes2", "-classpath", "classes1", "src2/q/B.java");
    1.56 +
    1.57 +        writeFile("src/Test.java",
    1.58 +                "/** &0; */ class Test extends q.B { }");
    1.59 +
    1.60 +        test("src/Test.java", "-sourcepath", "src1" + PS + "src2");
    1.61 +        test("src/Test.java", "-classpath", "classes1" + PS + "classes2");
    1.62 +        String sysBootClassPath = System.getProperty("sun.boot.class.path");
    1.63 +        test("src/Test.java", "-bootclasspath",
    1.64 +                sysBootClassPath + PS + "classes1" + PS + "classes2");
    1.65 +
    1.66 +        if (errors > 0)
    1.67 +            throw new Exception(errors + " errors found");
    1.68 +    }
    1.69 +
    1.70 +    Pattern pkgNotFound = Pattern.compile("package [a-z]+ does not exist");
    1.71 +    Pattern badHtmlEntity = Pattern.compile("bad HTML entity");
    1.72 +
    1.73 +    void test(String file, String pathOpt, String path) throws BadArgs, IOException {
    1.74 +        System.err.println("test " + pathOpt);
    1.75 +        String out1 = doclint("-Xmsgs", file);
    1.76 +        if (!pkgNotFound.matcher(out1).find())
    1.77 +            error("message not found: " + pkgNotFound);
    1.78 +
    1.79 +        String out2 = doclint("-Xmsgs", pathOpt, path, file);
    1.80 +        if (pkgNotFound.matcher(out2).find())
    1.81 +            error("unexpected message found: " + pkgNotFound);
    1.82 +        if (!badHtmlEntity.matcher(out1).find())
    1.83 +            error("message not found: " + badHtmlEntity);
    1.84 +
    1.85 +        try {
    1.86 +            doclint("-Xmsgs", pathOpt);
    1.87 +            error("expected exception not thrown");
    1.88 +        } catch (BadArgs e) {
    1.89 +            System.err.println(e);
    1.90 +        }
    1.91 +    }
    1.92 +
    1.93 +    void compile(String... args) {
    1.94 +        for (int i = 0; i < args.length; i++) {
    1.95 +            if (args[i].equals("-d")) {
    1.96 +                new File(args[++i]).mkdirs();
    1.97 +                break;
    1.98 +            }
    1.99 +        }
   1.100 +
   1.101 +        StringWriter sw = new StringWriter();
   1.102 +        PrintWriter pw = new PrintWriter(sw);
   1.103 +        int rc = com.sun.tools.javac.Main.compile(args, pw);
   1.104 +        pw.close();
   1.105 +        String out = sw.toString();
   1.106 +        if (!out.isEmpty())
   1.107 +            System.err.println(out);
   1.108 +        if (rc != 0)
   1.109 +            error("compilation failed: rc=" + rc);
   1.110 +    }
   1.111 +
   1.112 +    String doclint(String... args) throws BadArgs, IOException {
   1.113 +        StringWriter sw = new StringWriter();
   1.114 +        PrintWriter pw = new PrintWriter(sw);
   1.115 +        DocLint dl = new DocLint();
   1.116 +        dl.run(pw, args);
   1.117 +        pw.close();
   1.118 +        String out = sw.toString();
   1.119 +        if (!out.isEmpty())
   1.120 +            System.err.println(out);
   1.121 +        return out;
   1.122 +    }
   1.123 +
   1.124 +    File writeFile(String path, String body) throws IOException {
   1.125 +        File f = new File(path);
   1.126 +        f.getParentFile().mkdirs();
   1.127 +        try (FileWriter fw = new FileWriter(path)) {
   1.128 +            fw.write(body);
   1.129 +        }
   1.130 +        return f;
   1.131 +    }
   1.132 +
   1.133 +    void error(String msg) {
   1.134 +        System.err.println("Error: " + msg);
   1.135 +        errors++;
   1.136 +    }
   1.137 +
   1.138 +    int errors;
   1.139 +}

mercurial