1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *
15 *  See the License for the specific language governing permissions and
16 *  limitations under the License.
17 */
18
19/**
20 * @author Vitaly A. Provodin
21 */
22
23/**
24 * Created on 15.02.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
27
28import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
29import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
30import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
31import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
32import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
33
34
35/**
36 * JDWP Unit test for ThreadReference.ThreadGroup command.
37 */
38public class ThreadGroupTest extends JDWPSyncTestCase {
39
40    protected String getDebuggeeClassName() {
41        return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.ThreadGroupDebuggee";
42    }
43
44    /**
45     * This testcase exercises ThreadReference.ThreadGroup command.
46     * <BR>At first the test starts ThreadGroupDebuggee which creates
47     * 'TESTED_GROUP' ThreadGroup and starts the 'TESTED_THREAD' thread which
48     * belongs to that thread group.
49     * <BR>After the tested thread starts but does not finish, test requests all
50     * debuggee threads by VirtualMachine.AllThreads command and looks for tested thread.
51     * <BR>If the tested thread is not found the test fails.
52     * <BR>If the tested thread is found the test checks that
53     * ThreadReference.ThreadGroup command for tested thread returns 'TESTED_GROUP' thread group.
54     */
55    public void testThreadGroup001() {
56        logWriter.println("wait for SGNL_READY");
57        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
58
59        // getting ID of the tested thread
60        CommandPacket packet;
61        logWriter.println("get all threads");
62        ReplyPacket replyThread, reply = debuggeeWrapper.vmMirror.getAllThreadID();
63        int threads = reply.getNextValueAsInt();
64        logWriter.println("exercise threads = " + threads);
65
66        long threadID, groupID;
67        String groupName, threadName;
68        int count = 0;
69
70        for (int i = 0; i < threads; i++) {
71            threadID = reply.getNextValueAsThreadID();
72
73            // getting the thread group ID
74            packet = new CommandPacket(
75                    JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
76                    JDWPCommands.ThreadReferenceCommandSet.ThreadGroupCommand);
77            packet.setNextValueAsThreadID(threadID);
78
79            replyThread = debuggeeWrapper.vmMirror.performCommand(packet);
80            checkReplyPacket(replyThread, "ThreadReference::ThreadGroup command");
81
82            groupID = replyThread.getNextValueAsThreadGroupID();
83
84            groupName = debuggeeWrapper.vmMirror.getThreadGroupName(groupID);
85            threadName = debuggeeWrapper.vmMirror.getThreadName(threadID);
86
87            logWriter.println("\tthreadID=" + threadID
88                    + "; threadName=" + threadName
89                    + "; groupID=" + groupID
90                    + "; groupName=" + groupName);
91
92            if (threadName.equals(ThreadGroupDebuggee.TESTED_THREAD)) {
93                if (!groupName.equals(ThreadGroupDebuggee.TESTED_GROUP)) {
94                    printErrorAndFail("unexpected group name, it is expected: "
95                            + ThreadGroupDebuggee.TESTED_GROUP);
96                }
97                count++;
98            }
99        }
100        if (count == 0) {
101            printErrorAndFail("Tested thread is not found in all_threads list.");
102        }
103
104        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
105        logWriter.println("waiting for finishing thread");
106        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
107        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
108    }
109}
110