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.5 $
022: */
023:
024: /**
025: * Created on 09.02.2005
026: */package org.apache.harmony.jpda.tests.jdwp.VirtualMachine;
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.jdwp.share.JDWPSyncTestCase;
033: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
034:
035: /**
036: * JDWP Unit test for VirtualMachine.AllThreads command.
037: */
038: public class AllThreadsTest extends JDWPSyncTestCase {
039:
040: protected String getDebuggeeClassName() {
041: return "org.apache.harmony.jpda.tests.jdwp.VirtualMachine.AllThreadsDebuggee";
042: }
043:
044: /**
045: * This testcase exercises VirtualMachine.AllThreads command.
046: * <BR>At first the test starts AllThreadsDebuggee which runs the TESTED_THREAD thread
047: * which starts and finishes.
048: * <BR> Then the test performs VirtualMachine.AllThreads command and checks that
049: * the tested thread is not returned by command;
050: */
051: public void testAllThreads003() {
052: synchronizer
053: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
054:
055: synchronizer
056: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
057: logWriter.println("waiting for finishing thread");
058: synchronizer
059: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
060:
061: logWriter.println("send AllThreads cmd");
062: CommandPacket packet = new CommandPacket(
063: JDWPCommands.VirtualMachineCommandSet.CommandSetID,
064: JDWPCommands.VirtualMachineCommandSet.AllThreadsCommand);
065:
066: ReplyPacket reply = debuggeeWrapper.vmMirror
067: .performCommand(packet);
068: checkReplyPacket(reply, "VirtualMachine::AllThreads command");
069:
070: long threadID;
071: int threadStatus, suspendStatus;
072: String threadName;
073: ReplyPacket replyName;
074:
075: int threads = reply.getNextValueAsInt();
076: logWriter.println("Number of threads = " + threads);
077: assertTrue("Number of threads must be > 0", threads > 0);
078:
079: for (int i = 0; i < threads; i++) {
080:
081: threadID = reply.getNextValueAsThreadID();
082: threadName = debuggeeWrapper.vmMirror
083: .getThreadName(threadID);
084:
085: packet = new CommandPacket(
086: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
087: JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
088: packet.setNextValueAsReferenceTypeID(threadID);
089:
090: replyName = debuggeeWrapper.vmMirror.performCommand(packet);
091: checkReplyPacket(replyName,
092: "ThreadReference::Status command");
093:
094: threadStatus = replyName.getNextValueAsInt();
095: suspendStatus = replyName.getNextValueAsInt();
096:
097: logWriter.println("\t"
098: + threadID
099: + " "
100: + "\""
101: + threadName
102: + "\" "
103: + JDWPConstants.ThreadStatus.getName(threadStatus)
104: + " "
105: + JDWPConstants.SuspendStatus
106: .getName(suspendStatus));
107:
108: if (threadName.equals(AllThreadsDebuggee.TESTED_THREAD)) {
109: printErrorAndFail(AllThreadsDebuggee.TESTED_THREAD
110: + " must not be in all_thread list");
111: }
112: }
113: assertAllDataRead(reply);
114: synchronizer
115: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
116: }
117:
118: /**
119: * This testcase exercises VirtualMachine.AllThreads command.
120: * <BR>At first the test starts AllThreadsDebuggee which runs the TESTED_THREAD thread
121: * which starts but does not finish.
122: * <BR> Then the test performs VirtualMachine.AllThreads command and checks that
123: * all threads returned by command have only valid thread status:
124: * <BR>'RUNNING' or 'MONITOR' or 'SLEEPING' or 'ZOMBIE' or 'WAIT' status;
125: */
126: public void testAllThreads002() {
127: synchronizer
128: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
129:
130: logWriter.println("send AllThreads cmd");
131: CommandPacket packet = new CommandPacket(
132: JDWPCommands.VirtualMachineCommandSet.CommandSetID,
133: JDWPCommands.VirtualMachineCommandSet.AllThreadsCommand);
134:
135: ReplyPacket reply = debuggeeWrapper.vmMirror
136: .performCommand(packet);
137: checkReplyPacket(reply, "VirtualMachine::AllThreads command");
138:
139: long threadID;
140: int threadStatus, suspendStatus;
141: String threadName;
142: int count = 0;
143: ReplyPacket replyName;
144:
145: int threads = reply.getNextValueAsInt();
146: logWriter.println("Number of threads = " + threads);
147: assertTrue("Number of threads must be > 0", threads > 0);
148:
149: boolean threadStatusFailed = false;
150: for (int i = 0; i < threads; i++) {
151:
152: threadID = reply.getNextValueAsThreadID();
153: threadName = debuggeeWrapper.vmMirror
154: .getThreadName(threadID);
155:
156: packet = new CommandPacket(
157: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
158: JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
159: packet.setNextValueAsReferenceTypeID(threadID);
160:
161: replyName = debuggeeWrapper.vmMirror.performCommand(packet);
162: checkReplyPacket(replyName,
163: "ThreadReference::Status command");
164:
165: threadStatus = replyName.getNextValueAsInt();
166: suspendStatus = replyName.getNextValueAsInt();
167:
168: logWriter.println("\t"
169: + threadID
170: + " "
171: + "\""
172: + threadName
173: + "\" "
174: + JDWPConstants.ThreadStatus.getName(threadStatus)
175: + " "
176: + JDWPConstants.SuspendStatus
177: .getName(suspendStatus));
178: if (threadStatus == JDWPConstants.ThreadStatus.RUNNING
179: || threadStatus == JDWPConstants.ThreadStatus.MONITOR
180: || threadStatus == JDWPConstants.ThreadStatus.SLEEPING
181: || threadStatus == JDWPConstants.ThreadStatus.ZOMBIE
182: || threadStatus == JDWPConstants.ThreadStatus.WAIT) {
183: } else {
184: logWriter
185: .println("## FAILURE: Unknown thread status is found out!");
186: threadStatusFailed = true;
187: count++;
188: }
189: }
190: if (threadStatusFailed) {
191: printErrorAndFail("\nThreads with unknown thread status found out!\n"
192: + "Number of such threads = " + count + "\n");
193: }
194: assertAllDataRead(reply);
195:
196: synchronizer
197: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
198: logWriter.println("waiting for finishing thread");
199: synchronizer
200: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
201: synchronizer
202: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
203: }
204:
205: /**
206: * This testcase exercises VirtualMachine.AllThreads command.
207: * <BR>At first the test starts AllThreadsDebuggee which runs the TESTED_THREAD thread
208: * which starts but does not finish.
209: * <BR> Then the test performs VirtualMachine.AllThreads command and checks that
210: * the tested thread is returned by command in the list of threads;
211: */
212: public void testAllThreads001() {
213: synchronizer
214: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
215:
216: logWriter.println("send AllThreads cmd");
217: CommandPacket packet = new CommandPacket(
218: JDWPCommands.VirtualMachineCommandSet.CommandSetID,
219: JDWPCommands.VirtualMachineCommandSet.AllThreadsCommand);
220:
221: ReplyPacket reply = debuggeeWrapper.vmMirror
222: .performCommand(packet);
223: checkReplyPacket(reply, "VirtualMachine::AllThreads command");
224:
225: long threadID;
226: String threadName;
227: int count = 0;
228:
229: int threads = reply.getNextValueAsInt();
230: logWriter.println("Number of threads = " + threads);
231: assertTrue("Number of threads must be > 0", threads > 0);
232:
233: for (int i = 0; i < threads; i++) {
234: threadID = reply.getNextValueAsThreadID();
235: threadName = debuggeeWrapper.vmMirror
236: .getThreadName(threadID);
237:
238: if (threadName.equals(AllThreadsDebuggee.TESTED_THREAD)) {
239: count++;
240: logWriter.println("\t" + threadID + " " + "\""
241: + threadName + "\" found");
242: }
243: }
244: if (count != 1) {
245: if (count == 0) {
246: printErrorAndFail(AllThreadsDebuggee.TESTED_THREAD
247: + " not found");
248: }
249: if (count > 1 || count < 0) {
250: printErrorAndFail(AllThreadsDebuggee.TESTED_THREAD
251: + " unexpected amount: " + count);
252: }
253: }
254: assertAllDataRead(reply);
255: synchronizer
256: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
257: logWriter.println("waiting for finishing thread");
258: synchronizer
259: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
260: synchronizer
261: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
262: }
263:
264: public static void main(String[] args) {
265: junit.textui.TestRunner.run(AllThreadsTest.class);
266: }
267: }
|