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.4 $
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.OwnedMonitors command.
038: */
039: public class OwnedMonitorsTest extends JDWPSyncTestCase {
040:
041: protected String getDebuggeeClassName() {
042: return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.OwnedMonitorsDebuggee";
043: }
044:
045: /**
046: * This testcase exercises ThreadReference.OwnedMonitors command.
047: * <BR>At first the test starts OwnedMonitorsDebuggee which runs
048: * the tested thread 'TESTED_THREAD'.
049: * <BR>Then the test performs the ThreadReference.OwnedMonitors command
050: * for the tested thread and gets list of monitor objects.
051: * It is expected that this command returns at least two monitors
052: * owned by 'TESTED_THREAD' thread
053: * <BR>After this for each received monitor object the test performs
054: * ObjectReference.MonitorInfo command.
055: * It is expected that this command returns the 'TESTED_THREAD' thread
056: * as owner for each monitor.
057: */
058: public void testOwnedMonitors001() {
059:
060: synchronizer
061: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
062:
063: // checking capability relevant for this test
064: logWriter
065: .println("==> Check capability: canGetOwnedMonitorInfo");
066: debuggeeWrapper.vmMirror.capabilities();
067: boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canGetOwnedMonitorInfo;
068: if (!isCapability) {
069: logWriter
070: .println("##WARNING: this VM dosn't possess capability: canGetOwnedMonitorInfo");
071: synchronizer
072: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
073: return;
074: }
075:
076: // getting ID of the tested thread
077: logWriter.println("==> testedThreadName = "
078: + OwnedMonitorsDebuggee.TESTED_THREAD);
079: logWriter.println("==> Get testedThreadID...");
080: long testedThreadID = debuggeeWrapper.vmMirror
081: .getThreadID(OwnedMonitorsDebuggee.TESTED_THREAD);
082: logWriter.println("==> testedThreadID = " + testedThreadID);
083: logWriter.println("==> suspend testedThread...");
084: debuggeeWrapper.vmMirror.suspendThread(testedThreadID);
085:
086: // getting the thread group ID
087: CommandPacket packet = new CommandPacket(
088: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
089: JDWPCommands.ThreadReferenceCommandSet.OwnedMonitorsCommand);
090: packet.setNextValueAsThreadID(testedThreadID);
091: ReplyPacket reply = debuggeeWrapper.vmMirror
092: .performCommand(packet);
093: checkReplyPacket(reply,
094: "ThreadReference::OwnedMonitors command");
095:
096: int owned = reply.getNextValueAsInt();
097: logWriter.println("owned monitors: " + owned);
098:
099: String ownerName;
100: long ownerThread;
101:
102: for (int i = 0; i < owned; i++) {
103: TaggedObject tobj = reply.getNextValueAsTaggedObject();
104:
105: logWriter.println("\t" + i + " tagged-object tag: "
106: + JDWPConstants.Tag.getName(tobj.tag) + "("
107: + tobj.tag + ") " + "ID: " + tobj.objectID);
108:
109: packet = new CommandPacket(
110: JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
111: JDWPCommands.ObjectReferenceCommandSet.MonitorInfoCommand);
112: packet.setNextValueAsObjectID(tobj.objectID);
113: ReplyPacket replyObj = debuggeeWrapper.vmMirror
114: .performCommand(packet);
115: checkReplyPacket(replyObj,
116: "ObjectReference::MonitorInfo command");
117:
118: ownerThread = replyObj.getNextValueAsThreadID();
119: logWriter.println("\t\t" + "ownerThread ID: "
120: + ownerThread);
121:
122: ownerName = debuggeeWrapper.vmMirror
123: .getThreadName(ownerThread);
124: logWriter
125: .println("\t\t" + "ownerThread name: " + ownerName);
126:
127: if (ownerThread != testedThreadID) {
128: printErrorAndFail("wrong owner thread: " + ownerThread);
129: }
130: if (!ownerName.equals(OwnedMonitorsDebuggee.TESTED_THREAD)) {
131: printErrorAndFail("wrong owner thread name: "
132: + ownerName);
133: }
134: }
135:
136: // check that at least two owned monitors are returned
137: if (owned < 2) {
138: printErrorAndFail("wrong number of owned monitors: "
139: + owned + " (expected at least 2)");
140: }
141:
142: synchronizer
143: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
144: }
145:
146: public static void main(String[] args) {
147: junit.textui.TestRunner.run(OwnedMonitorsTest.class);
148: }
149: }
|