001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Anatoly F. Bondarenko
021: * @version $Revision: 1.9 $
022: */
023:
024: /**
025: * Created on 03.03.2005
026: */package org.apache.harmony.jpda.tests.jdwp.ObjectReference;
027:
028: import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
029: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
030: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
031: import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
032: import org.apache.harmony.jpda.tests.framework.jdwp.Value;
033: import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
034: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
035:
036: /**
037: * JDWP Unit test for ObjectReference.MonitorInfo command.
038: */
039: public class MonitorInfoTest extends JDWPSyncTestCase {
040:
041: static final String this CommandName = "ObjectReference.MonitorInfo command";
042: static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ObjectReference/MonitorInfoDebuggee;";
043:
044: protected String getDebuggeeClassName() {
045: return "org.apache.harmony.jpda.tests.jdwp.ObjectReference.MonitorInfoDebuggee";
046: }
047:
048: /**
049: * This test exercises ObjectReference.MonitorInfo command.
050: * <BR>The test starts MonitorInfoDebuggee class, gets objectID
051: * as value of static field of this class which (field) represents checked object.
052: * Then for this objectID test executes ObjectReference.MonitorInfo command for
053: * checked object and checks that command returns the expected monitor info:
054: * <BR>monitorOwnerThreadID = 0, monitorEntryCount = 0, monitorWaiters = 0
055: * <BR>Then test waits for Debuggee to continue and to enter in synchronized block
056: * and again executes ObjectReference.MonitorInfo for checked object.
057: * Then test checks that expected results are received:
058: * <BR>monitorOwnerThreadID = 'not null', monitorEntryCount = 1, monitorWaiters = 0
059: */
060: public void testMonitorInfo001() {
061: String this TestName = "testMonitorInfo001";
062:
063: //check capability, relevant for this test
064: logWriter.println("=> Check capability: canGetMonitorInfo");
065: debuggeeWrapper.vmMirror.capabilities();
066: boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canGetMonitorInfo;
067: if (!isCapability) {
068: logWriter
069: .println("##WARNING: this VM doesn't possess capability: canGetMonitorInfo");
070: return;
071: }
072:
073: logWriter.println("==> " + this TestName + " for "
074: + this CommandName + ": START...");
075: String failMessage = "";
076: synchronizer
077: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
078: finalSyncMessage = "TO_FINISH";
079:
080: long refTypeID = getClassIDBySignature(debuggeeSignature);
081:
082: logWriter.println("=> Debuggee class = "
083: + getDebuggeeClassName());
084: logWriter.println("=> referenceTypeID for Debuggee class = "
085: + refTypeID);
086:
087: long checkedFieldID = checkField(refTypeID, "lockObject");
088:
089: logWriter
090: .println("=> Send ReferenceType::GetValues command for received fieldID and get ObjectID to check...");
091:
092: CommandPacket getValuesCommand = new CommandPacket(
093: JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
094: JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
095: getValuesCommand.setNextValueAsReferenceTypeID(refTypeID);
096: getValuesCommand.setNextValueAsInt(1);
097: getValuesCommand.setNextValueAsFieldID(checkedFieldID);
098:
099: ReplyPacket getValuesReply = debuggeeWrapper.vmMirror
100: .performCommand(getValuesCommand);
101: getValuesCommand = null;
102: checkReplyPacket(getValuesReply,
103: "ReferenceType::GetValues command");
104:
105: int returnedValuesNumber = getValuesReply.getNextValueAsInt();
106: logWriter.println("=> Returned values number = "
107: + returnedValuesNumber);
108: assertEquals(
109: "Invalid number of values returned by ReferenceType::GetValues command,",
110: 1, returnedValuesNumber);
111:
112: Value checkedObjectFieldValue = getValuesReply
113: .getNextValueAsValue();
114: byte checkedObjectFieldTag = checkedObjectFieldValue.getTag();
115: logWriter
116: .println("=> Returned field value tag for checked object= "
117: + checkedObjectFieldTag
118: + "("
119: + JDWPConstants.Tag
120: .getName(checkedObjectFieldTag) + ")");
121: assertEquals(
122: "Invalid value tag for checked object,",
123: JDWPConstants.Tag.OBJECT_TAG,
124: checkedObjectFieldTag,
125: JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG),
126: JDWPConstants.Tag.getName(checkedObjectFieldTag));
127:
128: long checkedObjectID = checkedObjectFieldValue.getLongValue();
129: logWriter.println("=> Returned checked ObjectID = "
130: + checkedObjectID);
131:
132: logWriter.println("=> Send VirtualMachine::Suspend command...");
133:
134: CommandPacket suspendCommand = new CommandPacket(
135: JDWPCommands.VirtualMachineCommandSet.CommandSetID,
136: JDWPCommands.VirtualMachineCommandSet.SuspendCommand);
137:
138: ReplyPacket suspendReply = debuggeeWrapper.vmMirror
139: .performCommand(suspendCommand);
140: suspendCommand = null;
141: checkReplyPacket(suspendReply,
142: "VirtualMachine::Suspend command");
143:
144: logWriter.println("\n=> CHECK 1: send " + this CommandName
145: + " for checked ObjectID and check reply...");
146:
147: CommandPacket checkedCommand = new CommandPacket(
148: JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
149: JDWPCommands.ObjectReferenceCommandSet.MonitorInfoCommand);
150: checkedCommand.setNextValueAsObjectID(checkedObjectID);
151:
152: ReplyPacket checkedReply = debuggeeWrapper.vmMirror
153: .performCommand(checkedCommand);
154: checkedCommand = null;
155:
156: short errorCode = checkedReply.getErrorCode();
157: if (errorCode == JDWPConstants.Error.NOT_IMPLEMENTED) {
158: // it is possible case
159: logWriter.println("=> " + this CommandName
160: + " returns ERROR = " + errorCode + "("
161: + JDWPConstants.Error.getName(errorCode) + ")");
162: logWriter
163: .println("=> It is possible case - CHECK 1 PASSED");
164: logWriter
165: .println("=> Send to Debuggee signal to funish ...");
166: synchronizer.sendMessage("TO_FINISH");
167: logWriter.println("==> " + this TestName + " for "
168: + this CommandName + ": FINISH");
169: } else {
170: checkReplyPacket(checkedReply, this CommandName);
171: }
172:
173: long monitorOwnerThreadID = checkedReply
174: .getNextValueAsThreadID();
175: logWriter.println("=> Returned monitorOwnerThreadID = "
176: + monitorOwnerThreadID);
177: if (monitorOwnerThreadID != 0) {
178: logWriter.println("## FAILURE: " + this CommandName
179: + " returns unexpected monitorOwnerThreadID:"
180: + monitorOwnerThreadID);
181: logWriter.println("## Expected monitorOwnerThreadID = 0");
182: failMessage = failMessage + this CommandName
183: + " returns unexpected monitorOwnerThreadID: "
184: + monitorOwnerThreadID + ", Expected: 0\n";
185: }
186:
187: int monitorEntryCount = checkedReply.getNextValueAsInt();
188: logWriter.println("=> Returned monitorEntryCount = "
189: + monitorEntryCount);
190: if (monitorEntryCount != 0) {
191: logWriter.println("## FAILURE: " + this CommandName
192: + " returns unexpected monitorEntryCount:");
193: logWriter.println("## Expected monitorEntryCount = 0");
194: failMessage = failMessage + this CommandName
195: + " returns unexpected monitorEntryCount:"
196: + monitorEntryCount + ", expected: 0\n";
197: }
198:
199: int monitorWaiters = checkedReply.getNextValueAsInt();
200: logWriter.println("=> Returned monitorWaiters = "
201: + monitorWaiters);
202: if (monitorWaiters != 0) {
203: logWriter.println("## FAILURE: " + this CommandName
204: + " returns unexpected monitorWaiters:");
205: logWriter.println("## Expected monitorWaiters = 0");
206: failMessage = failMessage + this CommandName
207: + " returns unexpected monitorWaiters:"
208: + monitorWaiters + ", expected: 0\n";
209: }
210:
211: assertAllDataRead(checkedReply);
212: logWriter
213: .println("=> CHECK 1: PASSED - expected monitor info is received");
214: checkedReply = null;
215:
216: logWriter
217: .println("\n=> Send VirtualMachine::Resume command ...");
218:
219: resumeDebuggee();
220:
221: logWriter
222: .println("=> Send to Debuggee signal to continue and to enter in synchronized block ...");
223: finalSyncMessage = null;
224: synchronizer
225: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
226: synchronizer
227: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
228:
229: logWriter.println("=> Send VirtualMachine::Suspend command...");
230:
231: suspendCommand = new CommandPacket(
232: JDWPCommands.VirtualMachineCommandSet.CommandSetID,
233: JDWPCommands.VirtualMachineCommandSet.SuspendCommand);
234:
235: suspendReply = debuggeeWrapper.vmMirror
236: .performCommand(suspendCommand);
237: suspendCommand = null;
238: checkReplyPacket(suspendReply,
239: "VirtualMachine::Suspend command");
240:
241: logWriter.println("\n=> CHECK 2: send " + this CommandName
242: + " for checked ObjectID when it is locked...");
243:
244: checkedCommand = new CommandPacket(
245: JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
246: JDWPCommands.ObjectReferenceCommandSet.MonitorInfoCommand);
247: checkedCommand.setNextValueAsObjectID(checkedObjectID);
248:
249: checkedReply = debuggeeWrapper.vmMirror
250: .performCommand(checkedCommand);
251: checkedCommand = null;
252: checkReplyPacket(checkedReply, this CommandName);
253:
254: monitorOwnerThreadID = checkedReply.getNextValueAsThreadID();
255: logWriter.println("=> Returned monitorOwnerThreadID = "
256: + monitorOwnerThreadID);
257: if (monitorOwnerThreadID == 0) {
258: logWriter.println("## FAILURE: " + this CommandName
259: + " returns unexpected monitorOwnerThreadID:");
260: logWriter
261: .println("## Expected monitorOwnerThreadID = 'not null'");
262: failMessage = failMessage + this CommandName
263: + " returns unexpected monitorOwnerThreadID: 0"
264: + ", Expected monitorOwnerThreadID: 'not null'\n";
265: }
266:
267: monitorEntryCount = checkedReply.getNextValueAsInt();
268: logWriter.println("=> Returned monitorEntryCount = "
269: + monitorEntryCount);
270: if (monitorEntryCount != 1) {
271: logWriter.println("## FAILURE: " + this CommandName
272: + " returns unexpected monitorEntryCount:"
273: + monitorEntryCount);
274: logWriter.println("## Expected monitorEntryCount = 1");
275: failMessage = failMessage + this CommandName
276: + " returns unexpected monitorEntryCount: "
277: + monitorEntryCount + ", expected: 1\n";
278: }
279:
280: monitorWaiters = checkedReply.getNextValueAsInt();
281: logWriter.println("=> Returned monitorWaiters = "
282: + monitorWaiters);
283: if (monitorWaiters != 0) {
284: logWriter.println("## FAILURE: " + this CommandName
285: + " returns unexpected monitorWaiters:"
286: + monitorWaiters);
287: logWriter.println("## Expected monitorWaiters = 0");
288: failMessage = failMessage + this CommandName
289: + " returns unexpected monitorWaiters: "
290: + monitorWaiters + ", expected: 0\n";
291: }
292:
293: logWriter
294: .println("=> CHECK 2: PASSED - expected monitor info is received");
295: logWriter
296: .println("\n=> Send VirtualMachine::Resume command ...");
297:
298: resumeDebuggee();
299:
300: logWriter.println("=> Send to Debuggee signal to funish ...");
301: synchronizer
302: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
303:
304: logWriter.println("==> " + this TestName + " for "
305: + this CommandName + ": FINISH");
306:
307: assertAllDataRead(checkedReply);
308:
309: if (failMessage.length() > 0) {
310: fail(failMessage);
311: }
312: }
313:
314: public static void main(String[] args) {
315: junit.textui.TestRunner.run(MonitorInfoTest.class);
316: }
317: }
|