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 which has started and finished.
38 */
39public class Status006Test 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.Status006Debuggee";
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    /**
58     * This testcase exercises ThreadReference.Status command for the Thread which has started and finished.
59     * <BR>At first the test starts Status006Debuggee which runs
60     * the tested thread which starts and finishes.
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 ZOMBIE status;
65     * <BR>&nbsp;&nbsp; - returned suspend status is not SUSPEND_STATUS_SUSPENDED status;
66     */
67    public void testStatus007() {
68        String thisTestName = "testStatus007";
69        logWriter.println("==> " + thisTestName + " for ThreadReference.Status command: START...");
70        logWriter.println("==> This " + thisTestName
71                + " checks command for TERMINATED Thread: which has started and finished...");
72//        String checkedThreadName = synchronizer.receiveMessage();
73        String checkedThreadName = synchronizer.receiveMessageWithoutException
74        ("ThreadReference.Status006Test: testStatus007(#1)");
75        logWriter.println("=> checkedThreadName = " + checkedThreadName);
76        if ( checkedThreadName.startsWith("Exception:") ) {
77            logWriter.println("=> Can NOT get checkedThreadName => test con NOT be executed!");
78            return;
79        }
80
81        long checkedThreadID = debuggeeWrapper.vmMirror.getThreadID(checkedThreadName);
82        logWriter.println("=> checkedThreadID = " + checkedThreadID);
83
84        logWriter.println("=> Send to Debuggee signal to continue ...");
85        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
86//        synchronizer.receiveMessage();
87        synchronizer.receiveMessageWithoutException
88        ("ThreadReference.Status006Test: testStatus007(#2)");
89
90        logWriter.println
91        ("=> Send ThreadReference.Status command for checked Thread and check reply...");
92        CommandPacket checkedCommand = new CommandPacket(
93                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
94                JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
95        checkedCommand.setNextValueAsThreadID(checkedThreadID);
96
97        ReplyPacket checkedReply = debuggeeWrapper.vmMirror.performCommand(checkedCommand);
98        checkReplyPacket(checkedReply, "ThreadReference.Status command");
99
100        int threadStatus = checkedReply.getNextValueAsInt();
101        int suspendStatus = checkedReply.getNextValueAsInt();
102
103        logWriter.println("\n=> Returned thread status = 0x" + Integer.toHexString(threadStatus)
104                + "(" + JDWPConstants.ThreadStatus.getName(threadStatus) + ")");
105        if (threadStatus != JDWPConstants.ThreadStatus.ZOMBIE) {
106            finalSyncMessage = JPDADebuggeeSynchronizer.SGNL_CONTINUE;
107            printErrorAndFail("Unexpected thread status is returned:"
108                + "\n Expected thread status = 0x"
109                + Integer.toHexString(JDWPConstants.ThreadStatus.ZOMBIE)
110                + "(" + JDWPConstants.ThreadStatus.getName(JDWPConstants.ThreadStatus.ZOMBIE) + ")");
111        } else {
112            logWriter.println("=> OK - Expected thread status is returned");
113        }
114
115        logWriter.println
116            ("\n=> Returned thread suspend status = 0x" + Integer.toHexString(suspendStatus)
117            + "(" + getThreadSuspendStatusName(suspendStatus) + ")");
118        if (suspendStatus == JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {
119            finalSyncMessage = JPDADebuggeeSynchronizer.SGNL_CONTINUE;
120            printErrorAndFail("Unexpected thread status is returned:"
121                + "\n Expected thread status = 0x"
122                + Integer.toHexString(0)
123                + "(" + getThreadSuspendStatusName(0) + ")");
124        } else {
125            logWriter.println("=> OK - Expected thread suspend status is returned");
126        }
127
128        logWriter.println("=> Send to Debuggee signal to funish ...");
129        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
130
131        logWriter.println("==> " + thisTestName + " for ThreadReference.Status command: FINISH...");
132    }
133}
134