summaryrefslogtreecommitdiff
path: root/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ArrayReference/GetValuesTest.java
blob: 066df975071546a0494ed13ef7e47efcb674ebdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @author Anton V. Karnachuk
 */

/**
 * Created on 09.03.2005
 */
package org.apache.harmony.jpda.tests.jdwp.ArrayReference;

import org.apache.harmony.jpda.tests.framework.jdwp.ArrayRegion;
import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
import org.apache.harmony.jpda.tests.framework.jdwp.Field;
import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
import org.apache.harmony.jpda.tests.framework.jdwp.Value;
import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;



/**
 * JDWP unit test for ArrayReference.GetValues command.
 */

public class GetValuesTest extends JDWPArrayReferenceTestCase {
    /**
     * This testcase exercises ArrayReference.GetValues command.
     * <BR>Starts <A HREF="ArrayReferenceDebuggee.html">ArrayReferenceDebuggee</A>.
     * <BR>Receives fields with ReferenceType.fields command,
     * receives values with ArrayReference.GetValues then checks them.
     */
    public void testGetValues001() {
        logWriter.println("==> GetValuesTest.testGetValues001 started...");
        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);

        String debuggeeSig = "Lorg/apache/harmony/jpda/tests/jdwp/ArrayReference/ArrayReferenceDebuggee;";

        // obtain classID
        long classID = getClassIDBySignature(debuggeeSig);

        // obtain fields
        Field[] fields = debuggeeWrapper.vmMirror.getFieldsInfo(classID);

        for (Field fieldInfo : fields) {
            long fieldID = fieldInfo.getFieldID();
            String name = fieldInfo.getName();

            if (name.equals("threadArray")) {
                logWriter.println
                    ("\n==> testGetValues001: check for array field: 'threadArray'...");
                checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 1,
                        1, JDWPConstants.Tag.OBJECT_TAG, JDWPConstants.Tag.THREAD_TAG, false);
            }
            if (name.equals("threadGroupArray")) {
                logWriter.println
                    ("\n==> testGetValues001: check for array field: 'threadGroupArray...");
                checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 1,
                        1, JDWPConstants.Tag.OBJECT_TAG, JDWPConstants.Tag.THREAD_GROUP_TAG, false);
            }
            if (name.equals("classArray")) {
                logWriter.println
                    ("\n==> testGetValues001: check for array field: 'classArray'...");
                checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 1,
                        1, JDWPConstants.Tag.OBJECT_TAG, JDWPConstants.Tag.CLASS_OBJECT_TAG, false);
            }
            if (name.equals("ClassLoaderArray")) {
                logWriter.println
                    ("\n==> testGetValues001: check for array field: 'ClassLoaderArray'...");
                checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 1,
                        1, JDWPConstants.Tag.OBJECT_TAG, JDWPConstants.Tag.CLASS_LOADER_TAG, false);
            }
            if (name.equals("myThreadArray")) {
                logWriter.println
                    ("\n==> testGetValues001: check for array field: 'myThreadArray'...");
                checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 1,
                        1, JDWPConstants.Tag.OBJECT_TAG, JDWPConstants.Tag.THREAD_TAG, false);
            }
            if (name.equals("objectArrayArray")) {
                logWriter.println
                    ("\n==> testGetValues001: check for array field: 'objectArrayArray'...");
                checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 1,
                        1, JDWPConstants.Tag.ARRAY_TAG, JDWPConstants.Tag.ARRAY_TAG, false);
            }
            if (name.equals("intArray")) {
                // int[] intArray = new int[10]
                logWriter.println
                    ("\n==> testGetValues001: check for array field: 'int[] intArray'...");
                checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 10,
                4, JDWPConstants.Tag.INT_TAG, JDWPConstants.Tag.INT_TAG, true);
            }
            if (name.equals("strArray")) {
                // String[] strArray = new String[8]
                logWriter.println
                    ("\n==> testGetValues001: check for array field: 'String[] strArray'...");
                checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 8,
                4, JDWPConstants.Tag.OBJECT_TAG, JDWPConstants.Tag.STRING_TAG, true);
            }
            if (name.equals("intField")) {
                // Integer intField = new Integer(-1)
                logWriter.println
                ("\n==> testGetValues001: check for non-array field: 'Integer intField = new Integer(-1)'...");
               checkArrayValues(classID, fieldID, JDWPConstants.Error.INVALID_ARRAY, 0,
               4, (byte)0, (byte)0, false);
            }
        }

        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    }

    private void checkArrayValues(long classID, long fieldID, int error, int length,
            int checksNumber, byte expectedArrayTag, byte expectedElementTag, boolean checkValues) {
        CommandPacket packet = new CommandPacket(
                JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
                JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
        // set referenceTypeID
        packet.setNextValueAsReferenceTypeID(classID);
        // repeat 1 time
        packet.setNextValueAsInt(1);
        // set fieldID
        packet.setNextValueAsFieldID(fieldID);
        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
        checkReplyPacket(reply, "ReferenceType::GetValues command");

        assertEquals("ReferenceType::GetValues command returned invalid int value,", reply.getNextValueAsInt(), 1);
        Value value = reply.getNextValueAsValue();
        long arrayID = value.getLongValue();
        logWriter.println("==> testGetValues001: checked arrayID = " + arrayID);

        logWriter.println("==> testGetValues001: checkArrayRegion: arrayID = " + arrayID
                + "; Expected error = " + error + "(" + JDWPConstants.Error.getName(error) + ")"
                + "; firstIndex = 0; length = " + length);
        checkArrayRegion
        (arrayID, error, 0, length, expectedArrayTag, expectedElementTag, checkValues);
        logWriter.println("==> PASSED!");

        if ( checksNumber > 1 ) {
            logWriter.println("==> testGetValues001: checkArrayRegion: arrayID = " + arrayID
                + "; Expected error = " + error+ "(" + JDWPConstants.Error.getName(error) + ")"
                + "; firstIndex = 1; length = " + (length-1));
            checkArrayRegion
            (arrayID, error, 1, length-1, expectedArrayTag, expectedElementTag, checkValues);
            logWriter.println("==> PASSED!");

            logWriter.println("==> testGetValues001: checkArrayRegion: arrayID = " + arrayID
                + "; Expected error = " + error+ "(" + JDWPConstants.Error.getName(error) + ")"
                + "; firstIndex = 0; length = " + (length-1));
            checkArrayRegion
            (arrayID, error, 0, length-1, expectedArrayTag, expectedElementTag, checkValues);
            logWriter.println("==> PASSED!");

            logWriter.println("==> testGetValues001: checkArrayRegion: arrayID = " + arrayID
                + "; Expected error = " + error+ "(" + JDWPConstants.Error.getName(error) + ")"
                + "; firstIndex = " + (length-1) + " length = 1");
            checkArrayRegion
            (arrayID, error, length-1, 1, expectedArrayTag, expectedElementTag, checkValues);
            logWriter.println("==> PASSED!");
        }
    }

    private void checkArrayRegion(long arrayID, int error, int firstIndex, int length,
            byte expectedArrayTag, byte expectedElementTag, boolean checkValues) {

        CommandPacket packet = new CommandPacket(
                JDWPCommands.ArrayReferenceCommandSet.CommandSetID,
                JDWPCommands.ArrayReferenceCommandSet.GetValuesCommand);
        packet.setNextValueAsArrayID(arrayID);
        packet.setNextValueAsInt(firstIndex);
        packet.setNextValueAsInt(length);

        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
        checkReplyPacket(reply, "ArrayReference::GetValues command", error);

        if (reply.getErrorCode() == JDWPConstants.Error.NONE) {
            // do not check values for non-array fields
            ArrayRegion region = reply.getNextValueAsArrayRegion();
            //System.err.println("length="+region.length);
            byte arrayTag = region.getTag();
            logWriter.println("==> arrayTag =  " + arrayTag
                    + "(" + JDWPConstants.Tag.getName(arrayTag) + ")");
            logWriter.println("==> arrayLength =  "+region.getLength());
            Value value_0 = region.getValue(0);
            byte elementTag = value_0.getTag();
            logWriter.println("==> elementTag =  " + elementTag
                    + "(" + JDWPConstants.Tag.getName(elementTag) + ")");

            assertEquals("ArrayReference::GetValues returned invalid array tag,",
                    expectedArrayTag, arrayTag,
                    JDWPConstants.Tag.getName(expectedArrayTag),
                    JDWPConstants.Tag.getName(arrayTag));
            assertEquals("ArrayReference::GetValues returned invalid array length,",
                    length, region.getLength());
            assertEquals("ArrayReference::GetValues returned invalid element tag",
                    expectedElementTag, elementTag,
                    JDWPConstants.Tag.getName(expectedElementTag),
                    JDWPConstants.Tag.getName(elementTag));

            if (checkValues) {
                for (int i = 0; i < region.getLength(); i++) {
                    Value value = region.getValue(i);
                    if (value.getTag() == JDWPConstants.Tag.INT_TAG) {
                        assertEquals("ArrayReference::GetValues returned invalid value on index:<" + i + ">,",
                                value.getIntValue(), i + firstIndex);
                    }
                    else if (value.getTag() == JDWPConstants.Tag.STRING_TAG) {
                        long stringID = value.getLongValue();
                        String s = getStringValue(stringID);
                        assertEquals("ArrayReference::GetValues returned invalid value on index:<" + i + ">,",
                                Integer.parseInt(s), i + firstIndex);
                    }
                }
            }
        }
    }
}