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 03.03.2005
25  */
26 package org.apache.harmony.jpda.tests.jdwp.ObjectReference;
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.Value;
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 ObjectReference.MonitorInfo command.
39  */
40 public class MonitorInfoTest extends JDWPSyncTestCase {
41 
42     static final String thisCommandName = "ObjectReference.MonitorInfo command";
43     static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ObjectReference/MonitorInfoDebuggee;";
44 
getDebuggeeClassName()45     protected String getDebuggeeClassName() {
46         return "org.apache.harmony.jpda.tests.jdwp.ObjectReference.MonitorInfoDebuggee";
47     }
48 
49     /**
50      * This test exercises ObjectReference.MonitorInfo command.
51      * <BR>The test starts MonitorInfoDebuggee class, gets objectID
52      * as value of static field of this class which (field) represents checked object.
53      * Then for this objectID test executes ObjectReference.MonitorInfo command for
54      * checked object and checks that command returns the expected monitor info:
55      * <BR>monitorOwnerThreadID = 0, monitorEntryCount = 0, monitorWaiters = 0
56      * <BR>Then test waits for Debuggee to continue and to enter in synchronized block
57      * and again executes ObjectReference.MonitorInfo for checked object.
58      * Then test checks that expected results are received:
59      * <BR>monitorOwnerThreadID = 'not null', monitorEntryCount = 1, monitorWaiters = 0
60      */
testMonitorInfo001()61     public void testMonitorInfo001() {
62         String thisTestName = "testMonitorInfo001";
63 
64         //check capability, relevant for this test
65         logWriter.println("=> Check capability: canGetMonitorInfo");
66         debuggeeWrapper.vmMirror.capabilities();
67         boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canGetMonitorInfo;
68         if (!isCapability) {
69             logWriter.println("##WARNING: this VM doesn't possess capability: canGetMonitorInfo");
70             return;
71         }
72 
73 
74         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
75         String failMessage = "";
76         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
77         finalSyncMessage = "TO_FINISH";
78 
79         long refTypeID = getClassIDBySignature(debuggeeSignature);
80 
81         logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
82         logWriter.println("=> referenceTypeID for Debuggee class = " + refTypeID);
83 
84         long checkedFieldID = checkField(refTypeID, "lockObject");
85 
86         logWriter.println
87         ("=> Send ReferenceType::GetValues command for received fieldID and get ObjectID to check...");
88 
89         CommandPacket getValuesCommand = new CommandPacket(
90                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
91                 JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
92         getValuesCommand.setNextValueAsReferenceTypeID(refTypeID);
93         getValuesCommand.setNextValueAsInt(1);
94         getValuesCommand.setNextValueAsFieldID(checkedFieldID);
95 
96         ReplyPacket getValuesReply = debuggeeWrapper.vmMirror.performCommand(getValuesCommand);
97         getValuesCommand = null;
98         checkReplyPacket(getValuesReply, "ReferenceType::GetValues command");
99 
100         int returnedValuesNumber = getValuesReply.getNextValueAsInt();
101         logWriter.println("=> Returned values number = " + returnedValuesNumber);
102         assertEquals("Invalid number of values returned by ReferenceType::GetValues command,", 1, returnedValuesNumber);
103 
104         Value checkedObjectFieldValue = getValuesReply.getNextValueAsValue();
105         byte checkedObjectFieldTag = checkedObjectFieldValue.getTag();
106         logWriter.println("=> Returned field value tag for checked object= " + checkedObjectFieldTag
107             + "(" + JDWPConstants.Tag.getName(checkedObjectFieldTag) + ")");
108         assertEquals("Invalid value tag for checked object,", JDWPConstants.Tag.OBJECT_TAG, checkedObjectFieldTag
109                 , JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG)
110                 , JDWPConstants.Tag.getName(checkedObjectFieldTag));
111 
112         long checkedObjectID = checkedObjectFieldValue.getLongValue();
113         logWriter.println("=> Returned checked ObjectID = " + checkedObjectID);
114 
115         logWriter.println("=> Send VirtualMachine::Suspend command...");
116 
117         CommandPacket suspendCommand = new CommandPacket(
118                 JDWPCommands.VirtualMachineCommandSet.CommandSetID,
119                 JDWPCommands.VirtualMachineCommandSet.SuspendCommand);
120 
121         ReplyPacket suspendReply = debuggeeWrapper.vmMirror.performCommand(suspendCommand);
122         suspendCommand = null;
123         checkReplyPacket(suspendReply, "VirtualMachine::Suspend command");
124 
125         logWriter.println
126             ("\n=> CHECK 1: send " + thisCommandName + " for checked ObjectID and check reply...");
127 
128         CommandPacket checkedCommand = new CommandPacket(
129                 JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
130                 JDWPCommands.ObjectReferenceCommandSet.MonitorInfoCommand);
131         checkedCommand.setNextValueAsObjectID(checkedObjectID);
132 
133         ReplyPacket checkedReply = debuggeeWrapper.vmMirror.performCommand(checkedCommand);
134         checkedCommand = null;
135 
136         short errorCode = checkedReply.getErrorCode();
137         if ( errorCode == JDWPConstants.Error.NOT_IMPLEMENTED ) {
138             // it is possible case
139             logWriter.println("=> " +  thisCommandName + " returns ERROR = " + errorCode
140                     + "(" + JDWPConstants.Error.getName(errorCode) + ")");
141             logWriter.println("=> It is possible case - CHECK 1 PASSED");
142             logWriter.println("=> Send to Debuggee signal to funish ...");
143             synchronizer.sendMessage("TO_FINISH");
144             logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
145         } else {
146             checkReplyPacket(checkedReply, thisCommandName);
147         }
148 
149         long monitorOwnerThreadID = checkedReply.getNextValueAsThreadID();
150         logWriter.println("=> Returned monitorOwnerThreadID = " + monitorOwnerThreadID);
151         if ( monitorOwnerThreadID != 0) {
152             logWriter.println
153                 ("## FAILURE: " + thisCommandName + " returns unexpected monitorOwnerThreadID:" +
154                         monitorOwnerThreadID);
155             logWriter.println("## Expected monitorOwnerThreadID = 0");
156             failMessage = failMessage +
157                 thisCommandName + " returns unexpected monitorOwnerThreadID: " +
158                 monitorOwnerThreadID +
159                 ", Expected: 0\n";
160         }
161 
162         int monitorEntryCount = checkedReply.getNextValueAsInt();
163         logWriter.println("=> Returned monitorEntryCount = " + monitorEntryCount);
164         if ( monitorEntryCount != 0) {
165             logWriter.println
166                 ("## FAILURE: " + thisCommandName + " returns unexpected monitorEntryCount:");
167             logWriter.println("## Expected monitorEntryCount = 0");
168             failMessage = failMessage +
169                 thisCommandName + " returns unexpected monitorEntryCount:" +
170                 monitorEntryCount +
171                 ", expected: 0\n";
172         }
173 
174         int monitorWaiters = checkedReply.getNextValueAsInt();
175         logWriter.println("=> Returned monitorWaiters = " + monitorWaiters);
176         if ( monitorWaiters != 0) {
177             logWriter.println
178                 ("## FAILURE: " + thisCommandName + " returns unexpected monitorWaiters:");
179             logWriter.println("## Expected monitorWaiters = 0");
180             failMessage = failMessage +
181                 thisCommandName + " returns unexpected monitorWaiters:" +
182                 monitorWaiters +
183                 ", expected: 0\n";
184         }
185 
186         assertAllDataRead(checkedReply);
187         logWriter.println("=> CHECK 1: PASSED - expected monitor info is received");
188         checkedReply = null;
189 
190         resumeDebuggee();
191 
192         logWriter.println("=> Send to Debuggee signal to continue and to enter in synchronized block ...");
193         finalSyncMessage = null;
194         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
195         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
196 
197         logWriter.println("=> Send VirtualMachine::Suspend command...");
198 
199         suspendCommand = new CommandPacket(
200                 JDWPCommands.VirtualMachineCommandSet.CommandSetID,
201                 JDWPCommands.VirtualMachineCommandSet.SuspendCommand);
202 
203         suspendReply = debuggeeWrapper.vmMirror.performCommand(suspendCommand);
204         suspendCommand = null;
205         checkReplyPacket(suspendReply, "VirtualMachine::Suspend command");
206 
207         logWriter.println
208             ("\n=> CHECK 2: send " + thisCommandName + " for checked ObjectID when it is locked...");
209 
210         checkedCommand = new CommandPacket(
211                 JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
212                 JDWPCommands.ObjectReferenceCommandSet.MonitorInfoCommand);
213         checkedCommand.setNextValueAsObjectID(checkedObjectID);
214 
215         checkedReply = debuggeeWrapper.vmMirror.performCommand(checkedCommand);
216         checkedCommand = null;
217         checkReplyPacket(checkedReply, thisCommandName);
218 
219         monitorOwnerThreadID = checkedReply.getNextValueAsThreadID();
220         logWriter.println("=> Returned monitorOwnerThreadID = " + monitorOwnerThreadID);
221         if ( monitorOwnerThreadID == 0) {
222             logWriter.println
223                 ("## FAILURE: " + thisCommandName + " returns unexpected monitorOwnerThreadID:");
224             logWriter.println("## Expected monitorOwnerThreadID = 'not null'");
225             failMessage = failMessage +
226                 thisCommandName + " returns unexpected monitorOwnerThreadID: 0" +
227                 ", Expected monitorOwnerThreadID: 'not null'\n";
228         }
229 
230         monitorEntryCount = checkedReply.getNextValueAsInt();
231         logWriter.println("=> Returned monitorEntryCount = " + monitorEntryCount);
232         if ( monitorEntryCount != 1) {
233             logWriter.println
234                 ("## FAILURE: " + thisCommandName + " returns unexpected monitorEntryCount:" +
235                         monitorEntryCount);
236             logWriter.println("## Expected monitorEntryCount = 1");
237             failMessage = failMessage +
238                 thisCommandName + " returns unexpected monitorEntryCount: " +
239                 monitorEntryCount +
240                 ", expected: 1\n";
241         }
242 
243         monitorWaiters = checkedReply.getNextValueAsInt();
244         logWriter.println("=> Returned monitorWaiters = " + monitorWaiters);
245         if ( monitorWaiters != 0) {
246             logWriter.println
247                 ("## FAILURE: " + thisCommandName + " returns unexpected monitorWaiters:" +
248                         monitorWaiters);
249             logWriter.println("## Expected monitorWaiters = 0");
250             failMessage = failMessage +
251                 thisCommandName + " returns unexpected monitorWaiters: " +
252                 monitorWaiters +
253                 ", expected: 0\n";
254         }
255 
256         logWriter.println("=> CHECK 2: PASSED - expected monitor info is received");
257         logWriter.println("\n=> Send VirtualMachine::Resume command ...");
258 
259         resumeDebuggee();
260 
261         logWriter.println("=> Send to Debuggee signal to funish ...");
262         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
263 
264         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
265 
266         assertAllDataRead(checkedReply);
267 
268         if (failMessage.length() > 0) {
269             fail(failMessage);
270         }
271     }
272 }
273