test/tools/javap/8006334/JavapTaskCtorFailWithNPE.java

changeset 1561
073696f59241
child 1819
7fe655cad9b1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javap/8006334/JavapTaskCtorFailWithNPE.java	Tue Feb 12 13:36:56 2013 +0000
     1.3 @@ -0,0 +1,85 @@
     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 8006334
    1.30 + * @summary javap: JavapTask constructor breaks with null pointer exception if
    1.31 + * parameter options is null
    1.32 + */
    1.33 +
    1.34 +import java.io.File;
    1.35 +import java.util.Arrays;
    1.36 +import java.io.PrintWriter;
    1.37 +import java.io.StringWriter;
    1.38 +import java.util.List;
    1.39 +import java.util.Locale;
    1.40 +import javax.tools.Diagnostic;
    1.41 +import javax.tools.DiagnosticCollector;
    1.42 +import javax.tools.JavaFileManager;
    1.43 +import javax.tools.JavaFileObject;
    1.44 +import com.sun.tools.javap.JavapFileManager;
    1.45 +import com.sun.tools.javap.JavapTask;
    1.46 +
    1.47 +public class JavapTaskCtorFailWithNPE {
    1.48 +
    1.49 +    //we will also check the output just to confirm that we get the expected one
    1.50 +    private static final String expOutput =
    1.51 +        "Compiled from \"JavapTaskCtorFailWithNPE.java\"\n" +
    1.52 +        "public class JavapTaskCtorFailWithNPE {\n" +
    1.53 +        "  public JavapTaskCtorFailWithNPE();\n" +
    1.54 +        "  public static void main(java.lang.String[]);\n" +
    1.55 +        "}\n";
    1.56 +
    1.57 +    public static void main(String[] args) {
    1.58 +        new JavapTaskCtorFailWithNPE().run();
    1.59 +    }
    1.60 +
    1.61 +    private void run() {
    1.62 +        File classToCheck = new File(System.getProperty("test.classes"),
    1.63 +            getClass().getSimpleName() + ".class");
    1.64 +
    1.65 +        DiagnosticCollector<JavaFileObject> dc =
    1.66 +                new DiagnosticCollector<JavaFileObject>();
    1.67 +        StringWriter sw = new StringWriter();
    1.68 +        PrintWriter pw = new PrintWriter(sw);
    1.69 +        JavaFileManager fm = JavapFileManager.create(dc, pw);
    1.70 +        JavapTask t = new JavapTask(pw, fm, dc, null,
    1.71 +                Arrays.asList(classToCheck.getPath()));
    1.72 +        boolean ok = t.run();
    1.73 +        if (!ok)
    1.74 +            throw new Error("javap failed unexpectedly");
    1.75 +
    1.76 +        List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
    1.77 +        for (Diagnostic<? extends JavaFileObject> d: diags) {
    1.78 +            if (d.getKind() == Diagnostic.Kind.ERROR)
    1.79 +                throw new AssertionError(d.getMessage(Locale.ENGLISH));
    1.80 +        }
    1.81 +        String lineSep = System.getProperty("line.separator");
    1.82 +        String out = sw.toString().replace(lineSep, "\n");
    1.83 +        if (!out.equals(expOutput)) {
    1.84 +            throw new AssertionError("The output is not equal to the one expected");
    1.85 +        }
    1.86 +    }
    1.87 +
    1.88 +}

mercurial