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 Anton V. Karnachuk
21 */
22
23/**
24 * Created on 09.02.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.ClassType;
27
28import java.io.UnsupportedEncodingException;
29
30import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
31import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
32import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
33import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
34import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
35
36
37
38/**
39 * JDWP unit test for ClassType.SuperClass command.
40 * Contains three testcases: testSuperClass001, testSuperClass002, testSuperClass003.
41 */
42public class SuperClassTest extends JDWPClassTypeTestCase {
43    private ReplyPacket jdwpGetSuperClassReply(long classID, int errorExpected) {
44        CommandPacket packet = new CommandPacket(
45                JDWPCommands.ClassTypeCommandSet.CommandSetID,
46                JDWPCommands.ClassTypeCommandSet.SuperclassCommand);
47        packet.setNextValueAsClassID(classID);
48        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
49        checkReplyPacket(reply, "ClassType.Superclass command", errorExpected);
50        return reply;
51    }
52
53    private void asserSuperClassReplyIsValid(ReplyPacket reply, String expectedSignature) {
54        assertTrue(reply.getErrorCode() == JDWPConstants.Error.NONE);
55        long superClassID = reply.getNextValueAsClassID();
56        logWriter.println("superClassID=" + superClassID);
57        if (superClassID == 0) {
58            // for superclass of Object expectedSignature is null
59            assertNull
60            ("ClassType::Superclass command returned invalid expectedSignature that must be null",
61                    expectedSignature);
62        } else {
63            String signature = getClassSignature(superClassID);
64            logWriter.println("Signature: "+signature);
65            assertString("ClassType::Superclass command returned invalid signature,",
66                    expectedSignature, signature);
67        }
68    }
69
70    /**
71     * This testcase exercises ClassType.Superclass command.
72     * <BR>Starts <A HREF="ClassTypeDebuggee.html">ClassTypeDebuggee</A>.
73     * <BR>Then does the following checks:
74     * <BR>&nbsp;&nbsp; - superclass for java.lang.String is java.lang.Object;
75     * <BR>&nbsp;&nbsp; - superclass for array of Objects is java.lang.Object;
76     * <BR>&nbsp;&nbsp; - superclass for primitive array is java.lang.Object;
77     * <BR>&nbsp;&nbsp; - superclass for <A HREF="ClassTypeDebuggee.html">ClassTypeDebuggee</A>
78     * class is <A HREF="../../share/SyncDebuggee.html">SyncDebuggee</A> class.;
79     */
80    public void testSuperClass001() throws UnsupportedEncodingException {
81        logWriter.println("testSuperClassTest001 started");
82        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
83
84        // check that superclass for java.lang.String is java.lang.Object
85        {
86            // test with String[] class
87            long classID = getClassIDBySignature("Ljava/lang/String;");
88
89            ReplyPacket reply = jdwpGetSuperClassReply(classID, JDWPConstants.Error.NONE);
90            // complare returned signature with superclass signature
91            asserSuperClassReplyIsValid(reply, "Ljava/lang/Object;");
92        }
93
94        // check that superclass for array is java.lang.Object
95        {
96            // test with String[] class
97            long classID = getClassIDBySignature("[Ljava/lang/String;");
98
99            ReplyPacket reply = jdwpGetSuperClassReply(classID, JDWPConstants.Error.NONE);
100            // complare returned signature with superclass signature
101            asserSuperClassReplyIsValid(reply, "Ljava/lang/Object;");
102        }
103
104        // check that superclass for primitive array is java.lang.Object
105        {
106            // test with int[] class
107            long classID = getClassIDBySignature("[I");
108
109            ReplyPacket reply = jdwpGetSuperClassReply(classID, JDWPConstants.Error.NONE);
110            // complare returned signature with superclass signature
111            asserSuperClassReplyIsValid(reply, "Ljava/lang/Object;");
112        }
113
114        // check that superclass for Debuggee is SyncDebuggee
115        {
116            long classID = getClassIDBySignature(getDebuggeeSignature());
117
118            ReplyPacket reply = jdwpGetSuperClassReply(classID, JDWPConstants.Error.NONE);
119            // complare returned signature with superclass signature
120            asserSuperClassReplyIsValid(reply, "Lorg/apache/harmony/jpda/tests/share/SyncDebuggee;");
121        }
122
123        // check that there is no superclass for java.lang.Object
124        {
125            // test with java.lang.Object class
126            long classID = getClassIDBySignature("Ljava/lang/Object;");
127
128            ReplyPacket reply = jdwpGetSuperClassReply(classID, JDWPConstants.Error.NONE);
129            // complare returned signature with superclass signature
130            // (expects null for this case)
131            asserSuperClassReplyIsValid(reply, null);
132        }
133
134        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
135    }
136
137    /**
138     * This testcase exercises ClassType.Superclass command.
139     * <BR>Starts <A HREF="ClassTypeDebuggee.html">ClassTypeDebuggee</A>.
140     * <BR>Then does the following checks:
141     * <BR>&nbsp;&nbsp; - there is no superclass for interface;
142     * <BR>&nbsp;&nbsp; - INVALID_OBJECT is returned if classID is non-existent;
143     * <BR>&nbsp;&nbsp; - INVALID_OBJECT is returned if instead of classID FieldID is passed;
144     */
145    public void testSuperClass002() throws UnsupportedEncodingException {
146        logWriter.println("testSuperClassTest002 started");
147        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
148
149        // check that there is no superclass for interface objects
150        {
151            long interfaceID = getClassIDBySignature("Ljava/lang/Cloneable;");
152
153            ReplyPacket reply = jdwpGetSuperClassReply(interfaceID, JDWPConstants.Error.NONE);
154            // compare returned signature with superclass signature
155            // (null for interfaces)
156            asserSuperClassReplyIsValid(reply, null);
157        }
158
159        // check that INVALID_OBJECT returns if classID is non-existent
160        {
161            jdwpGetSuperClassReply(10000
162                , JDWPConstants.Error.INVALID_OBJECT);
163        }
164
165        // check that reply error code is INVALID_OBJECT for a FieldID Out Data
166        {
167            long classID = getClassIDBySignature(getDebuggeeSignature());
168
169            FieldInfo[] fields = jdwpGetFields(classID);
170            // assert stringID is not null
171            assertTrue("Invalid fields.length: 0", fields.length > 0);
172            // test with the first field
173
174            jdwpGetSuperClassReply(fields[0].getFieldID()
175                , JDWPConstants.Error.INVALID_OBJECT);
176        }
177
178        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
179    }
180
181    /**
182     * This testcase exercises ClassType.Superclass command.
183     * <BR>Starts <A HREF="ClassTypeDebuggee.html">ClassTypeDebuggee</A>.
184     * <BR>Then does the following check:
185     * <BR>&nbsp;&nbsp; - INVALID_CLASS is returned if instead of classID ObjectID is passed;
186     */
187    public void testSuperClass003() throws UnsupportedEncodingException {
188        logWriter.println("testSuperClassTest003 started");
189        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
190
191        // check that reply error code is INVALID_CLASS for a StringID Out Data
192        {
193            long stringID = createString("Some test string");
194            // assert stringID is not null
195            assertFalse("Invalid stringID: 0", stringID == 0);
196            jdwpGetSuperClassReply(stringID, JDWPConstants.Error.INVALID_CLASS);
197        }
198
199        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
200    }
201}
202