SetValuesTest.java revision 8f0a9f81b462e205a352a1caa259ee98d41174e6
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 Anton V. Karnachuk
21 */
22
23/**
24 * Created on 09.03.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.ArrayReference;
27
28import org.apache.harmony.jpda.tests.framework.jdwp.ArrayRegion;
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.share.JPDADebuggeeSynchronizer;
35
36
37/**
38 * JDWP unit test for ArrayReference.SetValues command.
39 *
40 */
41
42public class SetValuesTest extends JDWPArrayReferenceTestCase {
43    /**
44     * This testcase exercises ArrayReference.SetValues command.
45     * <BR>Starts <A HREF="ArrayReferenceDebuggee.html">ArrayReferenceDebuggee</A>.
46     * <BR>Receives fields with ReferenceType.fields command,
47     * sets values with ArrayReference.SetValues then checks changes.
48     */
49    public void testSetValues001() {
50        logWriter.println("testLength001 started");
51        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
52
53        // obtain classID
54        long classID = getClassIDBySignature("Lorg/apache/harmony/jpda/tests/jdwp/ArrayReference/ArrayReferenceDebuggee;");
55
56        // obtain fields
57        CommandPacket packet = new CommandPacket(
58                JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
59                JDWPCommands.ReferenceTypeCommandSet.FieldsCommand);
60        packet.setNextValueAsReferenceTypeID(classID);
61        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
62        checkReplyPacket(reply, "ReferenceType::Fields command");
63
64        int declared = reply.getNextValueAsInt();
65        for (int i = 0; i < declared; i++) {
66            long fieldID = reply.getNextValueAsFieldID();
67            String name = reply.getNextValueAsString();
68            reply.getNextValueAsString();
69            reply.getNextValueAsInt();
70
71            if (name.equals("intArray")) {
72                ArrayRegion valuesRegion = new ArrayRegion(
73                        JDWPConstants.Tag.INT_TAG, 10);
74                for (int j = 0; j < valuesRegion.getLength(); j++) {
75                    valuesRegion.setValue(j, new Value(-j));
76                }
77                checkArrayValues(valuesRegion, classID, fieldID);
78            } else if (name.equals("longArray")) {
79                ArrayRegion valuesRegion = new ArrayRegion(
80                        JDWPConstants.Tag.LONG_TAG, 10);
81                for (int j = 0; j < valuesRegion.getLength(); j++) {
82                    valuesRegion.setValue(j, new Value((long)-j));
83                }
84                checkArrayValues(valuesRegion, classID, fieldID);
85            } else if (name.equals("byteArray")) {
86                ArrayRegion valuesRegion = new ArrayRegion(
87                        JDWPConstants.Tag.BYTE_TAG, 10);
88                for (int j = 0; j < valuesRegion.getLength(); j++) {
89                    valuesRegion.setValue(j, new Value((byte)-j));
90                }
91                checkArrayValues(valuesRegion, classID, fieldID);
92            }
93        }
94
95        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
96    }
97
98    private void checkArrayValues(ArrayRegion valuesRegion, long classID,
99                                  long fieldID) {
100        CommandPacket packet = new CommandPacket(
101                JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
102                JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
103        packet.setNextValueAsReferenceTypeID(classID);
104        packet.setNextValueAsInt(1);
105        packet.setNextValueAsFieldID(fieldID);
106
107        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
108        checkReplyPacket(reply, "ReferenceType::GetValues command");
109
110        assertEquals("GetValuesCommand returned invalid number of values,", 1, reply.getNextValueAsInt());
111        Value value = reply.getNextValueAsValue();
112        //System.err.println("value="+value);
113        long arrayID = value.getLongValue();
114        int length = valuesRegion.getLength();
115
116        checkArrayRegion(valuesRegion, arrayID, 0, length);
117    }
118
119    private void checkArrayRegion(ArrayRegion valuesRegion, long arrayID,
120                                  int firstIndex, int length) {
121        // set values
122        CommandPacket packet = new CommandPacket(
123                JDWPCommands.ArrayReferenceCommandSet.CommandSetID,
124                JDWPCommands.ArrayReferenceCommandSet.SetValuesCommand);
125        packet.setNextValueAsArrayID(arrayID);
126        packet.setNextValueAsInt(firstIndex);
127        packet.setNextValueAsInt(length);
128        for (int i = 0; i < length; i++) {
129            packet.setNextValueAsUntaggedValue(valuesRegion.getValue(i));
130        }
131
132        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
133        checkReplyPacket(reply, "ArrayReference::SetValues command");
134
135        // get values
136        packet = new CommandPacket(
137                JDWPCommands.ArrayReferenceCommandSet.CommandSetID,
138                JDWPCommands.ArrayReferenceCommandSet.GetValuesCommand);
139        packet.setNextValueAsArrayID(arrayID);
140        packet.setNextValueAsInt(firstIndex);
141        packet.setNextValueAsInt(length);
142        reply = debuggeeWrapper.vmMirror.performCommand(packet);
143        checkReplyPacket(reply, "ArrayReference::GetValues command");
144
145        // do not check values for non-array fields
146        ArrayRegion region = reply.getNextValueAsArrayRegion();
147        assertEquals("Invalud returned array length,", length, region.getLength());
148        for (int i = 0; i < region.getLength(); i++) {
149            Value value = region.getValue(i);
150            logWriter.println(value.toString());
151            assertEquals("ArrayReference::GetValues returned invalid value on index:<" + i + ">",
152                    value, valuesRegion.getValue(i));
153        }
154    }
155}
156