PathsTest.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2013, 2015, 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 */
23
24/*
25 * @test
26 * @bug 8006263
27 * @summary Supplementary test cases needed for doclint
28 * @library /tools/lib
29 * @modules jdk.compiler/com.sun.tools.doclint
30 *          jdk.compiler/com.sun.tools.javac.api
31 *          jdk.compiler/com.sun.tools.javac.file
32 *          jdk.compiler/com.sun.tools.javac.main
33 *          jdk.compiler/com.sun.tools.javac.util
34 *          jdk.jdeps/com.sun.tools.javap
35 * @build ToolBox
36 * @run main PathsTest
37 */
38
39import com.sun.tools.javac.util.Context;
40import com.sun.tools.javac.file.JavacFileManager;
41import com.sun.tools.doclint.DocLint;
42import com.sun.tools.doclint.DocLint.BadArgs;
43import java.io.File;
44import java.io.FileWriter;
45import java.io.IOException;
46import java.io.PrintWriter;
47import java.io.StringWriter;
48import java.util.regex.Pattern;
49import javax.tools.StandardLocation;
50import javax.tools.JavaFileManager;
51
52public class PathsTest {
53    public static void main(String... args) throws Exception {
54        new PathsTest().run();
55    }
56
57    void run() throws Exception {
58        String PS = File.pathSeparator;
59        writeFile("src1/p/A.java",
60                "package p; public class A { }");
61        compile("-d", "classes1", "src1/p/A.java");
62
63        writeFile("src2/q/B.java",
64                "package q; public class B extends p.A { }");
65        compile("-d", "classes2", "-classpath", "classes1", "src2/q/B.java");
66
67        writeFile("src/Test.java",
68                "/** &0; */ class Test extends q.B { }");
69
70        test("src/Test.java", "-sourcepath", "src1" + PS + "src2");
71        test("src/Test.java", "-classpath", "classes1" + PS + "classes2");
72
73        File testJar = createJar();
74        test("src/Test.java", "-bootclasspath",
75                testJar + PS + "classes1" + PS + "classes2");
76
77        if (errors > 0)
78            throw new Exception(errors + " errors found");
79    }
80
81    Pattern pkgNotFound = Pattern.compile("package [a-z]+ does not exist");
82    Pattern badHtmlEntity = Pattern.compile("bad HTML entity");
83
84    void test(String file, String pathOpt, String path) throws BadArgs, IOException {
85        System.err.println("test " + pathOpt);
86        String out1 = doclint("-Xmsgs", file);
87        if (!pkgNotFound.matcher(out1).find())
88            error("message not found: " + pkgNotFound);
89
90        String out2;
91        if (needTarget8(pathOpt)) {
92            out2 = doclint("-Xmsgs", "-source", "8", "-target", "8", pathOpt, path, file);
93        } else {
94            out2 = doclint("-Xmsgs", pathOpt, path, file);
95        }
96        if (pkgNotFound.matcher(out2).find())
97            error("unexpected message found: " + pkgNotFound);
98        if (!badHtmlEntity.matcher(out1).find())
99            error("message not found: " + badHtmlEntity);
100
101        try {
102            doclint("-Xmsgs", pathOpt);
103            error("expected exception not thrown");
104        } catch (BadArgs e) {
105            System.err.println(e);
106        }
107    }
108
109    boolean needTarget8(String opt) {
110        switch (opt) {
111            case "-bootclasspath":
112                return true;
113            default:
114                return false;
115        }
116    }
117
118    File createJar() throws IOException {
119        File f = new File("test.jar");
120        try (JavaFileManager fm = new JavacFileManager(new Context(), false, null)) {
121            ToolBox tb = new ToolBox();
122            tb.new JarTask(f.getPath())
123                .files(fm, StandardLocation.PLATFORM_CLASS_PATH, "java.lang.*")
124                .run();
125        }
126        return f;
127    }
128
129    void compile(String... args) {
130        for (int i = 0; i < args.length; i++) {
131            if (args[i].equals("-d")) {
132                new File(args[++i]).mkdirs();
133                break;
134            }
135        }
136
137        StringWriter sw = new StringWriter();
138        PrintWriter pw = new PrintWriter(sw);
139        int rc = com.sun.tools.javac.Main.compile(args, pw);
140        pw.close();
141        String out = sw.toString();
142        if (!out.isEmpty())
143            System.err.println(out);
144        if (rc != 0)
145            error("compilation failed: rc=" + rc);
146    }
147
148    String doclint(String... args) throws BadArgs, IOException {
149        StringWriter sw = new StringWriter();
150        PrintWriter pw = new PrintWriter(sw);
151        DocLint dl = new DocLint();
152        dl.run(pw, args);
153        pw.close();
154        String out = sw.toString();
155        if (!out.isEmpty())
156            System.err.println(out);
157        return out;
158    }
159
160    File writeFile(String path, String body) throws IOException {
161        File f = new File(path);
162        f.getParentFile().mkdirs();
163        try (FileWriter fw = new FileWriter(path)) {
164            fw.write(body);
165        }
166        return f;
167    }
168
169    void error(String msg) {
170        System.err.println("Error: " + msg);
171        errors++;
172    }
173
174    int errors;
175}
176