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 18.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.Fields command.
37  */
38 public class FieldsTest extends JDWPSyncTestCase {
39 
40     static final int testStatusPassed = 0;
41     static final int testStatusFailed = -1;
42     static final String thisCommandName = "ReferenceType.Fields command";
43     static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/FieldsDebuggee;";
44 
getDebuggeeClassName()45     protected String getDebuggeeClassName() {
46         return "org.apache.harmony.jpda.tests.jdwp.ReferenceType.FieldsDebuggee";
47     }
48 
49     /**
50      * This testcase exercises ReferenceType.Fields command.
51      * <BR>The test starts FieldsDebuggee class, requests referenceTypeId
52      * for this class by VirtualMachine.ClassesBySignature command, then
53      * performs ReferenceType.Fields command and checks that returned
54      * list of fields corresponds to expected list with expected attributes.
55      */
testFields001()56     public void testFields001() {
57         String thisTestName = "testFields001";
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         logWriter.println("=> CHECK: send " + thisCommandName + " and check reply...");
67 
68         CommandPacket fieldsCommand = new CommandPacket(
69                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
70                 JDWPCommands.ReferenceTypeCommandSet.FieldsCommand);
71         fieldsCommand.setNextValueAsReferenceTypeID(refTypeID);
72 
73         ReplyPacket fieldsReply = debuggeeWrapper.vmMirror.performCommand(fieldsCommand);
74         fieldsCommand = null;
75         checkReplyPacket(fieldsReply, thisCommandName);
76 
77         int returnedFieldsNumber = fieldsReply.getNextValueAsInt();
78         logWriter.println("=> Returned fields number = " + returnedFieldsNumber);
79 
80         String fieldNames[] = {
81                 "staticIntField",
82                 "stringField",
83                 "objectField"
84         };
85 
86         String fieldSignatures[] = {
87                 "I",
88                 "Ljava/lang/String;",
89                 "Ljava/lang/Object;"
90         };
91 
92         int fieldModifiers[] = {
93                 0x8,
94                 0x0,
95                 0x0
96         };
97 
98         boolean fieldFound[] = {
99                 false,
100                 false,
101                 false
102         };
103         int expectedFieldsNumber = fieldNames.length;
104         int fieldSyntheticFlag = 0xf0000000;
105         String failMessage = null;
106 
107         logWriter.println("=> CHECK for all expected fields...");
108         for (int i = 0; i < returnedFieldsNumber; i++) {
109             long returnedFieldID = fieldsReply.getNextValueAsFieldID();
110             String returnedFieldName = fieldsReply.getNextValueAsString();
111             String returnedFieldSignature = fieldsReply.getNextValueAsString();
112             int returnedFieldModifiers = fieldsReply.getNextValueAsInt();
113             logWriter.println("\n=> Field ID = " + returnedFieldID);
114             logWriter.println("=> Field name = " + returnedFieldName);
115             logWriter.println("=> Field signature = " + returnedFieldSignature);
116             logWriter.println("=> Field modifiers = 0x" + Integer.toHexString(returnedFieldModifiers));
117             if ( (returnedFieldModifiers & fieldSyntheticFlag) == fieldSyntheticFlag ) {
118                 continue; // do not check synthetic fields
119             }
120             int k = 0;
121             for (; k < expectedFieldsNumber; k++) {
122                 if (!fieldNames[k].equals(returnedFieldName)) {
123                     continue;
124                 }
125                 if (fieldFound[k]) {
126                     logWriter.println("\n## FAILURE: The field is found repeatedly in the list");
127                     logWriter.println("## Field name = " + returnedFieldName);
128                     testStatus = testStatusFailed;
129                     failMessage = "The field is found repeatedly in the list: " +
130                         returnedFieldName;
131                     break;
132                 }
133                 fieldFound[k] = true;
134                 if (!fieldSignatures[k].equals(returnedFieldSignature) ) {
135                     logWriter.println("\n## FAILURE: Unexpected field signature is returned:");
136                     logWriter.println("## Field name = " + returnedFieldName);
137                     logWriter.println("## Expected signature = " + fieldSignatures[k]);
138                     logWriter.println("## Returned signature = " + returnedFieldSignature);
139                     testStatus = testStatusFailed;
140                     failMessage = "Unexpected signature is returned for field: " +
141                         returnedFieldName +
142                         ", expected: " + fieldSignatures[k] +
143                         ", returned: " + returnedFieldSignature;
144                 }
145                 if (fieldModifiers[k] != returnedFieldModifiers) {
146                     logWriter.println("\n## FAILURE: Unexpected field modifiers are returned:");
147                     logWriter.println("## Field name = " + returnedFieldName);
148                     logWriter.println
149                     ("## Expected modifiers = 0x" + Integer.toHexString(fieldModifiers[k]));
150                     logWriter.println
151                     ("## Returned modifiers = 0x" + Integer.toHexString(returnedFieldModifiers));
152                     testStatus = testStatusFailed;
153                     failMessage = "Unexpected modifiers are returned for field: " +
154                         returnedFieldName +
155                         ", expected: 0x" + Integer.toHexString(fieldModifiers[k]) +
156                         ", returned: 0x" + Integer.toHexString(returnedFieldModifiers);
157                 }
158                 break;
159             }
160             if (k == expectedFieldsNumber) {
161                 // returned field is not found out in the list of expected fields
162                 logWriter.println("\n## FAILURE: It is found out unexpected returned field:");
163                 logWriter.println("## Field name = " + returnedFieldName);
164                 logWriter.println("## Field signature = " + returnedFieldSignature);
165                 logWriter.println
166                 ("## Field modifiers = 0x" + Integer.toHexString(returnedFieldModifiers));
167                 testStatus = testStatusFailed;
168                 failMessage =
169                     "Unexpected returned field: " + returnedFieldName +
170                     ", signature = " + returnedFieldSignature +
171                     ", modifiers = 0x" + Integer.toHexString(returnedFieldModifiers);
172             }
173         }
174 
175         for (int k = 0; k < expectedFieldsNumber; k++) {
176             if (!fieldFound[k]) {
177                 logWriter.println
178                 ("\n## FAILURE: Expected field is NOT found out in the list of retuned fields:");
179                 logWriter.println("## Field name = " + fieldNames[k]);
180                 testStatus = testStatusFailed;
181                 failMessage = "Expected field is NOT found in the list of retuned fields: " +
182                     fieldNames[k];
183             }
184         }
185 
186         if (testStatus == testStatusPassed) {
187             logWriter.println
188             ("=> CHECK PASSED: All expected fields are found out and have expected attributes");
189         }
190 
191         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
192         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
193         if (testStatus == testStatusFailed) {
194             fail(failMessage);
195         }
196 
197         assertAllDataRead(fieldsReply);
198     }
199 }
200