1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 /** 20 * @author Viacheslav G. Rybalov 21 */ 22 23 /** 24 * Created on 15.03.2005 25 */ 26 package org.apache.harmony.jpda.tests.jdwp.ObjectReference; 27 28 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket; 29 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands; 30 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants; 31 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket; 32 import org.apache.harmony.jpda.tests.framework.jdwp.TaggedObject; 33 import org.apache.harmony.jpda.tests.framework.jdwp.Value; 34 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase; 35 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer; 36 37 38 39 /** 40 * Unit test for NewInstance command. 41 * Runs <code>InvokeMethodDebuggee</code>, creates NewInstance of testClass2: 42 * Case 1: with options 43 * Case 2: without options 44 * 45 * Then invokes it's testMethod3: 46 * Case 1: with and without exceptions 47 * Case 2: nonvirtual child method without exception and nonvirtual super method without exception 48 */ 49 50 /** 51 * JDWP unit test for ObjectReference.InvokeMethod command. 52 */ 53 public class InvokeMethodTest extends JDWPSyncTestCase { 54 55 protected String getDebuggeeClassName() { 56 return "org.apache.harmony.jpda.tests.jdwp.share.debuggee.InvokeMethodDebuggee"; 57 } 58 59 protected int setEventRequest() { 60 CommandPacket packet = new CommandPacket( 61 JDWPCommands.EventRequestCommandSet.CommandSetID, 62 JDWPCommands.EventRequestCommandSet.SetCommand); 63 packet.setNextValueAsByte(JDWPConstants.EventKind.METHOD_ENTRY); 64 packet.setNextValueAsByte(JDWPConstants.SuspendPolicy.ALL); 65 packet.setNextValueAsInt(1); 66 packet.setNextValueAsByte((byte) 5); 67 packet.setNextValueAsString("*.InvokeMethodDebuggee"); 68 69 logWriter.println("\nSend EventRequest::Set command..."); 70 ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet); 71 checkReplyPacket(reply, "EventRequest::Set command"); 72 73 int requestID = reply.getNextValueAsInt(); 74 logWriter.println(" EventRequest.Set: requestID=" + requestID); 75 assertTrue(reply.isAllDataRead()); 76 return requestID; 77 } 78 79 protected long waitEvent(int requestID) { 80 long targetThreadID = 0; 81 82 CommandPacket event = debuggeeWrapper.vmMirror 83 .receiveCertainEvent(JDWPConstants.EventKind.METHOD_ENTRY); 84 byte suspendPolicy = event.getNextValueAsByte(); 85 int events = event.getNextValueAsInt(); 86 logWriter.println(" EVENT_THREAD event: suspendPolicy=" + suspendPolicy 87 + " events=" + events); 88 for (int i = 0; i < events; i++) { 89 byte eventKind = event.getNextValueAsByte(); 90 int newRequestID = event.getNextValueAsInt(); 91 long threadID = event.getNextValueAsThreadID(); 92 //Location location = 93 event.getNextValueAsLocation(); 94 logWriter.println(" EVENT_THREAD event " + i + ": eventKind=" 95 + eventKind + " requestID=" + newRequestID + " threadID=" 96 + threadID); 97 if (newRequestID == requestID) { 98 targetThreadID = threadID; 99 } 100 } 101 assertAllDataRead(event); 102 assertTrue("targetThreadID must be != 0", targetThreadID != 0); 103 return targetThreadID; 104 } 105 106 protected void clearEvent(int requestID) { 107 CommandPacket packet = new CommandPacket( 108 JDWPCommands.EventRequestCommandSet.CommandSetID, 109 JDWPCommands.EventRequestCommandSet.ClearCommand); 110 packet.setNextValueAsByte(JDWPConstants.EventKind.METHOD_ENTRY); 111 packet.setNextValueAsInt(requestID); 112 logWriter.println("\nSend EventRequest::Clear command..."); 113 ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet); 114 checkReplyPacket(reply, "EventRequest::Clear command"); 115 assertAllDataRead(reply); 116 } 117 118 protected TaggedObject makeNewInstance(long typeID, long threadID, 119 long constructorID, int testNumber) { 120 CommandPacket packet = new CommandPacket( 121 JDWPCommands.ClassTypeCommandSet.CommandSetID, 122 JDWPCommands.ClassTypeCommandSet.NewInstanceCommand); 123 packet.setNextValueAsClassID(typeID); 124 packet.setNextValueAsThreadID(threadID); 125 packet.setNextValueAsMethodID(constructorID); 126 if ( testNumber == 1 ) { 127 packet.setNextValueAsInt(1); // number of parameters 128 packet.setNextValueAsValue(new Value(false)); 129 } 130 if ( testNumber == 2 ) { 131 packet.setNextValueAsInt(0); // number of parameters 132 } 133 packet.setNextValueAsInt(0); 134 logWriter.println("\nSend ClassType.NewInstance"); 135 ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet); 136 checkReplyPacket(reply, "ClassType::NewInstance command"); 137 138 TaggedObject newObject = reply.getNextValueAsTaggedObject(); 139 logWriter.println(" ClassType.NewInstance: newObject.tag=" 140 + newObject.tag + " newObject.objectID=" + newObject.objectID); 141 142 TaggedObject exception = reply.getNextValueAsTaggedObject(); 143 logWriter.println(" ClassType.NewInstance: exception.tag=" 144 + exception.tag + " exception.objectID=" + exception.objectID); 145 146 assertTrue("newObject must be != null", newObject != null); 147 assertTrue("newObject.objectID must be != 0", newObject.objectID != 0); 148 assertEquals("Invalid object tag,", JDWPConstants.Tag.OBJECT_TAG, newObject.tag 149 , JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG) 150 , JDWPConstants.Tag.getName(newObject.tag)); 151 152 assertTrue("exception must be != null", exception != null); 153 assertTrue("exception.objectID must be == 0", exception.objectID == 0); 154 assertEquals("Invalid exception.tag,", JDWPConstants.Tag.OBJECT_TAG, exception.tag 155 , JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG) 156 , JDWPConstants.Tag.getName(exception.tag)); 157 158 assertAllDataRead(reply); 159 return newObject; 160 } 161 162 /** 163 * This testcase exercises ObjectReference.InvokeMethod command. 164 * <BR>At first the test starts debuggee. 165 * <BR>Then does the following checks: 166 * <BR> - send ObjectReference.InvokeMethod command for method, 167 * which should not throw any Exception, and checks, 168 * that returned value is int value and returned 169 * exception object is null; 170 * <BR> - send ObjectReference.InvokeMethod command for method, 171 * which should throw some Exception, and checks, that 172 * returned exception object is not null and has expected attributes; 173 */ 174 public void testInvokeMethod001() { 175 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY); 176 177 // Get referenceTypeID 178 String classSig = "Lorg/apache/harmony/jpda/tests/jdwp/share/debuggee/testClass2;"; 179 long typeID = getReferenceTypeID(classSig); 180 181 // Get methodIDs 182 long targetMethodID = getMethodID(typeID, "testMethod3"); 183 long targetConstructorID = getMethodID(typeID, "<init>"); 184 185 // Set EventRequest 186 int requestID = setEventRequest(); 187 188 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 189 190 // Wait event 191 long targetThreadID = waitEvent(requestID); 192 193 // Let's clear event request 194 clearEvent(requestID); 195 196 // Make NewInstance 197 TaggedObject newObject = makeNewInstance(typeID, targetThreadID, 198 targetConstructorID, 1 /* test number */); 199 200 // Make InvokeMethod without exception 201 CommandPacket packet = new CommandPacket( 202 JDWPCommands.ObjectReferenceCommandSet.CommandSetID, 203 JDWPCommands.ObjectReferenceCommandSet.InvokeMethodCommand); 204 packet.setNextValueAsObjectID(newObject.objectID); 205 packet.setNextValueAsThreadID(targetThreadID); 206 packet.setNextValueAsClassID(typeID); 207 packet.setNextValueAsMethodID(targetMethodID); 208 packet.setNextValueAsInt(1); 209 packet.setNextValueAsValue(new Value(false)); 210 packet.setNextValueAsInt(0); 211 logWriter.println("\nSend ObjectReference.InvokeMethod without exception..."); 212 ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet); 213 checkReplyPacket(reply, "ObjectReference::InvokeMethod command"); 214 215 Value returnValue = reply.getNextValueAsValue(); 216 logWriter.println(" ObjectReference.InvokeMethod: returnValue.getIntValue()=" 217 + returnValue.getIntValue()); 218 219 TaggedObject exception = reply.getNextValueAsTaggedObject(); 220 logWriter.println(" ObjectReference.InvokeMethod: exception.tag=" 221 + exception.tag + " exception.objectID=" + exception.objectID); 222 223 assertTrue("returnValue must be != null", returnValue != null); 224 assertEquals("Invalid returned value,", 345, returnValue.getIntValue()); 225 226 assertTrue("exception must be != null", exception != null); 227 assertTrue("exception.objectID must be == 0", exception.objectID == 0); 228 assertEquals("invalid exception.tag,", JDWPConstants.Tag.OBJECT_TAG, exception.tag 229 , JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG) 230 , JDWPConstants.Tag.getName(exception.tag)); 231 232 assertAllDataRead(reply); 233 234 // Make InvokeMethod with exception 235 packet = new CommandPacket( 236 JDWPCommands.ObjectReferenceCommandSet.CommandSetID, 237 JDWPCommands.ObjectReferenceCommandSet.InvokeMethodCommand); 238 packet.setNextValueAsObjectID(newObject.objectID); 239 packet.setNextValueAsThreadID(targetThreadID); 240 packet.setNextValueAsClassID(typeID); 241 packet.setNextValueAsMethodID(targetMethodID); 242 packet.setNextValueAsInt(1); 243 packet.setNextValueAsValue(new Value(true)); 244 packet.setNextValueAsInt(0); 245 logWriter.println("\nSend ObjectReference.InvokeMethod with exception..."); 246 reply = debuggeeWrapper.vmMirror.performCommand(packet); 247 checkReplyPacket(reply, "ObjectReference::InvokeMethod command"); 248 249 returnValue = reply.getNextValueAsValue(); 250 logWriter.println(" ObjectReference.InvokeMethod: returnValue.getIntValue()=" 251 + returnValue.getIntValue()); 252 253 exception = reply.getNextValueAsTaggedObject(); 254 logWriter.println(" ObjectReference.InvokeMethod: exception.tag=" 255 + exception.tag + " exception.objectID=" + exception.objectID); 256 257 assertTrue("exception must be != null", exception != null); 258 assertTrue("exception.objectID must be != 0", exception.objectID != 0); 259 assertEquals("Invalid exception.tag", JDWPConstants.Tag.OBJECT_TAG, exception.tag 260 , JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG) 261 , JDWPConstants.Tag.getName(exception.tag)); 262 263 assertAllDataRead(reply); 264 265 // Let's resume application 266 resumeDebuggee(); 267 268 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 269 } 270 271 /** 272 * This testcase exercises ObjectReference.InvokeMethod command with INVOKE_NONVIRTUAL InvokeOption. 273 * <BR>At first the test starts debuggee. 274 * <BR>Then does the following checks: 275 * <BR> - send ObjectReference.InvokeMethod command for nonvirtual 276 * child method (from subclass), which should not throw any Exception, and checks, 277 * that returned value is expected int value and returned 278 * exception object is null; 279 * <BR> - send ObjectReference.InvokeMethod command for nonvirtual 280 * super method (from super class), which should not throw any Exception, and checks, 281 * that returned value is expected int value and returned 282 * exception object is null; 283 */ 284 public void testInvokeMethod002() { 285 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY); 286 287 // Get referenceTypeID of super class 288 String classSig = "Lorg/apache/harmony/jpda/tests/jdwp/share/debuggee/testClass2;"; 289 long typeIDSuper = getReferenceTypeID(classSig); 290 291 // Get referenceTypeID of child class 292 classSig = "Lorg/apache/harmony/jpda/tests/jdwp/share/debuggee/testClass3;"; 293 long typeIDChild = getReferenceTypeID(classSig); 294 295 // Get methodID 296 long targetMethodIDSuper = getMethodID(typeIDSuper, "testMethod3"); 297 long targetMethodIDChild = getMethodID(typeIDChild, "testMethod3"); 298 long targetConstructorID = getMethodID(typeIDChild, "<init>"); 299 300 // Set EventRequest 301 int requestID = setEventRequest(); 302 303 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 304 305 // Wait event 306 long targetThreadID = waitEvent(requestID); 307 308 // Let's clear event request 309 clearEvent(requestID); 310 311 // Make NewInstance 312 TaggedObject newObject = makeNewInstance(typeIDChild, targetThreadID, 313 targetConstructorID, 2 /* test number */); 314 315 // Make InvokeMethod: nonvirtual child method without exception 316 CommandPacket packet = new CommandPacket( 317 JDWPCommands.ObjectReferenceCommandSet.CommandSetID, 318 JDWPCommands.ObjectReferenceCommandSet.InvokeMethodCommand); 319 packet.setNextValueAsObjectID(newObject.objectID); 320 packet.setNextValueAsThreadID(targetThreadID); 321 packet.setNextValueAsClassID(typeIDChild); 322 packet.setNextValueAsMethodID(targetMethodIDChild); 323 packet.setNextValueAsInt(1); 324 packet.setNextValueAsValue(new Value(false)); 325 packet.setNextValueAsInt(JDWPConstants.InvokeOptions.INVOKE_NONVIRTUAL); 326 logWriter.println 327 ("\nSend ObjectReference.InvokeMethod:: nonvirtual child method without exception..."); 328 ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet); 329 checkReplyPacket(reply, "ObjectReference::InvokeMethod command"); 330 331 Value returnValue = reply.getNextValueAsValue(); 332 logWriter.println(" ObjectReference.InvokeMethod: returnValue.getIntValue()=" 333 + returnValue.getIntValue()); 334 335 TaggedObject exception = reply.getNextValueAsTaggedObject(); 336 logWriter.println(" ObjectReference.InvokeMethod: exception.tag=" 337 + exception.tag + " exception.objectID=" + exception.objectID); 338 339 assertTrue("returnValue must be != null", returnValue != null); 340 assertEquals("Invalid value,", 456, returnValue.getIntValue()); 341 342 assertTrue("exception must be != null", exception != null); 343 assertTrue("exception.objectID must be == 0", exception.objectID == 0); 344 assertEquals("Invalid exception.tag", JDWPConstants.Tag.OBJECT_TAG, exception.tag 345 , JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG) 346 , JDWPConstants.Tag.getName(exception.tag)); 347 348 assertAllDataRead(reply); 349 350 // Make InvokeMethod: nonvirtual super method without exception 351 packet = new CommandPacket( 352 JDWPCommands.ObjectReferenceCommandSet.CommandSetID, 353 JDWPCommands.ObjectReferenceCommandSet.InvokeMethodCommand); 354 packet.setNextValueAsObjectID(newObject.objectID); 355 packet.setNextValueAsThreadID(targetThreadID); 356 packet.setNextValueAsClassID(typeIDSuper); 357 packet.setNextValueAsMethodID(targetMethodIDSuper); 358 packet.setNextValueAsInt(1); 359 packet.setNextValueAsValue(new Value(false)); 360 packet.setNextValueAsInt(JDWPConstants.InvokeOptions.INVOKE_NONVIRTUAL); 361 logWriter.println 362 ("\nSend ObjectReference.InvokeMethod: nonvirtual super method without exception..."); 363 reply = debuggeeWrapper.vmMirror.performCommand(packet); 364 checkReplyPacket(reply, "ObjectReference::InvokeMethod command"); 365 366 returnValue = reply.getNextValueAsValue(); 367 logWriter.println(" ObjectReference.InvokeMethod: returnValue.getIntValue()=" 368 + returnValue.getIntValue()); 369 370 exception = reply.getNextValueAsTaggedObject(); 371 logWriter.println(" ObjectReference.InvokeMethod: exception.tag=" 372 + exception.tag + " exception.objectID=" + exception.objectID); 373 374 assertTrue("returnValue must be != null", returnValue != null); 375 assertEquals("Invalid value,", 345, returnValue.getIntValue()); 376 377 assertTrue("exception must be != null", exception != null); 378 assertTrue("exception.objectID must be == 0" ,exception.objectID == 0); 379 assertEquals("Invalid exception.tag", JDWPConstants.Tag.OBJECT_TAG, exception.tag 380 , JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG) 381 , JDWPConstants.Tag.getName(exception.tag)); 382 383 assertAllDataRead(reply); 384 385 // Let's resume application 386 resumeDebuggee(); 387 388 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 389 } 390 } 391