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 UNDEFINITELY.
38 */
39public class Status004Test 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.Status004Debuggee";
46    }
47
48    String getThreadSuspendStatusName(int suspendStatus) {
49        String result = JDWPConstants.SuspendStatus.getName(suspendStatus);
50        if ( result.equals("") ) {
51            result = "NOT_SUSPENDED";
52        }
53        return result;
54    }
55
56    /**
57     * This testcase exercises ThreadReference.Status command for the Thread waiting UNDEFINITELY.
58     * <BR>At first the test starts Status004Debuggee which runs
59     * the tested thread and blocks it in invocation of
60     * the 'Object.wait()' method.
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 WAIT status;
65     * <BR>&nbsp;&nbsp; - returned suspend status is not SUSPEND_STATUS_SUSPENDED status;
66     */
67    public void testStatus005() {
68        String thisTestName = "testStatus005";
69        logWriter.println("==> " + thisTestName + " for ThreadReference.Status command: START...");
70        logWriter.println("==> This " + thisTestName
71            + " checks command for WAITING Thread: which is waiting UNDEFINITELY in Object.wait() method...");
72        String checkedThreadName = synchronizer.receiveMessage();
73        logWriter.println("=> checkedThreadName = " + checkedThreadName);
74
75        long checkedThreadID = debuggeeWrapper.vmMirror.getThreadID(checkedThreadName);
76        logWriter.println("=> checkedThreadID = " + checkedThreadID);
77
78        logWriter.println
79        ("=> Send ThreadReference.Status command for checked Thread and check reply...");
80        CommandPacket checkedCommand = new CommandPacket(
81                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
82                JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
83        checkedCommand.setNextValueAsThreadID(checkedThreadID);
84
85        ReplyPacket checkedReply = debuggeeWrapper.vmMirror.performCommand(checkedCommand);
86        checkReplyPacket(checkedReply, "ThreadReference.Status command");
87
88        int threadStatus = checkedReply.getNextValueAsInt();
89        int suspendStatus = checkedReply.getNextValueAsInt();
90
91        logWriter.println("\n=> Returned thread status = 0x" + Integer.toHexString(threadStatus)
92                + "(" + JDWPConstants.ThreadStatus.getName(threadStatus) + ")");
93        if (threadStatus != JDWPConstants.ThreadStatus.WAIT) {
94            finalSyncMessage = JPDADebuggeeSynchronizer.SGNL_CONTINUE;
95            printErrorAndFail("Unexpected thread status is returned:"
96                + "\n Expected thread status = 0x"
97                + Integer.toHexString(JDWPConstants.ThreadStatus.WAIT)
98                + "(" + JDWPConstants.ThreadStatus.getName(JDWPConstants.ThreadStatus.WAIT) + ")");
99        } else {
100            logWriter.println("=> OK - Expected thread status is returned");
101        }
102
103        logWriter.println
104            ("\n=> Returned thread suspend status = 0x" + Integer.toHexString(suspendStatus)
105            + "(" + getThreadSuspendStatusName(suspendStatus) + ")");
106        if (suspendStatus == JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {
107            finalSyncMessage = JPDADebuggeeSynchronizer.SGNL_CONTINUE;
108            printErrorAndFail("Unexpected thread status is returned:"
109                + "\n Expected thread status = 0x"
110                + Integer.toHexString(0)
111                + "(" + getThreadSuspendStatusName(0) + ")");
112        } else {
113            logWriter.println("=> OK - Expected thread suspend status is returned");
114        }
115
116        logWriter.println("=> Send to Debuggee signal to funish ...");
117        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
118
119        logWriter.println("==> " + thisTestName + " for ThreadReference.Status command: FINISH...");
120    }
121}
122