1 /*
2  * Copyright (c) 2001, 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 
25 package nsk.jdi.ArrayReference.getValues_ii;
26 
27 import nsk.share.*;
28 import nsk.share.jpda.*;
29 import nsk.share.jdi.*;
30 
31 import com.sun.jdi.*;
32 import java.io.*;
33 import java.util.*;
34 
35 public class getvaluesii003 {
36     final static int MIN_INDEX = -50;
37     final static int MAX_INDEX = 51;
38     final static String FIELD_NAME[][] = {
39         {"z1", "5"},
40         {"b1", "5"},
41         {"c1", "6"},
42         {"d1", "1"},
43         {"f1", "1"},
44         {"i1", "10"},
45         {"l1", "2"},
46         {"r1", "5"},
47 
48         {"lF1", "1"},
49         {"lP1", "1"},
50         {"lU1", "2"},
51         {"lR1", "3"},
52         {"lT1", "4"},
53         {"lV1", "5"}
54     };
55 
56     private static Log log;
57     private final static String prefix = "nsk.jdi.ArrayReference.getValues_ii.";
58     private final static String className = "getvaluesii003";
59     private final static String debugerName = prefix + className;
60     private final static String debugeeName = debugerName + "a";
61     private final static String classToCheckName = prefix + "getvaluesii003aClassToCheck";
62 
main(String argv[])63     public static void main(String argv[]) {
64         System.exit(95 + run(argv, System.out));
65     }
66 
run(String argv[], PrintStream out)67     public static int run(String argv[], PrintStream out) {
68         ArgumentHandler argHandler = new ArgumentHandler(argv);
69         log = new Log(out, argHandler);
70         Binder binder = new Binder(argHandler, log);
71         Debugee debugee = binder.bindToDebugee(debugeeName
72                               + (argHandler.verbose() ? " -verbose" : ""));
73         IOPipe pipe = debugee.createIOPipe();
74         boolean testFailed = false;
75 
76         // Connect with debugee and resume it
77         debugee.redirectStderr(out);
78         debugee.resume();
79         String line = pipe.readln();
80         if (line == null) {
81             log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null");
82             return 2;
83         }
84         if (!line.equals("ready")) {
85             log.complain("debuger FAILURE> UNEXPECTED debugee's signal - "
86                       + line);
87             return 2;
88         }
89         else {
90             log.display("debuger> debugee's \"ready\" signal recieved.");
91         }
92 
93         ReferenceType refType = debugee.classByName(classToCheckName);
94         if (refType == null) {
95            log.complain("debuger FAILURE> Class " + classToCheckName
96                       + " not found.");
97            return 2;
98         }
99 
100         log.display("debuger> Total fields in debugee read: "
101                   + refType.allFields().size() + " total fields in debuger: "
102                   + FIELD_NAME.length + "\n");
103 
104         // Check all array fields from debugee
105         for (int i = 0; i < FIELD_NAME.length; i++) {
106             Field field;
107             String name = FIELD_NAME[i][0];
108             Integer totalElements = new Integer(FIELD_NAME[i][1]);
109             int lastElementIndex = totalElements.intValue() - 1;
110             Value value;
111             ArrayReference arrayRef;
112 
113             // Get field from debuggee by name
114             try {
115                 field = refType.fieldByName(name);
116             } catch (ClassNotPreparedException e) {
117                 log.complain("debuger FAILURE 1> Can't get field by name "
118                            + name);
119                 log.complain("debuger FAILURE 1> Exception: " + e);
120                 testFailed = true;
121                 continue;
122             } catch (ObjectCollectedException e) {
123                 log.complain("debuger FAILURE 1> Can't get field by name "
124                            + name);
125                 log.complain("debuger FAILURE 1> Exception: " + e);
126                 testFailed = true;
127                 continue;
128             }
129             log.display("debuger> " + i + " field " + field + " read.");
130 
131             // Get field's value
132             try {
133                 value = refType.getValue(field);
134             } catch (IllegalArgumentException e) {
135                 log.complain("debuger FAILURE 2> Cannot get value for field "
136                            + name);
137                 log.complain("debuger FAILURE 2> Exception: " + e);
138                 testFailed = true;
139                 continue;
140             } catch (ObjectCollectedException e) {
141                 log.complain("debuger FAILURE 2> Cannot get value for field "
142                            + name);
143                 log.complain("debuger FAILURE 2> Exception: " + e);
144                 testFailed = true;
145                 continue;
146             }
147             log.display("debuger> " + i + " field value is " + value);
148 
149             // Cast to ArrayReference. All fields in debugee are
150             // arrays, so ClassCastException should not be thrown
151             try {
152                 arrayRef = (ArrayReference)value;
153             } catch (ClassCastException e) {
154                 log.complain("debuger FAILURE 3> Cannot cast value for field "
155                            + name + " to ArrayReference.");
156                 log.complain("debuger FAILURE 3> Exception: " + e);
157                 testFailed = true;
158                 continue;
159             }
160 
161             // Try to get values by index from MIN_INDEX to -1 and from
162             // arrayRef.length() to MAX_INDEX, while length is 1
163             for (int j = MIN_INDEX; j < MAX_INDEX; j++) {
164                 if ( (j < 0) || (j > lastElementIndex) ) {
165                     List listOfValues;
166 
167                     try {
168                         listOfValues = arrayRef.getValues(j, 1);
169                         log.complain("debuger FAILURE 4> Values for " + j
170                                    + " element of field " + name + " is "
171                                    + listOfValues + ", but "
172                                    + "IndexOutOfBoundsException expected.");
173                         testFailed = true;
174                     } catch (ObjectCollectedException e) {
175                         log.display("debuger> Cannot get " + j + " value from "
176                                   + "field " + name);
177                         log.display("debuger> Exception: " + e);
178                         testFailed = true;
179                     } catch (IndexOutOfBoundsException e) {
180                         // Index is always out of bounds, so
181                         // IndexOutOfBoundsException is expected
182                         log.display("debuger> " + i + " field: cannot get "
183                                   + "list of components from index " + j
184                                   + " with length 1. Expected exception: " + e);
185                     }
186                 }
187             }
188             log.display("debuger> " + i + " field checked.\n");
189         }
190 
191         pipe.println("quit");
192         debugee.waitFor();
193         int status = debugee.getStatus();
194         if (testFailed) {
195             log.complain("debuger FAILURE> TEST FAILED");
196             return 2;
197         } else {
198             if (status == 95) {
199                 log.display("debuger> expected Debugee's exit "
200                           + "status - " + status);
201                 return 0;
202             } else {
203                 log.complain("debuger FAILURE> UNEXPECTED Debugee's exit "
204                            + "status (not 95) - " + status);
205                 return 2;
206             }
207         }
208     }
209 }
210