1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/*
* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package nsk.jdi.ArrayReference.setValue;
import nsk.share.*;
import nsk.share.jpda.*;
import nsk.share.jdi.*;
import com.sun.jdi.*;
import java.io.*;
import java.util.*;
public class setvalue003 {
// exit code when test failed
public final static int TEST_FAILED = 2;
// exit code when test passed
public final static int TEST_PASSED = 0;
// shift of exit code
public final static int JCK_STATUS_BASE = 95;
// parameters arrays to call com.sum.jdi.ArrayReference.setValue(int, Value)
private static long[] INDEX_PARAM = { Integer.MAX_VALUE + 1,
-1,
0,
Integer.MAX_VALUE
};
private final static String BYTE_VALUES_FIELD = "BYTE_VALUE_PARAM";
private final static String CHAR_VALUES_FIELD = "CHAR_VALUE_PARAM";
private final static String DBL_VALUES_FIELD = "DBL_VALUE_PARAM";
private final static String FLT_VALUES_FIELD = "FLT_VALUE_PARAM";
private final static String INT_VALUES_FIELD = "INT_VALUE_PARAM";
private final static String LNG_VALUES_FIELD = "LNG_VALUE_PARAM";
private final static String SHORT_VALUES_FIELD= "SHORT_VALUE_PARAM";
private final static String prefix = "nsk.jdi.ArrayReference.setValue.";
private final static String className = "setvalue003";
private final static String debuggerName = prefix + className;
private final static String debugeeName = debuggerName + "a";
private final static String objectToCheck = "testedObj";
private int exitStatus;
private Log log;
private Debugee debugee;
private IOPipe pipe;
private ReferenceType refType;
public static void main(String argv[]) {
System.exit(JCK_STATUS_BASE + run(argv, System.out));
}
public static int run(String argv[], PrintStream out) {
setvalue003 tstObj = new setvalue003();
if ( tstObj.prepareDebugee(argv, out) ) {
tstObj.execTest();
tstObj.disposeOfDebugee();
}
if ( tstObj.exitStatus == TEST_FAILED )
tstObj.complain("run:: TEST FAILED");
else
tstObj.display("run:: TEST PASSED");
return tstObj.exitStatus;
}
private boolean prepareDebugee(String argv[], PrintStream out) {
ArgumentHandler argHandler = new ArgumentHandler(argv);
log = new Log(out, argHandler);
Binder binder = new Binder(argHandler, log);
display("prepareDebugee:: binder created.");
debugee = binder.bindToDebugee(debugeeName);
log.display("prepareDebugee:: binded to debugee.");
pipe = debugee.createIOPipe();
log.display("prepareDebugee:: pipe created.");
debugee.redirectStderr(out);
debugee.resume();
String line = pipe.readln();
if ( line == null ) {
complain("prepareDebugee:: UNEXPECTED debugee's signal - null");
return false;
}
if ( !line.equals("ready") ) {
complain("prepareDebugee:: UNEXPECTED debugee's signal - "
+ line);
return false;
}
display("prepareDebugee:: debugee's \"ready\" signal received.");
return true;
}
private boolean disposeOfDebugee() {
pipe.println("quit");
debugee.waitFor();
int status = debugee.getStatus();
if ( status != JCK_STATUS_BASE ) {
complain("disposeOfDebugee:: UNEXPECTED Debugee's exit "
+ "status (not " + JCK_STATUS_BASE + ") - " + status);
return false;
}
display("disposeOfDebugee:: expected Debugee's exit "
+ "status - " + status);
return true;
}
private void display(String msg) {
if ( log != null )
log.display("debugger> " + msg);
}
private void complain(String msg) {
if ( log != null )
log.complain("debugger FAILURE> " + msg);
}
private boolean execTest() {
exitStatus = TEST_FAILED;
refType = debugee.classByName(debugeeName);
if ( refType == null ) {
complain("eventHandler:: Class '" + debugeeName + "' not found.");
return false;
}
Field field = refType.fieldByName(objectToCheck);
if ( field == null ) {
complain("eventHandler:: Field '" + objectToCheck + "' not found.");
return false;
}
Value objectValue = refType.getValue(field);
if ( objectValue == null ) {
complain("eventHandler:: Field '" + objectToCheck
+ "' not initialized.");
return false;
}
return checkObjectFields(objectValue);
}
public boolean checkObjectFields(Value objectValue) {
List fieldList;
if ( ! (objectValue instanceof ObjectReference) )
return false;
fieldList = ((ClassType )objectValue.type()).allFields();
// Check all array fields from debugee
display("checkObjectFields:: Tests starts >>>");
boolean res = true;
for (int i = 0; i < fieldList.size(); i++) {
res = checkFieldValue((ObjectReference )objectValue,
(Field )fieldList.get(i)) && res;
}
exitStatus = res ? TEST_PASSED : TEST_FAILED;
return res;
}
private boolean checkFieldValue(ObjectReference object, Field field) {
Value fieldValue;
ArrayReference arrayRef;
String fieldName = field.name();
log.display("");
display("<" + fieldName + "> field is being checked.");
try {
fieldValue = object.getValue(field);
} catch (IllegalArgumentException e) {
complain("checkFieldValue:: can not get value for field " + fieldName);
complain("checkFieldValue:: " + e);
return false;
}
display("***" + fieldName + " = " + fieldValue);
// Tested object doesn't have non-initialized fields!
if ( fieldValue == null ) {
complain("unexpected field value <"
+ fieldValue + ">");
return false;
}
display("*** type of " + fieldName + " = " + fieldValue.type());
// Checking up of value type.
// Tested object doesn't have other fields than be ArrayType
if ( ! (fieldValue.type() instanceof ArrayType) ) {
display("type of value is not ArrayType.");
return false;
}
boolean res = true;
Type itemType;
try {
itemType = ((ArrayType )fieldValue.type()).componentType();
} catch(Exception e) {
complain("Unexpected " + e.getClass().getName() );
return false;
}
// getting value to set from debugee with defined type
Field fieldOfValues = null;
if ( itemType instanceof ByteType ) {
fieldOfValues = refType.fieldByName(BYTE_VALUES_FIELD);
if ( fieldOfValues == null ) {
complain("Field '" + BYTE_VALUES_FIELD + "' not found.");
return false;
}
} else if ( itemType instanceof CharType ) {
fieldOfValues = refType.fieldByName(CHAR_VALUES_FIELD);
if ( fieldOfValues == null ) {
complain("Field '" + CHAR_VALUES_FIELD + "' not found.");
return false;
}
} else if ( itemType instanceof DoubleType ) {
fieldOfValues = refType.fieldByName(DBL_VALUES_FIELD);
if ( fieldOfValues == null ) {
complain("Field '" + DBL_VALUES_FIELD + "' not found.");
return false;
}
} else if ( itemType instanceof FloatType ) {
fieldOfValues = refType.fieldByName(FLT_VALUES_FIELD);
if ( fieldOfValues == null ) {
complain("Field '" + FLT_VALUES_FIELD + "' not found.");
return false;
}
} else if ( itemType instanceof IntegerType ) {
fieldOfValues = refType.fieldByName(INT_VALUES_FIELD);
if ( fieldOfValues == null ) {
complain("Field '" + INT_VALUES_FIELD + "' not found.");
return false;
}
} else if ( itemType instanceof LongType ) {
fieldOfValues = refType.fieldByName(LNG_VALUES_FIELD);
if ( fieldOfValues == null ) {
complain("Field '" + LNG_VALUES_FIELD + "' not found.");
return false;
}
} else if ( itemType instanceof ShortType ) {
fieldOfValues = refType.fieldByName(SHORT_VALUES_FIELD);
if ( fieldOfValues == null ) {
complain("Field '" + SHORT_VALUES_FIELD + "' not found.");
return false;
}
}
ArrayReference values = (ArrayReference )refType.getValue(fieldOfValues);
// Checking up of test cases.
for ( int i = 0; i < INDEX_PARAM.length; i++ ) {
for ( int j = 0; j < values.length(); j++ ) {
res = checkValueUpdating(fieldName,
(ArrayReference )fieldValue, INDEX_PARAM[i],
values.getValue(j)) && res;
}
}
return res;
}
private boolean checkValueUpdating(String name,
ArrayReference arrayRef, long index, Value value) {
Value itemValue;
List list;
String il2Str = "for index=" + index + " value=" + value;
int arrayLength = arrayRef.length();
try {
arrayRef.setValue((int )index, value);
Value v1 = arrayRef.getValue((int )index);
if ( !value.equals(v1) ) {
complain("not correct value " + v1
+ " " + il2Str);
return false;
}
if ( index < 0 || index >= arrayLength ) {
complain("IndexOutOfBoundsException is expected " + il2Str);
return false;
} else {
display("Value " + v1 + " is set " + il2Str);
}
} catch (IndexOutOfBoundsException e) {
// checking specification conditions
if ( index < 0 || index >= arrayLength ) {
display("expected IndexOutOfBoundsException "
+ il2Str);
} else {
complain("unexpected IndexOutOfBoundsException "
+ il2Str);
return false;
}
} catch (Exception e) {
complain("Unexpected exception: "
+ e + " " + il2Str);
return false;
}
return true;
}
}
|