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/**
20 * @author Vitaly A. Provodin
21 */
22
23/**
24 * Created on 18.02.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
27
28import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
29import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
30import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
31import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
32import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
33import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
34
35
36/**
37 * JDWP Unit test for ThreadReference.FrameCount command.
38 */
39public class FrameCountTest extends JDWPSyncTestCase {
40
41    protected String getDebuggeeClassName() {
42        return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.FramesDebuggee";
43    }
44
45    /**
46     * This testcase exercises ThreadReference.CurrentContendedMonitor command.
47     * <BR>At first the test starts FramesDebuggee which recursively invokes
48     * the method 'FramesDebuggee.recursiveMethod' thereby the specified depth
49     * 'FramesDebuggee.DEPTH' of recursion is reached.
50     * <BR> Then the tests requests list of frames for main thread by invoking
51     * the JDWP command ThreadReference.Frames and compares size of
52     * this list with the value got via invoking ThreadReference.FrameCount command.
53     * <BR>The test expects that these values are equal, otherwise the test fail.
54     *
55     */
56    public void testFrameCount001() {
57        logWriter.println("==> testFrameCount001 START ");
58        String testedThreadName = synchronizer.receiveMessage();
59
60        logWriter.println
61        ("==> testedThreadName = |" + testedThreadName +"|");
62        long threadID = debuggeeWrapper.vmMirror.getThreadID(testedThreadName);
63        logWriter.println("==> threadID = " + threadID);
64        debuggeeWrapper.vmMirror.suspendThread(threadID);
65
66        int expectedCount = getFramesCount(threadID);
67        logWriter.println("\texpected count = " + expectedCount);
68
69        CommandPacket packet = new CommandPacket(
70                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
71                JDWPCommands.ThreadReferenceCommandSet.FrameCountCommand);
72        packet.setNextValueAsThreadID(threadID);
73        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
74        checkReplyPacket(reply, "ThreadReference::FrameCount command");
75
76        int frameCount = reply.getNextValueAsInt();
77        logWriter.println("\tframe count = " + frameCount);
78
79        if (frameCount != expectedCount) {
80            printErrorAndFail("Unexpected frame count = " + frameCount
81                    + ", expected value " + expectedCount);
82        }
83        debuggeeWrapper.vmMirror.resumeThread(threadID);
84        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
85    }
86
87    private int getFramesCount(long threadID) {
88
89        logWriter.println("getting frames of the thread");
90
91        short err;
92
93        // getting frames of the thread
94        CommandPacket packet = new CommandPacket(
95                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
96                JDWPCommands.ThreadReferenceCommandSet.FramesCommand);
97        packet.setNextValueAsThreadID(threadID);
98        packet.setNextValueAsInt(0);
99        packet.setNextValueAsInt(-1);
100        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
101        err = reply.getErrorCode();
102        if ( err != JDWPConstants.Error.NONE) {
103            logWriter.println("\tthreadID=" + threadID
104                    + " - " + JDWPConstants.Error.getName(err));
105            return 0;
106        }
107        int framesCount = reply.getNextValueAsInt();
108        return framesCount;
109    }
110}
111