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 Anatoly F. Bondarenko
21 */
22
23/**
24 * Created on 21.02.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.ReferenceType;
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.JDWPConstants;
31import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
32import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
33import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
34
35
36/**
37 * JDWP Unit test for ReferenceType.NestedTypes command.
38 */
39public class NestedTypesTest extends JDWPSyncTestCase {
40
41    static final int testStatusPassed = 0;
42    static final int testStatusFailed = -1;
43    static final String thisCommandName = "ReferenceType.NestedTypes command";
44    static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/NestedTypesDebuggee;";
45
46    protected String getDebuggeeClassName() {
47        return "org.apache.harmony.jpda.tests.jdwp.ReferenceType.NestedTypesDebuggee";
48    }
49
50    /**
51     * This testcase exercises ReferenceType.NestedTypes command.
52     * The test starts NestedTypesDebuggee class, requests referenceTypeId
53     * for this class by VirtualMachine.ClassesBySignature command, then
54     * performs ReferenceType.NestedTypes command and checks that returned
55     * list of nested classes corresponds to expected list.
56     */
57    public void testNestedTypes001() {
58        String thisTestName = "testNestedTypes001";
59        logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
60        String failMessage = "";
61        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
62
63        long refTypeID = getClassIDBySignature(debuggeeSignature);
64
65        logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
66        logWriter.println("=> referenceTypeID for Debuggee class = " + refTypeID);
67        logWriter.println("=> CHECK: send " + thisCommandName + " and check reply...");
68
69        CommandPacket checkedCommand = new CommandPacket(
70                JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
71                JDWPCommands.ReferenceTypeCommandSet.NestedTypesCommand);
72        checkedCommand.setNextValueAsReferenceTypeID(refTypeID);
73        ReplyPacket checkedReply =
74            debuggeeWrapper.vmMirror.performCommand(checkedCommand);
75        checkedCommand = null;
76        checkReplyPacket(checkedReply, thisCommandName);
77
78        int returnedNestedTypesNumber = checkedReply.getNextValueAsInt();
79        logWriter.println("=> Returned nested types number = " + returnedNestedTypesNumber);
80
81        String nestedTypeSignatures[] = {
82                "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/NestedTypesDebuggee$StatInterf_1;",
83                "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/NestedTypesDebuggee$StatClass_1;",
84                "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/NestedTypesDebuggee$NonStatClass_1;",
85        };
86
87        byte nestedTypeTags[] = {
88                JDWPConstants.TypeTag.INTERFACE,
89                JDWPConstants.TypeTag.CLASS,
90                JDWPConstants.TypeTag.CLASS,
91        };
92
93        boolean nestedTypeFound[] = {
94                false,
95                false,
96                false,
97        };
98
99        int expectedNestedTypesNumber = nestedTypeSignatures.length;
100
101        logWriter.println("=> CHECK for all returned NestedTypes...");
102        for (int i = 0; i < returnedNestedTypesNumber; i++) {
103            logWriter.println("\n=> Check for returned nested type[" + i + "] ...");
104            byte returnedRefTypeTag = checkedReply.getNextValueAsByte();
105            logWriter.println("=> RefTypeTag of nested type = " + returnedRefTypeTag + "("
106                + JDWPConstants.TypeTag.getName(returnedRefTypeTag) + ")");
107            long returnedRefTypeID = checkedReply.getNextValueAsReferenceTypeID();
108            logWriter.println("=> RefTypeID of nested type = " + returnedRefTypeID);
109            logWriter.println("=> Get signature for nested type...");
110            CommandPacket signatureCommand = new CommandPacket(
111            JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
112            JDWPCommands.ReferenceTypeCommandSet.SignatureCommand);
113            signatureCommand.setNextValueAsReferenceTypeID(returnedRefTypeID);
114            ReplyPacket signatureReply =
115                debuggeeWrapper.vmMirror.performCommand(signatureCommand);
116            signatureCommand = null;
117            checkReplyPacket(signatureReply, "ReferenceType::Signature command");
118
119            String returnedSignature = signatureReply.getNextValueAsString();
120            signatureReply = null;
121            logWriter.println("=> Signature of nested type = " + returnedSignature);
122
123            int k = 0;
124            for (; k < expectedNestedTypesNumber; k++) {
125                if ( ! nestedTypeSignatures[k].equals(returnedSignature)) {
126                    continue;
127                }
128                if ( nestedTypeFound[k] ) {
129                    logWriter.println("\n## FAILURE: This nested type is found out repeatedly in the list");
130                    failMessage = failMessage +
131                        "This nested type is found repeatedly in the list;\n";
132                    break;
133                }
134                nestedTypeFound[k] = true;
135                if ( nestedTypeTags[k] != returnedRefTypeTag ) {
136                    logWriter.println("\n## FAILURE: Unexpected RefTypeTag is returned:");
137                    logWriter.println("## Expected RefTypeTag = " + nestedTypeTags[k] + "("
138                    + JDWPConstants.TypeTag.getName(nestedTypeTags[k]) + ")");
139                    failMessage = failMessage +
140                        "Unexpected RefTypeTag is returned:" +
141                        returnedRefTypeTag + "(" +
142                        JDWPConstants.TypeTag.getName(returnedRefTypeTag) + ")" +
143                        ", Expected: " + nestedTypeTags[k] + "(" +
144                        JDWPConstants.TypeTag.getName(nestedTypeTags[k]) + ");\n";
145                }
146                break;
147            }
148            if ( k == expectedNestedTypesNumber ) {
149                // returned nested type is not found out in the list of expected nested types
150                logWriter.println("\n## FAILURE: It is unexpected nested type");
151                failMessage = failMessage +
152                    "It is unexpected nested type;\n";
153            }
154        }
155
156        for (int k=0; k < expectedNestedTypesNumber; k++) {
157            if ( ! nestedTypeFound[k] ) {
158                logWriter.println
159                ("\n## FAILURE: Expected nested type is NOT found out in the returned list:");
160                logWriter.println("=> Signature of nested type = " + nestedTypeSignatures[k]);
161                failMessage = failMessage +
162                    "Expected nested type is NOT found in the returned list: " + nestedTypeSignatures[k];
163            }
164        }
165
166        finalSyncMessage = JPDADebuggeeSynchronizer.SGNL_CONTINUE;
167        if (failMessage.length() > 0) {
168            fail(failMessage);
169        }
170        assertAllDataRead(checkedReply);
171        finalSyncMessage = null;
172
173        logWriter.println
174        ("\n=> CHECK PASSED: All expected nested types are found out and have expected attributes");
175
176        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
177        logWriter.println("\n==> " + thisTestName + " for " + thisCommandName + ": OK.");
178    }
179}
180