T6729471.java revision 2721:f7ce2cfa4cdb
1/*
2 * Copyright (c) 2009, 2014, 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/*
26 * @test
27 * @bug 6729471
28 * @summary javap does not output inner interfaces of an interface
29 * @library /tools/lib
30 * @build ToolBox
31 * @run main T6729471
32 */
33
34import java.io.*;
35import java.net.*;
36import java.util.*;
37import javax.tools.*;
38
39public class T6729471
40{
41    public static void main(String... args) throws IOException {
42        new T6729471().run();
43    }
44
45    void run() throws IOException {
46        File testClasses = new File(System.getProperty("test.classes"));
47
48        // simple class
49        verify("java.util.Map",
50                "public abstract boolean containsKey(java.lang.Object)");
51
52        // inner class
53        verify("java.util.Map.Entry",
54                "public abstract K getKey()");
55
56        // file name
57        verify(new File(testClasses, "T6729471.class").getPath(),
58                "public static void main(java.lang.String...)");
59
60        // file url
61        verify(new File(testClasses, "T6729471.class").toURI().toString(),
62                "public static void main(java.lang.String...)");
63
64        // jar url
65        File testJar = createJar("test.jar", "java.util.*");
66        try {
67            verify("jar:" + testJar.toURL() + "!/java/util/Map.class",
68                "public abstract boolean containsKey(java.lang.Object)");
69        } catch (MalformedURLException e) {
70            error(e.toString());
71        }
72
73        if (errors > 0)
74            throw new Error(errors + " found.");
75    }
76
77    File createJar(String name, String... paths) throws IOException {
78        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
79        try (JavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
80            File f = new File(name);
81            ToolBox tb = new ToolBox();
82            tb.new JarTask(f.getPath())
83                .files(fm, StandardLocation.PLATFORM_CLASS_PATH, paths)
84                .run();
85            return f;
86        }
87    }
88
89    void verify(String className, String... expects) {
90        String output = javap(className);
91        for (String expect: expects) {
92            if (output.indexOf(expect)< 0)
93                error(expect + " not found");
94        }
95    }
96
97    void error(String msg) {
98        System.err.println(msg);
99        errors++;
100    }
101
102    int errors;
103
104    String javap(String className) {
105        String testClasses = System.getProperty("test.classes", ".");
106        StringWriter sw = new StringWriter();
107        PrintWriter out = new PrintWriter(sw);
108        String[] args = { "-classpath", testClasses, className };
109        int rc = com.sun.tools.javap.Main.run(args, out);
110        out.close();
111        String output = sw.toString();
112        System.out.println("class " + className);
113        System.out.println(output);
114        if (rc != 0)
115            throw new Error("javap failed. rc=" + rc);
116        if (output.indexOf("Error:") != -1)
117            throw new Error("javap reported error.");
118        return output;
119    }
120}
121
122