15f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes/*
25f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes * Licensed to the Apache Software Foundation (ASF) under one or more
35f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes * contributor license agreements.  See the NOTICE file distributed with
45f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes * this work for additional information regarding copyright ownership.
55f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes * The ASF licenses this file to You under the Apache License, Version 2.0
65f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes * (the "License"); you may not use this file except in compliance with
75f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes * the License.  You may obtain a copy of the License at
85f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes *
95f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes *     http://www.apache.org/licenses/LICENSE-2.0
105f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes *
115f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes *  Unless required by applicable law or agreed to in writing, software
125f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes *  distributed under the License is distributed on an "AS IS" BASIS,
135f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
145f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes *
155f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes *  See the License for the specific language governing permissions and
165f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes *  limitations under the License.
175f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes */
185f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
195f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes/**
205f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes * @author Vitaly A. Provodin
215f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes */
225f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
235f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes/**
245f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes * Created on 25.02.2005
255f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes */
265f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughespackage org.apache.harmony.jpda.tests.jdwp.ThreadGroupReference;
275f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
285f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughesimport org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
295f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughesimport org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
30077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertzimport org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
315f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughesimport org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
325f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughesimport org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
33077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertzimport org.apache.harmony.jpda.tests.jdwp.share.JDWPTestConstants;
345f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughesimport org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
355f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
365f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
375f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes/**
385f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes * JDWP Unit test for ThreadGroupReference.Children command.
395f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes */
405f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughespublic class ChildrenTest extends JDWPSyncTestCase {
415f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
425f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
435f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes    protected String getDebuggeeClassName() {
445f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        return "org.apache.harmony.jpda.tests.jdwp.ThreadGroupReference.ChildrenDebuggee";
455f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes    }
465f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
475f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes    /**
485f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes     * This testcase exercises ThreadGroupReference.Children command.
495f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes     * <BR>At first the test starts ChildrenDebuggee.
505f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes     * <BR> Then the test with help of the ThreadGroupReference.Children command checks
515f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes     * that the group 'PARENT_GROUP' has one child thread - 'TESTED_THREAD'
525f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes     * and one child group - 'CHILD_GROUP'.
535f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes     *
545f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes     */
555f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes    public void testChildren001() {
565f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        logWriter.println("==> ChildrenTest.testChildren001 START...");
575f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        logWriter.println("==> Wait for SGNL_READY...");
585f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
595f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
605f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        // getting ID of the tested thread
615f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        logWriter.println("==> Get testedThreadID...");
625f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        CommandPacket packet;
635f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        long threadID = debuggeeWrapper.vmMirror.getThreadID(NameDebuggee.TESTED_THREAD);
645f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        logWriter.println("==> testedThreadID = " + threadID);
655f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
665f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        long groupID;
675f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        String groupName;
685f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
695f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        // getting the thread group ID
705f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        logWriter.println("==> Send ThreadReference.ThreadGroup command for testedThreadID...");
715f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        packet = new CommandPacket(
725f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
735f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes                JDWPCommands.ThreadReferenceCommandSet.ThreadGroupCommand);
745f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        packet.setNextValueAsThreadID(threadID);
755f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
765f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        checkReplyPacket(reply, "ThreadReference.ThreadGroup command");
775f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
785f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        groupID = reply.getNextValueAsThreadGroupID();
795f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        logWriter.println("==> groupID = " + groupID);
805f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
815f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        logWriter.println("==> Send ThreadGroupReference.Children command...");
825f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        packet = new CommandPacket(
835f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes                JDWPCommands.ThreadGroupReferenceCommandSet.CommandSetID,
845f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes                JDWPCommands.ThreadGroupReferenceCommandSet.ChildrenCommand);
855f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        packet.setNextValueAsThreadID(groupID);
865f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        reply = debuggeeWrapper.vmMirror.performCommand(packet);
875f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        checkReplyPacket(reply, "ThreadGroupReference.Children command");
885f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
895f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        logWriter.println("\n==> Children of the group: \""
905f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes                + debuggeeWrapper.vmMirror.getThreadGroupName(groupID)
915f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes                + "\": ");
925f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
935f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        int childThreads = reply.getNextValueAsInt();
945f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        if (childThreads != 1) {
955f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes            logWriter.println("## FAILURE: Unexpected number of child threads = " + childThreads);
965f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes            logWriter.println("## Expected number of child threads = 1");
975f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes            assertEquals("Invalid number of child threads,", 1, childThreads);
985f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        }
995f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        long childThreadID = reply.getNextValueAsThreadID();
1005f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        String threadName = debuggeeWrapper.vmMirror.getThreadName(childThreadID);
1015f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        logWriter.println
1025f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        ("==> thread: threadID = " + childThreadID + "; threadName = " + threadName);
1035f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
1045f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        if (threadID != childThreadID) {
1055f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes            logWriter.println("## FAILURE: Unexpected ID of child thread = " + childThreadID);
1065f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes            logWriter.println("## Expected ID of child thread = " + threadID);
1075f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes            assertEquals("Invalid ID of child thread,", threadID, childThreadID);
1085f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        }
1095f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        if (!threadName.equals(NameDebuggee.TESTED_THREAD)) {
1105f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes            logWriter.println("## FAILURE: unexpected thread name, it is expected: "
1115f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes                    + NameDebuggee.TESTED_THREAD);
1125f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes            assertString("Invalid thread name,", NameDebuggee.TESTED_THREAD, threadName);
1135f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        }
1145f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
1155f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        int childGroups = reply.getNextValueAsInt();
1165f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        if (childGroups != 1) {
1175f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes            logWriter.println("## FAILURE: Unexpected number of child groups " + childGroups);
1185f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes            logWriter.println("## Expected number = 1");
1195f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes            assertEquals("Invalid number of child groups,", 1, childGroups);
1205f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        }
1215f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
1225f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        groupID = reply.getNextValueAsThreadGroupID();
1235f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        groupName = debuggeeWrapper.vmMirror.getThreadGroupName(groupID);
1245f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
1255f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        logWriter.println("\n==> group: groupID = " + groupID + "; groupName = " + groupName);
1265f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
1275f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        assertString("Invalid group name,", NameDebuggee.CHILD_GROUP, groupName);
1285f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
1295f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
1305f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes    }
1315f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes
132077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz    /**
133077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     * This testcase exercises ThreadGroupReference.Children command.
134077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     * <BR>At first the test starts NameDebuggee.
135077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     * <BR> Then the test with help of the ThreadGroupReference.Children command
136077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     * checks that INVALID_OBJECT error is returned for the null object id.
137077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     *
138077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     */
139077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz    public void testChildren_NullObject() {
140077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        logWriter.println("wait for SGNL_READY");
141077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
142077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz
143077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        checkCommandError(JDWPTestConstants.NULL_OBJECT_ID,
144077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz                          JDWPConstants.Error.INVALID_OBJECT);
145077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz
146077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
147077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz    }
148077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz
149077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz    /**
150077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     * This testcase exercises ThreadGroupReference.Children command.
151077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     * <BR>At first the test starts NameDebuggee.
152077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     * <BR> Then the test with help of the ThreadGroupReference.Children command
153077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     * checks that INVALID_OBJECT error is returned for an invalid object id.
154077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     *
155077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     */
156077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz    public void testChildren_InvalidObject() {
157077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        logWriter.println("wait for SGNL_READY");
158077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
159077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz
160077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        checkCommandError(JDWPTestConstants.INVALID_OBJECT_ID,
161077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz                          JDWPConstants.Error.INVALID_OBJECT);
162077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz
163077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
164077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz    }
165077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz
166077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz    /**
167077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     * This testcase exercises ThreadGroupReference.Children command.
168077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     * <BR>At first the test starts NameDebuggee.
169077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     * <BR> Then the test with help of the ThreadGroupReference.Children command
170077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     * checks that INVALID_THREAD_GROUP error is returned for an object that is
171077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     * not a java.lang.ThreadGroup.
172077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     *
173077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz     */
174077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz    public void testChildren_InvalidThreadGroup() {
175077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        logWriter.println("wait for SGNL_READY");
176077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
177077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz
178077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        long threadID = debuggeeWrapper.vmMirror.getThreadID(NameDebuggee.TESTED_THREAD);
179077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz
180077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        checkCommandError(threadID, JDWPConstants.Error.INVALID_THREAD_GROUP);
181077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz
182077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
183077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz    }
184077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz
185077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz    private void checkCommandError(long groupID, int expectedError) {
186077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        logWriter.println("Send ThreadGroupReference.Children command with id " + groupID);
187077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz
188077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        CommandPacket packet = new CommandPacket(
189077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz                JDWPCommands.ThreadGroupReferenceCommandSet.CommandSetID,
190077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz                JDWPCommands.ThreadGroupReferenceCommandSet.ChildrenCommand);
191077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        packet.setNextValueAsThreadGroupID(groupID);
192077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
193077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz
194077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz        checkReplyPacket(reply, "ThreadGroupReference::Name command",
195077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz                         expectedError);
196077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz    }
197077820b93e27ba418eb4e7efaeeb1883f3db231cSebastien Hertz
1985f0a23683aa603d8c50b6dd071a565821b76067bElliott Hughes}
199