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.visibleFields;
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>visibleFields()</code>
36  * of the JDI interface <code>ReferenceType</code> of com.sun.jdi package
37  */
38 
39 public class visibfield004 extends Log {
40     static java.io.PrintStream out_stream;
41     static boolean verbose_mode = false;  // test argument -vbs or -verbose switches to static
42                                           // - for more easy failure evaluation
43 
44     /** The main class names of the debugger & debugee applications. */
45     private final static String
46         package_prefix = "nsk.jdi.ReferenceType.visibleFields.",
47 //        package_prefix = "",    //  for DEBUG without package
48         thisClassName = package_prefix + "visibfield004",
49         debugeeName   = thisClassName + "a";
50 
51     /** Debugee's classes for check **/
52     private final static String class_for_check = package_prefix + "visibfield004aClassForCheck";
53 
54     /**
55      * Re-call to <code>run(args,out)</code>, and exit with
56      * either status 95 or 97 (JCK-like exit status).
57      */
main(String argv[])58     public static void main (String argv[]) {
59         int exitCode = run(argv,System.out);
60         System.exit(exitCode + 95/*STATUS_TEMP*/);
61     }
62 
63     /**
64      * JCK-like entry point to the test: perform testing, and
65      * return exit code 0 (PASSED) or either 2 (FAILED).
66      */
run(String argv[], PrintStream out)67     public static int run (String argv[], PrintStream out) {
68         out_stream = out;
69 
70         int v_test_result = new visibfield004().runThis(argv,out_stream);
71         if ( v_test_result == 2/*STATUS_FAILED*/ ) {
72             out_stream.println("\n==> nsk/jdi/ReferenceType/visibleFields/visibfield004 test FAILED");
73         }
74         else {
75             out_stream.println("\n==> nsk/jdi/ReferenceType/visibleFields/visibfield004 test PASSED");
76         }
77         return v_test_result;
78     }
79 
print_log_on_verbose(String message)80     private void print_log_on_verbose(String message) {
81         display(message);
82     }
83 
84     /**
85      * Non-static variant of the method <code>run(args,out)</code>
86      */
runThis(String argv[], PrintStream out)87     private int runThis (String argv[], PrintStream out) {
88         ArgumentHandler argHandler = new ArgumentHandler(argv);
89         verbose_mode = argHandler.verbose();
90 
91         if ( out_stream == null ) {
92             out_stream = out;
93         }
94 
95         out_stream.println("==> nsk/jdi/ReferenceType/visibleFields/visibfield004 test LOG:");
96         out_stream.println("==> test checks visibleFields() method of ReferenceType interface ");
97         out_stream.println("    of the com.sun.jdi package for class without visible fields\n");
98 
99         String debugee_launch_command = debugeeName;
100         if (verbose_mode) {
101             logTo(out_stream);
102         }
103 
104         Binder binder = new Binder(argHandler,this);
105         Debugee debugee = binder.bindToDebugee(debugee_launch_command);
106         IOPipe pipe = new IOPipe(debugee);
107 
108         debugee.redirectStderr(out);
109         print_log_on_verbose("--> visibfield004: visibfield004a debugee launched");
110         debugee.resume();
111 
112         String line = pipe.readln();
113         if (line == null) {
114             out_stream.println
115                 ("##> visibfield004: UNEXPECTED debugee's signal (not \"ready\") - " + line);
116             return 2/*STATUS_FAILED*/;
117         }
118         if (!line.equals("ready")) {
119             out_stream.println
120                 ("##> visibfield004: UNEXPECTED debugee's signal (not \"ready\") - " + line);
121             return 2/*STATUS_FAILED*/;
122         }
123         else {
124             print_log_on_verbose("--> visibfield004: debugee's \"ready\" signal recieved!");
125         }
126 
127         out_stream.println
128             ("--> visibfield004: check ReferenceType.visibleFields() method for debugee's "
129             + class_for_check + " class...");
130         boolean class_not_found_error = false;
131         boolean visibleFields_method_error = false;
132         int visible_fields_number = 0;
133 
134         while ( true ) {
135             ReferenceType refType = debugee.classByName(class_for_check);
136             if (refType == null) {
137                 out_stream.println("##> visibfield004: Could NOT FIND class: " + class_for_check);
138                 class_not_found_error = true;
139                 break;
140             }
141             List<Field> visible_fields_list = null;
142             try {
143                 visible_fields_list = refType.visibleFields();
144             }
145             catch (Throwable thrown) {
146                 out_stream.println("##> visibfield004: FAILED: ReferenceType.visibleFields() throws unexpected "
147                     + thrown);
148                 visibleFields_method_error = true;
149                 break;
150             }
151             visible_fields_number = visible_fields_list.size();
152             if ( visible_fields_number == 0 ) {
153                 break;
154             }
155             Field visible_fields[] = new Field[visible_fields_number];
156             visible_fields_list.toArray(visible_fields);
157             for (int i=0; i<visible_fields_number; i++) {
158                 Field checked_field = visible_fields[i];
159                 String checked_field_name = checked_field.name();
160                 String checked_field_typename = checked_field.typeName();
161                 String checked_field_static;
162                 if ( checked_field.isStatic() ) {
163                     checked_field_static = " static";
164                 }
165                 else {
166                     checked_field_static = "";
167                 }
168                 String full_checked_field = checked_field_static
169                     + " " + checked_field_typename + " " + checked_field_name;
170                 out_stream.println
171                     ("##> visibfield004: FAILED: unexpected visible field: " + full_checked_field);
172             }
173             break;
174         }
175 
176         int v_test_result = 0/*STATUS_PASSED*/;
177         if ( class_not_found_error || visibleFields_method_error ) {
178             v_test_result = 2/*STATUS_FAILED*/;
179         }
180 
181         if ( visible_fields_number > 0 ) {
182             out_stream.println
183                 ("##> visibfield004: UNEXPECTED visible fields number = " + visible_fields_number);
184             v_test_result = 2/*STATUS_FAILED*/;
185         }
186         else {
187             out_stream.println
188                 ("--> visibfield004: PASSED: returned list of fields is empty!");
189         }
190 
191         print_log_on_verbose("--> visibfield004: waiting for debugee finish...");
192         pipe.println("quit");
193         debugee.waitFor();
194 
195         int status = debugee.getStatus();
196         if (status != 0/*STATUS_PASSED*/ + 95/*STATUS_TEMP*/) {
197             out_stream.println
198                 ("##> visibfield004: UNEXPECTED Debugee's exit status (not 95) - " + status);
199             v_test_result = 2/*STATUS_FAILED*/;
200         }
201         else {
202             print_log_on_verbose
203                 ("--> visibfield004: expected Debugee's exit status - " + status);
204         }
205 
206         return v_test_result;
207     }
208 }
209