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 Vitaly A. Provodin
021: * @version $Revision: 1.4 $
022: */
023:
024: /**
025: * Created on 15.02.2005
026: */package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
027:
028: import java.util.Vector;
029: import java.util.Enumeration;
030:
031: import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
032: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
033: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
034: import org.apache.harmony.jpda.tests.framework.jdwp.Location;
035: import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
036: import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
037: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
038:
039: /**
040: * JDWP Unit test for ThreadReference.Frames command.
041: */
042: public class FramesTest extends JDWPSyncTestCase {
043:
044: short err;
045: long threadID;
046:
047: class FrameStruct {
048: long frameID;
049: Location loc;
050:
051: FrameStruct(long frameID, Location loc) {
052: this .frameID = frameID;
053: this .loc = loc;
054: }
055: }
056:
057: protected String getDebuggeeClassName() {
058: return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.FramesDebuggee";
059: }
060:
061: /**
062: * This testcase exercises ThreadReference.Frames command.
063: * <BR>At first the test starts FramesDebuggee which recursively invokes
064: * the method 'FramesDebuggee.recursiveMethod' thereby the specified depth
065: * 'FramesDebuggee.DEPTH' of recursion is reached.
066: * <BR> Then the tests performs the ThreadReference.Frames command
067: * for the main thread with parameters:
068: * <BR>startFrame=0, length=(amount_of_all_frames + 1)
069: * <BR>It is expected the error INVALID_LENGTH is returned.
070: */
071: public void testFrames005() {
072: logWriter.println("==> testFrames005 START ");
073: String testedThreadName = synchronizer.receiveMessage();
074:
075: logWriter.println("==> testedThreadName = |" + testedThreadName
076: + "|");
077: threadID = debuggeeWrapper.vmMirror
078: .getThreadID(testedThreadName);
079: logWriter.println("==> threadID = " + threadID);
080: debuggeeWrapper.vmMirror.suspendThread(threadID);
081:
082: Vector allFrames = getFrames(0, -1);
083: if (err != JDWPConstants.Error.NONE) {
084: printErrorAndFail("Unexpected ERROR = " + err + "("
085: + JDWPConstants.Error.getName(err) + ")");
086: }
087: String methodName, classSignature;
088: int i = 0;
089: for (Enumeration e = allFrames.elements(); e.hasMoreElements(); i++) {
090: FrameStruct frame = (FrameStruct) e.nextElement();
091: methodName = getMethodName(frame.loc.classID,
092: frame.loc.methodID);
093: classSignature = getClassSignature(frame.loc.classID);
094: logWriter.println("\t" + i + ". frameID=" + frame.frameID
095: + " - " + classSignature + methodName + "("
096: + frame.loc.index + ")");
097: }
098:
099: allFrames = getFrames(0, allFrames.size() + 1);
100: if (err == JDWPConstants.Error.INVALID_LENGTH) {
101: logWriter.println("Cought expected error - "
102: + JDWPConstants.Error.getName(err) + "(" + err
103: + ")");
104: } else {
105: printErrorAndFail("unexpected behaviour: error is "
106: + JDWPConstants.Error.getName(err)
107: + "("
108: + err
109: + ")"
110: + " but must be "
111: + JDWPConstants.Error
112: .getName(JDWPConstants.Error.INVALID_LENGTH)
113: + "(" + JDWPConstants.Error.INVALID_LENGTH + ")");
114: }
115: logWriter.println("==> testFrames005 OK. ");
116: debuggeeWrapper.vmMirror.resumeThread(threadID);
117:
118: synchronizer
119: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
120: }
121:
122: /**
123: * This testcase exercises ThreadReference.Frames command.
124: * <BR>At first the test starts FramesDebuggee which recursively invokes
125: * the method 'FramesDebuggee.recursiveMethod' thereby the specified depth
126: * 'FramesDebuggee.DEPTH' of recursion is reached.
127: * <BR> Then the tests performs the ThreadReference.Frames command
128: * for the main thread with parameters:
129: * <BR>startFrame=(amount_of_all_frames + 1), length=-1
130: * <BR>It is expected the error INVALID_INDEX is returned.
131: */
132: public void testFrames004() {
133: logWriter.println("==> testFrames004 START ");
134: String testedThreadName = synchronizer.receiveMessage();
135:
136: logWriter.println("==> testedThreadName = |" + testedThreadName
137: + "|");
138: threadID = debuggeeWrapper.vmMirror
139: .getThreadID(testedThreadName);
140: logWriter.println("==> threadID = " + threadID);
141: debuggeeWrapper.vmMirror.suspendThread(threadID);
142:
143: Vector allFrames = getFrames(0, -1);
144: if (err != JDWPConstants.Error.NONE) {
145: printErrorAndFail("Unexpected ERROR = " + err + "("
146: + JDWPConstants.Error.getName(err) + ")");
147: }
148: String methodName, classSignature;
149: int i = 0;
150: for (Enumeration e = allFrames.elements(); e.hasMoreElements(); i++) {
151: FrameStruct frame = (FrameStruct) e.nextElement();
152: methodName = getMethodName(frame.loc.classID,
153: frame.loc.methodID);
154: classSignature = getClassSignature(frame.loc.classID);
155: logWriter.println("\t" + i + ". frameID=" + frame.frameID
156: + " - " + classSignature + methodName + "("
157: + frame.loc.index + ")");
158: }
159:
160: allFrames = getFrames(allFrames.size() + 1, -1);
161: if (err == JDWPConstants.Error.INVALID_INDEX) {
162: logWriter.println("Cought expected error - "
163: + JDWPConstants.Error.getName(err) + "(" + err
164: + ")");
165: } else {
166: printErrorAndFail("unexpected behaviour: error is "
167: + JDWPConstants.Error.getName(err)
168: + "("
169: + err
170: + ")"
171: + " but must be "
172: + JDWPConstants.Error
173: .getName(JDWPConstants.Error.INVALID_INDEX)
174: + "(" + JDWPConstants.Error.INVALID_INDEX + ")");
175: }
176:
177: logWriter.println("==> testFrames004 OK. ");
178: debuggeeWrapper.vmMirror.resumeThread(threadID);
179:
180: synchronizer
181: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
182: }
183:
184: /**
185: * This testcase exercises ThreadReference.Frames command.
186: * <BR>At first the test starts FramesDebuggee which recursively invokes
187: * the method 'FramesDebuggee.recursiveMethod' thereby the specified depth
188: * 'FramesDebuggee.DEPTH' of recursion is reached.
189: * <BR> Then the tests performs the ThreadReference.Frames command
190: * for the main thread with parameters:
191: * <BR>startFrame=amount_of_all_frames, length=-1
192: * <BR>It is expected an empty set of frames is returned.
193: */
194: public void testFrames003() {
195: logWriter.println("==> testFrames003 START ");
196: String testedThreadName = synchronizer.receiveMessage();
197:
198: logWriter.println("==> testedThreadName = |" + testedThreadName
199: + "|");
200: threadID = debuggeeWrapper.vmMirror
201: .getThreadID(testedThreadName);
202: logWriter.println("==> threadID = " + threadID);
203: debuggeeWrapper.vmMirror.suspendThread(threadID);
204:
205: Vector allFrames = getFrames(0, -1);
206: if (err != JDWPConstants.Error.NONE) {
207: printErrorAndFail("Unexpected ERROR = " + err + "("
208: + JDWPConstants.Error.getName(err) + ")");
209: }
210: String methodName, classSignature;
211: int i = 0;
212: for (Enumeration e = allFrames.elements(); e.hasMoreElements(); i++) {
213: FrameStruct frame = (FrameStruct) e.nextElement();
214: methodName = getMethodName(frame.loc.classID,
215: frame.loc.methodID);
216: classSignature = getClassSignature(frame.loc.classID);
217: logWriter.println("\t" + i + ". frameID=" + frame.frameID
218: + " - " + classSignature + methodName + "("
219: + frame.loc.index + ")");
220: }
221:
222: allFrames = getFrames(allFrames.size(), -1);
223: if (err != JDWPConstants.Error.NONE) {
224: printErrorAndFail("Unexpected ERROR = " + err + "("
225: + JDWPConstants.Error.getName(err) + ")");
226: }
227: if (allFrames.size() == 0) {
228: logWriter.println("empty set of frames is returned");
229: } else {
230: printErrorAndFail("it is expected an empty set of frames, but frameCount = "
231: + allFrames.size());
232: }
233:
234: logWriter.println("==> testFrames003 OK. ");
235: debuggeeWrapper.vmMirror.resumeThread(threadID);
236:
237: synchronizer
238: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
239: }
240:
241: /**
242: * This testcase exercises ThreadReference.Frames command.
243: * <BR>At first the test starts FramesDebuggee which recursively invokes
244: * the method 'FramesDebuggee.recursiveMethod' thereby the specified depth
245: * 'FramesDebuggee.DEPTH' of recursion is reached.
246: * <BR>Then the test by ThreadReference.Frames command
247: * requests all frames and looks for the first frame
248: * which has location in the 'recursiveMethod'.
249: * <BR>The index of such frame is passed as startFrame parameter for
250: * the second invocation of ThreadReference.Frames command.
251: * <BR>The length (the second parameter) is set to 'FramesDebuggee.DEPTH'.
252: * <BR>It is expected that the amount of returned frames is equal to
253: * 'FramesDebuggee.DEPTH' and all returned frames have locations in
254: * 'recursiveMethod'.
255: */
256: public void testFrames002() {
257: logWriter.println("==> testFrames002 START ");
258: String testedThreadName = synchronizer.receiveMessage();
259:
260: logWriter.println("==> testedThreadName = |" + testedThreadName
261: + "|");
262: threadID = debuggeeWrapper.vmMirror
263: .getThreadID(testedThreadName);
264: logWriter.println("==> threadID = " + threadID);
265: debuggeeWrapper.vmMirror.suspendThread(threadID);
266:
267: Vector allFrames = getFrames(0, -1);
268: if (err != JDWPConstants.Error.NONE) {
269: printErrorAndFail("Unexpected ERROR = " + err + "("
270: + JDWPConstants.Error.getName(err) + ")");
271: }
272: String methodName, classSignature;
273: int frameNumber = -1;
274: int i = 0;
275: for (Enumeration e = allFrames.elements(); e.hasMoreElements(); i++) {
276: FrameStruct frame = (FrameStruct) e.nextElement();
277: methodName = getMethodName(frame.loc.classID,
278: frame.loc.methodID);
279: classSignature = getClassSignature(frame.loc.classID);
280: if (frameNumber < 0
281: && FramesDebuggee.METHOD_NAME.equals(methodName)) {
282: frameNumber = i;
283: }
284: logWriter.println("\t" + i + ". frameID=" + frame.frameID
285: + " - " + classSignature + methodName + "("
286: + frame.loc.index + ")");
287: }
288:
289: if (frameNumber < 0) {
290: printErrorAndFail("frameNumber is unexpectedly equal to "
291: + frameNumber);
292: }
293:
294: allFrames = getFrames(frameNumber, FramesDebuggee.DEPTH);
295: if (err != JDWPConstants.Error.NONE) {
296: printErrorAndFail("Unexpected ERROR = " + err + "("
297: + JDWPConstants.Error.getName(err) + ")");
298: }
299: i = frameNumber;
300: int methodCount = 0;
301: String unexpectedMethods = null;
302: String depthError = null;
303: for (Enumeration e = allFrames.elements(); e.hasMoreElements(); i++) {
304: FrameStruct frame = (FrameStruct) e.nextElement();
305: methodName = getMethodName(frame.loc.classID,
306: frame.loc.methodID);
307: classSignature = getClassSignature(frame.loc.classID);
308: logWriter.println("\t" + i + ". frameID=" + frame.frameID
309: + " - " + classSignature + methodName + "("
310: + frame.loc.index + ")");
311:
312: if (methodName.equals(FramesDebuggee.METHOD_NAME)) {
313: methodCount++;
314: } else {
315: logWriter.printError("unexpected method - "
316: + methodName);
317: unexpectedMethods = null == unexpectedMethods ? methodName
318: : unexpectedMethods + "," + methodName;
319: }
320: }
321:
322: if (methodCount != FramesDebuggee.DEPTH) {
323: logWriter
324: .printError(depthError = ("Number of "
325: + FramesDebuggee.METHOD_NAME
326: + " in frames " + methodCount
327: + " but expected " + FramesDebuggee.DEPTH));
328: }
329:
330: debuggeeWrapper.vmMirror.resumeThread(threadID);
331: synchronizer
332: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
333:
334: if (null != unexpectedMethods) {
335: logWriter.println("==> testFrames002 FAILED ");
336: fail("unexpected method(s): " + unexpectedMethods);
337: } else if (null != depthError) {
338: logWriter.println("==> testFrames002 FAILED ");
339: fail(depthError);
340: } else {
341: logWriter.println("==> testFrames002 OK. ");
342: }
343: }
344:
345: /**
346: * This testcase exercises ThreadReference.Frames command.
347: * <BR>At first the test starts FramesDebuggee which recursively invokes
348: * the method 'FramesDebuggee.recursiveMethod' thereby the specified depth
349: * 'FramesDebuggee.DEPTH' of recursion is reached.
350: * <BR>Then the test by ThreadReference.Frames command
351: * requests all frames and looks for the first frame
352: * which has location in the 'recursiveMethod'.
353: * <BR>The index of such frame is passed as startFrame parameter for
354: * the second invocation of ThreadReference.Frames command.
355: * <BR>The length (the second parameter) is set to '-1'.
356: * <BR>It is expected that the amount of returned frames which are located in
357: * 'recursiveMethod' is equal to 'FramesDebuggee.DEPTH'.
358: */
359: public void testFrames001() {
360: logWriter.println("==> testFrames001 START ");
361: String testedThreadName = synchronizer.receiveMessage();
362:
363: logWriter.println("==> testedThreadName = |" + testedThreadName
364: + "|");
365: threadID = debuggeeWrapper.vmMirror
366: .getThreadID(testedThreadName);
367: logWriter.println("==> threadID = " + threadID);
368: debuggeeWrapper.vmMirror.suspendThread(threadID);
369:
370: Vector allFrames = getFrames(0, -1);
371: if (err != JDWPConstants.Error.NONE) {
372: printErrorAndFail("Unexpected ERROR = " + err + "("
373: + JDWPConstants.Error.getName(err) + ")");
374: }
375: String methodName, classSignature;
376: int frameNumber = -1;
377: int i = 0;
378: for (Enumeration e = allFrames.elements(); e.hasMoreElements(); i++) {
379: FrameStruct frame = (FrameStruct) e.nextElement();
380: methodName = getMethodName(frame.loc.classID,
381: frame.loc.methodID);
382: classSignature = getClassSignature(frame.loc.classID);
383: if (frameNumber < 0
384: && FramesDebuggee.METHOD_NAME.equals(methodName)) {
385: frameNumber = i;
386: }
387: logWriter.println("\t" + i + ". frameID=" + frame.frameID
388: + " - " + classSignature + methodName + "("
389: + frame.loc.index + ")");
390: }
391:
392: if (frameNumber < 0) {
393: logWriter
394: .printError("frameNumber is unexpectedly equal to "
395: + frameNumber);
396: assertTrue("Invalid frameNumber", frameNumber > 0);
397: }
398:
399: allFrames = getFrames(frameNumber, -1);
400: if (err != JDWPConstants.Error.NONE) {
401: printErrorAndFail("Unexpected ERROR = " + err + "("
402: + JDWPConstants.Error.getName(err) + ")");
403: }
404: i = frameNumber;
405: int methodCount = 0;
406: boolean testCondition;
407: String unexpectedMethods = null;
408: String depthError = null;
409: for (Enumeration e = allFrames.elements(); e.hasMoreElements(); i++) {
410: FrameStruct frame = (FrameStruct) e.nextElement();
411: methodName = getMethodName(frame.loc.classID,
412: frame.loc.methodID);
413: classSignature = getClassSignature(frame.loc.classID);
414: logWriter.println("\t" + i + ". frameID=" + frame.frameID
415: + " - " + classSignature + methodName + "("
416: + frame.loc.index + ")");
417: testCondition = (i == frameNumber && !methodName
418: .equals(FramesDebuggee.METHOD_NAME));
419: if (testCondition) {
420: logWriter
421: .printError("unexpected method name of the first frame - "
422: + methodName);
423: unexpectedMethods = null == unexpectedMethods ? methodName
424: : unexpectedMethods + "," + methodName;
425: }
426:
427: if (methodName.equals(FramesDebuggee.METHOD_NAME)) {
428: methodCount++;
429: }
430: }
431:
432: if (methodCount != FramesDebuggee.DEPTH) {
433: logWriter
434: .printError(depthError = ("Number of "
435: + FramesDebuggee.METHOD_NAME
436: + " in frames " + methodCount
437: + " but expected " + FramesDebuggee.DEPTH));
438: }
439:
440: debuggeeWrapper.vmMirror.resumeThread(threadID);
441: synchronizer
442: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
443:
444: if (null != unexpectedMethods) {
445: logWriter.println("==> testFrames001 FAILED ");
446: fail("unexpected method(s): " + unexpectedMethods);
447: } else if (null != depthError) {
448: logWriter.println("==> testFrames001 FAILED ");
449: fail(depthError);
450: } else {
451: logWriter.println("==> testFrames001 OK. ");
452: }
453: }
454:
455: private Vector getFrames(int startFrame, int length) {
456:
457: Vector<FrameStruct> frames = new Vector<FrameStruct>();
458:
459: logWriter.println("startFrame=" + startFrame + "; length="
460: + length);
461:
462: // getting frames of the thread
463: CommandPacket packet = new CommandPacket(
464: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
465: JDWPCommands.ThreadReferenceCommandSet.FramesCommand);
466: packet.setNextValueAsThreadID(threadID);
467: packet.setNextValueAsInt(startFrame);
468: packet.setNextValueAsInt(length);
469: ReplyPacket reply = debuggeeWrapper.vmMirror
470: .performCommand(packet);
471: err = reply.getErrorCode();
472: if (err != JDWPConstants.Error.NONE) {
473: logWriter.println("\tthreadID=" + threadID + " - "
474: + JDWPConstants.Error.getName(err));
475: return null;
476: }
477: int framesCount = reply.getNextValueAsInt();
478: long frameID;
479: Location loc;
480: logWriter.println("framesCount=" + framesCount);
481: for (int j = 0; j < framesCount; j++) {
482: frameID = reply.getNextValueAsFrameID();
483: loc = reply.getNextValueAsLocation();
484: frames.add(new FrameStruct(frameID, loc));
485: }
486: return frames;
487: }
488:
489: public static void main(String[] args) {
490: junit.textui.TestRunner.run(FramesTest.class);
491: }
492: }
|