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 Thread waiting to enter in locked Monitor.
38 */
39public class Status005Test 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.Status005Debuggee";
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 Thread waiting to enter in locked Monitor.
59     * <BR>At first the test starts Status005Debuggee which runs
60     * the tested thread and blocks it in entering in synchronized block.
61     * <BR> Then the tests performs the ThreadReference.Status command
62     * for tested thread.
63     * <BR>It is expected that:
64     * <BR>&nbsp;&nbsp; - returned thread status is MONITOR status;
65     * <BR>&nbsp;&nbsp; - returned suspend status is not SUSPEND_STATUS_SUSPENDED status;
66     */
67    public void testStatus006() {
68        String thisTestName = "testStatus006";
69        logWriter.println("==> " + thisTestName + " for ThreadReference.Status command: START...");
70        logWriter.println("==> This " + thisTestName
71                + " checks command for BLOCKED Thread: which is waiting to enter in locked Monitor...");
72        //int testStatus = testStatusPassed;
73        String checkedThreadName = synchronizer.receiveMessage();
74        logWriter.println("=> checkedThreadName = " + checkedThreadName);
75
76        long checkedThreadID = debuggeeWrapper.vmMirror.getThreadID(checkedThreadName);
77        logWriter.println("=> checkedThreadID = " + checkedThreadID);
78
79        logWriter.println
80        ("=> Send ThreadReference.Status command for checked Thread and check reply...");
81        CommandPacket checkedCommand = new CommandPacket(
82                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
83                JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
84        checkedCommand.setNextValueAsThreadID(checkedThreadID);
85
86        ReplyPacket checkedReply = debuggeeWrapper.vmMirror.performCommand(checkedCommand);
87        checkReplyPacket(checkedReply, "ThreadReference.Status command");
88
89        int threadStatus = checkedReply.getNextValueAsInt();
90        int suspendStatus = checkedReply.getNextValueAsInt();
91
92        logWriter.println("\n=> Returned thread status = 0x" + Integer.toHexString(threadStatus)
93                + "(" + JDWPConstants.ThreadStatus.getName(threadStatus) + ")");
94        if (threadStatus != JDWPConstants.ThreadStatus.MONITOR) {
95            finalSyncMessage = JPDADebuggeeSynchronizer.SGNL_CONTINUE;
96            printErrorAndFail("Unexpected thread status is returned:"
97                + "\n Expected thread status = 0x"
98                + Integer.toHexString(JDWPConstants.ThreadStatus.MONITOR)
99                + "(" + JDWPConstants.ThreadStatus.getName(JDWPConstants.ThreadStatus.MONITOR) + ")");
100        } else {
101            logWriter.println("=> OK - Expected thread status is returned");
102        }
103
104        logWriter.println
105            ("\n=> Returned thread suspend status = 0x" + Integer.toHexString(suspendStatus)
106            + "(" + getThreadSuspendStatusName(suspendStatus) + ")");
107        if (suspendStatus == JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {
108            finalSyncMessage = JPDADebuggeeSynchronizer.SGNL_CONTINUE;
109            printErrorAndFail("Unexpected thread status is returned:"
110                + "\n Expected thread status = 0x"
111                + Integer.toHexString(0)
112                + "(" + getThreadSuspendStatusName(0) + ")");
113        } else {
114            logWriter.println("=> OK - Expected thread suspend status is returned");
115        }
116
117        logWriter.println("=> Send to Debuggee signal to funish ...");
118        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
119
120        logWriter.println("==> " + thisTestName + " for ThreadReference.Status command: FINISH...");
121    }
122}
123