Path: blob/master/test/jdk/com/sun/jdi/ArgumentValuesTest.java
41149 views
/*1* Copyright (c) 2007, 2017, 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*/2223// THIS TEST IS LINE NUMBER SENSITIVE2425/**26* @test27* @bug 449082428* @summary JDI: provide arguments when no debug attributes present29*30* @author jjh31*32* @run build TestScaffold VMConnection TargetListener TargetAdapter33* @run compile ArgumentValuesTest.java34* @run driver ArgumentValuesTest35*/36import com.sun.jdi.*;37import com.sun.jdi.event.*;38import com.sun.jdi.request.*;3940import java.util.*;4142/********** target program **********/4344class ArgumentValuesTarg {45static char s_char1 = 'a';46static byte s_byte1 = (byte) 146;47static short s_short1 = (short) 28123;48static int s_int1 = 3101246;49static long s_long1 = 0x0123456789ABCDEFL;50static float s_float1 = 2.3145f;51static double s_double1 = 1.469d;52static int s_iarray1[] = {1, 2, 3};53static int s_marray1[][] = {{1, 2, 3}, {3, 4, 5}, null, {6, 7}};54static String s_sarray1[] = {"abc", null, "def", "ghi"};55static String s_string1 = "abcdef";5657static String s_string2 = "xy";58static String s_string3 = "wz";59static List<Integer> intList;6061public static void noArgs() {62int index = 0; // line NO_ARGS_LINE_163}6465public static void allArgs(char p_char, byte p_byte, short p_short,66int p_int, long p_long, float p_float,67double p_double, int p_iarray[], int p_marray[][],68String p_sarray1[], String p_string) {69int index = 0; // line ALL_ARGS_LINE_170}7172public static void varArgs(String ... p1) {73int index = 0; // line VAR_ARGS_LINE_174}7576public static void genericArgs(List<Integer> p1) {77int index = 0; // line GENERIC_ARGS_LINE_178}7980public void instanceMethod(char p_char, byte p_byte) {81int index = 0; // line INSTANCE_METHOD_LINE_182}8384public static void main(String[] args) {85System.out.println("Howdy!");86allArgs(87s_char1, s_byte1, s_short1, s_int1,88s_long1, s_float1, s_double1, s_iarray1,89s_marray1, s_sarray1, s_string1);9091noArgs();92varArgs(s_string1, s_string2, s_string3);93ArgumentValuesTarg avt = new ArgumentValuesTarg();94intList = new ArrayList<Integer>(10);95intList.add(10);96intList.add(20);97genericArgs(intList);9899avt.instanceMethod(s_char1, s_byte1);100101System.out.println("Goodbye from ArgumentValuesTarg!");102}103}104105/********** test program **********/106107public class ArgumentValuesTest extends TestScaffold {108static final int NO_ARGS_LINE_1 = 63;109static final int ALL_ARGS_LINE_1 = 70;110static final int VAR_ARGS_LINE_1 = 74;111static final int GENERIC_ARGS_LINE_1 = 78;112static final int INSTANCE_METHOD_LINE_1 = 82;113114// Must be in same order as args to allArgs(....)115String fieldNames[] = {"s_char1", "s_byte1", "s_short1", "s_int1",116"s_long1", "s_float1", "s_double1", "s_iarray1",117"s_marray1", "s_sarray1", "s_string1"};118119String fieldNamesVarArgs[] = {"s_string1", "s_string2", "s_string3"};120String fieldNamesInstance[] = {"s_char1", "s_byte1"};121122ReferenceType targetClass;123ThreadReference mainThread;124125ArgumentValuesTest (String args[]) {126super(args);127}128129public static void main(String[] args)130throws Exception131{132new ArgumentValuesTest (args).startTests();133}134135/********** test core **********/136137protected void runTests()138throws Exception139{140/*141* Get to the top of main() to determine targetClass and mainThread142*/143BreakpointEvent bpe = startToMain("ArgumentValuesTarg");144targetClass = bpe.location().declaringType();145mainThread = bpe.thread();146EventRequestManager erm = vm().eventRequestManager();147148149{150System.out.println("----- Testing each type of arg");151bpe = resumeTo("ArgumentValuesTarg", ALL_ARGS_LINE_1);152StackFrame frame = bpe.thread().frame(0);153154Method mmm = frame.location().method();155System.out.println("Arg types are: " + mmm.argumentTypeNames());156157List<Value> argVals = frame.getArgumentValues();158159if (argVals.size() != fieldNames.length) {160failure("failure: Varargs: expected length " + fieldNames.length +161" args, got: " + argVals);162}163for (int ii = 0; ii < argVals.size(); ii++) {164Value gotVal = argVals.get(ii);165166Field theField = targetClass.fieldByName(fieldNames[ii]);167Value expectedVal = targetClass.getValue(theField);168System.out.println(fieldNames[ii] + ": gotVal = " + gotVal +169", expected = " + expectedVal);170//System.out.println(gotVal.getClass() + ", " + expectedVal.getClass());171if (!gotVal.equals(expectedVal)) {172failure(" failure: gotVal != expected");173}174}175}176177// a method with no params178{179System.out.println("----- Testing no args");180bpe = resumeTo("ArgumentValuesTarg", NO_ARGS_LINE_1);181StackFrame frame = bpe.thread().frame(0);182183Method mmm = frame.location().method();184System.out.println("Arg types are: " + mmm.argumentTypeNames());185186List<Value> argVals = frame.getArgumentValues();187if (argVals.size() == 0) {188System.out.println("Empty arg list ok");189} else {190failure("failure: Expected empty val list, got: " + argVals);191}192}193194// var args. 3 Strings are passed in and they appear195// as a String[3] in the method.196{197System.out.println("----- Testing var args");198bpe = resumeTo("ArgumentValuesTarg", VAR_ARGS_LINE_1);199StackFrame frame = bpe.thread().frame(0);200201Method mmm = frame.location().method();202System.out.println("Arg types are: " + mmm.argumentTypeNames());203204List<Value> argVals = frame.getArgumentValues();205if (argVals.size() != 1) {206failure("failure: Varargs: expected one arg, got: " + argVals);207}208argVals = ((ArrayReference)argVals.get(0)).getValues();209210if (argVals.size() != fieldNamesVarArgs.length) {211failure("failure: Varargs: expected length " + fieldNamesVarArgs.length +212" array elements, got: " + argVals);213}214215for (int ii = 0; ii < argVals.size(); ii++) {216Value gotVal = argVals.get(ii);217218Field theField = targetClass.fieldByName(fieldNamesVarArgs[ii]);219Value expectedVal = targetClass.getValue(theField);220System.out.println(fieldNamesVarArgs[ii] + ": gotVal = " + gotVal +221", expected = " + expectedVal);222//System.out.println(gotVal.getClass() + ", " + expectedVal.getClass());223if (!gotVal.equals(expectedVal)) {224failure(" failure: gotVal != expected");225}226}227}228229// a method with with one generic param230{231System.out.println("----- Testing generic args");232bpe = resumeTo("ArgumentValuesTarg", GENERIC_ARGS_LINE_1);233StackFrame frame = bpe.thread().frame(0);234235Method mmm = frame.location().method();236System.out.println("Arg types are: " + mmm.argumentTypeNames());237238List<Value> argVals = frame.getArgumentValues();239if (argVals.size() != 1) {240failure("failure: Expected one arg, got: " + argVals);241} else {242Value gotVal = argVals.get(0);243244Field theField = targetClass.fieldByName("intList");245Value expectedVal = targetClass.getValue(theField);246System.out.println("intList " + ": gotVal = " + gotVal +247", expected = " + expectedVal);248if (!gotVal.equals(expectedVal)) {249failure("failure: gotVal != expected");250}251}252}253254// test instance method call255{256System.out.println("----- Testing instance method call");257bpe = resumeTo("ArgumentValuesTarg", INSTANCE_METHOD_LINE_1);258StackFrame frame = bpe.thread().frame(0);259260Method mmm = frame.location().method();261System.out.println("Arg types are: " + mmm.argumentTypeNames());262263List<Value> argVals = frame.getArgumentValues();264265if (argVals.size() != fieldNamesInstance.length) {266failure("failure: Varargs: expected length " + fieldNamesInstance.length +267" args, got: " + argVals);268}269for (int ii = 0; ii < argVals.size(); ii++) {270Value gotVal = argVals.get(ii);271272Field theField = targetClass.fieldByName(fieldNamesInstance[ii]);273Value expectedVal = targetClass.getValue(theField);274System.out.println(fieldNamesInstance[ii] + ": gotVal = " + gotVal +275", expected = " + expectedVal);276//System.out.println(gotVal.getClass() + ", " + expectedVal.getClass());277if (!gotVal.equals(expectedVal)) {278failure(" failure: gotVal != expected");279}280}281}282283284/*285* resume the target listening for events286*/287listenUntilVMDisconnect();288289/*290* deal with results of test if anything has called failure("foo")291* testFailed will be true292*/293if (!testFailed) {294println("ArgumentValuesTest: passed");295} else {296throw new Exception("ArgumentValuesTest: failed");297}298}299}300301302