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 Anatoly F. Bondarenko
21 */
22
23/**
24 * Created on 31.03.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 for the sleeping Thread.
38 */
39public class Status002Test extends JDWPSyncTestCase {
40
41    static final int testStatusPassed = 0;
42    static final int testStatusFailed = -1;
43
44    protected String getDebuggeeClassName() {
45        return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.Status002Debuggee";
46    }
47
48    String getThreadSuspendStatusName(int suspendStatus) {
49
50        String result = JDWPConstants.SuspendStatus.getName(suspendStatus);
51        if ( result.equals("") ) {
52            result = "NOT_SUSPENDED";
53        }
54        return result;
55    }
56
57    /**
58     * This testcase exercises ThreadReference.Status command for the sleeping Thread.
59     * <BR>At first the test starts Status002Debuggee which runs
60     * the tested thread and blocks it in invocation of
61     * the 'Thread.sleep()' method.
62     * <BR> Then the tests performs the ThreadReference.Status command
63     * for tested thread.
64     * <BR>It is expected that:
65     * <BR>&nbsp;&nbsp; - returned thread status is SLEEPING status;
66     * <BR>&nbsp;&nbsp; - returned suspend status is not SUSPEND_STATUS_SUSPENDED status;
67     */
68    public void testStatus003() {
69        String thisTestName = "testStatus003";
70        logWriter.println("==> " + thisTestName +
71                " for ThreadReference.Status command: START...");
72        logWriter.println("==> This " + thisTestName +
73            " checks command for SLEEPING Thread: which is sleeping in Thread.sleep(Time) method...");
74        String checkedThreadName = synchronizer.receiveMessage();
75        logWriter.println("=> checkedThreadName = " + checkedThreadName);
76
77        long checkedThreadID = debuggeeWrapper.vmMirror.getThreadID(checkedThreadName);
78        logWriter.println("=> checkedThreadID = " + checkedThreadID);
79
80        logWriter.println
81        ("=> Send ThreadReference.Status command for checked Thread and check reply...");
82        CommandPacket checkedCommand = new CommandPacket(
83                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
84                JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
85        checkedCommand.setNextValueAsThreadID(checkedThreadID);
86
87        ReplyPacket checkedReply = debuggeeWrapper.vmMirror.performCommand(checkedCommand);
88        checkReplyPacket(checkedReply, "ThreadReference.Status command");
89
90        int threadStatus = checkedReply.getNextValueAsInt();
91        int suspendStatus = checkedReply.getNextValueAsInt();
92
93        logWriter.println("\n=> Returned thread status = 0x" + Integer.toHexString(threadStatus)
94                + "(" + JDWPConstants.ThreadStatus.getName(threadStatus) + ")");
95        if (threadStatus != JDWPConstants.ThreadStatus.SLEEPING) {
96            finalSyncMessage = JPDADebuggeeSynchronizer.SGNL_CONTINUE;
97            printErrorAndFail("Unexpected thread status is returned: "
98                + Integer.toHexString(threadStatus)
99                + "(" + JDWPConstants.ThreadStatus.getName(threadStatus) + ")"
100                + "\nExpected thread status: 0x"
101                + Integer.toHexString(JDWPConstants.ThreadStatus.SLEEPING)
102                + "(" + JDWPConstants.ThreadStatus.getName(JDWPConstants.ThreadStatus.SLEEPING) + ")");
103        } else {
104            logWriter.println("=> OK - Expected thread status is returned");
105        }
106
107        logWriter.println
108            ("\n=> Returned thread suspend status = 0x" + Integer.toHexString(suspendStatus)
109            + "(" + getThreadSuspendStatusName(suspendStatus) + ")");
110        if (suspendStatus == JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {
111            finalSyncMessage = JPDADebuggeeSynchronizer.SGNL_CONTINUE;
112            printErrorAndFail("Unexpected thread status is returned:"
113                + Integer.toHexString(0)
114                + "(" + getThreadSuspendStatusName(0) + ")");
115        } else {
116            logWriter.println("=> OK - Expected thread suspend status is returned");
117        }
118
119        logWriter.println("=> Send to Debuggee signal to funish ...");
120        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
121
122        logWriter.println("==> " + thisTestName +
123                " for ThreadReference.Status command: FINISH...");
124    }
125}
126