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 24.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.framework.jdwp.TaggedObject;
     33 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
     34 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
     35 
     36 
     37 /**
     38  * JDWP Unit test for ThreadReference.CurrentContendedMonitor command.
     39  */
     40 public class CurrentContendedMonitorTest extends JDWPSyncTestCase {
     41 
     42     @Override
     43     protected String getDebuggeeClassName() {
     44         return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.CurrentContendedMonitorDebuggee";
     45     }
     46 
     47     /**
     48      * This testcase exercises ThreadReference.CurrentContendedMonitor command.
     49      * <BR>At first the test starts CurrentContendedMonitorDebuggee which runs
     50      * the tested thread 'TESTED_THREAD'.
     51      * <BR> Then the test performs the ThreadReference.CurrentContendedMonitor command
     52      * for the tested thread.
     53      * <BR>After getting monitor object from command, the test
     54      * performs the ObjectReference.MonitorInfo command for this monitor object
     55      * and checks that the waiter for this monitor is the 'TESTED_THREAD' thread.
     56      *
     57      */
     58     public void testCurrentContendedMonitor001() {
     59         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     60 
     61         // getting ID of the tested thread
     62         logWriter.println
     63         ("==> testedThreadName = " + CurrentContendedMonitorDebuggee.TESTED_THREAD);
     64         logWriter.println("==> Get testedThreadID...");
     65         long testedThreadID =
     66             debuggeeWrapper.vmMirror.getThreadID(CurrentContendedMonitorDebuggee.TESTED_THREAD);
     67         logWriter.println("==> testedThreadID = " + testedThreadID);
     68         logWriter.println("==> suspend testedThread...");
     69         debuggeeWrapper.vmMirror.suspendThread(testedThreadID);
     70 
     71         // getting the thread group ID
     72         CommandPacket packet = new CommandPacket(
     73                 JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
     74                 JDWPCommands.ThreadReferenceCommandSet.CurrentContendedMonitorCommand);
     75         packet.setNextValueAsThreadID(testedThreadID);
     76         logWriter.println("send \"CurrentContendedMonitor\" command");
     77         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
     78         checkReplyPacket(reply, "ThreadReference::CurrentContendedMonitor command");
     79 
     80         TaggedObject tobj = reply.getNextValueAsTaggedObject();
     81 
     82         logWriter.println("\ttagged-object tag: "
     83                 + JDWPConstants.Tag.getName(tobj.tag) + "(" + tobj.tag + ") "
     84                 + "ID: " + tobj.objectID);
     85 
     86         packet = new CommandPacket(
     87                 JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
     88                 JDWPCommands.ObjectReferenceCommandSet.MonitorInfoCommand);
     89         packet.setNextValueAsObjectID(tobj.objectID);
     90         ReplyPacket replyObj = debuggeeWrapper.vmMirror.performCommand(packet);
     91         checkReplyPacket(replyObj, "ObjectReference::MonitorInfo command");
     92 
     93         replyObj.getNextValueAsThreadID();
     94         replyObj.getNextValueAsInt();
     95         int waiters = replyObj.getNextValueAsInt();
     96         long waiterID;
     97         String waiterName;
     98         for (int i = 0; i < waiters; i++) {
     99             waiterID = replyObj.getNextValueAsThreadID();
    100             waiterName = debuggeeWrapper.vmMirror.getThreadName(waiterID);
    101             logWriter.println("\twaiter: "
    102                     + " " + waiterName
    103                     + "(" + waiterID + ")");
    104             if (waiterID != testedThreadID) {
    105                 logWriter.printError("wrong owner: " + waiterID);
    106                 assertEquals("ObjectReference::MonitorInfo returned wrong owner ID,",
    107                         testedThreadID, waiterID);
    108             }
    109             assertString("ObjectReference::MonitorInfo  returned invalid waiter name,",
    110                     OwnedMonitorsDebuggee.TESTED_THREAD, waiterName);
    111         }
    112 
    113         // interrupt
    114         packet = new CommandPacket(
    115                 JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
    116                 JDWPCommands.ThreadReferenceCommandSet.InterruptCommand);
    117         packet.setNextValueAsThreadID(testedThreadID);
    118         logWriter.println("send \"Interrupt\" command");
    119         reply = debuggeeWrapper.vmMirror.performCommand(packet);
    120         checkReplyPacket(reply, "ThreadReference::Interrupt command");
    121 
    122         short err = reply.getErrorCode();
    123         if (err != JDWPConstants.Error.NONE) {
    124             logWriter.printError("Unexpected " + JDWPConstants.Error.getName(err));
    125         }
    126 
    127         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    128     }
    129 
    130 }
    131