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