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 24.02.2005
25  */
26 package org.apache.harmony.jpda.tests.jdwp.ReferenceType;
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.ReplyPacket;
31 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
32 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
33 
34 
35 /**
36  * JDWP Unit test for ReferenceType.ClassObject command.
37  */
38 public class ClassObjectTest extends JDWPSyncTestCase {
39 
40     static final int testStatusPassed = 0;
41     static final int testStatusFailed = -1;
42     static final String thisCommandName = "ReferenceType.ClassObject command";
43     static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/ClassObjectDebuggee;";
44 
getDebuggeeClassName()45     protected String getDebuggeeClassName() {
46         return "org.apache.harmony.jpda.tests.jdwp.ReferenceType.ClassObjectDebuggee";
47     }
48 
49     /**
50      * This testcase exercises ReferenceType.ClassObject command.
51      * <BR>The test starts ClassObjectDebuggee class, requests referenceTypeId
52      * for this class by VirtualMachine.ClassesBySignature command, then
53      * performs ReferenceType.ClassObject command and checks that returned
54      * class object is object of reference type 'java/lang/Class'.
55      */
testClassObject001()56     public void testClassObject001() {
57         String thisTestName = "testClassObject001";
58         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
59         //int testStatus = testStatusPassed;
60         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
61 
62         long refTypeID = getClassIDBySignature(debuggeeSignature);
63 
64         logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
65         logWriter.println("=> referenceTypeID for Debuggee class = " + refTypeID);
66 
67         String debuggeeExtraClassSignature
68             = "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/ClassObjectDebuggee_ExtraClass;";
69 
70         long debuggeeExtraClassRefTypeID = getClassIDBySignature(debuggeeExtraClassSignature);
71 
72         logWriter.println("=> referenceTypeID for Debuggee extra class = " + debuggeeExtraClassRefTypeID);
73         logWriter.println("=> CHECK: send " + thisCommandName + " and check reply...");
74 
75         CommandPacket checkedCommand = new CommandPacket(
76                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
77                 JDWPCommands.ReferenceTypeCommandSet.ClassObjectCommand);
78         checkedCommand.setNextValueAsReferenceTypeID(refTypeID);
79 
80         ReplyPacket checkedReply = debuggeeWrapper.vmMirror.performCommand(checkedCommand);
81         checkedCommand = null;
82         checkReplyPacket(checkedReply, thisCommandName);
83 
84         long classObjectID = checkedReply.getNextValueAsObjectID();
85         logWriter.println("=> Returned class ObjectID = " + classObjectID);
86         logWriter.println("=> Get reference type for returned class Object...");
87 
88         CommandPacket referenceTypeCommand = new CommandPacket(
89                 JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
90                 JDWPCommands.ObjectReferenceCommandSet.ReferenceTypeCommand);
91         referenceTypeCommand.setNextValueAsObjectID(classObjectID);
92 
93         ReplyPacket referenceTypeReply = debuggeeWrapper.vmMirror.performCommand(referenceTypeCommand);
94         referenceTypeCommand = null;
95 
96         checkReplyPacket(referenceTypeReply, "ObjectReference::ReferenceType command");
97 
98         referenceTypeReply.getNextValueAsByte();
99         // refTypeTag of class - is NOT used here
100 
101         long refTypeIDOfClassObject = referenceTypeReply.getNextValueAsReferenceTypeID();
102         referenceTypeReply = null;
103 
104         logWriter.println("=> ReferenceTypeID of ClassObject = " + refTypeIDOfClassObject);
105         logWriter.println("=> Get signature for reference type of ClassObject...");
106 
107         CommandPacket signatureCommand = new CommandPacket(
108                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
109                 JDWPCommands.ReferenceTypeCommandSet.SignatureCommand);
110         signatureCommand.setNextValueAsReferenceTypeID(refTypeIDOfClassObject);
111 
112         ReplyPacket signatureReply = debuggeeWrapper.vmMirror.performCommand(signatureCommand);
113         signatureCommand = null;
114         checkReplyPacket(signatureReply, "ReferenceType::Signature command");
115 
116         String returnedSignature = signatureReply.getNextValueAsString();
117         signatureReply = null;
118         logWriter.println("=> Signature of reference type of ClassObject = " + returnedSignature);
119 
120         String expectedSignature = "Ljava/lang/Class;";
121         assertString("Invalid signature of reference type of ClassObject,", expectedSignature, returnedSignature);
122         logWriter.println("=> CHECK: PASSED: expected signature is returned");
123 
124         assertAllDataRead(checkedReply);
125 
126         CommandPacket classLoaderCommand = new CommandPacket(
127                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
128                 JDWPCommands.ReferenceTypeCommandSet.ClassLoaderCommand);
129         classLoaderCommand.setNextValueAsReferenceTypeID(refTypeID);
130 
131         ReplyPacket classLoaderReply = debuggeeWrapper.vmMirror.performCommand(classLoaderCommand);
132         classLoaderCommand = null;
133         checkReplyPacket(classLoaderReply, "ReferenceType::ClassLoader command");
134 
135         long classLoaderID = classLoaderReply.getNextValueAsObjectID();
136         logWriter.println("=> ClassLoaderID of debuggee = " + classLoaderID);
137         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
138         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
139     }
140 }
141