Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/ArrayReference/setValue/setvalue003.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.setValue;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdi.*;2829import com.sun.jdi.*;30import java.io.*;31import java.util.*;3233public class setvalue003 {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;4142// parameters arrays to call com.sum.jdi.ArrayReference.setValue(int, Value)43private static long[] INDEX_PARAM = { Integer.MAX_VALUE + 1,44-1,450,46Integer.MAX_VALUE47};48private final static String BYTE_VALUES_FIELD = "BYTE_VALUE_PARAM";49private final static String CHAR_VALUES_FIELD = "CHAR_VALUE_PARAM";50private final static String DBL_VALUES_FIELD = "DBL_VALUE_PARAM";51private final static String FLT_VALUES_FIELD = "FLT_VALUE_PARAM";52private final static String INT_VALUES_FIELD = "INT_VALUE_PARAM";53private final static String LNG_VALUES_FIELD = "LNG_VALUE_PARAM";54private final static String SHORT_VALUES_FIELD= "SHORT_VALUE_PARAM";5556private final static String prefix = "nsk.jdi.ArrayReference.setValue.";57private final static String className = "setvalue003";58private final static String debuggerName = prefix + className;59private final static String debugeeName = debuggerName + "a";60private final static String objectToCheck = "testedObj";6162private int exitStatus;63private Log log;64private Debugee debugee;65private IOPipe pipe;66private ReferenceType refType;6768public static void main(String argv[]) {69System.exit(JCK_STATUS_BASE + run(argv, System.out));70}7172public static int run(String argv[], PrintStream out) {7374setvalue003 tstObj = new setvalue003();7576if ( tstObj.prepareDebugee(argv, out) ) {77tstObj.execTest();78tstObj.disposeOfDebugee();79}8081if ( tstObj.exitStatus == TEST_FAILED )82tstObj.complain("run:: TEST FAILED");83else84tstObj.display("run:: TEST PASSED");85return tstObj.exitStatus;86}8788private boolean prepareDebugee(String argv[], PrintStream out) {89ArgumentHandler argHandler = new ArgumentHandler(argv);90log = new Log(out, argHandler);91Binder binder = new Binder(argHandler, log);92display("prepareDebugee:: binder created.");9394debugee = binder.bindToDebugee(debugeeName);95log.display("prepareDebugee:: binded to debugee.");96pipe = debugee.createIOPipe();97log.display("prepareDebugee:: pipe created.");9899debugee.redirectStderr(out);100debugee.resume();101102String line = pipe.readln();103if ( line == null ) {104complain("prepareDebugee:: UNEXPECTED debugee's signal - null");105return false;106}107if ( !line.equals("ready") ) {108complain("prepareDebugee:: UNEXPECTED debugee's signal - "109+ line);110return false;111}112113display("prepareDebugee:: debugee's \"ready\" signal received.");114return true;115}116117private boolean disposeOfDebugee() {118pipe.println("quit");119debugee.waitFor();120int status = debugee.getStatus();121122if ( status != JCK_STATUS_BASE ) {123complain("disposeOfDebugee:: UNEXPECTED Debugee's exit "124+ "status (not " + JCK_STATUS_BASE + ") - " + status);125return false;126}127display("disposeOfDebugee:: expected Debugee's exit "128+ "status - " + status);129return true;130}131132private void display(String msg) {133if ( log != null )134log.display("debugger> " + msg);135}136137private void complain(String msg) {138if ( log != null )139log.complain("debugger FAILURE> " + msg);140}141142private boolean execTest() {143exitStatus = TEST_FAILED;144145refType = debugee.classByName(debugeeName);146if ( refType == null ) {147complain("eventHandler:: Class '" + debugeeName + "' not found.");148return false;149}150151Field field = refType.fieldByName(objectToCheck);152if ( field == null ) {153complain("eventHandler:: Field '" + objectToCheck + "' not found.");154return false;155}156157Value objectValue = refType.getValue(field);158if ( objectValue == null ) {159complain("eventHandler:: Field '" + objectToCheck160+ "' not initialized.");161return false;162}163164return checkObjectFields(objectValue);165}166167public boolean checkObjectFields(Value objectValue) {168List fieldList;169if ( ! (objectValue instanceof ObjectReference) )170return false;171172fieldList = ((ClassType )objectValue.type()).allFields();173174// Check all array fields from debugee175display("checkObjectFields:: Tests starts >>>");176boolean res = true;177for (int i = 0; i < fieldList.size(); i++) {178res = checkFieldValue((ObjectReference )objectValue,179(Field )fieldList.get(i)) && res;180}181182exitStatus = res ? TEST_PASSED : TEST_FAILED;183return res;184}185186private boolean checkFieldValue(ObjectReference object, Field field) {187Value fieldValue;188ArrayReference arrayRef;189String fieldName = field.name();190log.display("");191display("<" + fieldName + "> field is being checked.");192try {193fieldValue = object.getValue(field);194} catch (IllegalArgumentException e) {195complain("checkFieldValue:: can not get value for field " + fieldName);196complain("checkFieldValue:: " + e);197return false;198}199200display("***" + fieldName + " = " + fieldValue);201202// Tested object doesn't have non-initialized fields!203if ( fieldValue == null ) {204complain("unexpected field value <"205+ fieldValue + ">");206return false;207}208209display("*** type of " + fieldName + " = " + fieldValue.type());210211// Checking up of value type.212// Tested object doesn't have other fields than be ArrayType213if ( ! (fieldValue.type() instanceof ArrayType) ) {214display("type of value is not ArrayType.");215return false;216}217218boolean res = true;219220Type itemType;221try {222itemType = ((ArrayType )fieldValue.type()).componentType();223} catch(Exception e) {224complain("Unexpected " + e.getClass().getName() );225return false;226}227228// getting value to set from debugee with defined type229Field fieldOfValues = null;230if ( itemType instanceof ByteType ) {231fieldOfValues = refType.fieldByName(BYTE_VALUES_FIELD);232if ( fieldOfValues == null ) {233complain("Field '" + BYTE_VALUES_FIELD + "' not found.");234return false;235}236} else if ( itemType instanceof CharType ) {237fieldOfValues = refType.fieldByName(CHAR_VALUES_FIELD);238if ( fieldOfValues == null ) {239complain("Field '" + CHAR_VALUES_FIELD + "' not found.");240return false;241}242} else if ( itemType instanceof DoubleType ) {243fieldOfValues = refType.fieldByName(DBL_VALUES_FIELD);244if ( fieldOfValues == null ) {245complain("Field '" + DBL_VALUES_FIELD + "' not found.");246return false;247}248} else if ( itemType instanceof FloatType ) {249fieldOfValues = refType.fieldByName(FLT_VALUES_FIELD);250if ( fieldOfValues == null ) {251complain("Field '" + FLT_VALUES_FIELD + "' not found.");252return false;253}254} else if ( itemType instanceof IntegerType ) {255fieldOfValues = refType.fieldByName(INT_VALUES_FIELD);256if ( fieldOfValues == null ) {257complain("Field '" + INT_VALUES_FIELD + "' not found.");258return false;259}260} else if ( itemType instanceof LongType ) {261fieldOfValues = refType.fieldByName(LNG_VALUES_FIELD);262if ( fieldOfValues == null ) {263complain("Field '" + LNG_VALUES_FIELD + "' not found.");264return false;265}266} else if ( itemType instanceof ShortType ) {267fieldOfValues = refType.fieldByName(SHORT_VALUES_FIELD);268if ( fieldOfValues == null ) {269complain("Field '" + SHORT_VALUES_FIELD + "' not found.");270return false;271}272}273274ArrayReference values = (ArrayReference )refType.getValue(fieldOfValues);275276// Checking up of test cases.277for ( int i = 0; i < INDEX_PARAM.length; i++ ) {278for ( int j = 0; j < values.length(); j++ ) {279res = checkValueUpdating(fieldName,280(ArrayReference )fieldValue, INDEX_PARAM[i],281values.getValue(j)) && res;282}283}284return res;285}286287private boolean checkValueUpdating(String name,288ArrayReference arrayRef, long index, Value value) {289Value itemValue;290List list;291String il2Str = "for index=" + index + " value=" + value;292int arrayLength = arrayRef.length();293294try {295arrayRef.setValue((int )index, value);296Value v1 = arrayRef.getValue((int )index);297298if ( !value.equals(v1) ) {299complain("not correct value " + v1300+ " " + il2Str);301return false;302}303304if ( index < 0 || index >= arrayLength ) {305complain("IndexOutOfBoundsException is expected " + il2Str);306return false;307} else {308display("Value " + v1 + " is set " + il2Str);309}310311} catch (IndexOutOfBoundsException e) {312313// checking specification conditions314if ( index < 0 || index >= arrayLength ) {315display("expected IndexOutOfBoundsException "316+ il2Str);317} else {318complain("unexpected IndexOutOfBoundsException "319+ il2Str);320return false;321}322323} catch (Exception e) {324complain("Unexpected exception: "325+ e + " " + il2Str);326return false;327}328return true;329}330}331332333