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 Viacheslav G. Rybalov
21 */
22
23/**
24 * Created on 05.03.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.ClassLoaderReference;
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 ClassLoaderReference.VisibleClasses command.
38 */
39public class VisibleClassesTest extends JDWPSyncTestCase {
40
41    /**
42     * Returns full name of debuggee class which is used by this test.
43     * @return full name of debuggee class.
44     */
45    protected String getDebuggeeClassName() {
46        return "org.apache.harmony.jpda.tests.jdwp.share.debuggee.HelloWorld";
47    }
48
49    /**
50     * This testcase exercises ClassLoaderReference.VisibleClasses command.
51     * <BR>Starts <A HREF="../share/debuggee/HelloWorld.html">HelloWorld</A> debuggee.
52     * <BR>Then the following statements are checked:
53     * <BR>It is expected:
54     * <BR>&nbsp;&nbsp; - number of reference types has non-zero value;
55     * <BR>&nbsp;&nbsp; - refTypeTag takes one of the TypeTag constants: CLASS, INTERFACE, ARRAY;
56     * <BR>&nbsp;&nbsp; - All data were read;
57     */
58    public void testVisibleClasses001() {
59        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
60
61        CommandPacket packet = new CommandPacket(
62                JDWPCommands.ClassLoaderReferenceCommandSet.CommandSetID,
63                JDWPCommands.ClassLoaderReferenceCommandSet.VisibleClassesCommand);
64        packet.setNextValueAsClassLoaderID(0);
65        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
66        checkReplyPacket(reply, "ClassLoaderReference::VisibleClasses command");
67
68        byte refTypeTag;
69        String refTypeTagName;
70        long typeID;
71        String msgLine;
72
73        int classes = reply.getNextValueAsInt();
74        logWriter.println("Number of reference types = " + classes);
75        assertTrue("Invalid returned number of reference types = " + classes, classes > 0);
76
77        int printBound_1 = classes;
78        int printBound_2 = 0;
79        if (classes > 50) {
80            printBound_1 = 5;
81            printBound_2 = classes - 5;
82        }
83        for (int i = 0; i < classes; i++) {
84            refTypeTag = reply.getNextValueAsByte();
85            refTypeTagName = JDWPConstants.TypeTag.getName(refTypeTag);
86            msgLine = "" + i + ".  refTypeTag = " + refTypeTagName + "(" + refTypeTag + ")";
87            typeID = reply.getNextValueAsReferenceTypeID();
88            msgLine = msgLine + ";  referenceTypeID = " + typeID;
89            if ( (i < printBound_1) || (i >= printBound_2) ) {
90                logWriter.println(msgLine);
91                if ( i == (printBound_1 - 1) ) {
92                    logWriter.println("...\n...\n...");
93                }
94            }
95            assertTrue("Unexpected reference TagType:<" + refTypeTag + "(" + refTypeTagName + ")>",
96                    refTypeTag == JDWPConstants.TypeTag.ARRAY
97                    || refTypeTag == JDWPConstants.TypeTag.CLASS
98                    || refTypeTag == JDWPConstants.TypeTag.INTERFACE);
99        }
100
101        assertAllDataRead(reply);
102        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
103    }
104}
105