JavapReturns0AfterClassNotFoundTest.java revision 2942:08092deced3f
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 8007907
27 * @summary javap, method com.sun.tools.javap.Main.run returns 0 even in case
28 * of class not found error
29 * @modules jdk.jdeps
30 */
31
32import java.io.IOException;
33import java.io.PrintWriter;
34import java.io.StringWriter;
35
36public class JavapReturns0AfterClassNotFoundTest {
37
38    static final String fileNotFoundErrorMsg =
39            "Error: class not found: Unexisting.class";
40    static final String exitCodeClassNotFoundAssertionMsg =
41            "Javap's exit code for class not found should be 1";
42    static final String classNotFoundMsgAssertionMsg =
43            "Javap's error message for class not found is incorrect";
44
45    public static void main(String args[]) throws Exception {
46        new JavapReturns0AfterClassNotFoundTest().run();
47    }
48
49    void run() throws IOException {
50        check(exitCodeClassNotFoundAssertionMsg, classNotFoundMsgAssertionMsg,
51                fileNotFoundErrorMsg, "-v", "Unexisting.class");
52    }
53
54    void check(String exitCodeAssertionMsg, String errMsgAssertionMsg,
55            String expectedErrMsg, String... params) {
56        int result;
57        StringWriter s;
58        String out;
59        try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
60            result = com.sun.tools.javap.Main.run(params, pw);
61            //no line separator, no problem
62            out = s.toString().trim();
63        }
64        if (result != 1) {
65            System.out.println("actual exit code " + result);
66            throw new AssertionError(exitCodeAssertionMsg);
67        }
68
69        if (!out.equals(expectedErrMsg)) {
70            System.out.println("actual " + out);
71            System.out.println("expected " + expectedErrMsg);
72            throw new AssertionError(errMsgAssertionMsg);
73        }
74    }
75
76}
77