JavapTaskCtorFailWithNPE.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2013, 2016, 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 8006334
27 * @summary javap: JavapTask constructor breaks with null pointer exception if
28 * parameter options is null
29 * @modules jdk.jdeps/com.sun.tools.javap
30 */
31
32import java.io.File;
33import java.util.Arrays;
34import java.io.PrintWriter;
35import java.io.StringWriter;
36import java.util.List;
37import java.util.Locale;
38import javax.tools.Diagnostic;
39import javax.tools.DiagnosticCollector;
40import javax.tools.JavaFileManager;
41import javax.tools.JavaFileObject;
42import com.sun.tools.javap.JavapFileManager;
43import com.sun.tools.javap.JavapTask;
44
45public class JavapTaskCtorFailWithNPE {
46
47    //we will also check the output just to confirm that we get the expected one
48    private static final String expOutput =
49        "Compiled from \"JavapTaskCtorFailWithNPE.java\"\n" +
50        "public class JavapTaskCtorFailWithNPE {\n" +
51        "  public JavapTaskCtorFailWithNPE();\n" +
52        "  public static void main(java.lang.String[]);\n" +
53        "}\n";
54
55    public static void main(String[] args) {
56        new JavapTaskCtorFailWithNPE().run();
57    }
58
59    private void run() {
60        File classToCheck = new File(System.getProperty("test.classes"),
61            getClass().getSimpleName() + ".class");
62
63        DiagnosticCollector<JavaFileObject> dc =
64                new DiagnosticCollector<JavaFileObject>();
65        StringWriter sw = new StringWriter();
66        PrintWriter pw = new PrintWriter(sw);
67        JavaFileManager fm = JavapFileManager.create(dc, pw);
68        JavapTask t = new JavapTask(pw, fm, dc, null,
69                Arrays.asList(classToCheck.getPath()));
70        if (t.run() != 0)
71            throw new Error("javap failed unexpectedly");
72
73        List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
74        for (Diagnostic<? extends JavaFileObject> d: diags) {
75            if (d.getKind() == Diagnostic.Kind.ERROR)
76                throw new AssertionError(d.getMessage(Locale.ENGLISH));
77        }
78        String lineSep = System.getProperty("line.separator");
79        String out = sw.toString().replace(lineSep, "\n");
80        if (!out.equals(expOutput)) {
81            throw new AssertionError("The output is not equal to the one expected");
82        }
83    }
84
85}
86