Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/ArrayReference/getValues/getvalues003.java
41162 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.getValues;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdi.*;2829import com.sun.jdi.*;30import java.io.*;31import java.util.*;3233public class getvalues003 {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.getValues.";43private final static String className = "getvalues003";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 getvalues003() {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) {6465getvalues003 tstObj = new getvalues003();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 name = field.name();186try {187value = object.getValue(field);188} catch (IllegalArgumentException e) {189complain("checkFieldValue:: can not get value for field " + name);190complain("checkFieldValue:: " + e);191return false;192}193194display("checkFieldValue:: ***" + field.name() + " = " + value);195196// non-initialized fields hav not to be197if ( value == null ) {198complain("checkFieldValue:: value is null.");199return false;200}201display("checkFieldValue:: *** type of " + field.name() + " = " + value.type());202203// check up type of value. it has to be ArrayType204if ( ! (value.type() instanceof ArrayType) ) {205display("checkFieldValue:: type of value is not ArrayType.");206return true;207}208209// Cast to ArrayReference. All fields in debugee are210// arrays, so ClassCastException should not be thrown211return checkValue(name, (ArrayReference )value);212}213214private boolean checkValue(String name, ArrayReference arrayRef) {215216// Get all components from array217List listOfValues;218try {219listOfValues = arrayRef.getValues();220} catch (Exception e) {221complain("checkValue:: Unexpected exception: " + e);222return false;223}224display("checkValue:: length of ArrayReference object - " + arrayRef.length());225display("checkValue:: size of values list - " + listOfValues.size());226227if ( listOfValues.size() == 0 ) {228display("checkValue:: <" + name + "> field has been checked.\n");229return true;230}231complain("checkValue:: <" + name + "> field has non-zero length.\n");232return false;233}234235}236237238