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