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.OwnedMonitors command.
39  */
40 public class OwnedMonitorsTest extends JDWPSyncTestCase {
41 
getDebuggeeClassName()42     protected String getDebuggeeClassName() {
43         return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.OwnedMonitorsDebuggee";
44     }
45 
46     /**
47      * This testcase exercises ThreadReference.OwnedMonitors command.
48      * <BR>At first the test starts OwnedMonitorsDebuggee which runs
49      * the tested thread 'TESTED_THREAD'.
50      * <BR>Then the test performs the ThreadReference.OwnedMonitors command
51      * for the tested thread and gets list of monitor objects.
52      * It is expected that this command returns at least two monitors
53      * owned by 'TESTED_THREAD' thread
54      * <BR>After this for each received monitor object the test performs
55      * ObjectReference.MonitorInfo command.
56      * It is expected that this command returns the 'TESTED_THREAD' thread
57      * as owner for each monitor.
58      */
testOwnedMonitors001()59     public void testOwnedMonitors001() {
60 
61         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
62 
63         // checking capability relevant for this test
64         logWriter.println("==> Check capability: canGetOwnedMonitorInfo");
65         debuggeeWrapper.vmMirror.capabilities();
66         boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canGetOwnedMonitorInfo;
67         if (!isCapability) {
68             logWriter.println("##WARNING: this VM dosn't possess capability: canGetOwnedMonitorInfo");
69             synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
70             return;
71         }
72 
73         // getting ID of the tested thread
74         logWriter.println("==> testedThreadName = " + OwnedMonitorsDebuggee.TESTED_THREAD);
75         logWriter.println("==> Get testedThreadID...");
76         long testedThreadID =
77             debuggeeWrapper.vmMirror.getThreadID(OwnedMonitorsDebuggee.TESTED_THREAD);
78         logWriter.println("==> testedThreadID = " + testedThreadID);
79         logWriter.println("==> suspend testedThread...");
80         debuggeeWrapper.vmMirror.suspendThread(testedThreadID);
81 
82         // getting the thread group ID
83         CommandPacket packet = new CommandPacket(
84                 JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
85                 JDWPCommands.ThreadReferenceCommandSet.OwnedMonitorsCommand);
86         packet.setNextValueAsThreadID(testedThreadID);
87         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
88         checkReplyPacket(reply, "ThreadReference::OwnedMonitors command");
89 
90         int owned = reply.getNextValueAsInt();
91         logWriter.println("owned monitors: " + owned);
92 
93         String ownerName;
94         long ownerThread;
95 
96         for (int i =0; i < owned; i++) {
97             TaggedObject tobj = reply.getNextValueAsTaggedObject();
98 
99             logWriter.println("\t" + i + " tagged-object tag: "
100                     + JDWPConstants.Tag.getName(tobj.tag) + "(" + tobj.tag + ") "
101                     + "ID: " + tobj.objectID);
102 
103             packet = new CommandPacket(
104                     JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
105                     JDWPCommands.ObjectReferenceCommandSet.MonitorInfoCommand);
106             packet.setNextValueAsObjectID(tobj.objectID);
107             ReplyPacket replyObj = debuggeeWrapper.vmMirror.performCommand(packet);
108             checkReplyPacket(replyObj, "ObjectReference::MonitorInfo command");
109 
110             ownerThread = replyObj.getNextValueAsThreadID();
111             logWriter.println("\t\t" + "ownerThread ID:   " + ownerThread);
112 
113             ownerName = debuggeeWrapper.vmMirror.getThreadName(ownerThread);
114             logWriter.println("\t\t" + "ownerThread name: " + ownerName);
115 
116             if (ownerThread != testedThreadID) {
117                 printErrorAndFail("wrong owner thread: " + ownerThread);
118             }
119             if (!ownerName.equals(OwnedMonitorsDebuggee.TESTED_THREAD)) {
120                 printErrorAndFail("wrong owner thread name: " + ownerName);
121             }
122         }
123 
124         // check that at least two owned monitors are returned
125         if (owned < 2) {
126              printErrorAndFail("wrong number of owned monitors: " + owned + " (expected at least 2)");
127         }
128 
129         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
130     }
131 }
132