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 25.02.2005
25  */
26 package org.apache.harmony.jpda.tests.jdwp.ThreadGroupReference;
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.JDWPConstants;
31 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
32 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
33 import org.apache.harmony.jpda.tests.jdwp.share.JDWPTestConstants;
34 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
35 
36 
37 /**
38  * JDWP Unit test for ThreadGroupReference.Children command.
39  */
40 public class ChildrenTest extends JDWPSyncTestCase {
41 
42 
getDebuggeeClassName()43     protected String getDebuggeeClassName() {
44         return "org.apache.harmony.jpda.tests.jdwp.ThreadGroupReference.ChildrenDebuggee";
45     }
46 
47     /**
48      * This testcase exercises ThreadGroupReference.Children command.
49      * <BR>At first the test starts ChildrenDebuggee.
50      * <BR> Then the test with help of the ThreadGroupReference.Children command checks
51      * that the group 'PARENT_GROUP' has one child thread - 'TESTED_THREAD'
52      * and one child group - 'CHILD_GROUP'.
53      *
54      */
testChildren001()55     public void testChildren001() {
56         logWriter.println("==> ChildrenTest.testChildren001 START...");
57         logWriter.println("==> Wait for SGNL_READY...");
58         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
59 
60         // getting ID of the tested thread
61         logWriter.println("==> Get testedThreadID...");
62         CommandPacket packet;
63         long threadID = debuggeeWrapper.vmMirror.getThreadID(NameDebuggee.TESTED_THREAD);
64         logWriter.println("==> testedThreadID = " + threadID);
65 
66         long groupID;
67         String groupName;
68 
69         // getting the thread group ID
70         logWriter.println("==> Send ThreadReference.ThreadGroup command for testedThreadID...");
71         packet = new CommandPacket(
72                 JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
73                 JDWPCommands.ThreadReferenceCommandSet.ThreadGroupCommand);
74         packet.setNextValueAsThreadID(threadID);
75         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
76         checkReplyPacket(reply, "ThreadReference.ThreadGroup command");
77 
78         groupID = reply.getNextValueAsThreadGroupID();
79         logWriter.println("==> groupID = " + groupID);
80 
81         logWriter.println("==> Send ThreadGroupReference.Children command...");
82         packet = new CommandPacket(
83                 JDWPCommands.ThreadGroupReferenceCommandSet.CommandSetID,
84                 JDWPCommands.ThreadGroupReferenceCommandSet.ChildrenCommand);
85         packet.setNextValueAsThreadID(groupID);
86         reply = debuggeeWrapper.vmMirror.performCommand(packet);
87         checkReplyPacket(reply, "ThreadGroupReference.Children command");
88 
89         logWriter.println("\n==> Children of the group: \""
90                 + debuggeeWrapper.vmMirror.getThreadGroupName(groupID)
91                 + "\": ");
92 
93         int childThreads = reply.getNextValueAsInt();
94         if (childThreads != 1) {
95             logWriter.println("## FAILURE: Unexpected number of child threads = " + childThreads);
96             logWriter.println("## Expected number of child threads = 1");
97             assertEquals("Invalid number of child threads,", 1, childThreads);
98         }
99         long childThreadID = reply.getNextValueAsThreadID();
100         String threadName = debuggeeWrapper.vmMirror.getThreadName(childThreadID);
101         logWriter.println
102         ("==> thread: threadID = " + childThreadID + "; threadName = " + threadName);
103 
104         if (threadID != childThreadID) {
105             logWriter.println("## FAILURE: Unexpected ID of child thread = " + childThreadID);
106             logWriter.println("## Expected ID of child thread = " + threadID);
107             assertEquals("Invalid ID of child thread,", threadID, childThreadID);
108         }
109         if (!threadName.equals(NameDebuggee.TESTED_THREAD)) {
110             logWriter.println("## FAILURE: unexpected thread name, it is expected: "
111                     + NameDebuggee.TESTED_THREAD);
112             assertString("Invalid thread name,", NameDebuggee.TESTED_THREAD, threadName);
113         }
114 
115         int childGroups = reply.getNextValueAsInt();
116         if (childGroups != 1) {
117             logWriter.println("## FAILURE: Unexpected number of child groups " + childGroups);
118             logWriter.println("## Expected number = 1");
119             assertEquals("Invalid number of child groups,", 1, childGroups);
120         }
121 
122         groupID = reply.getNextValueAsThreadGroupID();
123         groupName = debuggeeWrapper.vmMirror.getThreadGroupName(groupID);
124 
125         logWriter.println("\n==> group: groupID = " + groupID + "; groupName = " + groupName);
126 
127         assertString("Invalid group name,", NameDebuggee.CHILD_GROUP, groupName);
128 
129         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
130     }
131 
132     /**
133      * This testcase exercises ThreadGroupReference.Children command.
134      * <BR>At first the test starts NameDebuggee.
135      * <BR> Then the test with help of the ThreadGroupReference.Children command
136      * checks that INVALID_OBJECT error is returned for the null object id.
137      *
138      */
testChildren_NullObject()139     public void testChildren_NullObject() {
140         logWriter.println("wait for SGNL_READY");
141         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
142 
143         checkCommandError(JDWPTestConstants.NULL_OBJECT_ID,
144                           JDWPConstants.Error.INVALID_OBJECT);
145 
146         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
147     }
148 
149     /**
150      * This testcase exercises ThreadGroupReference.Children command.
151      * <BR>At first the test starts NameDebuggee.
152      * <BR> Then the test with help of the ThreadGroupReference.Children command
153      * checks that INVALID_OBJECT error is returned for an invalid object id.
154      *
155      */
testChildren_InvalidObject()156     public void testChildren_InvalidObject() {
157         logWriter.println("wait for SGNL_READY");
158         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
159 
160         checkCommandError(JDWPTestConstants.INVALID_OBJECT_ID,
161                           JDWPConstants.Error.INVALID_OBJECT);
162 
163         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
164     }
165 
166     /**
167      * This testcase exercises ThreadGroupReference.Children command.
168      * <BR>At first the test starts NameDebuggee.
169      * <BR> Then the test with help of the ThreadGroupReference.Children command
170      * checks that INVALID_THREAD_GROUP error is returned for an object that is
171      * not a java.lang.ThreadGroup.
172      *
173      */
testChildren_InvalidThreadGroup()174     public void testChildren_InvalidThreadGroup() {
175         logWriter.println("wait for SGNL_READY");
176         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
177 
178         long threadID = debuggeeWrapper.vmMirror.getThreadID(NameDebuggee.TESTED_THREAD);
179 
180         checkCommandError(threadID, JDWPConstants.Error.INVALID_THREAD_GROUP);
181 
182         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
183     }
184 
checkCommandError(long groupID, int expectedError)185     private void checkCommandError(long groupID, int expectedError) {
186         logWriter.println("Send ThreadGroupReference.Children command with id " + groupID);
187 
188         CommandPacket packet = new CommandPacket(
189                 JDWPCommands.ThreadGroupReferenceCommandSet.CommandSetID,
190                 JDWPCommands.ThreadGroupReferenceCommandSet.ChildrenCommand);
191         packet.setNextValueAsThreadGroupID(groupID);
192         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
193 
194         checkReplyPacket(reply, "ThreadGroupReference::Name command",
195                          expectedError);
196     }
197 
198 }
199