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 28.02.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.ObjectReference;
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.framework.jdwp.Value;
33import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
34import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
35
36
37/**
38 * JDWP Unit test for ObjectReference.GetValues command .
39 */
40public class GetValuesTest extends JDWPSyncTestCase {
41
42    static final String thisCommandName = "ObjectReference::GetValues command";
43    static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ObjectReference/GetValuesDebuggee;";
44
45    protected String getDebuggeeClassName() {
46        return "org.apache.harmony.jpda.tests.jdwp.ObjectReference.GetValuesDebuggee";
47    }
48
49    /**
50     * This test exercises ObjectReference.GetValues command.
51     * <BR>The test starts GetValuesDebuggee class, gets objectID
52     * as value of static field of this class which (field) represents checked object.
53     * Then for this objectID test executes ObjectReference::GetValues command for special
54     * set of fieldIDs and checks that command returns expected jdwpTags for all checked
55     * fields and expected values for primitive fields.
56     */
57    public void testGetValues001() {
58        String thisTestName = "testGetValues001";
59        logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
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 checkedFieldNames[] = {
68                "getValuesDebuggeeObject",
69
70                "intField",
71                "longField",
72                "objectField",
73                "stringArrayField",
74                "objectArrayField",
75                "threadField",
76                "threadGroupField",
77                "classField",
78                "classLoaderField",
79                "stringField",
80        };
81        long checkedFieldIDs[] = checkFields(refTypeID, checkedFieldNames);
82        int checkedFieldsNumber = checkedFieldNames.length;
83
84        logWriter.println
85        ("=> Send ReferenceType::GetValues command and and get ObjectID to check...");
86
87        CommandPacket getValuesCommand = new CommandPacket(
88                JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
89                JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
90        getValuesCommand.setNextValueAsReferenceTypeID(refTypeID);
91        getValuesCommand.setNextValueAsInt(1);
92        getValuesCommand.setNextValueAsFieldID(checkedFieldIDs[0]);
93
94        ReplyPacket getValuesReply = debuggeeWrapper.vmMirror.performCommand(getValuesCommand);
95        getValuesCommand = null;
96        checkReplyPacket(getValuesReply, "ReferenceType::GetValues command");
97
98        int returnedValuesNumber = getValuesReply.getNextValueAsInt();
99        logWriter.println("=> Returned values number = " + returnedValuesNumber);
100        assertEquals("Invalid number of values,", 1, returnedValuesNumber);
101
102        Value checkedObjectFieldValue = getValuesReply.getNextValueAsValue();
103        byte checkedObjectFieldTag = checkedObjectFieldValue.getTag();
104        logWriter.println("=> Returned field value tag for checked object= " + checkedObjectFieldTag
105            + "(" + JDWPConstants.Tag.getName(checkedObjectFieldTag) + ")");
106        assertEquals("Invalid value tag for checked object,", JDWPConstants.Tag.OBJECT_TAG, checkedObjectFieldTag
107                , JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG)
108                , JDWPConstants.Tag.getName(checkedObjectFieldTag));
109
110        long checkedObjectID = checkedObjectFieldValue.getLongValue();
111        logWriter.println("=> Returned checked ObjectID = " + checkedObjectID);
112        logWriter.println("=> CHECK: send " + thisCommandName + " for this ObjectID and check reply...");
113
114        CommandPacket checkedCommand = new CommandPacket(
115                JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
116                JDWPCommands.ObjectReferenceCommandSet.GetValuesCommand);
117        checkedCommand.setNextValueAsObjectID(checkedObjectID);
118        checkedCommand.setNextValueAsInt(checkedFieldsNumber-1);
119        int fieldIndex = 1; // !!!
120        for (; fieldIndex < checkedFieldsNumber; fieldIndex++) {
121            checkedCommand.setNextValueAsFieldID(checkedFieldIDs[fieldIndex]);
122        }
123
124        ReplyPacket checkedReply = debuggeeWrapper.vmMirror.performCommand(checkedCommand);
125        checkedCommand = null;
126        checkReplyPacket(checkedReply, thisCommandName);
127
128        returnedValuesNumber = checkedReply.getNextValueAsInt();
129        logWriter.println("=> Returned values number = " + returnedValuesNumber);
130        assertEquals("Invalid number of values,", checkedFieldsNumber - 1, returnedValuesNumber);
131
132        byte expectedFieldTags[] = {
133                0, // dummy
134                JDWPConstants.Tag.INT_TAG,
135                JDWPConstants.Tag.LONG_TAG,
136                JDWPConstants.Tag.OBJECT_TAG,
137                JDWPConstants.Tag.ARRAY_TAG,
138                JDWPConstants.Tag.ARRAY_TAG,
139                JDWPConstants.Tag.THREAD_TAG,
140                JDWPConstants.Tag.THREAD_GROUP_TAG,
141                JDWPConstants.Tag.CLASS_OBJECT_TAG,
142                JDWPConstants.Tag.CLASS_LOADER_TAG,
143                JDWPConstants.Tag.STRING_TAG,
144        };
145
146        logWriter.println("=> CHECK for returned values...");
147        fieldIndex = 1; // !!!
148        for (; fieldIndex < checkedFieldsNumber; fieldIndex++) {
149            Value fieldValue = checkedReply.getNextValueAsValue();
150            byte fieldTag = fieldValue.getTag();
151            logWriter.println
152            ("\n=> Check for returned value for field: " + checkedFieldNames[fieldIndex] + " ...");
153            logWriter.println("=> Returned value tag = " + fieldTag
154                + "(" + JDWPConstants.Tag.getName(fieldTag) + ")");
155
156            assertEquals("Invalid value tag is returned,", expectedFieldTags[fieldIndex], fieldTag
157                    , JDWPConstants.Tag.getName(expectedFieldTags[fieldIndex])
158                    , JDWPConstants.Tag.getName(fieldTag));
159
160            switch ( fieldTag ) {
161            case JDWPConstants.Tag.INT_TAG:
162                int intValue = fieldValue.getIntValue();
163                logWriter.println("=> Returned value = " + intValue);
164                // here expected value = 9999 (staticIntField)
165                int expectedIntValue = 9999;
166                assertEquals("Invalid int value,", expectedIntValue, intValue);
167                break;
168            case JDWPConstants.Tag.LONG_TAG:
169                long longValue = fieldValue.getLongValue();
170                logWriter.println("=> Returned value = " + longValue);
171                // here expected value = 999999 (staticLongField)
172                long expectedLongValue = 999999;
173                assertEquals("Invalid long value,", expectedLongValue, longValue);
174                break;
175            case JDWPConstants.Tag.STRING_TAG:
176            case JDWPConstants.Tag.OBJECT_TAG:
177            case JDWPConstants.Tag.ARRAY_TAG:
178            case JDWPConstants.Tag.THREAD_TAG:
179            case JDWPConstants.Tag.THREAD_GROUP_TAG:
180            case JDWPConstants.Tag.CLASS_OBJECT_TAG:
181            case JDWPConstants.Tag.CLASS_LOADER_TAG:
182                long objectIDValue = fieldValue.getLongValue();
183                logWriter.println("=> ObjectId value = " + objectIDValue);
184                break;
185            }
186        }
187
188        assertAllDataRead(checkedReply);
189
190        logWriter.println
191        ("=> CHECK PASSED: All expected field values are got and have expected attributes");
192
193        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
194        logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
195    }
196}
197