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 Vitaly A. Provodin
021: * @version $Revision: 1.7 $
022: */
023:
024: /**
025: * Created on 24.02.2005
026: */package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
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.TaggedObject;
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 ThreadReference.CurrentContendedMonitor command.
038: */
039: public class CurrentContendedMonitorTest extends JDWPSyncTestCase {
040:
041: protected String getDebuggeeClassName() {
042: return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.CurrentContendedMonitorDebuggee";
043: }
044:
045: /**
046: * This testcase exercises ThreadReference.CurrentContendedMonitor command.
047: * <BR>At first the test starts CurrentContendedMonitorDebuggee which runs
048: * the tested thread 'TESTED_THREAD'.
049: * <BR> Then the test performs the ThreadReference.CurrentContendedMonitor command
050: * for the tested thread.
051: * <BR>After getting monitor object from command, the test
052: * performs the ObjectReference.MonitorInfo command for this monitor object
053: * and checks that the waiter for this monitor is the 'TESTED_THREAD' thread.
054: *
055: */
056: public void testCurrentContendedMonitor001() {
057: synchronizer
058: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
059:
060: //check capability, relevant for this test
061: debuggeeWrapper.vmMirror.capabilities();
062: logWriter.println("=> Check capability: canGetMonitorInfo");
063: boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canGetMonitorInfo;
064: if (!isCapability) {
065: logWriter
066: .println("##WARNING: this VM doesn't possess capability: canGetMonitorInfo");
067: return;
068: }
069: logWriter
070: .println("=> Check capability: canGetCurrentContendedMonitor");
071: isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canGetCurrentContendedMonitor;
072: if (!isCapability) {
073: logWriter
074: .println("##WARNING: this VM doesn't possess capability: canGetCurrentContendedMonitor");
075: return;
076: }
077:
078: // getting ID of the tested thread
079: logWriter.println("==> testedThreadName = "
080: + CurrentContendedMonitorDebuggee.TESTED_THREAD);
081: logWriter.println("==> Get testedThreadID...");
082: long testedThreadID = debuggeeWrapper.vmMirror
083: .getThreadID(CurrentContendedMonitorDebuggee.TESTED_THREAD);
084: logWriter.println("==> testedThreadID = " + testedThreadID);
085: logWriter.println("==> suspend testedThread...");
086: debuggeeWrapper.vmMirror.suspendThread(testedThreadID);
087:
088: // getting the thread group ID
089: CommandPacket packet = new CommandPacket(
090: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
091: JDWPCommands.ThreadReferenceCommandSet.CurrentContendedMonitorCommand);
092: packet.setNextValueAsThreadID(testedThreadID);
093: logWriter.println("send \"CurrentContendedMonitor\" command");
094: ReplyPacket reply = debuggeeWrapper.vmMirror
095: .performCommand(packet);
096: checkReplyPacket(reply,
097: "ThreadReference::CurrentContendedMonitor command");
098:
099: TaggedObject tobj = reply.getNextValueAsTaggedObject();
100:
101: logWriter.println("\ttagged-object tag: "
102: + JDWPConstants.Tag.getName(tobj.tag) + "(" + tobj.tag
103: + ") " + "ID: " + tobj.objectID);
104:
105: packet = new CommandPacket(
106: JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
107: JDWPCommands.ObjectReferenceCommandSet.MonitorInfoCommand);
108: packet.setNextValueAsObjectID(tobj.objectID);
109: ReplyPacket replyObj = debuggeeWrapper.vmMirror
110: .performCommand(packet);
111: checkReplyPacket(replyObj,
112: "ObjectReference::MonitorInfo command");
113:
114: replyObj.getNextValueAsThreadID();
115: replyObj.getNextValueAsInt();
116: int waiters = replyObj.getNextValueAsInt();
117: long waiterID;
118: String waiterName;
119: for (int i = 0; i < waiters; i++) {
120: waiterID = replyObj.getNextValueAsThreadID();
121: waiterName = debuggeeWrapper.vmMirror
122: .getThreadName(waiterID);
123: logWriter.println("\twaiter: " + " " + waiterName + "("
124: + waiterID + ")");
125: if (waiterID != testedThreadID) {
126: logWriter.printError("wrong owner: " + waiterID);
127: assertEquals(
128: "ObjectReference::MonitorInfo returned wrong owner ID,",
129: testedThreadID, waiterID);
130: }
131: assertString(
132: "ObjectReference::MonitorInfo returned invalid waiter name,",
133: OwnedMonitorsDebuggee.TESTED_THREAD, waiterName);
134: }
135:
136: // interrupt
137: packet = new CommandPacket(
138: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
139: JDWPCommands.ThreadReferenceCommandSet.InterruptCommand);
140: packet.setNextValueAsThreadID(testedThreadID);
141: logWriter.println("send \"Interrupt\" command");
142: reply = debuggeeWrapper.vmMirror.performCommand(packet);
143: checkReplyPacket(reply, "ThreadReference::Interrupt command");
144:
145: short err = reply.getErrorCode();
146: if (err != JDWPConstants.Error.NONE) {
147: logWriter.printError("Unexpected "
148: + JDWPConstants.Error.getName(err));
149: }
150:
151: synchronizer
152: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
153: }
154:
155: public static void main(String[] args) {
156: junit.textui.TestRunner.run(CurrentContendedMonitorTest.class);
157: }
158: }
|