JavapTaskCtorFailWithNPE.java revision 2942:08092deced3f
172165Sphantom/*
287658Sphantom * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
372165Sphantom * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
472165Sphantom *
5227753Stheraven * This code is free software; you can redistribute it and/or modify it
6227753Stheraven * under the terms of the GNU General Public License version 2 only, as
7227753Stheraven * published by the Free Software Foundation.
8227753Stheraven *
9227753Stheraven * This code is distributed in the hope that it will be useful, but WITHOUT
1072165Sphantom * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1172165Sphantom * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1272165Sphantom * version 2 for more details (a copy is included in the LICENSE file that
1372165Sphantom * accompanied this code).
1472165Sphantom *
1572165Sphantom * You should have received a copy of the GNU General Public License version
1672165Sphantom * 2 along with this work; if not, write to the Free Software Foundation,
1772165Sphantom * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1872165Sphantom *
1972165Sphantom * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2072165Sphantom * or visit www.oracle.com if you need additional information or have any
2172165Sphantom * questions.
2272165Sphantom */
2372165Sphantom
2472165Sphantom/*
2572165Sphantom * @test
2672165Sphantom * @bug 8006334
2772165Sphantom * @summary javap: JavapTask constructor breaks with null pointer exception if
2872165Sphantom * parameter options is null
2972165Sphantom * @modules jdk.jdeps
3072165Sphantom */
3172165Sphantom
3292986Sobrienimport java.io.File;
3392986Sobrienimport java.util.Arrays;
3492986Sobrienimport java.io.PrintWriter;
3572321Sphantomimport java.io.StringWriter;
36104711Stjrimport java.util.List;
3789907Sacheimport java.util.Locale;
38116875Sphantomimport javax.tools.Diagnostic;
39116875Sphantomimport javax.tools.DiagnosticCollector;
4072165Sphantomimport javax.tools.JavaFileManager;
4172165Sphantomimport javax.tools.JavaFileObject;
4272321Sphantomimport com.sun.tools.javap.JavapFileManager;
4372165Sphantomimport com.sun.tools.javap.JavapTask;
44104982Sache
45104982Sachepublic class JavapTaskCtorFailWithNPE {
46104982Sache
47104982Sache    //we will also check the output just to confirm that we get the expected one
4872165Sphantom    private static final String expOutput =
4972165Sphantom        "Compiled from \"JavapTaskCtorFailWithNPE.java\"\n" +
5087658Sphantom        "public class JavapTaskCtorFailWithNPE {\n" +
5172165Sphantom        "  public JavapTaskCtorFailWithNPE();\n" +
5272165Sphantom        "  public static void main(java.lang.String[]);\n" +
5387658Sphantom        "}\n";
5487658Sphantom
5587658Sphantom    public static void main(String[] args) {
5687658Sphantom        new JavapTaskCtorFailWithNPE().run();
5787658Sphantom    }
5887658Sphantom
5987658Sphantom    private void run() {
6087658Sphantom        File classToCheck = new File(System.getProperty("test.classes"),
6187658Sphantom            getClass().getSimpleName() + ".class");
6287658Sphantom
6387658Sphantom        DiagnosticCollector<JavaFileObject> dc =
6487658Sphantom                new DiagnosticCollector<JavaFileObject>();
6587658Sphantom        StringWriter sw = new StringWriter();
6687658Sphantom        PrintWriter pw = new PrintWriter(sw);
67104711Stjr        JavaFileManager fm = JavapFileManager.create(dc, pw);
68104711Stjr        JavapTask t = new JavapTask(pw, fm, dc, null,
69104711Stjr                Arrays.asList(classToCheck.getPath()));
70104711Stjr        if (t.run() != 0)
71104711Stjr            throw new Error("javap failed unexpectedly");
72104711Stjr
73104711Stjr        List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
7472165Sphantom        for (Diagnostic<? extends JavaFileObject> d: diags) {
7572165Sphantom            if (d.getKind() == Diagnostic.Kind.ERROR)
76227753Stheraven                throw new AssertionError(d.getMessage(Locale.ENGLISH));
7772165Sphantom        }
7889907Sache        String lineSep = System.getProperty("line.separator");
79101498Sache        String out = sw.toString().replace(lineSep, "\n");
80101470Sache        if (!out.equals(expOutput)) {
8189907Sache            throw new AssertionError("The output is not equal to the one expected");
82101470Sache        }
8389907Sache    }
8489907Sache
85101470Sache}
8689907Sache