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 15.02.2005
026: */package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
027:
028: import org.apache.harmony.jpda.tests.framework.jdwp.exceptions.*;
029: import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
030: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
031: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
032: import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
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.SuspendCount command.
038: */
039: public class SuspendCountTest extends JDWPSyncTestCase {
040:
041: static final int testStatusPassed = 0;
042: static final int testStatusFailed = -1;
043: static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ThreadReference/SuspendCountDebuggee;";
044:
045: protected String getDebuggeeClassName() {
046: return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.SuspendCountDebuggee";
047: }
048:
049: /**
050: * This testcase exercises ThreadReference.SuspendCount command.
051: * <BR>At first the test starts SuspendCountDebuggee which starts and runs some tested threads.
052: * <BR>After the tested threads starts, the test suspends every
053: * tested thread in debuggee some times and
054: * check that ThreadReference.SuspendCount command returns
055: * expected number of times thread was suspended.
056: * <BR>Then the test suspends all debuggee by VirtualMachine.Suspend command
057: * and check that ThreadReference.SuspendCount command returns 1 value
058: * for all tested threads and main thread.
059: * <BR>Then the test resumes all debuggee by VirtualMachine.Resume command
060: * and check that ThreadReference.SuspendCount command returns 0 value
061: * for all tested threads and main thread.
062: */
063: public void testSuspendCount001() {
064: logWriter.println("==> testSuspendCount001: START...");
065: String debuggeeMessage = synchronizer.receiveMessage();
066: int testedThreadsNumber = 0;
067: try {
068: testedThreadsNumber = Integer.valueOf(debuggeeMessage)
069: .intValue();
070: } catch (NumberFormatException exception) {
071: logWriter
072: .println("## FAILURE: Exception while getting number of started threads from debuggee = "
073: + exception);
074: synchronizer.sendMessage("FINISH");
075: printErrorAndFail("Can NOT get number of started threads from debuggee! ");
076: }
077: if (testedThreadsNumber == 0) {
078: logWriter
079: .println("==> There are no started threads in debuggee to test"
080: + " - testSuspendCount001 finishes!");
081: synchronizer.sendMessage("FINISH");
082: return;
083: }
084: logWriter
085: .println("==> Number of started threads in debuggee to test = "
086: + testedThreadsNumber);
087: String[] testedThreadsNames = new String[testedThreadsNumber + 1]; // +1 is for main debuggee thread
088: long[] testedThreadsIDs = new long[testedThreadsNumber + 1];
089: for (int i = 0; i < testedThreadsNumber; i++) {
090: testedThreadsNames[i] = SuspendCountDebuggee.THREAD_NAME_PATTERN
091: + i;
092: testedThreadsIDs[i] = 0;
093: }
094:
095: // getting ID of the tested thread
096: ReplyPacket allThreadIDReply = null;
097: try {
098: allThreadIDReply = debuggeeWrapper.vmMirror
099: .getAllThreadID();
100: } catch (ReplyErrorCodeException exception) {
101: logWriter
102: .println("## FAILURE: Exception in vmMirror.getAllThreadID() = "
103: + exception);
104: synchronizer.sendMessage("FINISH");
105: printErrorAndFail("Can NOT get all ThreadID in debuggee! ");
106: }
107: int threads = allThreadIDReply.getNextValueAsInt();
108: logWriter.println("==> Number of all threads in debuggee = "
109: + threads);
110: String[] allThreadsNames = new String[threads];
111: long[] allThreadsIDs = new long[threads];
112: boolean suspendCommandFailed = false;
113: boolean suspendCountCommandFailed = false;
114: boolean resumeCommandFailed = false;
115:
116: int suspendNumber = 0;
117: for (int i = 0; i < threads; i++) {
118: long threadID = allThreadIDReply.getNextValueAsThreadID();
119: allThreadsIDs[i] = threadID;
120: String threadName = null;
121: try {
122: threadName = debuggeeWrapper.vmMirror
123: .getThreadName(threadID);
124: } catch (ReplyErrorCodeException exception) {
125: logWriter
126: .println("==> WARNING: Can NOT get thread name for threadID = "
127: + threadID);
128: continue;
129: }
130: allThreadsNames[i] = threadName;
131: int k = 0;
132: for (; k < testedThreadsNumber; k++) {
133: if (threadName.equals(testedThreadsNames[k])) {
134: testedThreadsIDs[k] = threadID;
135: break;
136: }
137: }
138: if (k == testedThreadsNumber) {
139: // it is not thread to test
140: continue;
141: }
142:
143: logWriter.println("\n==> Check for Thread: threadID = "
144: + threadID + "; threadName = " + threadName);
145:
146: logWriter
147: .println("==> Send ThreadReference.SuspendCount command...");
148: CommandPacket packet = new CommandPacket(
149: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
150: JDWPCommands.ThreadReferenceCommandSet.SuspendCountCommand);
151: packet.setNextValueAsThreadID(threadID);
152: ReplyPacket reply = debuggeeWrapper.vmMirror
153: .performCommand(packet);
154: if (!checkReplyPacketWithoutFail(reply,
155: "ThreadReference.SuspendCount command")) {
156: suspendCountCommandFailed = true;
157: } else {
158: int suspendCount = reply.getNextValueAsInt();
159: logWriter
160: .println("==> ThreadReference.SuspendCount command returns suspendCount = "
161: + suspendCount);
162: if (suspendCount != 0) {
163: logWriter
164: .println("## FAILURE: Unexpected suspendCount for thread = "
165: + threadName);
166: logWriter
167: .println("## Expected suspendCount = 0");
168: suspendCountCommandFailed = true;
169: }
170: }
171:
172: suspendNumber++;
173: logWriter
174: .println("==> Send ThreadReference.Suspend command number of times = "
175: + suspendNumber + "...");
176: int j = 0;
177: for (; j < suspendNumber; j++) {
178: packet = new CommandPacket(
179: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
180: JDWPCommands.ThreadReferenceCommandSet.SuspendCommand);
181: packet.setNextValueAsThreadID(threadID);
182: reply = debuggeeWrapper.vmMirror.performCommand(packet);
183: if (!checkReplyPacketWithoutFail(reply,
184: "ThreadReference.Suspend command")) {
185: break;
186: }
187: }
188: if (j < suspendNumber) {
189: suspendCommandFailed = true;
190: continue;
191: }
192:
193: logWriter
194: .println("==> Send ThreadReference.SuspendCount command...");
195: packet = new CommandPacket(
196: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
197: JDWPCommands.ThreadReferenceCommandSet.SuspendCountCommand);
198: packet.setNextValueAsThreadID(threadID);
199: reply = debuggeeWrapper.vmMirror.performCommand(packet);
200: if (!checkReplyPacketWithoutFail(reply,
201: "ThreadReference.SuspendCount command")) {
202: suspendCountCommandFailed = true;
203: } else {
204: int suspendCount = reply.getNextValueAsInt();
205: logWriter
206: .println("==> ThreadReference.SuspendCount command returns suspendCount = "
207: + suspendCount);
208: if (suspendCount != suspendNumber) {
209: logWriter
210: .println("## FAILURE: Unexpected suspendCount for thread = "
211: + threadName);
212: logWriter
213: .println("## Expected suspendCount = "
214: + suspendNumber);
215: suspendCountCommandFailed = true;
216: }
217: }
218:
219: logWriter
220: .println("==> Send ThreadReference.Resume command...");
221: packet = new CommandPacket(
222: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
223: JDWPCommands.ThreadReferenceCommandSet.ResumeCommand);
224: packet.setNextValueAsThreadID(threadID);
225: reply = debuggeeWrapper.vmMirror.performCommand(packet);
226: if (!checkReplyPacketWithoutFail(reply,
227: "ThreadReference.Resume command")) {
228: resumeCommandFailed = true;
229: }
230:
231: logWriter
232: .println("==> Send ThreadReference.SuspendCount command...");
233: packet = new CommandPacket(
234: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
235: JDWPCommands.ThreadReferenceCommandSet.SuspendCountCommand);
236: packet.setNextValueAsThreadID(threadID);
237: reply = debuggeeWrapper.vmMirror.performCommand(packet);
238: if (!checkReplyPacketWithoutFail(reply,
239: "ThreadReference.SuspendCount command")) {
240: suspendCountCommandFailed = true;
241: } else {
242: int suspendCount = reply.getNextValueAsInt();
243: logWriter
244: .println("==> ThreadReference.SuspendCount command returns suspendCount = "
245: + suspendCount);
246: if (suspendCount != (suspendNumber - 1)) {
247: logWriter
248: .println("## FAILURE: Unexpected suspendCount for thread = "
249: + threadName);
250: logWriter
251: .println("## Expected suspendCount = "
252: + (suspendNumber - 1));
253: suspendCountCommandFailed = true;
254: }
255: }
256:
257: if (suspendNumber == 1) {
258: continue;
259: }
260:
261: logWriter
262: .println("==> Send ThreadReference.Resume command number of times = "
263: + (suspendNumber - 1) + "...");
264: j = 0;
265: for (; j < (suspendNumber - 1); j++) {
266: packet = new CommandPacket(
267: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
268: JDWPCommands.ThreadReferenceCommandSet.ResumeCommand);
269: packet.setNextValueAsThreadID(threadID);
270: reply = debuggeeWrapper.vmMirror.performCommand(packet);
271: if (!checkReplyPacketWithoutFail(reply,
272: "ThreadReference.Resume command")) {
273: break;
274: }
275: }
276: if (j < (suspendNumber - 1)) {
277: resumeCommandFailed = true;
278: continue;
279: }
280:
281: logWriter
282: .println("==> Send ThreadReference.SuspendCount command...");
283: packet = new CommandPacket(
284: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
285: JDWPCommands.ThreadReferenceCommandSet.SuspendCountCommand);
286: packet.setNextValueAsThreadID(threadID);
287: reply = debuggeeWrapper.vmMirror.performCommand(packet);
288: if (!checkReplyPacketWithoutFail(reply,
289: "ThreadReference.SuspendCount command")) {
290: suspendCountCommandFailed = true;
291: continue;
292: }
293: int suspendCount = reply.getNextValueAsInt();
294: logWriter
295: .println("==> ThreadReference.SuspendCount command returns suspendCount = "
296: + suspendCount);
297: if (suspendCount != 0) {
298: logWriter
299: .println("## FAILURE: Unexpected suspendCount for thread = "
300: + threadName);
301: logWriter
302: .println("## Expected suspendCount = 0");
303: suspendCountCommandFailed = true;
304: }
305: }
306:
307: String errorMessage = "";
308: if (suspendCountCommandFailed) {
309: errorMessage = errorMessage
310: + "## Error found out while ThreadReference.SuspendCount command performing!\n";
311: }
312: if (suspendCommandFailed) {
313: errorMessage = errorMessage
314: + "## Error found out while ThreadReference.Suspend command performing!\n";
315: }
316: if (resumeCommandFailed) {
317: errorMessage = errorMessage
318: + "## Error found out while ThreadReference.Resume command performing!\n";
319: }
320:
321: boolean testedThreadNotFound = false;
322: for (int i = 0; i < testedThreadsNumber; i++) {
323: if (testedThreadsIDs[i] == 0) {
324: logWriter
325: .println("## FAILURE: Tested thread is not found out among debuggee threads!");
326: logWriter.println("## Thread name = "
327: + testedThreadsNames[i]);
328: testedThreadNotFound = true;
329: }
330: }
331:
332: if (testedThreadNotFound) {
333: errorMessage = errorMessage
334: + "## Some of tested threads are not found!\n";
335: }
336: if (!errorMessage.equals("")) {
337: synchronizer.sendMessage("FINISH");
338: printErrorAndFail("\ntestSuspendCount001 FAILED:\n"
339: + errorMessage);
340: }
341:
342: synchronizer
343: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
344: logWriter
345: .println("\n==> Check ThreadReference.SuspendCount command when all debuggee is suspended...");
346: testedThreadsNames[testedThreadsNumber] = synchronizer
347: .receiveMessage(); // debuggee main thread name
348:
349: testedThreadsIDs[testedThreadsNumber] = 0; // debuggee main thread ID
350: for (int i = 0; i < threads; i++) {
351: if (testedThreadsNames[testedThreadsNumber]
352: .equals(allThreadsNames[i])) {
353: testedThreadsIDs[testedThreadsNumber] = allThreadsIDs[i];
354: break;
355: }
356: }
357: if (testedThreadsIDs[testedThreadsNumber] == 0) {
358: setStaticIntField(debuggeeSignature,
359: SuspendCountDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME,
360: 99);
361: logWriter
362: .println("## FAILURE: Debuggee main thread is not found out among debuggee threads!");
363: logWriter.println("## Thread name = "
364: + testedThreadsNames[testedThreadsNumber]);
365: printErrorAndFail("\nCan NOT found out debuggee main thread!");
366: }
367:
368: logWriter
369: .println("\n==> Send VirtualMachine.Suspend command...");
370: CommandPacket packet = new CommandPacket(
371: JDWPCommands.VirtualMachineCommandSet.CommandSetID,
372: JDWPCommands.VirtualMachineCommandSet.SuspendCommand);
373: ReplyPacket reply = debuggeeWrapper.vmMirror
374: .performCommand(packet);
375: int errorCode = reply.getErrorCode();
376: if (errorCode != JDWPConstants.Error.NONE) {
377: setStaticIntField(debuggeeSignature,
378: SuspendCountDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME,
379: 99);
380: logWriter
381: .println("## FAILURE: VirtualMachine.Suspend command returns error = "
382: + errorCode
383: + "("
384: + JDWPConstants.Error.getName(errorCode)
385: + ")");
386: printErrorAndFail("\nVirtualMachine.Suspend command FAILED!");
387: }
388:
389: for (int i = 0; i < (testedThreadsNumber + 1); i++) {
390: logWriter
391: .println("==> Send ThreadReference.SuspendCount command for thread = "
392: + testedThreadsNames[i] + " ...");
393: packet = new CommandPacket(
394: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
395: JDWPCommands.ThreadReferenceCommandSet.SuspendCountCommand);
396: packet.setNextValueAsThreadID(testedThreadsIDs[i]);
397: reply = debuggeeWrapper.vmMirror.performCommand(packet);
398: if (!checkReplyPacketWithoutFail(reply,
399: "ThreadReference.SuspendCount command")) {
400: suspendCountCommandFailed = true;
401: continue;
402: }
403: int suspendCount = reply.getNextValueAsInt();
404: logWriter
405: .println("==> ThreadReference.SuspendCount command returns suspendCount = "
406: + suspendCount);
407: if (suspendCount != 1) {
408: logWriter
409: .println("## FAILURE: Unexpected suspendCount for thread = "
410: + testedThreadsNames[i]);
411: logWriter
412: .println("## Expected suspendCount = 1");
413: suspendCountCommandFailed = true;
414: }
415: }
416:
417: logWriter
418: .println("\n==> Send VirtualMachine.Resume command ...");
419: packet = new CommandPacket(
420: JDWPCommands.VirtualMachineCommandSet.CommandSetID,
421: JDWPCommands.VirtualMachineCommandSet.ResumeCommand);
422: reply = debuggeeWrapper.vmMirror.performCommand(packet);
423: if (!checkReplyPacketWithoutFail(reply,
424: "VirtualMachine.Resume command")) {
425: resumeCommandFailed = true;
426: } else {
427: logWriter
428: .println("\n==> Check ThreadReference.SuspendCount command after debuggee is resumed...");
429: for (int i = 0; i < (testedThreadsNumber + 1); i++) {
430: logWriter
431: .println("==> Send ThreadReference.SuspendCount command for thread = "
432: + testedThreadsNames[i] + " ...");
433: packet = new CommandPacket(
434: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
435: JDWPCommands.ThreadReferenceCommandSet.SuspendCountCommand);
436: packet.setNextValueAsThreadID(testedThreadsIDs[i]);
437: reply = debuggeeWrapper.vmMirror.performCommand(packet);
438: if (!checkReplyPacketWithoutFail(reply,
439: "ThreadReference.SuspendCount command")) {
440: suspendCountCommandFailed = true;
441: continue;
442: }
443: int suspendCount = reply.getNextValueAsInt();
444: logWriter
445: .println("==> ThreadReference.SuspendCount command returns suspendCount = "
446: + suspendCount);
447: if (suspendCount != 0) {
448: logWriter
449: .println("## FAILURE: Unexpected suspendCount for thread = "
450: + testedThreadsNames[i]);
451: logWriter
452: .println("## Expected suspendCount = 0");
453: suspendCountCommandFailed = true;
454: }
455: }
456: }
457:
458: setStaticIntField(debuggeeSignature,
459: SuspendCountDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
460:
461: if (suspendCountCommandFailed) {
462: errorMessage = "## Error found out while ThreadReference.SuspendCount command performing!\n";
463: }
464:
465: if (resumeCommandFailed) {
466: errorMessage = "## Error found out while VirtualMachine.Resume command performing!\n";
467: }
468:
469: if (!errorMessage.equals("")) {
470: printErrorAndFail("\ntestSuspendCount001 FAILED:\n"
471: + errorMessage);
472: }
473: logWriter.println("\n==> testSuspendCount001 - OK!");
474: }
475:
476: /*
477: protected int setStaticIntField (String classSignature, String fieldName, int newValue) {
478:
479: long classID = debuggeeWrapper.vmMirror.getClassID(classSignature);
480: if ( classID == -1 ) {
481: logWriter.println
482: ("## setStaticIntField(): Can NOT get classID for class signature = '"
483: + classSignature + "'");
484: return -1;
485: }
486:
487: long fieldID =
488: debuggeeWrapper.vmMirror.getFieldID(classID, fieldName);
489: if ( fieldID == -1 ) {
490: logWriter.println
491: ("## setStaticIntField(): Can NOT get fieldID for field = '"
492: + fieldName + "'");
493: return -1;
494: }
495:
496: CommandPacket packet = new CommandPacket(
497: JDWPCommands.ClassTypeCommandSet.CommandSetID,
498: JDWPCommands.ClassTypeCommandSet.SetValuesCommand);
499: packet.setNextValueAsReferenceTypeID(classID);
500: packet.setNextValueAsInt(1);
501: packet.setNextValueAsFieldID(fieldID);
502: packet.setNextValueAsInt(newValue);
503:
504: ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
505: int errorCode = reply.getErrorCode();
506: if ( errorCode != JDWPConstants.Error.NONE ) {
507: logWriter.println
508: ("## setStaticIntField(): Can NOT set value for field = '"
509: + fieldName + "' in class = '" + classSignature
510: + "'; ClassType.SetValues command reurns error = " + errorCode);
511: return -1;
512: }
513: return 0;
514: }
515: */
516: public static void main(String[] args) {
517: junit.textui.TestRunner.run(SuspendCountTest.class);
518: }
519: }
|