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 package org.apache.harmony.jpda.tests.jdwp.ObjectReference; 20 21 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket; 22 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands; 23 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants; 24 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket; 25 import org.apache.harmony.jpda.tests.framework.jdwp.TaggedObject; 26 import org.apache.harmony.jpda.tests.framework.jdwp.Value; 27 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase; 28 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer; 29 30 31 /** 32 * JDWP unit test for ObjectReference.InvokeMethod command. 33 */ 34 public class InvokeMethod003Test extends JDWPSyncTestCase { 35 @Override 36 protected String getDebuggeeClassName() { 37 return "org.apache.harmony.jpda.tests.jdwp.ObjectReference.InvokeMethod003Debuggee"; 38 } 39 40 /** 41 * This testcase exercises ObjectReference.InvokeMethod command. 42 * <BR>The test first starts the debuggee, requests METHOD_ENTRY event so 43 * the application suspends on first invoke. 44 * <BR>Then sends ObjectReference.InvokeMethod command to invoke method 45 * Object.toString on an instance of a subclass of Object overriding the 46 * method toString. 47 * <BR>Checks that returned value is expected string value and returned 48 * exception object is null. 49 * <BR>Finally resumes the application. 50 */ 51 public void testInvokeMethod_toString() { 52 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY); 53 54 // Get debuggee class ID. 55 String debuggeeClassSig = "Lorg/apache/harmony/jpda/tests/jdwp/ObjectReference/InvokeMethod003Debuggee;"; 56 long debuggeeTypeID = debuggeeWrapper.vmMirror.getClassID(debuggeeClassSig); 57 assertTrue("Failed to find debuggee class", debuggeeTypeID != 0); 58 59 // Set METHOD_ENTRY event request so application is suspended. 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) 4); // class-only modifier. 67 packet.setNextValueAsReferenceTypeID(debuggeeTypeID); 68 ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet); 69 checkReplyPacket(reply, "EventRequest::Set command"); 70 71 int requestID = reply.getNextValueAsInt(); 72 logWriter.println(" EventRequest.Set: requestID=" + requestID); 73 assertAllDataRead(reply); 74 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 75 76 long targetThreadID = 0; 77 // Wait for METHOD_ENTRY event and collect event thread. 78 CommandPacket event = debuggeeWrapper.vmMirror.receiveEvent(); 79 byte suspendPolicy = event.getNextValueAsByte(); 80 int events = event.getNextValueAsInt(); 81 logWriter.println(" EVENT_THREAD event: suspendPolicy=" + suspendPolicy + " events=" + events); 82 for (int i = 0; i < events; i++) { 83 byte eventKind = event.getNextValueAsByte(); 84 int newRequestID = event.getNextValueAsInt(); 85 long threadID = event.getNextValueAsThreadID(); 86 //Location location = 87 event.getNextValueAsLocation(); 88 logWriter.println(" EVENT_THREAD event " + i + ": eventKind=" 89 + eventKind + " requestID=" + newRequestID + " threadID=" 90 + threadID); 91 if (newRequestID == requestID) { 92 targetThreadID = threadID; 93 } 94 } 95 assertAllDataRead(event); 96 assertTrue("Invalid targetThreadID, must be != 0", targetThreadID != 0); 97 98 // Now we're suspended, clear event request. 99 packet = new CommandPacket( 100 JDWPCommands.EventRequestCommandSet.CommandSetID, 101 JDWPCommands.EventRequestCommandSet.ClearCommand); 102 packet.setNextValueAsByte(JDWPConstants.EventKind.METHOD_ENTRY); 103 packet.setNextValueAsInt(requestID); 104 reply = debuggeeWrapper.vmMirror.performCommand(packet); 105 checkReplyPacket(reply, "EventRequest::Clear command"); 106 assertAllDataRead(reply); 107 108 // Load value of the invoke's receiver. 109 long receiverFieldID = debuggeeWrapper.vmMirror.getFieldID(debuggeeTypeID, "invokeReceiver"); 110 assertTrue("Failed to find receiver field", receiverFieldID != 0); 111 112 logWriter.println(" Send ReferenceType.GetValues"); 113 packet = new CommandPacket( 114 JDWPCommands.ReferenceTypeCommandSet.CommandSetID, 115 JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand); 116 packet.setNextValueAsReferenceTypeID(debuggeeTypeID); 117 packet.setNextValueAsInt(1); // looking for one field. 118 packet.setNextValueAsFieldID(receiverFieldID); // the field we're looking for. 119 reply = debuggeeWrapper.vmMirror.performCommand(packet); 120 checkReplyPacket(reply, "ReferenceType::GetValues command"); 121 int valuesCount = reply.getNextValueAsInt(); // number of field values. 122 Value fieldValue = reply.getNextValueAsValue(); 123 assertAllDataRead(reply); 124 assertEquals("Not the right number of values", 1, valuesCount); 125 assertNotNull("Received null value", fieldValue); 126 assertEquals("Expected an object value", JDWPConstants.Tag.OBJECT_TAG, fieldValue.getTag()); 127 long receiverObjectID = fieldValue.getLongValue(); 128 assertTrue("Field is null", receiverObjectID != 0); 129 130 // Get test class ID. 131 String testClassSig = "Lorg/apache/harmony/jpda/tests/jdwp/ObjectReference/InvokeMethod003Debuggee$TestClass;"; 132 long testTypeID = debuggeeWrapper.vmMirror.getClassID(testClassSig); 133 assertTrue("Failed to find test class", testTypeID != 0); 134 135 // Get java.lang.Object class ID. 136 long objectTypeID = debuggeeWrapper.vmMirror.getClassID("Ljava/lang/Object;"); 137 assertTrue("Failed to find java.lang.Object class", objectTypeID != 0); 138 139 // Get java.lang.Object.toString method ID. 140 long targetMethodID = debuggeeWrapper.vmMirror.getMethodID(objectTypeID, "toString"); 141 assertTrue("Failed to find method", targetMethodID != 0); 142 143 // Invoke method. 144 packet = new CommandPacket( 145 JDWPCommands.ObjectReferenceCommandSet.CommandSetID, 146 JDWPCommands.ObjectReferenceCommandSet.InvokeMethodCommand); 147 packet.setNextValueAsObjectID(receiverObjectID); 148 packet.setNextValueAsThreadID(targetThreadID); 149 packet.setNextValueAsClassID(testTypeID); // TestClass type ID. 150 packet.setNextValueAsMethodID(targetMethodID); // Object.toString method ID. 151 packet.setNextValueAsInt(0); // no arguments. 152 packet.setNextValueAsInt(0); // invoke options. 153 logWriter.println(" Send ObjectReference.InvokeMethod without Exception"); 154 reply = debuggeeWrapper.vmMirror.performCommand(packet); 155 checkReplyPacket(reply, "ObjectReference::InvokeMethod command"); 156 157 Value returnValue = reply.getNextValueAsValue(); 158 assertNotNull("Returned value is null", returnValue); 159 assertEquals("Returned value tag is incorrect", JDWPConstants.Tag.STRING_TAG, returnValue.getTag()); 160 long stringID = returnValue.getLongValue(); 161 assertTrue("Returned string is null", stringID != 0); 162 163 String returnedString = debuggeeWrapper.vmMirror.getStringValue(stringID); 164 assertEquals("Invalid returned value,", "TestClass.toString()", returnedString); 165 logWriter.println(" ObjectReference.InvokeMethod: returnedString=\"" 166 + returnedString + "\""); 167 168 TaggedObject exception = reply.getNextValueAsTaggedObject(); 169 assertNotNull("Returned exception is null", exception); 170 assertTrue("Invalid exception object ID:<" + exception.objectID + ">", exception.objectID == 0); 171 assertEquals("Invalid exception tag,", JDWPConstants.Tag.OBJECT_TAG, exception.tag 172 , JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG) 173 , JDWPConstants.Tag.getName(exception.tag)); 174 logWriter.println(" ClassType.InvokeMethod: exception.tag=" 175 + exception.tag + " exception.objectID=" + exception.objectID); 176 assertAllDataRead(reply); 177 178 // Let's resume application 179 debuggeeWrapper.vmMirror.resume(); 180 181 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 182 } 183 } 184