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 11.03.2005
026: */package org.apache.harmony.jpda.tests.jdwp.ClassType;
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: * JDWP unit test for ClassType.NewInstance command.
039: */
040: public class NewInstanceTest extends JDWPSyncTestCase {
041:
042: protected String getDebuggeeClassName() {
043: return "org.apache.harmony.jpda.tests.jdwp.share.debuggee.InvokeMethodDebuggee";
044: }
045:
046: /**
047: * This testcase exercises ClassType.NewInstance command.
048: * <BR>At first the test starts debuggee.
049: * <BR>Then does the following checks:
050: * <BR> - send ClassType.NewInstance command for class,
051: * constructor of which should not throw any Exception, and checks,
052: * that returned new object has expected attributes and returned
053: * exception object is null;
054: * <BR> - send ClassType.NewInstance command for class,
055: * constructor of which should throw some Exception, and checks,
056: * that returned new object is null and returned exception object
057: * is not null and has expected attributes;
058: */
059: public void testNewInstance001() {
060: synchronizer
061: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
062:
063: // Get referenceTypeID
064: CommandPacket packet = new CommandPacket(
065: JDWPCommands.VirtualMachineCommandSet.CommandSetID,
066: JDWPCommands.VirtualMachineCommandSet.ClassesBySignatureCommand);
067: String classSig = "Lorg/apache/harmony/jpda/tests/jdwp/share/debuggee/testClass2;";
068: packet.setNextValueAsString(classSig);
069: ReplyPacket reply = debuggeeWrapper.vmMirror
070: .performCommand(packet);
071: checkReplyPacket(reply,
072: "VirtualMachine::ClassesBySignature command");
073:
074: int classes = reply.getNextValueAsInt();
075: assertTrue(classes == 1); //this class may be loaded only once
076: byte refTypeTag = reply.getNextValueAsByte();
077: long typeID = reply.getNextValueAsReferenceTypeID();
078: int status = reply.getNextValueAsInt();
079: logWriter
080: .println(" VirtualMachine.ClassesBySignature: classes="
081: + classes + "; refTypeTag=" + refTypeTag
082: + "; typeID= " + typeID + "; status=" + status);
083:
084: assertAllDataRead(reply);
085: assertEquals(
086: "VirtualMachine::ClassesBySignature returned invalid TypeTag,",
087: JDWPConstants.TypeTag.CLASS, refTypeTag,
088: JDWPConstants.TypeTag
089: .getName(JDWPConstants.TypeTag.CLASS),
090: JDWPConstants.TypeTag.getName(refTypeTag));
091:
092: // Get methodID
093: packet = new CommandPacket(
094: JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
095: JDWPCommands.ReferenceTypeCommandSet.MethodsCommand);
096: packet.setNextValueAsClassID(typeID);
097: reply = debuggeeWrapper.vmMirror.performCommand(packet);
098: checkReplyPacket(reply, "ReferenceType::Methods command");
099:
100: int declared = reply.getNextValueAsInt();
101: logWriter.println(" ReferenceType.Methods: declared="
102: + declared);
103: long targetMethodID = 0;
104: for (int i = 0; i < declared; i++) {
105: long methodID = reply.getNextValueAsMethodID();
106: String name = reply.getNextValueAsString();
107: String signature = reply.getNextValueAsString();
108: int modBits = reply.getNextValueAsInt();
109: logWriter.println(" methodID=" + methodID + "; name="
110: + name + "; signature=" + signature + "; modBits="
111: + modBits);
112: if (name.equals("<init>")) {
113: targetMethodID = methodID;
114: }
115: }
116: assertAllDataRead(reply);
117:
118: // Set EventRequest
119: packet = new CommandPacket(
120: JDWPCommands.EventRequestCommandSet.CommandSetID,
121: JDWPCommands.EventRequestCommandSet.SetCommand);
122: packet.setNextValueAsByte(JDWPConstants.EventKind.METHOD_ENTRY);
123: packet.setNextValueAsByte(JDWPConstants.SuspendPolicy.ALL);
124: packet.setNextValueAsInt(1);
125: packet.setNextValueAsByte((byte) 5);
126: packet.setNextValueAsString("*.InvokeMethodDebuggee");
127:
128: reply = debuggeeWrapper.vmMirror.performCommand(packet);
129: checkReplyPacket(reply, "EventRequest::Set command");
130:
131: int requestID = reply.getNextValueAsInt();
132: logWriter.println(" EventRequest.Set: requestID=" + requestID);
133: assertAllDataRead(reply);
134: synchronizer
135: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
136:
137: long targetThreadID = 0;
138: // Wait event
139: CommandPacket event = debuggeeWrapper.vmMirror
140: .receiveCertainEvent(JDWPConstants.EventKind.METHOD_ENTRY);
141: byte suspendPolicy = event.getNextValueAsByte();
142: int events = event.getNextValueAsInt();
143: logWriter.println(" EVENT_THREAD event: suspendPolicy="
144: + suspendPolicy + " events=" + events);
145: for (int i = 0; i < events; i++) {
146: byte eventKind = event.getNextValueAsByte();
147: int newRequestID = event.getNextValueAsInt();
148: long threadID = event.getNextValueAsThreadID();
149: //Location location =
150: event.getNextValueAsLocation();
151: logWriter.println(" EVENT_THREAD event " + i
152: + ": eventKind=" + eventKind + "; requestID="
153: + newRequestID + "; threadID=" + threadID);
154: if (newRequestID == requestID) {
155: targetThreadID = threadID;
156: }
157: }
158: assertAllDataRead(event);
159: assertTrue("Invalid targetThreadID, must be != 0",
160: targetThreadID != 0);
161:
162: // Let's clear event request
163: packet = new CommandPacket(
164: JDWPCommands.EventRequestCommandSet.CommandSetID,
165: JDWPCommands.EventRequestCommandSet.ClearCommand);
166: packet.setNextValueAsByte(JDWPConstants.EventKind.METHOD_ENTRY);
167: packet.setNextValueAsInt(requestID);
168: reply = debuggeeWrapper.vmMirror.performCommand(packet);
169: checkReplyPacket(reply, "EventRequest::Clear command");
170: assertAllDataRead(reply);
171:
172: // Make NewInstance without Exception
173: packet = new CommandPacket(
174: JDWPCommands.ClassTypeCommandSet.CommandSetID,
175: JDWPCommands.ClassTypeCommandSet.NewInstanceCommand);
176: packet.setNextValueAsClassID(typeID);
177: packet.setNextValueAsThreadID(targetThreadID);
178: packet.setNextValueAsMethodID(targetMethodID);
179: packet.setNextValueAsInt(1);
180: packet.setNextValueAsValue(new Value(false));
181: packet.setNextValueAsInt(0);
182: logWriter
183: .println(" Send ClassType.NewInstance (without Exception)");
184: reply = debuggeeWrapper.vmMirror.performCommand(packet);
185: checkReplyPacket(reply, "ClassType::NewInstance command");
186:
187: TaggedObject newObject = reply.getNextValueAsTaggedObject();
188: logWriter.println(" ClassType.NewInstance: newObject.tag="
189: + newObject.tag + "; newObject.objectID="
190: + newObject.objectID);
191: TaggedObject exception = reply.getNextValueAsTaggedObject();
192: logWriter.println(" ClassType.NewInstance: exception.tag="
193: + exception.tag + "; exception.objectID="
194: + exception.objectID);
195:
196: assertNotNull("newObject is null", newObject);
197: assertTrue("newObject.objectID is 0", newObject.objectID != 0);
198: assertEquals(
199: "ClassType::NewInstance returned invalid newObject.tag,",
200: JDWPConstants.Tag.OBJECT_TAG,
201: newObject.tag,
202: JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG),
203: JDWPConstants.Tag.getName(newObject.tag));
204:
205: assertNotNull("exception is null", newObject);
206: assertEquals(
207: "ClassType::NewInstance returned invalid exception.objectID,",
208: 0, exception.objectID);
209: assertTrue(exception.tag == JDWPConstants.Tag.OBJECT_TAG);
210: assertAllDataRead(reply);
211:
212: // Let's check object reference
213: packet = new CommandPacket(
214: JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
215: JDWPCommands.ObjectReferenceCommandSet.ReferenceTypeCommand);
216: packet.setNextValueAsObjectID(newObject.objectID);
217: reply = debuggeeWrapper.vmMirror.performCommand(packet);
218: checkReplyPacket(reply,
219: "ObjectReference::ReferenceType command");
220:
221: byte newRefTypeTag = reply.getNextValueAsByte();
222: long newTypeID = reply.getNextValueAsReferenceTypeID();
223: logWriter.println(" ObjectReference.ReferenceType: refTypeTag="
224: + newRefTypeTag + "; typeID= " + newTypeID);
225: assertEquals(
226: "ObjectReference::ReferenceType returned invalid newRefTypeTag,",
227: JDWPConstants.TypeTag.CLASS, newRefTypeTag,
228: JDWPConstants.TypeTag
229: .getName(JDWPConstants.TypeTag.CLASS),
230: JDWPConstants.TypeTag.getName(newRefTypeTag));
231: assertEquals("Invalid type ID,", typeID, newTypeID);
232: assertAllDataRead(reply);
233:
234: // Make NewInstance with Exception
235: packet = new CommandPacket(
236: JDWPCommands.ClassTypeCommandSet.CommandSetID,
237: JDWPCommands.ClassTypeCommandSet.NewInstanceCommand);
238: packet.setNextValueAsClassID(typeID);
239: packet.setNextValueAsThreadID(targetThreadID);
240: packet.setNextValueAsMethodID(targetMethodID);
241: packet.setNextValueAsInt(1);
242: packet.setNextValueAsValue(new Value(true));
243: packet.setNextValueAsInt(0);
244: logWriter
245: .println(" Send ClassType.NewInstance (with Exception)");
246: reply = debuggeeWrapper.vmMirror.performCommand(packet);
247: checkReplyPacket(reply, "ClassType::NewInstance command");
248:
249: newObject = reply.getNextValueAsTaggedObject();
250: logWriter.println(" ClassType.NewInstance: newObject.tag="
251: + newObject.tag + "; newObject.objectID="
252: + newObject.objectID);
253: exception = reply.getNextValueAsTaggedObject();
254: logWriter.println(" ClassType.NewInstance: exception.tag="
255: + exception.tag + "; exception.objectID="
256: + exception.objectID);
257: assertNotNull("newObject is null", newObject);
258: assertEquals(
259: "ClassType::NewInstance returned invalid newObject.objectID,",
260: 0, newObject.objectID);
261: assertEquals(
262: "ClassType::NewInstance returned invalid invalid newObject.tag,",
263: JDWPConstants.Tag.OBJECT_TAG,
264: newObject.tag,
265: JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG),
266: JDWPConstants.Tag.getName(newObject.tag));
267:
268: assertNotNull(
269: "ClassType::NewInstance returned invalid exception = null",
270: newObject);
271: assertTrue(
272: "ClassType::NewInstance returned invalid exception.objectID = 0",
273: exception.objectID != 0);
274: assertEquals(
275: "ClassType::NewInstance returned invalid invalid exception.tag,",
276: JDWPConstants.Tag.OBJECT_TAG,
277: exception.tag,
278: JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG),
279: JDWPConstants.Tag.getName(exception.tag));
280: assertAllDataRead(reply);
281:
282: // Let's resume application
283: packet = new CommandPacket(
284: JDWPCommands.VirtualMachineCommandSet.CommandSetID,
285: JDWPCommands.VirtualMachineCommandSet.ResumeCommand);
286: reply = debuggeeWrapper.vmMirror.performCommand(packet);
287: checkReplyPacket(reply, "VirtualMachine::Resume command");
288: assertAllDataRead(reply);
289:
290: synchronizer
291: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
292: }
293:
294: /**
295: * This testcase exercises ClassType.NewInstance command.
296: * <BR>At first the test starts debuggee.
297: * <BR>Then does the following check:
298: * <BR> - send ClassType.NewInstance command for class
299: * and passes invalid arguments' list for constructor.
300: * <BR>The testcase expects that ILLEGAL_ARGUMENT error is returned.
301: */
302: public void testNewInstance002() {
303: synchronizer
304: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
305:
306: // Get referenceTypeID
307: CommandPacket packet = new CommandPacket(
308: JDWPCommands.VirtualMachineCommandSet.CommandSetID,
309: JDWPCommands.VirtualMachineCommandSet.ClassesBySignatureCommand);
310: String classSig = "Lorg/apache/harmony/jpda/tests/jdwp/share/debuggee/InvokeMethodDebuggee$testClass1;";
311: packet.setNextValueAsString(classSig);
312: ReplyPacket reply = debuggeeWrapper.vmMirror
313: .performCommand(packet);
314: checkReplyPacket(reply,
315: "VirtualMachine::ClassesBySignature command");
316:
317: int classes = reply.getNextValueAsInt();
318: assertEquals(
319: "VirtualMachine::ClassesBySignature returned invalid cluss number,",
320: 1, classes); //this class may be loaded only once
321: byte refTypeTag = reply.getNextValueAsByte();
322: long typeID = reply.getNextValueAsReferenceTypeID();
323: int status = reply.getNextValueAsInt();
324: logWriter
325: .println(" VirtualMachine.ClassesBySignature: classes="
326: + classes + "; refTypeTag=" + refTypeTag
327: + "; typeID= " + typeID + "; status=" + status);
328:
329: assertAllDataRead(reply);
330: assertEquals(
331: "VirtualMachine::ClassesBySignature returned invalid TypeTag,",
332: JDWPConstants.TypeTag.CLASS, refTypeTag,
333: JDWPConstants.TypeTag
334: .getName(JDWPConstants.TypeTag.CLASS),
335: JDWPConstants.TypeTag.getName(refTypeTag));
336:
337: // Get methodID
338: packet = new CommandPacket(
339: JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
340: JDWPCommands.ReferenceTypeCommandSet.MethodsCommand);
341: packet.setNextValueAsClassID(typeID);
342: reply = debuggeeWrapper.vmMirror.performCommand(packet);
343: checkReplyPacket(reply, "ReferenceType::Methods command");
344:
345: int declared = reply.getNextValueAsInt();
346: logWriter.println(" ReferenceType.Methods: declared="
347: + declared);
348: long targetMethodID = 0;
349: for (int i = 0; i < declared; i++) {
350: long methodID = reply.getNextValueAsMethodID();
351: String name = reply.getNextValueAsString();
352: String signature = reply.getNextValueAsString();
353: int modBits = reply.getNextValueAsInt();
354: logWriter.println(" methodID=" + methodID + "; name="
355: + name + "; signature=" + signature + "; modBits="
356: + modBits);
357: if (name.equals("<init>")) {
358: targetMethodID = methodID;
359: }
360: }
361: assertAllDataRead(reply);
362:
363: // Set EventRequest
364: packet = new CommandPacket(
365: JDWPCommands.EventRequestCommandSet.CommandSetID,
366: JDWPCommands.EventRequestCommandSet.SetCommand);
367: packet.setNextValueAsByte(JDWPConstants.EventKind.METHOD_ENTRY);
368: packet.setNextValueAsByte(JDWPConstants.SuspendPolicy.ALL);
369: packet.setNextValueAsInt(1);
370: packet.setNextValueAsByte((byte) 5);
371: packet.setNextValueAsString("*.InvokeMethodDebuggee");
372:
373: reply = debuggeeWrapper.vmMirror.performCommand(packet);
374: checkReplyPacket(reply, "EventRequest::Set command");
375:
376: int requestID = reply.getNextValueAsInt();
377: logWriter.println(" EventRequest.Set: requestID=" + requestID);
378: assertAllDataRead(reply);
379: synchronizer
380: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
381:
382: long targetThreadID = 0;
383: // Wait event
384: CommandPacket event = debuggeeWrapper.vmMirror
385: .receiveCertainEvent(JDWPConstants.EventKind.METHOD_ENTRY);
386: byte suspendPolicy = event.getNextValueAsByte();
387: int events = event.getNextValueAsInt();
388: logWriter.println(" EVENT_THREAD event: suspendPolicy="
389: + suspendPolicy + " events=" + events);
390: for (int i = 0; i < events; i++) {
391: byte eventKind = event.getNextValueAsByte();
392: int newRequestID = event.getNextValueAsInt();
393: long threadID = event.getNextValueAsThreadID();
394: //Location location =
395: event.getNextValueAsLocation();
396: logWriter.println(" EVENT_THREAD event " + i
397: + ": eventKind=" + eventKind + "; requestID="
398: + newRequestID + "; threadID=" + threadID);
399: if (newRequestID == requestID) {
400: targetThreadID = threadID;
401: }
402: }
403: assertAllDataRead(event);
404: assertTrue("Invalid targetThreadID, must be != 0",
405: targetThreadID != 0);
406:
407: // Let's clear event request
408: packet = new CommandPacket(
409: JDWPCommands.EventRequestCommandSet.CommandSetID,
410: JDWPCommands.EventRequestCommandSet.ClearCommand);
411: packet.setNextValueAsByte(JDWPConstants.EventKind.METHOD_ENTRY);
412: packet.setNextValueAsInt(requestID);
413: reply = debuggeeWrapper.vmMirror.performCommand(packet);
414: checkReplyPacket(reply, "EventRequest::Clear command");
415: assertAllDataRead(reply);
416:
417: // Make NewInstance without Exception
418: packet = new CommandPacket(
419: JDWPCommands.ClassTypeCommandSet.CommandSetID,
420: JDWPCommands.ClassTypeCommandSet.NewInstanceCommand);
421: packet.setNextValueAsClassID(typeID);
422: packet.setNextValueAsThreadID(targetThreadID);
423: packet.setNextValueAsMethodID(targetMethodID);
424: packet.setNextValueAsInt(0); // Providing of 'this' arg missed!
425: packet.setNextValueAsInt(0); // This int value will be interpreted as
426: // reference!?
427: logWriter.println(" Send ClassType.NewInstance");
428: reply = debuggeeWrapper.vmMirror.performCommand(packet);
429:
430: short error = reply.getErrorCode();
431: logWriter.println(" ClassType.NewInstance: ErrorCode=" + error
432: + "(" + JDWPConstants.Error.getName(error) + ")");
433: assertEquals(
434: "ClassType.NewInstance returned invalid error code,",
435: JDWPConstants.Error.ILLEGAL_ARGUMENT, error,
436: JDWPConstants.Error
437: .getName(JDWPConstants.Error.ILLEGAL_ARGUMENT),
438: JDWPConstants.Error.getName(error));
439: logWriter.println(" It is EXPECTED ERROR!");
440: assertAllDataRead(reply);
441:
442: // Let's resume application
443: packet = new CommandPacket(
444: JDWPCommands.VirtualMachineCommandSet.CommandSetID,
445: JDWPCommands.VirtualMachineCommandSet.ResumeCommand);
446: reply = debuggeeWrapper.vmMirror.performCommand(packet);
447: checkReplyPacket(reply, "VirtualMachine::Resume command");
448: assertAllDataRead(reply);
449:
450: synchronizer
451: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
452: }
453:
454: public static void main(String[] args) {
455: junit.textui.TestRunner.run(NewInstanceTest.class);
456: }
457: }
|