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 22.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.Status command.
38 */
39public class StatusTest extends JDWPSyncTestCase {
40
41    protected String getDebuggeeClassName() {
42        return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.StatusDebuggee";
43    }
44
45    /**
46     * This testcase exercises ThreadReference.Status command for suspended Thread.
47     * <BR>At first the test starts StatusDebuggee which runs
48     * the tested thread.
49     * <BR> At first the test suspends tested thread by ThreadReference.Suspend command.
50     * <BR> Then the tests performs the ThreadReference.Status command
51     * for tested thread.
52     * <BR>It is expected that:
53     * <BR>&nbsp;&nbsp; - returned thread status is RUNNING status;
54     * <BR>&nbsp;&nbsp; - returned suspend status is SUSPEND_STATUS_SUSPENDED status;
55     */
56    public void testStatus002() {
57        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
58
59        // getting ID of the tested thread
60        logWriter.println("get thread ID");
61        long threadID =
62            debuggeeWrapper.vmMirror.getThreadID(StatusDebuggee.TESTED_THREAD);
63        logWriter.println("suspend thread");
64        debuggeeWrapper.vmMirror.suspendThread(threadID);
65
66        // getting the thread group ID
67        CommandPacket packet = new CommandPacket(
68                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
69                JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
70        packet.setNextValueAsThreadID(threadID);
71
72        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
73        checkReplyPacket(reply, "ThreadReference::Status command");
74
75        int threadStatus = reply.getNextValueAsInt();
76        int suspendStatus = reply.getNextValueAsInt();
77
78        logWriter.println("\t" + threadID + " "
79                + "\"" + StatusDebuggee.TESTED_THREAD + "\" "
80                + JDWPConstants.ThreadStatus.getName(threadStatus) + " "
81                + JDWPConstants.SuspendStatus.getName(suspendStatus));
82
83        if (threadStatus != JDWPConstants.ThreadStatus.RUNNING) {
84            printErrorAndFail("Unexpected thread status: "
85                    + JDWPConstants.ThreadStatus.getName(threadStatus));
86        } else {
87            logWriter.println("Expected thread status "
88                    + JDWPConstants.ThreadStatus.getName(threadStatus));
89        }
90
91        if (suspendStatus != JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {
92            printErrorAndFail("Unexpected suspend status: "
93                    + JDWPConstants.ThreadStatus.getName(suspendStatus));
94        } else {
95            logWriter.println("Expected suspend status "
96                    + JDWPConstants.SuspendStatus.getName(suspendStatus));
97        }
98
99        logWriter.println("resume thread");
100        debuggeeWrapper.vmMirror.resumeThread(threadID);
101
102        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
103    }
104
105    /**
106     * This testcase exercises ThreadReference.Status command.
107     * <BR>At first the test starts StatusDebuggee which runs
108     * the tested thread.
109     * <BR> Then the tests performs the ThreadReference.Status command
110     * for tested thread.
111     * <BR>It is expected that:
112     * <BR>&nbsp;&nbsp; - returned thread status is RUNNING status;
113     * <BR>&nbsp;&nbsp; - returned suspend status is not SUSPEND_STATUS_SUSPENDED status;
114     */
115    public void testStatus001() {
116        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
117
118        // getting ID of the tested thread
119        logWriter.println("get thread ID");
120        long threadID =
121            debuggeeWrapper.vmMirror.getThreadID(StatusDebuggee.TESTED_THREAD);
122
123        // getting the thread group ID
124        CommandPacket packet = new CommandPacket(
125                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
126                JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
127        packet.setNextValueAsThreadID(threadID);
128
129        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
130        checkReplyPacket(reply, "ThreadReference::Status command");
131
132        int threadStatus = reply.getNextValueAsInt();
133        int suspendStatus = reply.getNextValueAsInt();
134
135        logWriter.println("\t" + threadID + " "
136                + "\"" + StatusDebuggee.TESTED_THREAD + "\" "
137                + JDWPConstants.ThreadStatus.getName(threadStatus) + " "
138                + JDWPConstants.SuspendStatus.getName(suspendStatus));
139
140        if (threadStatus != JDWPConstants.ThreadStatus.RUNNING) {
141            printErrorAndFail("Unexpected thread status: "
142                    + JDWPConstants.ThreadStatus.getName(threadStatus));
143        } else {
144            logWriter.println("Expected thread status "
145                    + JDWPConstants.ThreadStatus.getName(threadStatus));
146        }
147
148        if (suspendStatus == JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {
149            printErrorAndFail("Unexpected suspend status: "
150                    + JDWPConstants.ThreadStatus.getName(suspendStatus));
151        } else {
152            logWriter.println("Expected suspend status "
153                    + JDWPConstants.SuspendStatus.getName(suspendStatus));
154        }
155
156        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
157    }
158}
159