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, Anatoly F. Bondarenko
021: * @version $Revision: 1.6 $
022: */
023:
024: /**
025: * Created on 10.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.framework.jdwp.exceptions.ReplyErrorCodeException;
033: import org.apache.harmony.jpda.tests.jdwp.ThreadReference.ResumeDebuggee;
034: import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
035:
036: /**
037: * JDWP Unit test for VirtualMachine.Resume command.
038: */
039: public class ResumeTest extends JDWPSyncTestCase {
040:
041: static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/VirtualMachine/ResumeDebuggee;";
042:
043: protected String getDebuggeeClassName() {
044: return "org.apache.harmony.jpda.tests.jdwp.VirtualMachine.ResumeDebuggee";
045: }
046:
047: /**
048: * This testcase exercises VirtualMachine.Resume command.
049: * <BR>At first the test starts ResumeDebuggee which starts and runs some tested threads.
050: * <BR> Then the test performs VirtualMachine.Suspend command and checks with help of
051: * ThreadReference.Status command that all debuggee tested threads are suspended.
052: * <BR> Then the test performs VirtualMachine.Resume command and checks with help of
053: * ThreadReference.Status command that all debuggee tested threads are resumed.
054: */
055: public void testResume001() {
056: logWriter.println("==> testResume001: START...");
057: String debuggeeMessage = synchronizer.receiveMessage();
058: int testedThreadsNumber = 0;
059: try {
060: testedThreadsNumber = Integer.valueOf(debuggeeMessage)
061: .intValue();
062: } catch (NumberFormatException exception) {
063: logWriter
064: .println("## FAILURE: Exception while getting number of started threads from debuggee = "
065: + exception);
066: setStaticIntField(debuggeeSignature,
067: ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
068: printErrorAndFail("\n## Can NOT get number of started threads from debuggee! ");
069: }
070: testedThreadsNumber++; // to add debuggee main thread
071: logWriter
072: .println("==> Number of threads in debuggee to test = "
073: + testedThreadsNumber);
074: String[] testedThreadsNames = new String[testedThreadsNumber];
075: long[] testedThreadsIDs = new long[testedThreadsNumber];
076: String debuggeeMainThreadName = synchronizer.receiveMessage();
077: for (int i = 0; i < testedThreadsNumber; i++) {
078: if (i < (testedThreadsNumber - 1)) {
079: testedThreadsNames[i] = ResumeDebuggee.THREAD_NAME_PATTERN
080: + i;
081: } else {
082: testedThreadsNames[i] = debuggeeMainThreadName;
083: }
084: testedThreadsIDs[i] = 0;
085: }
086:
087: // getting ID of the tested thread
088: ReplyPacket allThreadIDReply = null;
089: try {
090: allThreadIDReply = debuggeeWrapper.vmMirror
091: .getAllThreadID();
092: } catch (ReplyErrorCodeException exception) {
093: logWriter
094: .println("## FAILURE: Exception in vmMirror.getAllThreadID() = "
095: + exception);
096: setStaticIntField(debuggeeSignature,
097: ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
098: printErrorAndFail("\n## Can NOT get all ThreadID in debuggee! ");
099: }
100: int threads = allThreadIDReply.getNextValueAsInt();
101: logWriter.println("==> Number of all threads in debuggee = "
102: + threads);
103: for (int i = 0; i < threads; i++) {
104: long threadID = allThreadIDReply.getNextValueAsThreadID();
105: String threadName = null;
106: try {
107: threadName = debuggeeWrapper.vmMirror
108: .getThreadName(threadID);
109: } catch (ReplyErrorCodeException exception) {
110: logWriter
111: .println("==> WARNING: Can NOT get thread name for threadID = "
112: + threadID);
113: continue;
114: }
115: int k = 0;
116: for (; k < testedThreadsNumber; k++) {
117: if (threadName.equals(testedThreadsNames[k])) {
118: testedThreadsIDs[k] = threadID;
119: break;
120: }
121: }
122: }
123:
124: boolean testedThreadNotFound = false;
125: for (int i = 0; i < testedThreadsNumber; i++) {
126: if (testedThreadsIDs[i] == 0) {
127: logWriter
128: .println("## FAILURE: Tested thread is not found out among debuggee threads!");
129: logWriter.println("## Thread name = "
130: + testedThreadsNames[i]);
131: testedThreadNotFound = true;
132: }
133: }
134: if (testedThreadNotFound) {
135: setStaticIntField(debuggeeSignature,
136: ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
137: printErrorAndFail("\n## Some of tested threads are not found!");
138: }
139:
140: logWriter
141: .println("\n==> Send VirtualMachine.Suspend command...");
142: CommandPacket packet = new CommandPacket(
143: JDWPCommands.VirtualMachineCommandSet.CommandSetID,
144: JDWPCommands.VirtualMachineCommandSet.SuspendCommand);
145: ReplyPacket reply = debuggeeWrapper.vmMirror
146: .performCommand(packet);
147: int errorCode = reply.getErrorCode();
148: if (errorCode != JDWPConstants.Error.NONE) {
149: setStaticIntField(debuggeeSignature,
150: ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
151: logWriter
152: .println("## FAILURE: VirtualMachine.Suspend command returns error = "
153: + errorCode
154: + "("
155: + JDWPConstants.Error.getName(errorCode)
156: + ")");
157: printErrorAndFail("\nVirtualMachine.Suspend command FAILED!");
158: } else {
159: logWriter
160: .println("==> VirtualMachine.Suspend command - OK.");
161: }
162:
163: logWriter
164: .println("\n==> Check that all tested threads are suspended after VirtualMachine.Suspend command...");
165:
166: boolean statusCommandFailed = false;
167: boolean suspendStatusFailed = false;
168: for (int i = 0; i < testedThreadsNumber; i++) {
169: logWriter.println("\n==> Check for Thread: threadID = "
170: + testedThreadsIDs[i] + "; threadName = "
171: + testedThreadsNames[i]);
172:
173: logWriter
174: .println("==> Send ThreadReference.Status command...");
175: packet = new CommandPacket(
176: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
177: JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
178: packet.setNextValueAsReferenceTypeID(testedThreadsIDs[i]);
179: reply = debuggeeWrapper.vmMirror.performCommand(packet);
180: if (!checkReplyPacketWithoutFail(reply,
181: "ThreadReference.Status command")) {
182: statusCommandFailed = true;
183: continue;
184: }
185:
186: int threadStatus = reply.getNextValueAsInt();
187: int suspendStatus = reply.getNextValueAsInt();
188:
189: logWriter.println("==> threadStatus = " + threadStatus
190: + "("
191: + JDWPConstants.ThreadStatus.getName(threadStatus)
192: + ")");
193: logWriter.println("==> suspendStatus = "
194: + suspendStatus
195: + "("
196: + JDWPConstants.SuspendStatus
197: .getName(suspendStatus) + ")");
198: if (suspendStatus != JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {
199: logWriter
200: .println("## FAILURE: Unexpected suspendStatus for checked thread!");
201: logWriter
202: .println("## Expected suspendStatus = "
203: + JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED
204: + "("
205: + JDWPConstants.SuspendStatus
206: .getName(JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED)
207: + ")");
208: suspendStatusFailed = true;
209: continue;
210: }
211: }
212:
213: String errorMessage = "";
214: if (statusCommandFailed) {
215: errorMessage = errorMessage
216: + "## Error found out while ThreadReference.Status command performing!\n";
217: }
218: if (suspendStatusFailed) {
219: errorMessage = errorMessage
220: + "## Unexpected suspendStatus found out!\n";
221: }
222:
223: logWriter
224: .println("\n==> Send VirtualMachine.Resume command...");
225: packet = new CommandPacket(
226: JDWPCommands.VirtualMachineCommandSet.CommandSetID,
227: JDWPCommands.VirtualMachineCommandSet.ResumeCommand);
228: reply = debuggeeWrapper.vmMirror.performCommand(packet);
229: errorCode = reply.getErrorCode();
230: if (errorCode != JDWPConstants.Error.NONE) {
231: setStaticIntField(debuggeeSignature,
232: ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
233: logWriter
234: .println("## FAILURE: VirtualMachine.Resume command returns error = "
235: + errorCode
236: + "("
237: + JDWPConstants.Error.getName(errorCode)
238: + ")");
239: printErrorAndFail("\nVirtualMachine.Resume command FAILED!");
240: } else {
241: logWriter
242: .println("==> VirtualMachine.Resume command - OK.");
243: }
244:
245: if (!errorMessage.equals("")) {
246: setStaticIntField(debuggeeSignature,
247: ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
248: printErrorAndFail("\ntestResume001 FAILED:\n"
249: + errorMessage);
250: }
251:
252: logWriter
253: .println("\n==> Check that all tested threads are resumed after VirtualMachine.Resume command...");
254:
255: for (int i = 0; i < testedThreadsNumber; i++) {
256: logWriter.println("\n==> Check for Thread: threadID = "
257: + testedThreadsIDs[i] + "; threadName = "
258: + testedThreadsNames[i]);
259:
260: logWriter
261: .println("==> Send ThreadReference.Status command...");
262: packet = new CommandPacket(
263: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
264: JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
265: packet.setNextValueAsReferenceTypeID(testedThreadsIDs[i]);
266: reply = debuggeeWrapper.vmMirror.performCommand(packet);
267: if (!checkReplyPacketWithoutFail(reply,
268: "ThreadReference.Status command")) {
269: statusCommandFailed = true;
270: continue;
271: }
272:
273: int threadStatus = reply.getNextValueAsInt();
274: int suspendStatus = reply.getNextValueAsInt();
275:
276: logWriter.println("==> threadStatus = " + threadStatus
277: + "("
278: + JDWPConstants.ThreadStatus.getName(threadStatus)
279: + ")");
280: logWriter.println("==> suspendStatus = "
281: + suspendStatus
282: + "("
283: + JDWPConstants.SuspendStatus
284: .getName(suspendStatus) + ")");
285: if (suspendStatus == JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {
286: logWriter
287: .println("## FAILURE: Thread still is suspended after VirtualMachine.Resume commands!");
288: suspendStatusFailed = true;
289: }
290: }
291:
292: if (statusCommandFailed) {
293: errorMessage = errorMessage
294: + "## Error found out while ThreadReference.Status command performing!\n";
295: }
296: if (suspendStatusFailed) {
297: errorMessage = errorMessage
298: + "## Unexpected suspendStatus found out!\n";
299: }
300:
301: setStaticIntField(debuggeeSignature,
302: ResumeDebuggee.TO_FINISH_DEBUGGEE_FIELD_NAME, 99);
303: if (!errorMessage.equals("")) {
304: printErrorAndFail("\ntestResume001 FAILED:\n"
305: + errorMessage);
306: }
307:
308: logWriter.println("\n==> testResume001 - OK!");
309: }
310:
311: public static void main(String[] args) {
312: junit.textui.TestRunner.run(ResumeTest.class);
313: }
314: }
|