1 /*
2  * Copyright (c) 2000, 2018, 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 package nsk.jdi.ReferenceType.equals;
25 
26 import nsk.share.*;
27 import nsk.share.jpda.*;
28 import nsk.share.jdi.*;
29 
30 import com.sun.jdi.*;
31 import java.util.*;
32 import java.io.*;
33 
34 /**
35  * This test checks the method <code>equals()</code>
36  * of the JDI interface <code>ReferenceType</code> of com.sun.jdi package
37  */
38 
39 public class equals002 {
40     static ArgumentHandler argsHandler;
41     static Log test_log_handler;
42     static boolean verbose_mode = false;  // test argument -verbose switches to true
43                                           // - for more easy failure evaluation
44 
45     /** The main class names of the debugger & debugee applications. */
46     private final static String
47         package_prefix = "nsk.jdi.ReferenceType.equals.",
48         thisClassName = package_prefix + "equals002",
49         debugeeName   = thisClassName + "a";
50 
51     /** Debugee's class for check **/
52     private final static String checked_class = package_prefix + "equals002b";
53 
54 
55     /**
56      * Re-call to <code>run(args,out)</code>, and exit with
57      * either status 95 or 97 (JCK-like exit status).
58      */
main(String argv[])59     public static void main (String argv[]) {
60         int exitCode = run(argv,System.out);
61         System.exit(exitCode + 95/*STATUS_TEMP*/);
62     }
63 
64     /**
65      * JCK-like entry point to the test: perform testing, and
66      * return exit code 0 (PASSED) or either 2 (FAILED).
67      */
run(String argv[], PrintStream out)68     public static int run (String argv[], PrintStream out) {
69 
70         int v_test_result = new equals002().runThis(argv,out);
71         if ( v_test_result == 2/*STATUS_FAILED*/ ) {
72             print_log_anyway
73                 ("\n==> nsk/jdi/ReferenceType/equals/equals002 test FAILED");
74         }
75         else {
76             print_log_anyway
77                 ("\n==> nsk/jdi/ReferenceType/equals/equals002 test PASSED");
78         }
79         return v_test_result;
80     }
81 
print_log_on_verbose(String message)82     private static void print_log_on_verbose(String message) {
83         test_log_handler.display(message);
84     }
85 
print_log_without_verbose(String message)86     private static void print_log_without_verbose(String message) {
87         test_log_handler.comment(message);
88     }
89 
print_log_anyway(String message)90     private static void print_log_anyway(String message) {
91         test_log_handler.println(message);
92     }
93 
94     /**
95      * Non-static variant of the method <code>run(args,out)</code>
96      */
runThis(String argv[], PrintStream out)97     private int runThis (String argv[], PrintStream out) {
98         argsHandler = new ArgumentHandler(argv);
99         verbose_mode = argsHandler.verbose();
100         argv = argsHandler.getArguments();
101         test_log_handler = new Log(out, argsHandler);
102 
103         print_log_anyway("==> nsk/jdi/ReferenceType/equals/equals002 test LOG:");
104         print_log_anyway("--> test checks equals() method of ReferenceType interface ");
105         print_log_anyway("    of the com.sun.jdi package for UNLOADED class\n");
106 
107         String debugee_launch_command = debugeeName;
108 
109         Binder binder = new Binder(argsHandler,test_log_handler);
110         Debugee debugee = binder.bindToDebugee(debugee_launch_command);
111         IOPipe pipe = debugee.createIOPipe();
112 
113         debugee.redirectStderr(out);
114         print_log_on_verbose("--> equals002: equals002a debugee launched");
115         debugee.resume();
116 
117         String debugee_signal = pipe.readln();
118         if (debugee_signal == null) {
119             print_log_anyway
120                 ("##> equals002: UNEXPECTED debugee's signal (not \"ready0\") - " + debugee_signal);
121             return 2/*STATUS_FAILED*/;
122         }
123         if (!debugee_signal.equals("ready0")) {
124             print_log_anyway
125                 ("##> equals002: UNEXPECTED debugee's signal (not \"ready0\") - " + debugee_signal);
126             return 2/*STATUS_FAILED*/;
127         }
128         else {
129             print_log_on_verbose("--> equals002: debugee's \"ready0\" signal recieved!");
130         }
131 
132         //  pass to debugee checked_class_dir...
133         debugee_signal = pipe.readln();
134         if (debugee_signal == null) {
135             print_log_anyway
136                 ("##> equals002: UNEXPECTED debugee's signal (not \"ready1\") - " + debugee_signal);
137             return 2/*STATUS_FAILED*/;
138         }
139         if (!debugee_signal.equals("ready1")) {
140             print_log_anyway
141                 ("##> equals002: UNEXPECTED debugee's signal (not \"ready1\") - " + debugee_signal);
142             return 2/*STATUS_FAILED*/;
143         }
144         else {
145             print_log_on_verbose("--> equals002: debugee's \"ready1\" signal recieved!");
146         }
147 
148         boolean class_not_found_error = false;
149         boolean equals_method_error = false;
150         while ( true ) {
151             print_log_on_verbose
152                 ("--> equals002: getting ReferenceType object for loaded checked class...");
153             ReferenceType refType = debugee.classByName(checked_class);
154             if (refType == null) {
155                 print_log_without_verbose
156                     ("--> equals002: getting ReferenceType object for loaded checked class...");
157                 print_log_anyway("##> equals002: FAILED: Could NOT FIND checked class: " + checked_class);
158                 class_not_found_error = true;
159                 break;
160             }
161             else {
162                 print_log_on_verbose("--> equals002: checked class FOUND: " + checked_class);
163             }
164             print_log_on_verbose
165                 ("--> equals002: waiting for \"ready2\" or \"not_unloaded\" signal from debugee...");
166             pipe.println("continue");
167             debugee_signal = pipe.readln();
168             if (debugee_signal == null) {
169                 print_log_anyway
170                     ("##> equals002: UNEXPECTED debugee's signal - " + debugee_signal);
171                 return 2/*STATUS_FAILED*/;
172             }
173             if ( debugee_signal.equals("not_unloaded")) {
174                 print_log_anyway
175                     ("--> equals002: debugee's \"not_unloaded\" signal recieved!");
176                 print_log_without_verbose
177                     ("-->            checked class may be NOT unloaded!");
178                 print_log_anyway
179                     ("-->            ReferenceType.equals() method can NOT be checked!");
180                 break;
181             }
182             if (!debugee_signal.equals("ready2")) {
183                 print_log_anyway
184                     ("##> equals002: UNEXPECTED debugee's signal (not \"ready2\") - " + debugee_signal);
185                 return 2/*STATUS_FAILED*/;
186             }
187             else {
188                 print_log_on_verbose("--> equals002: debugee's \"ready2\" signal recieved!");
189             }
190             print_log_on_verbose
191                 ("--> equals002: check that checked class has been unloaded realy...");
192             ReferenceType refType2 = debugee.classByName(checked_class);
193             if (refType2 == null) {
194                 print_log_on_verbose
195                     ("--> equals002: checked class has been unloaded realy: " + checked_class);
196             }
197             else {
198                 print_log_without_verbose
199                     ("--> equals002: check that checked class has been unloaded realy...");
200                 print_log_anyway
201                     ("--> equals002: checked class FOUND: " + checked_class
202                     + " => it has NOT been unloaded!");
203                 print_log_anyway
204                     ("-->            ReferenceType.equals() method can NOT be checked!");
205                 break;
206             }
207 
208             print_log_anyway
209                 ("--> equals002: check ReferenceType.equals() method for unloaded class...");
210             boolean equals_sign = false;
211             try {
212                 equals_sign = refType.equals(refType);
213             }
214             catch (Exception expt) {
215                 if (expt instanceof com.sun.jdi.ObjectCollectedException) {
216                     print_log_anyway
217                         ("--> equals002: PASSED: expected Exception thrown - " + expt.toString());
218                 }
219                 else {
220                     print_log_anyway
221                         ("##> equals002: FAILED: unexpected Exception thrown - " + expt.toString());
222                     print_log_anyway
223                         ("##>            expected Exception - com.sun.jdi.ObjectCollectedException");
224                     equals_method_error = true;
225                 }
226             }
227             break;
228         }
229         int v_test_result = 0/*STATUS_PASSED*/;
230         if ( class_not_found_error || equals_method_error ) {
231             v_test_result = 2/*STATUS_FAILED*/;
232         }
233 
234         print_log_on_verbose("--> equals002: waiting for debugee finish...");
235         pipe.println("quit");
236         debugee.waitFor();
237 
238         int status = debugee.getStatus();
239         if (status != 0/*STATUS_PASSED*/ + 95/*STATUS_TEMP*/) {
240             print_log_anyway
241                 ("##> equals002: UNEXPECTED Debugee's exit status (not 95) - " + status);
242             v_test_result = 2/*STATUS_FAILED*/;
243         }
244         else {
245             print_log_on_verbose
246                 ("--> equals002: expected Debugee's exit status - " + status);
247         }
248 
249         return v_test_result;
250     }
251 }  // end of equals002 class
252