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 13.07.2005
25 */
26
27package org.apache.harmony.jpda.tests.jdwp.ObjectReference;
28
29import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
30import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
31import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
32import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
33import org.apache.harmony.jpda.tests.framework.jdwp.Value;
34import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
35import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
36
37
38/**
39 * JDWP Unit test for ObjectReference.GetValues command for Object fields with value=null.
40 */
41public class GetValues003Test extends JDWPSyncTestCase {
42
43    static final String thisCommandName = "ObjectReference::GetValues command";
44    static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ObjectReference/GetValues003Debuggee;";
45
46    @Override
47    protected String getDebuggeeClassName() {
48        return "org.apache.harmony.jpda.tests.jdwp.ObjectReference.GetValues003Debuggee";
49    }
50
51    /**
52     * This test exercises ObjectReference.GetValues command for static fields.
53     * <BR>The test starts GetValues003Debuggee class, gets objectID
54     * as value of static field of this class which (field) represents checked object.
55     * Then for this objectID test executes ObjectReference.GetValues command for
56     * fields of different referenceTypes with value=null for all fields.
57     * The test expects the all returned values should be represented by expected
58     * JDWP tag with null value.
59     */
60    public void testGetValues003() {
61        String thisTestName = "testGetValues003";
62        logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
63        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
64
65        long refTypeID = getClassIDBySignature(debuggeeSignature);
66
67        logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
68        logWriter.println("=> referenceTypeID for Debuggee class = " + refTypeID);
69
70        String checkedFieldNames[] = {
71                "testedObject",
72
73                "intArrayField",
74                "objectArrayField",
75                "objectField",
76                "stringField",
77                "threadField",
78                "threadGroupField",
79                "classField",
80                "classLoaderField",
81        };
82        long checkedFieldIDs[] = checkFields(refTypeID, checkedFieldNames);
83        int checkedFieldsNumber = checkedFieldNames.length;
84
85        logWriter.println
86        ("=> Send ReferenceType::GetValues command and get ObjectID to check...");
87
88        CommandPacket getValuesCommand = new CommandPacket(
89                JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
90                JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
91        getValuesCommand.setNextValueAsReferenceTypeID(refTypeID);
92        getValuesCommand.setNextValueAsInt(1);
93        getValuesCommand.setNextValueAsFieldID(checkedFieldIDs[0]);
94        ReplyPacket getValuesReply =
95            debuggeeWrapper.vmMirror.performCommand(getValuesCommand);
96        getValuesCommand = null;
97        checkReplyPacket(getValuesReply, "ReferenceType::GetValue command");
98
99        int returnedValuesNumber = getValuesReply.getNextValueAsInt();
100        logWriter.println("=> Returned values number = " + returnedValuesNumber);
101        assertEquals("Invalid number of values,", 1, returnedValuesNumber);
102
103        Value checkedObjectFieldValue = getValuesReply.getNextValueAsValue();
104        byte checkedObjectFieldTag = checkedObjectFieldValue.getTag();
105        logWriter.println("=> Returned field value tag for checked object= " + checkedObjectFieldTag
106            + "(" + JDWPConstants.Tag.getName(checkedObjectFieldTag) + ")");
107        assertEquals("Invalid value tag for checked object,", JDWPConstants.Tag.OBJECT_TAG, checkedObjectFieldTag
108                , JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG)
109                , JDWPConstants.Tag.getName(checkedObjectFieldTag));
110
111        long checkedObjectID = checkedObjectFieldValue.getLongValue();
112        logWriter.println("=> Returned checked ObjectID = " + checkedObjectID);
113
114        logWriter.println
115        ("=> CHECK: send " + thisCommandName
116        + " for this ObjectID for fields of different referenceTypes with with null values...");
117
118        CommandPacket checkedCommand = new CommandPacket(
119                JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
120                JDWPCommands.ObjectReferenceCommandSet.GetValuesCommand);
121        checkedCommand.setNextValueAsObjectID(checkedObjectID);
122        checkedCommand.setNextValueAsInt(checkedFieldsNumber-1);
123        int fieldIndex = 1; // !!!
124        for (; fieldIndex < checkedFieldsNumber; fieldIndex++) {
125            checkedCommand.setNextValueAsFieldID(checkedFieldIDs[fieldIndex]);
126        }
127        ReplyPacket checkedReply =
128            debuggeeWrapper.vmMirror.performCommand(checkedCommand);
129        checkedCommand = null;
130
131        checkReplyPacket(checkedReply, thisCommandName);
132
133        returnedValuesNumber = checkedReply.getNextValueAsInt();
134        logWriter.println("=> Returned values number = " + returnedValuesNumber);
135        assertEquals("Invalid number of values,", checkedFieldsNumber - 1, returnedValuesNumber);
136
137        byte expectedFieldTags[] = {
138                JDWPConstants.Tag.ARRAY_TAG,
139                JDWPConstants.Tag.ARRAY_TAG,
140                JDWPConstants.Tag.OBJECT_TAG,
141                JDWPConstants.Tag.STRING_TAG,
142                JDWPConstants.Tag.THREAD_TAG,
143                JDWPConstants.Tag.THREAD_GROUP_TAG,
144                JDWPConstants.Tag.CLASS_OBJECT_TAG,
145                JDWPConstants.Tag.CLASS_LOADER_TAG,
146        };
147        logWriter.println("=> CHECK for returned values...");
148        for (int i = 0; i < returnedValuesNumber; i++) {
149            Value fieldValue = checkedReply.getNextValueAsValue();
150            byte fieldTag = fieldValue.getTag();
151            logWriter.println
152            ("\n=> Check for returned value for field: " + checkedFieldNames[i+1] + " ...");
153            logWriter.println("=> Returned value tag = " + fieldTag
154                + "(" + JDWPConstants.Tag.getName(fieldTag) + ")");
155            if ( (fieldTag != expectedFieldTags[i]) && (fieldTag != JDWPConstants.Tag.OBJECT_TAG) ) {
156                logWriter.println("\n## FAILURE:  Unexpected value tag is returned");
157                logWriter.println("## Expected value tag = " + expectedFieldTags[i]
158                + "(" + JDWPConstants.Tag.getName(expectedFieldTags[i]) + ")"
159                + " or = " + JDWPConstants.Tag.OBJECT_TAG
160                + "(" + JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG) + ")");
161                fail("Unexpected value tag is returned");
162            }
163            long objectIDValue = fieldValue.getLongValue();
164            logWriter.println("=> ObjectId value = " + objectIDValue);
165            assertEquals("Invalid objectID value is returned,", 0, objectIDValue);
166        }
167
168        assertAllDataRead(checkedReply);
169        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
170        logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": OK.");
171    }
172}
173