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 25.02.2005
25  */
26 package org.apache.harmony.jpda.tests.jdwp.ObjectReference;
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.JDWPConstants;
31 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
32 import org.apache.harmony.jpda.tests.framework.jdwp.Value;
33 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
34 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
35 
36 
37 /**
38  * JDWP Unit test for ObjectReference.ReferenceType command.
39  */
40 public class ReferenceTypeTest extends JDWPSyncTestCase {
41 
42     static final int testStatusPassed = 0;
43     static final int testStatusFailed = -1;
44     static final String thisCommandName = "ObjectReference.ReferenceType command";
45     static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ObjectReference/ReferenceTypeDebuggee;";
46 
getDebuggeeClassName()47     protected String getDebuggeeClassName() {
48         return "org.apache.harmony.jpda.tests.jdwp.ObjectReference.ReferenceTypeDebuggee";
49     }
50 
51     /**
52      * This test exercises ObjectReference.ReferenceType command.
53      * <BR>The test starts ReferenceTypeDebuggee class, gets objectIDs
54      * as values of some static fields of this class and then for each objectID
55      * executes ObjectReference.ReferenceType command and checks
56      * that command returns expected refTypeTags and returned Reference Types have
57      * expected signatures.
58      */
testReferenceType001()59     public void testReferenceType001() {
60         String thisTestName = "testReferenceType001";
61         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
62         int testStatus = testStatusPassed;
63         String failMessage = "";
64         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
65 
66         CommandPacket classesBySignatureCommand = new CommandPacket(
67                 JDWPCommands.VirtualMachineCommandSet.CommandSetID,
68                 JDWPCommands.VirtualMachineCommandSet.ClassesBySignatureCommand);
69         classesBySignatureCommand.setNextValueAsString(debuggeeSignature);
70 
71         ReplyPacket classesBySignatureReply = debuggeeWrapper.vmMirror.performCommand(classesBySignatureCommand);
72         classesBySignatureCommand = null;
73 
74         checkReplyPacket(classesBySignatureReply, "VirtualMachine::ClassesBySignature command");
75 
76         classesBySignatureReply.getNextValueAsInt();
77         // Number of returned reference types - is NOt used here
78 
79         classesBySignatureReply.getNextValueAsByte();
80         // refTypeTag of class - is NOt used here
81 
82         long refTypeID = classesBySignatureReply.getNextValueAsReferenceTypeID();
83         classesBySignatureReply = null;
84 
85         logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
86         logWriter.println("=> referenceTypeID for Debuggee class = " + refTypeID);
87 
88         String neededFieldNames[] = {
89                 "class_ReferenceType001Object",
90                 "referenceTypeDebuggeeArray",
91                 "stringArrayField",
92                 "stringField",
93         };
94 
95         long neededFieldIDs[] = checkFields(refTypeID, neededFieldNames);
96         int neededFieldsNumber = neededFieldNames.length;
97 
98         logWriter.println
99         ("=> Send ReferenceType::GetValues command and and get ObjectIDs to check...");
100         CommandPacket getValuesCommand = new CommandPacket(
101                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
102                 JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
103         getValuesCommand.setNextValueAsReferenceTypeID(refTypeID);
104         getValuesCommand.setNextValueAsInt(neededFieldsNumber);
105         for (int k=0; k < neededFieldsNumber; k++) {
106             getValuesCommand.setNextValueAsFieldID(neededFieldIDs[k]);
107         }
108 
109         ReplyPacket getValuesReply = debuggeeWrapper.vmMirror.performCommand(getValuesCommand);
110         getValuesCommand = null;
111         checkReplyPacket(getValuesReply, "ReferenceType::GetValues command");
112 
113         int returnedValuesNumber = getValuesReply.getNextValueAsInt();
114         logWriter.println("=> Returned values number = " + returnedValuesNumber);
115         if ( returnedValuesNumber != neededFieldsNumber ) {
116             logWriter.println
117             ("\n## FAILURE: ReferenceType::GetValues command returned unexpected number of values:");
118             logWriter.println("## Expected number = " + neededFieldsNumber);
119             logWriter.println("## Returned number = " + returnedValuesNumber);
120             logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
121             assertTrue(false);
122         }
123         logWriter.println("=> Check for returned objectIDs...");
124         byte expectedFieldTags[] = {
125                 JDWPConstants.Tag.OBJECT_TAG,
126                 JDWPConstants.Tag.ARRAY_TAG,
127                 JDWPConstants.Tag.ARRAY_TAG,
128                 JDWPConstants.Tag.STRING_TAG,
129         };
130         String expectedSignatures[] = {
131                 "Lorg/apache/harmony/jpda/tests/jdwp/ObjectReference/Class_ReferenceType001;",
132                 "[Lorg/apache/harmony/jpda/tests/jdwp/ObjectReference/ReferenceTypeDebuggee;",
133                 "[Ljava/lang/String;",
134                 "Ljava/lang/String;",
135         };
136         byte expectedRefTypeTags[] = {
137                 JDWPConstants.TypeTag.CLASS,
138                 JDWPConstants.TypeTag.ARRAY,
139                 JDWPConstants.TypeTag.ARRAY,
140                 JDWPConstants.TypeTag.CLASS,
141         };
142         ReplyPacket checkedReply = null;
143         for (int k=0; k < neededFieldsNumber; k++) {
144             Value fieldValue = getValuesReply.getNextValueAsValue();
145             byte fieldTag = fieldValue.getTag();
146             logWriter.println("\n=> Returned value for field: " + neededFieldNames[k] + ":");
147             logWriter.println("=> Value tag = " + fieldTag + "(" + JDWPConstants.Tag.getName(fieldTag) + ")");
148             if ( fieldTag != expectedFieldTags[k] ) {
149                 logWriter.println
150                 ("\n## FAILURE: ReferenceType::GetValues command returned unexpected value tag ");
151                 logWriter.println
152                 ("## Expected tag = " + expectedFieldTags[k]);
153                 assertTrue(false);
154             }
155             long objectIdValue = fieldValue.getLongValue();
156             logWriter.println("=> objectId = " + objectIdValue);
157             logWriter.println("=> CHECK: send " + thisCommandName + " for this objectID and check reply...");
158             CommandPacket checkedCommand = new CommandPacket(
159                     JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
160                     JDWPCommands.ObjectReferenceCommandSet.ReferenceTypeCommand);
161             checkedCommand.setNextValueAsObjectID(objectIdValue);
162 
163             checkedReply = debuggeeWrapper.vmMirror.performCommand(checkedCommand);
164             checkedCommand = null;
165             checkReplyPacket(checkedReply, thisCommandName);
166 
167             byte refTypeTag = checkedReply.getNextValueAsByte();
168             long objectRefTypeID = checkedReply.getNextValueAsReferenceTypeID();
169             logWriter.println("=> Returned refTypeTag = " + refTypeTag
170                     + "(" + JDWPConstants.TypeTag.getName(refTypeTag) + ")");
171             if ( refTypeTag != expectedRefTypeTags[k] ) {
172                 logWriter.println
173                 ("\n## FAILURE: " + thisCommandName + " returned unexpected refTypeTag:");
174                 logWriter.println("## Expected refTypeTag = " + expectedRefTypeTags[k]
175                     + "(" + JDWPConstants.TypeTag.getName(expectedRefTypeTags[k]) + ")");
176                 testStatus = testStatusFailed;
177                 failMessage = failMessage +
178                     thisCommandName + " returned unexpected refTypeTag: " +
179                     refTypeTag + "(" + JDWPConstants.TypeTag.getName(refTypeTag) + ")" +
180                     ", Expected: " + expectedRefTypeTags[k] +
181                     "(" + JDWPConstants.TypeTag.getName(expectedRefTypeTags[k]) + ")\n";
182             }
183             logWriter.println("=> ReferenceTypeID for this objectID = " + objectRefTypeID);
184             logWriter.println("=> Get signature for returned ReferenceTypeID...");
185             CommandPacket signatureCommand = new CommandPacket(
186                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
187                 JDWPCommands.ReferenceTypeCommandSet.SignatureCommand);
188             signatureCommand.setNextValueAsReferenceTypeID(objectRefTypeID);
189 
190             ReplyPacket signatureReply = debuggeeWrapper.vmMirror.performCommand(signatureCommand);
191             signatureCommand = null;
192             checkReplyPacket(signatureReply, "ReferenceType::Signature command");
193 
194             String returnedSignature = signatureReply.getNextValueAsString();
195             logWriter.println("=> Returned Signature = " + returnedSignature);
196 
197             if ( ! expectedSignatures[k].equals(returnedSignature) ) {
198                 logWriter.println("\n## FAILURE: Unexpected signature is returned:");
199                 logWriter.println("## Expected signature = " + expectedSignatures[k]);
200                 testStatus = testStatusFailed;
201                 failMessage = failMessage +
202                     "Unexpected signature is returned: " + returnedSignature +
203                     ", Expected: " + expectedSignatures[k] + "\n";
204             }
205         }
206 
207         if ( testStatus == testStatusPassed ) {
208             logWriter.println
209             ("\n=> CHECK PASSED: All expected reference types are got and have expected attributes");
210         }
211 
212         assertAllDataRead(checkedReply);
213 
214         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
215         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
216         if (failMessage.length() > 0) {
217             fail(failMessage);
218         }
219     }
220 }
221