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 Anton V. Karnachuk
021: * @version $Revision: 1.3 $
022: */
023:
024: /**
025: * Created on 24.02.2005
026: */package org.apache.harmony.jpda.tests.jdwp.StackFrame;
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.share.JPDADebuggeeSynchronizer;
034:
035: /**
036: * JDWP Unit test for StackFrame.ThisObject command.
037: */
038: public class ThisObjectTest extends JDWPStackFrameTestCase {
039:
040: public static String[] KNOWN_METHOD_NAMES = { "nestledMethod1",
041: "nestledMethod2", "nestledMethod3", };
042:
043: public static void main(String[] args) {
044: junit.textui.TestRunner.run(ThisObjectTest.class);
045: }
046:
047: /**
048: * This testcase exercises StackFrame.ThisObject command.
049: * <BR>The test starts StackTraceDebuggee and
050: * checks if StackFrame.ThisObject command returns correct data for each stack frame
051: * of main thread in debuggee, taking into account calls to known methods.
052: */
053: public void testThisObjectTest001() {
054: logWriter.println("==> ThisObjectTestTest001 started");
055: //boolean success = true;
056:
057: // select main thread
058: String mainThreadName = synchronizer.receiveMessage();
059: logWriter.println("==> Searching for main thread by name: "
060: + mainThreadName);
061: long mainThread = debuggeeWrapper.vmMirror
062: .getThreadID(mainThreadName);
063: logWriter.println("==> Found main thread: " + mainThread);
064:
065: // release on run()
066: synchronizer
067: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
068:
069: // pass nestledMethod1()
070: synchronizer
071: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
072: synchronizer
073: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
074:
075: // pass nestledMethod2()
076: synchronizer
077: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
078: synchronizer
079: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
080:
081: // enter nestledMethod3()
082: synchronizer
083: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
084:
085: if (mainThread == -1) {
086: logWriter.println("## FAILURE: main thread is not found!");
087: //assert True(false);
088: fail("main thread is not found!");
089: }
090:
091: // suspend thread
092: logWriter.println("==> Suspending main thread");
093: jdwpSuspendThread(mainThread);
094:
095: logWriter.println("==> Getting frames count");
096: int frameCount = jdwpGetFrameCount(mainThread);
097: logWriter.println("==> frames count = " + frameCount);
098:
099: logWriter.println("==> Getting frames");
100: FrameInfo[] frameIDs = jdwpGetFrames(mainThread, 0, frameCount);
101: logWriter.println("==> frames count = " + frameIDs.length);
102: assertEquals("Invlid number of frames,", frameCount,
103: frameIDs.length);
104: //assertTrue(frameIDs.length == frameCount);
105:
106: for (int i = 0; i < frameCount; i++) {
107: logWriter.println("\n==> frame #" + i);
108:
109: long frameID = frameIDs[i].frameID;
110: logWriter.println("==> frameID=" + frameID);
111: if (frameID == 0) {
112: logWriter
113: .println("## FAILURE: ThreadReference.Frames returned NULL FrameID for frame #"
114: + i);
115: //success = false;
116: fail("ThreadReference.Frames returned NULL FrameID for frame #"
117: + i);
118: continue;
119: }
120:
121: // logWriter.println(" location=" + frameIDs[i].location);
122:
123: String methodName = debuggeeWrapper.vmMirror.getMethodName(
124: frameIDs[i].location.classID,
125: frameIDs[i].location.methodID);
126: logWriter.println("==> method name=" + methodName);
127:
128: String methodSig = debuggeeWrapper.vmMirror
129: .getMethodSignature(frameIDs[i].location.classID,
130: frameIDs[i].location.methodID);
131: logWriter.println("==> method signature=" + methodSig);
132:
133: String classSig = debuggeeWrapper.vmMirror
134: .getClassSignature(frameIDs[i].location.classID);
135: logWriter.println("==> class signature=" + classSig);
136:
137: // get ThisObject
138: logWriter
139: .println("==> Send StackFrame::ThisObject command...");
140: CommandPacket packet = new CommandPacket(
141: JDWPCommands.StackFrameCommandSet.CommandSetID,
142: JDWPCommands.StackFrameCommandSet.ThisObjectCommand);
143: packet.setNextValueAsThreadID(mainThread);
144: packet.setNextValueAsLong(frameIDs[i].getFrameID());
145:
146: ReplyPacket reply = debuggeeWrapper.vmMirror
147: .performCommand(packet);
148: long knownMethodsThisObject = 0;
149: if (reply.getErrorCode() == JDWPConstants.Error.NONE) {
150: TaggedObject this Object = reply
151: .getNextValueAsTaggedObject();
152: logWriter.println("==> thisObject:");
153: logWriter.println("==> tag=" + this Object.tag + "("
154: + JDWPConstants.Tag.getName(this Object.tag)
155: + ")");
156: logWriter.println("==> id=" + this Object.objectID);
157: if (this Object.objectID != 0) {
158: long classID = getObjectReferenceType(this Object.objectID);
159: logWriter.println("==> class="
160: + debuggeeWrapper.vmMirror
161: .getClassSignature(classID));
162: }
163:
164: for (int j = 0; j < KNOWN_METHOD_NAMES.length; j++) {
165: if (KNOWN_METHOD_NAMES[j].equals(methodName)) {
166: logWriter
167: .println("==> frame for known method: "
168: + KNOWN_METHOD_NAMES[j]);
169: if (this Object.objectID == 0) {
170: logWriter
171: .println("## FAILURE: StackFrame.ThisObject returned NULL ObjectID for known method: "
172: + methodName);
173: //success = false;
174: fail("StackFrame.ThisObject returned NULL ObjectID for known method: "
175: + methodName);
176: } else {
177: if (knownMethodsThisObject != 0) {
178: if (knownMethodsThisObject != this Object.objectID) {
179: logWriter
180: .println("## FAILURE: Returned unexpected ObjectID for known method: "
181: + methodName);
182: logWriter
183: .println("## Expected ObjectID: "
184: + knownMethodsThisObject);
185: //success = false;
186: fail("Returned unexpected ObjectID for known method: "
187: + methodName);
188: }
189: } else {
190: knownMethodsThisObject = this Object.objectID;
191: }
192: }
193: if (this Object.tag != JDWPConstants.Tag.OBJECT_TAG) {
194: logWriter
195: .println("## FAILURE: StackFrame.ThisObject returned not OBJECT_TAG for known method: "
196: + methodName);
197: //success = false;
198: fail("StackFrame.ThisObject returned not OBJECT_TAG for known method: "
199: + methodName);
200: }
201: }
202: }
203:
204: String mainMethod = "main";
205: if (mainMethod.equals(methodName)) {
206: logWriter.println("==> frame for method: "
207: + mainMethod);
208: if (this Object.objectID != 0) {
209: logWriter
210: .println("## FAILURE: Returned unexpected ObjectID for method: "
211: + mainMethod);
212: logWriter.println("## Expected ObjectID: " + 0);
213: //success = false;
214: fail("Returned unexpected ObjectID for method: "
215: + mainMethod);
216: }
217: if (this Object.tag != JDWPConstants.Tag.OBJECT_TAG) {
218: logWriter
219: .println("## FAILURE: StackFrame.ThisObject returned not OBJECT_TAG for method: "
220: + mainMethod);
221: //success = false;
222: fail("StackFrame.ThisObject returned not OBJECT_TAG for method: "
223: + mainMethod);
224: }
225: }
226:
227: assertAllDataRead(reply);
228: /*if (!reply.isAllDataRead()) {
229: logWriter.println("## FAILURE: Extra bytes in reply for StackFrame.ThisObject");
230: success = false;
231: }*/
232:
233: } else {
234: logWriter
235: .println("## FAILURE: StackFrame::ThisObject command returns unexpected ERROR = "
236: + reply.getErrorCode()
237: + "("
238: + JDWPConstants.Error.getName(reply
239: .getErrorCode()) + ")");
240: logWriter.println("## Expected ERROR = "
241: + JDWPConstants.Error.NONE
242: + "("
243: + JDWPConstants.Error
244: .getName(JDWPConstants.Error.NONE)
245: + ")");
246: //success = false;
247: fail("StackFrame::ThisObject command returns unexpected ERROR = "
248: + reply.getErrorCode()
249: + "("
250: + JDWPConstants.Error.getName(reply
251: .getErrorCode())
252: + ")"
253: + ", Expected ERROR = "
254: + JDWPConstants.Error.NONE
255: + "("
256: + JDWPConstants.Error
257: .getName(JDWPConstants.Error.NONE)
258: + ")");
259: }
260: }
261:
262: // resume thread
263: logWriter.println("==> Resuming main thread");
264: jdwpResumeThread(mainThread);
265:
266: // release nestledMethod3()
267: synchronizer
268: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
269:
270: //assertTrue(success);
271: logWriter.println("==> ThisObjectTestTest001 finished");
272: }
273: }
|