Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/ArrayReference/getValue/getvalue003.java
41161 views
/*1* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.jdi.ArrayReference.getValue;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdi.*;2829import com.sun.jdi.*;30import java.io.*;31import java.util.*;3233public class getvalue003 {3435// exit code when test failed36public final static int TEST_FAILED = 2;37// exit code when test passed38public final static int TEST_PASSED = 0;39// shift of exit code40public final static int JCK_STATUS_BASE = 95;4142private final static String prefix = "nsk.jdi.ArrayReference.getValue.";43private final static String className = "getvalue003";44private final static String debuggerName = prefix + className;45private final static String debugeeName = debuggerName + "a";46private final static String fieldToCheck = "testedObj";4748private int exitStatus;49private Log log;50private Debugee debugee;51private IOPipe pipe;5253private getvalue003() {54log = null;55debugee = null;56pipe = null;57}5859public static void main(String argv[]) {60System.exit(JCK_STATUS_BASE + run(argv, System.out));61}6263public static int run(String argv[], PrintStream out) {6465getvalue003 tstObj = new getvalue003();6667if ( tstObj.prepareDebugee(argv, out) ) {68tstObj.execTest();69tstObj.disposeOfDebugee();70}7172if ( tstObj.exitStatus == TEST_FAILED )73tstObj.complain("run:: TEST FAILED");74else75tstObj.display("run:: TEST PASSED");76return tstObj.exitStatus;77}7879private boolean prepareDebugee(String argv[], PrintStream out) {80ArgumentHandler argHandler = new ArgumentHandler(argv);81log = new Log(out, argHandler);82Binder binder = new Binder(argHandler, log);83display("prepareDebugee:: binder created.");8485debugee = binder.bindToDebugee(debugeeName);86log.display("prepareDebugee:: binded to debugee.");87pipe = debugee.createIOPipe();88log.display("prepareDebugee:: pipe created.");8990debugee.redirectStderr(out);91debugee.resume();9293String line = pipe.readln();94if ( line == null ) {95complain("prepareDebugee:: UNEXPECTED debugee's signal - null");96return false;97}98if ( !line.equals("ready") ) {99complain("prepareDebugee:: UNEXPECTED debugee's signal - "100+ line);101return false;102}103104display("prepareDebugee:: debugee's \"ready\" signal recieved.");105return true;106}107108private boolean disposeOfDebugee() {109pipe.println("quit");110debugee.waitFor();111int status = debugee.getStatus();112113if ( status != JCK_STATUS_BASE ) {114complain("disposeOfDebugee:: UNEXPECTED Debugee's exit "115+ "status (not " + JCK_STATUS_BASE + ") - " + status);116return false;117}118display("disposeOfDebugee:: expected Debugee's exit "119+ "status - " + status);120return true;121}122123private void display(String msg) {124if ( log != null )125log.display("debugger> " + msg);126}127128private void complain(String msg) {129if ( log != null )130log.complain("debugger FAILURE> " + msg);131}132133private boolean execTest() {134exitStatus = TEST_FAILED;135136ReferenceType refType = debugee.classByName(debugeeName);137if ( refType == null ) {138complain("eventHandler:: Class '" + debugeeName + "' not found.");139return false;140}141142Field field = refType.fieldByName(fieldToCheck);143if ( field == null ) {144complain("eventHandler:: Field '" + fieldToCheck + "' not found.");145return false;146}147148Value value = refType.getValue(field);149if ( value == null ) {150complain("eventHandler:: Field '" + fieldToCheck + "' not initialized.");151return false;152}153154return checkObjectFields(value);155}156157public boolean checkObjectFields(Value value) {158List fieldList;159if ( ! (value instanceof ObjectReference) )160return false;161162fieldList = ((ClassType)value.type()).allFields();163164// Check all array fields from debugee165Field field;166display("\ncheckObjectFields:: Tests starts >>>");167for (int i = 0; i < fieldList.size(); i++) {168field = (Field)fieldList.get(i);169170display("");171display("checkObjectFields:: <" + field.name() + "> field is being "172+ " checked.");173174// Check getting of item from field-array175if ( !checkFieldValue((ObjectReference)value, field) )176return false;177}178exitStatus = TEST_PASSED;179return true;180}181182private boolean checkFieldValue(ObjectReference object, Field field) {183Value value;184ArrayReference arrayRef;185String fieldName = field.name();186try {187value = object.getValue(field);188} catch (IllegalArgumentException e) {189complain("checkFieldValue:: can not get value for field " + fieldName);190complain("checkFieldValue:: " + e);191return false;192}193194display("checkFieldValue:: ***" + fieldName + " = " + value);195196boolean checkNULL = false;197// scaning of non-initialized arrays198for ( int i = 0; i < getvalue003a.NON_INIT_FIELDS.length; i++ )199{200if ( fieldName.compareTo(getvalue003a.NON_INIT_FIELDS[i]) == 0 ) {201checkNULL = true;202break;203}204}205206// checking of field value207if ( checkNULL ) {208209// value is not null, but array has not to be initialized.210if ( value != null ) {211complain("checkFieldValue:: Value of '" + fieldName + "' is " + value212+ ", but IndexOutOfBoundsException expected.");213return false;214215// array is not initialized. Expected value is null216} else {217display("checkFieldValue:: Expected value is null.");218return true;219}220} else {221222// value is null, but array has to be initialized.223if ( value == null ) {224complain("checkFieldValue:: Unexpected value of '" + fieldName225+ "'" + value);226return false;227}228}229230display("checkFieldValue:: *** type of " + fieldName + " = " + value.type());231232// check up type of value. it has to be ArrayType233if ( ! (value.type() instanceof ArrayType) ) {234display("checkFieldValue:: type of value is not ArrayType.");235return false;236}237238// Cast to ArrayReference. All fields in debugee are239// arrays, so ClassCastException should not be thrown240return checkValue(0, fieldName, (ArrayReference )value, ((ArrayReference )value).length() + 1) &&241checkValue(0, fieldName, (ArrayReference )value, Integer.MAX_VALUE) &&242checkValue(0, fieldName, (ArrayReference )value, Integer.MAX_VALUE + 1);243}244245private boolean checkValue(int depth, String name, ArrayReference arrayRef,246long itemIndex) {247248Value itemValue;249int length = arrayRef.length();250try {251itemValue = arrayRef.getValue(0);252if ( itemValue != null ) {253if ( itemValue.type() instanceof ArrayType ) {254255// itemValue has array type, check it by the same way256long index = (length + 1 != itemIndex) ? itemIndex :257((ArrayReference )itemValue).length() + 1;258if ( !checkValue(depth + 1, name, (ArrayReference )itemValue, index) )259return false;260}261}262itemValue = arrayRef.getValue((int)itemIndex);263if ( itemIndex > length || itemIndex < 0 ) {264complain("checkValue[" + depth + "]:: " + name + "[" + itemIndex + "] = "265+ itemValue + ", but IndexOutOfBoundsException expected.");266return false;267}268269} catch (IndexOutOfBoundsException e) {270/* Index is always out of bounds, so271* IndexOutOfBoundsException is expected272*/273display("checkValue[" + depth + "]:: expected IndexOutOfBoundsException " +274"is thrown for " + itemIndex + " item.");275} catch (Exception e) {276complain("checkValue[" + depth + "]:: Unexpected exception: " + e);277return false;278}279return true;280}281282}283284285