001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Viacheslav G. Rybalov
021: * @version $Revision: 1.4 $
022: */
023:
024: /**
025: * Created on 15.03.2005
026: */package org.apache.harmony.jpda.tests.jdwp.ObjectReference;
027:
028: import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
029: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
030: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
031: import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
032: import org.apache.harmony.jpda.tests.framework.jdwp.TaggedObject;
033: import org.apache.harmony.jpda.tests.framework.jdwp.Value;
034: import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
035: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
036:
037: /**
038: * Unit test for NewInstance command.
039: * Runs <code>InvokeMethodDebuggee</code>, creates NewInstance of testClass2:
040: * Case 1: with options
041: * Case 2: without options
042: *
043: * Then invokes it's testMethod3:
044: * Case 1: with and without exceptions
045: * Case 2: nonvirtual child method without exception and nonvirtual super method without exception
046: */
047:
048: /**
049: * JDWP unit test for ObjectReference.InvokeMethod command.
050: */
051: public class InvokeMethodTest extends JDWPSyncTestCase {
052:
053: protected String getDebuggeeClassName() {
054: return "org.apache.harmony.jpda.tests.jdwp.share.debuggee.InvokeMethodDebuggee";
055: }
056:
057: protected int setEventRequest() {
058: CommandPacket packet = new CommandPacket(
059: JDWPCommands.EventRequestCommandSet.CommandSetID,
060: JDWPCommands.EventRequestCommandSet.SetCommand);
061: packet.setNextValueAsByte(JDWPConstants.EventKind.METHOD_ENTRY);
062: packet.setNextValueAsByte(JDWPConstants.SuspendPolicy.ALL);
063: packet.setNextValueAsInt(1);
064: packet.setNextValueAsByte((byte) 5);
065: packet.setNextValueAsString("*.InvokeMethodDebuggee");
066:
067: logWriter.println("\nSend EventRequest::Set command...");
068: ReplyPacket reply = debuggeeWrapper.vmMirror
069: .performCommand(packet);
070: checkReplyPacket(reply, "EventRequest::Set command");
071:
072: int requestID = reply.getNextValueAsInt();
073: logWriter.println(" EventRequest.Set: requestID=" + requestID);
074: assertTrue(reply.isAllDataRead());
075: return requestID;
076: }
077:
078: protected long waitEvent(int requestID) {
079: long targetThreadID = 0;
080:
081: CommandPacket event = debuggeeWrapper.vmMirror
082: .receiveCertainEvent(JDWPConstants.EventKind.METHOD_ENTRY);
083: byte suspendPolicy = event.getNextValueAsByte();
084: int events = event.getNextValueAsInt();
085: logWriter.println(" EVENT_THREAD event: suspendPolicy="
086: + suspendPolicy + " events=" + events);
087: for (int i = 0; i < events; i++) {
088: byte eventKind = event.getNextValueAsByte();
089: int newRequestID = event.getNextValueAsInt();
090: long threadID = event.getNextValueAsThreadID();
091: //Location location =
092: event.getNextValueAsLocation();
093: logWriter.println(" EVENT_THREAD event " + i
094: + ": eventKind=" + eventKind + " requestID="
095: + newRequestID + " threadID=" + threadID);
096: if (newRequestID == requestID) {
097: targetThreadID = threadID;
098: }
099: }
100: assertAllDataRead(event);
101: assertTrue("targetThreadID must be != 0", targetThreadID != 0);
102: return targetThreadID;
103: }
104:
105: protected void clearEvent(int requestID) {
106: CommandPacket packet = new CommandPacket(
107: JDWPCommands.EventRequestCommandSet.CommandSetID,
108: JDWPCommands.EventRequestCommandSet.ClearCommand);
109: packet.setNextValueAsByte(JDWPConstants.EventKind.METHOD_ENTRY);
110: packet.setNextValueAsInt(requestID);
111: logWriter.println("\nSend EventRequest::Clear command...");
112: ReplyPacket reply = debuggeeWrapper.vmMirror
113: .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
136: .performCommand(packet);
137: checkReplyPacket(reply, "ClassType::NewInstance command");
138:
139: TaggedObject newObject = reply.getNextValueAsTaggedObject();
140: logWriter.println(" ClassType.NewInstance: newObject.tag="
141: + newObject.tag + " newObject.objectID="
142: + newObject.objectID);
143:
144: TaggedObject exception = reply.getNextValueAsTaggedObject();
145: logWriter.println(" ClassType.NewInstance: exception.tag="
146: + exception.tag + " exception.objectID="
147: + exception.objectID);
148:
149: assertTrue("newObject must be != null", newObject != null);
150: assertTrue("newObject.objectID must be != 0",
151: newObject.objectID != 0);
152: assertEquals(
153: "Invalid object tag,",
154: JDWPConstants.Tag.OBJECT_TAG,
155: newObject.tag,
156: JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG),
157: JDWPConstants.Tag.getName(newObject.tag));
158:
159: assertTrue("exception must be != null", exception != null);
160: assertTrue("exception.objectID must be == 0",
161: exception.objectID == 0);
162: assertEquals(
163: "Invalid exception.tag,",
164: JDWPConstants.Tag.OBJECT_TAG,
165: exception.tag,
166: JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG),
167: JDWPConstants.Tag.getName(exception.tag));
168:
169: assertAllDataRead(reply);
170: return newObject;
171: }
172:
173: /**
174: * This testcase exercises ObjectReference.InvokeMethod command.
175: * <BR>At first the test starts debuggee.
176: * <BR>Then does the following checks:
177: * <BR> - send ObjectReference.InvokeMethod command for method,
178: * which should not throw any Exception, and checks,
179: * that returned value is int value and returned
180: * exception object is null;
181: * <BR> - send ObjectReference.InvokeMethod command for method,
182: * which should throw some Exception, and checks, that
183: * returned exception object is not null and has expected attributes;
184: */
185: public void testInvokeMethod001() {
186: synchronizer
187: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
188:
189: // Get referenceTypeID
190: String classSig = "Lorg/apache/harmony/jpda/tests/jdwp/share/debuggee/testClass2;";
191: long typeID = getReferenceTypeID(classSig);
192:
193: // Get methodIDs
194: long targetMethodID = getMethodID(typeID, "testMethod3");
195: long targetConstructorID = getMethodID(typeID, "<init>");
196:
197: // Set EventRequest
198: int requestID = setEventRequest();
199:
200: synchronizer
201: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
202:
203: // Wait event
204: long targetThreadID = waitEvent(requestID);
205:
206: // Let's clear event request
207: clearEvent(requestID);
208:
209: // Make NewInstance
210: TaggedObject newObject = makeNewInstance(typeID,
211: targetThreadID, targetConstructorID, 1 /* test number */);
212:
213: // Make InvokeMethod without exception
214: CommandPacket packet = new CommandPacket(
215: JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
216: JDWPCommands.ObjectReferenceCommandSet.InvokeMethodCommand);
217: packet.setNextValueAsObjectID(newObject.objectID);
218: packet.setNextValueAsThreadID(targetThreadID);
219: packet.setNextValueAsClassID(typeID);
220: packet.setNextValueAsMethodID(targetMethodID);
221: packet.setNextValueAsInt(1);
222: packet.setNextValueAsValue(new Value(false));
223: packet.setNextValueAsInt(0);
224: logWriter
225: .println("\nSend ObjectReference.InvokeMethod without exception...");
226: ReplyPacket reply = debuggeeWrapper.vmMirror
227: .performCommand(packet);
228: checkReplyPacket(reply, "ObjectReference::InvokeMethod command");
229:
230: Value returnValue = reply.getNextValueAsValue();
231: logWriter
232: .println(" ObjectReference.InvokeMethod: returnValue.getIntValue()="
233: + returnValue.getIntValue());
234:
235: TaggedObject exception = reply.getNextValueAsTaggedObject();
236: logWriter
237: .println(" ObjectReference.InvokeMethod: exception.tag="
238: + exception.tag
239: + " exception.objectID="
240: + exception.objectID);
241:
242: assertTrue("returnValue must be != null", returnValue != null);
243: assertEquals("Invalid returned value,", 345, returnValue
244: .getIntValue());
245:
246: assertTrue("exception must be != null", exception != null);
247: assertTrue("exception.objectID must be == 0",
248: exception.objectID == 0);
249: assertEquals(
250: "invalid exception.tag,",
251: JDWPConstants.Tag.OBJECT_TAG,
252: exception.tag,
253: JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG),
254: JDWPConstants.Tag.getName(exception.tag));
255:
256: assertAllDataRead(reply);
257:
258: // Make InvokeMethod with exception
259: packet = new CommandPacket(
260: JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
261: JDWPCommands.ObjectReferenceCommandSet.InvokeMethodCommand);
262: packet.setNextValueAsObjectID(newObject.objectID);
263: packet.setNextValueAsThreadID(targetThreadID);
264: packet.setNextValueAsClassID(typeID);
265: packet.setNextValueAsMethodID(targetMethodID);
266: packet.setNextValueAsInt(1);
267: packet.setNextValueAsValue(new Value(true));
268: packet.setNextValueAsInt(0);
269: logWriter
270: .println("\nSend ObjectReference.InvokeMethod with exception...");
271: reply = debuggeeWrapper.vmMirror.performCommand(packet);
272: checkReplyPacket(reply, "ObjectReference::InvokeMethod command");
273:
274: returnValue = reply.getNextValueAsValue();
275: logWriter
276: .println(" ObjectReference.InvokeMethod: returnValue.getIntValue()="
277: + returnValue.getIntValue());
278:
279: exception = reply.getNextValueAsTaggedObject();
280: logWriter
281: .println(" ObjectReference.InvokeMethod: exception.tag="
282: + exception.tag
283: + " exception.objectID="
284: + exception.objectID);
285:
286: assertTrue("exception must be != null", exception != null);
287: assertTrue("exception.objectID must be != 0",
288: exception.objectID != 0);
289: assertEquals(
290: "Invalid exception.tag",
291: JDWPConstants.Tag.OBJECT_TAG,
292: exception.tag,
293: JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG),
294: JDWPConstants.Tag.getName(exception.tag));
295:
296: assertAllDataRead(reply);
297:
298: // Let's resume application
299: resumeDebuggee();
300:
301: synchronizer
302: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
303: }
304:
305: /**
306: * This testcase exercises ObjectReference.InvokeMethod command with INVOKE_NONVIRTUAL InvokeOption.
307: * <BR>At first the test starts debuggee.
308: * <BR>Then does the following checks:
309: * <BR> - send ObjectReference.InvokeMethod command for nonvirtual
310: * child method (from subclass), which should not throw any Exception, and checks,
311: * that returned value is expected int value and returned
312: * exception object is null;
313: * <BR> - send ObjectReference.InvokeMethod command for nonvirtual
314: * super method (from super class), which should not throw any Exception, and checks,
315: * that returned value is expected int value and returned
316: * exception object is null;
317: */
318: public void testInvokeMethod002() {
319: synchronizer
320: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
321:
322: // Get referenceTypeID of super class
323: String classSig = "Lorg/apache/harmony/jpda/tests/jdwp/share/debuggee/testClass2;";
324: long typeIDSuper = getReferenceTypeID(classSig);
325:
326: // Get referenceTypeID of child class
327: classSig = "Lorg/apache/harmony/jpda/tests/jdwp/share/debuggee/testClass3;";
328: long typeIDChild = getReferenceTypeID(classSig);
329:
330: // Get methodID
331: long targetMethodIDSuper = getMethodID(typeIDSuper,
332: "testMethod3");
333: long targetMethodIDChild = getMethodID(typeIDChild,
334: "testMethod3");
335: long targetConstructorID = getMethodID(typeIDChild, "<init>");
336:
337: // Set EventRequest
338: int requestID = setEventRequest();
339:
340: synchronizer
341: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
342:
343: // Wait event
344: long targetThreadID = waitEvent(requestID);
345:
346: // Let's clear event request
347: clearEvent(requestID);
348:
349: // Make NewInstance
350: TaggedObject newObject = makeNewInstance(typeIDChild,
351: targetThreadID, targetConstructorID, 2 /* test number */);
352:
353: // Make InvokeMethod: nonvirtual child method without exception
354: CommandPacket packet = new CommandPacket(
355: JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
356: JDWPCommands.ObjectReferenceCommandSet.InvokeMethodCommand);
357: packet.setNextValueAsObjectID(newObject.objectID);
358: packet.setNextValueAsThreadID(targetThreadID);
359: packet.setNextValueAsClassID(typeIDChild);
360: packet.setNextValueAsMethodID(targetMethodIDChild);
361: packet.setNextValueAsInt(1);
362: packet.setNextValueAsValue(new Value(false));
363: packet
364: .setNextValueAsInt(JDWPConstants.InvokeOptions.INVOKE_NONVIRTUAL);
365: logWriter
366: .println("\nSend ObjectReference.InvokeMethod:: nonvirtual child method without exception...");
367: ReplyPacket reply = debuggeeWrapper.vmMirror
368: .performCommand(packet);
369: checkReplyPacket(reply, "ObjectReference::InvokeMethod command");
370:
371: Value returnValue = reply.getNextValueAsValue();
372: logWriter
373: .println(" ObjectReference.InvokeMethod: returnValue.getIntValue()="
374: + returnValue.getIntValue());
375:
376: TaggedObject exception = reply.getNextValueAsTaggedObject();
377: logWriter
378: .println(" ObjectReference.InvokeMethod: exception.tag="
379: + exception.tag
380: + " exception.objectID="
381: + exception.objectID);
382:
383: assertTrue("returnValue must be != null", returnValue != null);
384: assertEquals("Invalid value,", 456, returnValue.getIntValue());
385:
386: assertTrue("exception must be != null", exception != null);
387: assertTrue("exception.objectID must be == 0",
388: exception.objectID == 0);
389: assertEquals(
390: "Invalid exception.tag",
391: JDWPConstants.Tag.OBJECT_TAG,
392: exception.tag,
393: JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG),
394: JDWPConstants.Tag.getName(exception.tag));
395:
396: assertAllDataRead(reply);
397:
398: // Make InvokeMethod: nonvirtual super method without exception
399: packet = new CommandPacket(
400: JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
401: JDWPCommands.ObjectReferenceCommandSet.InvokeMethodCommand);
402: packet.setNextValueAsObjectID(newObject.objectID);
403: packet.setNextValueAsThreadID(targetThreadID);
404: packet.setNextValueAsClassID(typeIDSuper);
405: packet.setNextValueAsMethodID(targetMethodIDSuper);
406: packet.setNextValueAsInt(1);
407: packet.setNextValueAsValue(new Value(false));
408: packet
409: .setNextValueAsInt(JDWPConstants.InvokeOptions.INVOKE_NONVIRTUAL);
410: logWriter
411: .println("\nSend ObjectReference.InvokeMethod: nonvirtual super method without exception...");
412: reply = debuggeeWrapper.vmMirror.performCommand(packet);
413: checkReplyPacket(reply, "ObjectReference::InvokeMethod command");
414:
415: returnValue = reply.getNextValueAsValue();
416: logWriter
417: .println(" ObjectReference.InvokeMethod: returnValue.getIntValue()="
418: + returnValue.getIntValue());
419:
420: exception = reply.getNextValueAsTaggedObject();
421: logWriter
422: .println(" ObjectReference.InvokeMethod: exception.tag="
423: + exception.tag
424: + " exception.objectID="
425: + exception.objectID);
426:
427: assertTrue("returnValue must be != null", returnValue != null);
428: assertEquals("Invalid value,", 345, returnValue.getIntValue());
429:
430: assertTrue("exception must be != null", exception != null);
431: assertTrue("exception.objectID must be == 0",
432: exception.objectID == 0);
433: assertEquals(
434: "Invalid exception.tag",
435: JDWPConstants.Tag.OBJECT_TAG,
436: exception.tag,
437: JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG),
438: JDWPConstants.Tag.getName(exception.tag));
439:
440: assertAllDataRead(reply);
441:
442: // Let's resume application
443: resumeDebuggee();
444:
445: synchronizer
446: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
447: }
448:
449: public static void main(String[] args) {
450: junit.textui.TestRunner.run(InvokeMethodTest.class);
451: }
452:
453: }
|