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 Anatoly F. Bondarenko
021: * @version $Revision: 1.3 $
022: */
023:
024: /**
025: * Created on 19.06.2006
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.ThreadGroup command.
038: */
039: public class ThreadGroup002Test 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/ThreadGroup002Debuggee;";
044:
045: protected String getDebuggeeClassName() {
046: return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.ThreadGroup002Debuggee";
047: }
048:
049: /**
050: * This testcase exercises ThreadReference.ThreadGroup command.
051: * <BR>At first the test starts ThreadGroup002Debuggee which creates some thread
052: * groups and starts some tested threads which belong to different created thread groups.
053: * <BR>After the tested threads start, at first the test wait for the some first
054: * tested threads to finish.
055: * <BR> Then the test for every tested thread does:
056: * <BR> - performs ThreadReference.Status command;
057: * <BR> - performs the ThreadReference.ThreadGroup command;
058: * <BR> - performs the ThreadGroupReference.name command;
059: * <BR>It is expected that
060: * <BR> - all threads with status ZOMBIE are only finished tested threads;
061: * <BR> - all threads without status ZOMBIE are only NOT finished tested threads;
062: * <BR> - if status of thread is ZOMBIE then returned groupID must be null;
063: * <BR> - if status of thread is not ZOMBIE then returned groupID must not be null;
064: * <BR> - thread group name should be expected name for thread which is not ZOMBIE;
065: */
066: public void testThreadGroup002() {
067: logWriter.println("==> testThreadGroup002: START...");
068: String debuggeeMessage = synchronizer.receiveMessage();
069: int testedThreadsNumber = 0;
070: try {
071: testedThreadsNumber = Integer.valueOf(debuggeeMessage)
072: .intValue();
073: } catch (NumberFormatException exception) {
074: logWriter
075: .println("## FAILURE: Exception while getting number of started threads from debuggee = "
076: + exception);
077: synchronizer.sendMessage("FINISH");
078: printErrorAndFail("\n## Can NOT get number of started threads from debuggee! ");
079: }
080: testedThreadsNumber++; // to add debuggee main thread
081: logWriter
082: .println("==> Number of threads in debuggee to test = "
083: + testedThreadsNumber);
084: String[] testedThreadsNames = new String[testedThreadsNumber];
085: String[] testedThreadGroupsNames = new String[testedThreadsNumber];
086: long[] testedThreadsIDs = new long[testedThreadsNumber];
087: String debuggeeMainThreadName = synchronizer.receiveMessage();
088: String debuggeeMainThreadGroupName = synchronizer
089: .receiveMessage();
090: for (int i = 0; i < testedThreadsNumber; i++) {
091: if (i < (testedThreadsNumber - 1)) {
092: testedThreadsNames[i] = ThreadGroup002Debuggee.THREAD_NAME_PATTERN
093: + i;
094: testedThreadGroupsNames[i] = ThreadGroup002Debuggee.THREAD_GROUP_NAME_PATTERN
095: + (i % 2);
096: } else {
097: testedThreadsNames[i] = debuggeeMainThreadName;
098: testedThreadGroupsNames[i] = debuggeeMainThreadGroupName;
099: }
100: testedThreadsIDs[i] = 0;
101: }
102:
103: // getting ID of the tested thread
104: ReplyPacket allThreadIDReply = null;
105: try {
106: allThreadIDReply = debuggeeWrapper.vmMirror
107: .getAllThreadID();
108: } catch (ReplyErrorCodeException exception) {
109: logWriter
110: .println("## FAILURE: Exception in vmMirror.getAllThreadID() = "
111: + exception);
112: synchronizer.sendMessage("FINISH");
113: printErrorAndFail("\n## Can NOT get all ThreadID in debuggee! ");
114: }
115: int threads = allThreadIDReply.getNextValueAsInt();
116: logWriter.println("==> Number of all threads in debuggee = "
117: + threads);
118: for (int i = 0; i < threads; i++) {
119: long threadID = allThreadIDReply.getNextValueAsThreadID();
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: int k = 0;
131: for (; k < testedThreadsNumber; k++) {
132: if (threadName.equals(testedThreadsNames[k])) {
133: testedThreadsIDs[k] = threadID;
134: break;
135: }
136: }
137: }
138:
139: boolean testedThreadNotFound = false;
140: for (int i = 0; i < testedThreadsNumber; i++) {
141: if (testedThreadsIDs[i] == 0) {
142: logWriter
143: .println("## FAILURE: Tested thread is not found out among debuggee threads!");
144: logWriter.println("## Thread name = "
145: + testedThreadsNames[i]);
146: testedThreadNotFound = true;
147: }
148: }
149: if (testedThreadNotFound) {
150: synchronizer.sendMessage("FINISH");
151: printErrorAndFail("\n## Some of tested threads are not found!");
152: }
153:
154: logWriter
155: .println("==> Send signal to debuggee to continue and to finish some first threads...");
156: synchronizer
157: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
158:
159: logWriter
160: .println("==> Wait signal from the debuggee that some first threads finished...");
161: String messageFromDebuggee = synchronizer
162: .receiveMessageWithoutException("testThreadGroup002");
163: if (!JPDADebuggeeSynchronizer.SGNL_READY
164: .equals(messageFromDebuggee)) {
165: logWriter
166: .println("## FAILURE: Could NOT receive expected signal from debuggee!");
167: printErrorAndFail("\n## Could NOT receive expected signal from debuggee! ");
168: }
169: int finishedTestedThreadsNumber = testedThreadsNumber / 2;
170: logWriter
171: .println("==> Number of debuggee's finished threads = "
172: + finishedTestedThreadsNumber);
173:
174: CommandPacket packet = null;
175: ReplyPacket reply = null;
176: String errorMessage = "";
177:
178: boolean statusCommandFailed = false;
179: boolean threadStatusFailed = false;
180: boolean groupIDFailed = false;
181: boolean threadGroupCommandFailed = false;
182: boolean groupNameFailed = false;
183:
184: logWriter
185: .println("\n==> Check that ThreadReference.ThreadGroup command returns expected thread group for each tsted thread...");
186:
187: for (int threadCount = 0; threadCount < testedThreadsNumber; threadCount++) {
188: logWriter.println("\n==> Check for Thread: threadID = "
189: + testedThreadsIDs[threadCount] + "; threadName = "
190: + testedThreadsNames[threadCount]);
191: logWriter
192: .println("==> Send ThreadReference.Status command...");
193: packet = new CommandPacket(
194: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
195: JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
196: packet
197: .setNextValueAsReferenceTypeID(testedThreadsIDs[threadCount]);
198: reply = debuggeeWrapper.vmMirror.performCommand(packet);
199: if (!checkReplyPacketWithoutFail(reply,
200: "ThreadReference.Status command")) {
201: statusCommandFailed = true;
202: continue;
203: }
204:
205: int threadStatus = reply.getNextValueAsInt();
206: //int suspendStatus =
207: reply.getNextValueAsInt();
208:
209: logWriter.println("==> thread status of checked thread = "
210: + threadStatus + "("
211: + JDWPConstants.ThreadStatus.getName(threadStatus)
212: + ")");
213:
214: logWriter
215: .println("==> Send ThreadReference.ThreadGroup command...");
216: packet = new CommandPacket(
217: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
218: JDWPCommands.ThreadReferenceCommandSet.ThreadGroupCommand);
219: packet
220: .setNextValueAsThreadID(testedThreadsIDs[threadCount]);
221:
222: reply = debuggeeWrapper.vmMirror.performCommand(packet);
223: if (!checkReplyPacketWithoutFail(reply,
224: "ThreadReference.ThreadGroup command")) {
225: threadGroupCommandFailed = true;
226: continue;
227: }
228:
229: long threadGroupID = reply.getNextValueAsThreadGroupID();
230: logWriter
231: .println("==> thread groupID for checked thread = "
232: + threadGroupID);
233: if (threadStatus == JDWPConstants.ThreadStatus.ZOMBIE) {
234: if (threadCount >= finishedTestedThreadsNumber) {
235: logWriter
236: .println("## FAILURE: Unexpected status for checked thread!");
237: logWriter
238: .println("## Thread witn number = "
239: + threadCount
240: + " should NOT be ZOMBIE!");
241: threadStatusFailed = true;
242: continue;
243: }
244: // according to JVMTI spec groupID is NULL if the thread has died
245: if (threadGroupID != 0) {
246: logWriter
247: .println("## FAILURE: Unexpected thread groupID for checked thread with status = ZOMBIE!");
248: logWriter
249: .println("## Expected thread groupID = 0");
250: groupIDFailed = true;
251: }
252: continue;
253: } else {
254: if (threadCount < finishedTestedThreadsNumber) {
255: logWriter
256: .println("## FAILURE: Unexpected status for checked thread!");
257: logWriter
258: .println("## Thread witn number = "
259: + threadCount
260: + " should be ZOMBIE!");
261: threadStatusFailed = true;
262: continue;
263: }
264: if (threadGroupID == 0) {
265: logWriter
266: .println("## FAILURE: Unexpected thread groupID for checked thread with status != ZOMBIE!");
267: logWriter
268: .println("## Expected thread groupID != 0");
269: groupIDFailed = true;
270: continue;
271: }
272: }
273:
274: logWriter
275: .println("==> Getting thread group name by ThreadGroupReference.Name command...");
276: packet = new CommandPacket(
277: JDWPCommands.ThreadGroupReferenceCommandSet.CommandSetID,
278: JDWPCommands.ThreadGroupReferenceCommandSet.NameCommand);
279: packet.setNextValueAsThreadID(threadGroupID);
280:
281: reply = debuggeeWrapper.vmMirror.performCommand(packet);
282: if (!checkReplyPacketWithoutFail(reply,
283: "ThreadReference.ThreadGroup command")) {
284: threadGroupCommandFailed = true;
285: continue;
286: }
287:
288: String threadGroupName = reply.getNextValueAsString();
289: logWriter
290: .println("==> thread group name for checked thread = '"
291: + threadGroupName + "'");
292:
293: if (!testedThreadGroupsNames[threadCount]
294: .equals(threadGroupName)) {
295: logWriter
296: .println("## FAILURE: Unexpected thread group name for checked thread!");
297: logWriter
298: .println("## Expected thread group name = '"
299: + testedThreadGroupsNames[threadCount]
300: + "'");
301: groupNameFailed = true;
302: }
303: }
304:
305: if (statusCommandFailed) {
306: errorMessage = errorMessage
307: + "## Error found out while ThreadReference.Status command performing!\n";
308: }
309:
310: if (threadStatusFailed) {
311: errorMessage = errorMessage
312: + "## Unexpected thread status found out for some tested threads!\n";
313: }
314:
315: if (groupIDFailed) {
316: errorMessage = errorMessage
317: + "## Unexpected thread groupID found out for some tested threads!\n";
318: }
319:
320: if (threadGroupCommandFailed) {
321: errorMessage = errorMessage
322: + "## Error found out while ThreadReference.ThreadGroup command performing!\n";
323: }
324:
325: if (groupNameFailed) {
326: errorMessage = errorMessage
327: + "## Unexpected thread group name found out for some tested threads!\n";
328: }
329:
330: logWriter.println("==> Send signal to debuggee to finish...");
331: synchronizer
332: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
333:
334: if (!errorMessage.equals("")) {
335: printErrorAndFail("\ntestThreadGroup002 FAILED:\n"
336: + errorMessage);
337: }
338:
339: logWriter.println("\n==> testThreadGroup002 - OK!");
340: }
341:
342: public static void main(String[] args) {
343: junit.textui.TestRunner.run(ThreadGroup002Test.class);
344: }
345: }
|