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 09.02.2005
25  */
26 package org.apache.harmony.jpda.tests.jdwp.VirtualMachine;
27 
28 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
29 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
30 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
31 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
32 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
33 
34 
35 /**
36  * JDWP Unit test for VirtualMachine.TopLevelThreadGroups command.
37  */
38 public class TopLevelThreadGroupsTest extends JDWPSyncTestCase {
39 
getDebuggeeClassName()40     protected String getDebuggeeClassName() {
41         return "org.apache.harmony.jpda.tests.jdwp.share.debuggee.HelloWorld";
42     }
43 
44     /**
45      * This testcase exercises VirtualMachine.TopLevelThreadGroups command.
46      * <BR>At first the test starts HelloWorld debuggee.
47      * <BR> Then the test performs VirtualMachine.TopLevelThreadGroups command
48      * and checks that:
49      * <BR>&nbsp;&nbsp; - number of returned thread groups is equal to 1;
50      * <BR>&nbsp;&nbsp; - there are no extra data in the reply packet;
51      * <BR>Also the test prints information about returned thread groups.
52      */
testTopLevelThreadGroups001()53     public void testTopLevelThreadGroups001() {
54         logWriter.println("\n==> testTopLevelThreadGroups001: START...");
55         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
56 
57         logWriter.println("==> Send VirtualMachine::TopLevelThreadGroups command...");
58         CommandPacket packet = new CommandPacket(
59                 JDWPCommands.VirtualMachineCommandSet.CommandSetID,
60                 JDWPCommands.VirtualMachineCommandSet.TopLevelThreadGroupsCommand);
61         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
62         checkReplyPacket(reply, "VirtualMachine::TopLevelThreadGroups command");
63 
64         int groups = reply.getNextValueAsInt();
65         logWriter.println("==> Returned number of groups = " + groups);
66         assertEquals(1, groups);
67 
68         for (int i = 0; i < groups; i++) {
69 
70             long threadGroupID = reply.getNextValueAsThreadGroupID() ;
71             logWriter.println("\n==> Print info about ThreadGroup[" + i + "]... ");
72             printThreadGroup(threadGroupID);
73 
74         }
75         assertAllDataRead(reply);
76         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
77     }
78 
printThreadGroup(long rootID)79     private void printThreadGroup(long rootID) {
80         logWriter.println("==> ThreadGroupID = " + rootID);
81 
82         CommandPacket packet = new CommandPacket(
83                 JDWPCommands.ThreadGroupReferenceCommandSet.CommandSetID,
84                 JDWPCommands.ThreadGroupReferenceCommandSet.NameCommand);
85         packet.setNextValueAsThreadGroupID(rootID);
86         ReplyPacket replyParent = debuggeeWrapper.vmMirror.performCommand(packet);
87         checkReplyPacket(replyParent, "ThreadGroupReference::Name command");
88 
89         String threadGroupIDName = replyParent.getNextValueAsString();
90         logWriter.println("==> threadGroupIDName = |" + threadGroupIDName +"|");
91 
92         logWriter.println("==> Send ThreadGroupReference::Children command...");
93         packet = new CommandPacket(
94                 JDWPCommands.ThreadGroupReferenceCommandSet.CommandSetID,
95                 JDWPCommands.ThreadGroupReferenceCommandSet.ChildrenCommand);
96         packet.setNextValueAsThreadGroupID(rootID);
97         ReplyPacket replyChilds = debuggeeWrapper.vmMirror.performCommand(packet);
98         checkReplyPacket(replyChilds, "ThreadGroupReference::Children command");
99 
100         int childThreads = replyChilds.getNextValueAsInt();
101         logWriter.println("==> Returned child threads: " + childThreads);
102 
103         for (int j = 0; j < childThreads; j++) {
104             long id = replyChilds.getNextValueAsThreadID();
105             logWriter.println("\n==> childThreadID[" + j + "] = " + id);
106 
107             packet = new CommandPacket(
108                     JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
109                     JDWPCommands.ThreadReferenceCommandSet.NameCommand);
110             packet.setNextValueAsThreadID(id);
111             replyParent = debuggeeWrapper.vmMirror.performCommand(packet);
112             checkReplyPacket(replyParent, "ThreadReference::Name command");
113 
114             String name = replyParent.getNextValueAsString();
115             logWriter.println("==> childThreadName[" + j + "] = " + name);
116         }
117 
118         int childGroups = replyChilds.getNextValueAsInt();
119         logWriter.println("\n==> Returned child groups: " + childGroups);
120 
121         for (int j = 0; j < childGroups; j++) {
122             long id = replyChilds.getNextValueAsThreadGroupID();
123             logWriter.println("\n==> childGroupID[" + j + "] = " + id);
124 
125             packet = new CommandPacket(
126                     JDWPCommands.ThreadGroupReferenceCommandSet.CommandSetID,
127                     JDWPCommands.ThreadGroupReferenceCommandSet.NameCommand);
128             packet.setNextValueAsThreadGroupID(id);
129             replyParent = debuggeeWrapper.vmMirror.performCommand(packet);
130             checkReplyPacket(replyParent, "ThreadGroupReference::Name command");
131 
132             String name = replyParent.getNextValueAsString();
133             logWriter.println("==> childGroupName[" + j + "] = " + name);
134 
135             logWriter.println("\n==> Print info about child ThreadGroup \"main\"... ");
136             if ("main".equals(name)) {
137                 printThreadGroup(id);
138             }
139         }
140     }
141 }
142