Fri, 24 Apr 2020 03:58:51 +0100
Merge
1 /*
2 * Copyright (c) 2013, 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 */
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 */
31 import java.io.IOException;
32 import java.io.PrintWriter;
33 import java.io.StringWriter;
35 public class JavapReturns0AfterClassNotFoundTest {
37 static final String fileNotFoundErrorMsg =
38 "Error: class not found: Unexisting.class";
39 static final String exitCodeClassNotFoundAssertionMsg =
40 "Javap's exit code for class not found should be 1";
41 static final String classNotFoundMsgAssertionMsg =
42 "Javap's error message for class not found is incorrect";
44 public static void main(String args[]) throws Exception {
45 new JavapReturns0AfterClassNotFoundTest().run();
46 }
48 void run() throws IOException {
49 check(exitCodeClassNotFoundAssertionMsg, classNotFoundMsgAssertionMsg,
50 fileNotFoundErrorMsg, "-v", "Unexisting.class");
51 }
53 void check(String exitCodeAssertionMsg, String errMsgAssertionMsg,
54 String expectedErrMsg, String... params) {
55 int result;
56 StringWriter s;
57 String out;
58 try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
59 result = com.sun.tools.javap.Main.run(params, pw);
60 //no line separator, no problem
61 out = s.toString().trim();
62 }
63 if (result != 1) {
64 System.out.println("actual exit code " + result);
65 throw new AssertionError(exitCodeAssertionMsg);
66 }
68 if (!out.equals(expectedErrMsg)) {
69 System.out.println("actual " + out);
70 System.out.println("expected " + expectedErrMsg);
71 throw new AssertionError(errMsgAssertionMsg);
72 }
73 }
75 }