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.6 $
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.Suspend command.
038: */
039: public class SuspendTest extends JDWPSyncTestCase {
040:
041: static final int testStatusPassed = 0;
042: static final int testStatusFailed = -1;
043:
044: protected String getDebuggeeClassName() {
045: return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.SuspendDebuggee";
046: }
047:
048: /**
049: * This testcase exercises ThreadReference.Suspend command.
050: * <BR>At first the test starts SuspendDebuggee which starts and runs some tested threads.
051: * <BR>After the tested threads starts, the test for every tested thread does:
052: * <BR> - suspends thread by ThreadReference.Suspend command;
053: * <BR> - performs the ThreadReference.Status command for tested thread;
054: * <BR>It is expected that for every tested thread returned suspend
055: * status is SUSPEND_STATUS_SUSPENDED status;
056: */
057: public void testSuspend001() {
058: logWriter.println("==> testSuspend001: START...");
059: String debuggeeMessage = synchronizer.receiveMessage();
060: int testedThreadsNumber = 0;
061: try {
062: testedThreadsNumber = Integer.valueOf(debuggeeMessage)
063: .intValue();
064: } catch (NumberFormatException exception) {
065: logWriter
066: .println("## FAILURE: Exception while getting number of started threads from debuggee = "
067: + exception);
068: printErrorAndFail("Can NOT get number of started threads from debuggee! ");
069: }
070: if (testedThreadsNumber == 0) {
071: logWriter
072: .println("==> There are no started threads in debuggee to test!");
073: return;
074: }
075: logWriter
076: .println("==> Number of started threads in debuggee to test = "
077: + testedThreadsNumber);
078: String[] testedThreadsNames = new String[testedThreadsNumber];
079: long[] testedThreadsIDs = new long[testedThreadsNumber];
080: for (int i = 0; i < testedThreadsNumber; i++) {
081: testedThreadsNames[i] = SuspendDebuggee.THREAD_NAME_PATTERN
082: + i;
083: testedThreadsIDs[i] = 0;
084: }
085:
086: // getting ID of the tested thread
087: ReplyPacket allThreadIDReply = null;
088: try {
089: allThreadIDReply = debuggeeWrapper.vmMirror
090: .getAllThreadID();
091: } catch (ReplyErrorCodeException exception) {
092: logWriter
093: .println("## FAILURE: Exception in vmMirror.getAllThreadID() = "
094: + exception);
095: printErrorAndFail("Can NOT get all ThreadID in debuggee! ");
096: }
097: int threads = allThreadIDReply.getNextValueAsInt();
098: logWriter.println("==> Number of all threads in debuggee = "
099: + threads);
100: boolean suspendCommandFailed = false;
101: boolean statusCommandFailed = false;
102: boolean suspendStatusFailed = false;
103: boolean resumeThreadFailed = false;
104:
105: for (int i = 0; i < threads; i++) {
106: long threadID = allThreadIDReply.getNextValueAsThreadID();
107: String threadName = null;
108: try {
109: threadName = debuggeeWrapper.vmMirror
110: .getThreadName(threadID);
111: } catch (ReplyErrorCodeException exception) {
112: logWriter
113: .println("==> WARNING: Can NOT get thread name for threadID = "
114: + threadID);
115: continue;
116: }
117: int k = 0;
118: for (; k < testedThreadsNumber; k++) {
119: if (threadName.equals(testedThreadsNames[k])) {
120: testedThreadsIDs[k] = threadID;
121: break;
122: }
123: }
124: if (k == testedThreadsNumber) {
125: // it is not thread to test
126: continue;
127: }
128:
129: logWriter.println("\n==> Check for Thread: threadID = "
130: + threadID + "; threadName = " + threadName);
131:
132: // suspending the thread
133: logWriter
134: .println("==> Send ThreadReference.Suspend command...");
135: CommandPacket packet = new CommandPacket(
136: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
137: JDWPCommands.ThreadReferenceCommandSet.SuspendCommand);
138: packet.setNextValueAsThreadID(threadID);
139: ReplyPacket reply = debuggeeWrapper.vmMirror
140: .performCommand(packet);
141: if (!checkReplyPacketWithoutFail(reply,
142: "ThreadReference.Suspend command")) {
143: suspendCommandFailed = true;
144: continue;
145: }
146:
147: logWriter
148: .println("==> Send ThreadReference.Status command...");
149: packet = new CommandPacket(
150: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
151: JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
152: packet.setNextValueAsReferenceTypeID(threadID);
153: reply = debuggeeWrapper.vmMirror.performCommand(packet);
154: if (!checkReplyPacketWithoutFail(reply,
155: "ThreadReference.Status command")) {
156: statusCommandFailed = true;
157: continue;
158: }
159:
160: int threadStatus = reply.getNextValueAsInt();
161: int suspendStatus = reply.getNextValueAsInt();
162:
163: logWriter.println("==> threadStatus = " + threadStatus
164: + "("
165: + JDWPConstants.ThreadStatus.getName(threadStatus)
166: + ")");
167: logWriter.println("==> suspendStatus = "
168: + suspendStatus
169: + "("
170: + JDWPConstants.SuspendStatus
171: .getName(suspendStatus) + ")");
172: if (suspendStatus != JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {
173: logWriter
174: .println("## FAILURE: Unexpected suspendStatus for thread = "
175: + threadName);
176: logWriter
177: .println("## Expected suspendStatus = "
178: + JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED
179: + "("
180: + JDWPConstants.SuspendStatus
181: .getName(JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED)
182: + ")");
183: suspendStatusFailed = true;
184: continue;
185: }
186:
187: // resuming the thread
188: int resumeErr = debuggeeWrapper.vmMirror.resumeThread(
189: threadID).getErrorCode();
190: if (resumeErr != JDWPConstants.Error.NONE) {
191: logWriter
192: .println("## FAILURE: Can NOT resume thread = "
193: + threadName);
194: logWriter
195: .println("## Received ERROR while resume thread = "
196: + resumeErr
197: + "("
198: + JDWPConstants.Error
199: .getName(resumeErr) + ")");
200: resumeThreadFailed = true;
201: }
202: }
203:
204: synchronizer
205: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
206: String errorMessage = "";
207: if (suspendCommandFailed) {
208: errorMessage = "## Error found out while ThreadReference.Suspend command performing!\n";
209: }
210: if (statusCommandFailed) {
211: errorMessage = errorMessage
212: + "## Error found out while ThreadReference.Status command performing!\n";
213: }
214: if (suspendStatusFailed) {
215: errorMessage = errorMessage
216: + "## Unexpected suspendStatus found out!\n";
217: }
218: if (resumeThreadFailed) {
219: errorMessage = errorMessage
220: + "## Error found out while resuming thread!\n";
221: }
222:
223: boolean testedThreadNotFound = false;
224: for (int i = 0; i < testedThreadsNumber; i++) {
225: if (testedThreadsIDs[i] == 0) {
226: logWriter
227: .println("## FAILURE: Tested thread is not found out among debuggee threads!");
228: logWriter.println("## Thread name = "
229: + testedThreadsNames[i]);
230: testedThreadNotFound = true;
231: }
232: }
233:
234: if (testedThreadNotFound) {
235: errorMessage = errorMessage
236: + "## Some of tested threads are not found!\n";
237: }
238:
239: if (!errorMessage.equals("")) {
240: printErrorAndFail("\ntestSuspend001 FAILED:\n"
241: + errorMessage);
242: }
243: logWriter.println("\n==> testSuspend001 - OK!");
244: }
245:
246: public static void main(String[] args) {
247: junit.textui.TestRunner.run(SuspendTest.class);
248: }
249: }
|