001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Anton V. Karnachuk
021: * @version $Revision: 1.5 $
022: */
023:
024: /**
025: * Created on 09.03.2005
026: */package org.apache.harmony.jpda.tests.jdwp.ArrayReference;
027:
028: import org.apache.harmony.jpda.tests.framework.jdwp.ArrayRegion;
029: import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
030: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
031: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
032: import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
033: import org.apache.harmony.jpda.tests.framework.jdwp.Value;
034: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
035:
036: /**
037: * JDWP unit test for ArrayReference.SetValues command.
038: *
039: */
040:
041: public class SetValuesTest extends JDWPArrayReferenceTestCase {
042:
043: /**
044: * Starts this test by junit.textui.TestRunner.run() method.
045: */
046: public static void main(String[] args) {
047: junit.textui.TestRunner.run(SetValuesTest.class);
048: }
049:
050: /**
051: * This testcase exercises ArrayReference.SetValues command.
052: * <BR>Starts <A HREF="ArrayReferenceDebuggee.html">ArrayReferenceDebuggee</A>.
053: * <BR>Receives fields with ReferenceType.fields command,
054: * sets values with ArrayReference.SetValues then checks changes.
055: */
056: public void testSetValues001() {
057: logWriter.println("testLength001 started");
058: synchronizer
059: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
060:
061: // obtain classID
062: long classID = getClassIDBySignature("Lorg/apache/harmony/jpda/tests/jdwp/ArrayReference/ArrayReferenceDebuggee;");
063:
064: // obtain fields
065: CommandPacket packet = new CommandPacket(
066: JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
067: JDWPCommands.ReferenceTypeCommandSet.FieldsCommand);
068: packet.setNextValueAsReferenceTypeID(classID);
069: ReplyPacket reply = debuggeeWrapper.vmMirror
070: .performCommand(packet);
071: checkReplyPacket(reply, "ReferenceType::Fields command");
072:
073: int declared = reply.getNextValueAsInt();
074: for (int i = 0; i < declared; i++) {
075: long fieldID = reply.getNextValueAsFieldID();
076: String name = reply.getNextValueAsString();
077: reply.getNextValueAsString();
078: reply.getNextValueAsInt();
079:
080: if (name.equals("intArray")) {
081: ArrayRegion valuesRegion = new ArrayRegion(
082: JDWPConstants.Tag.INT_TAG, 10);
083: for (int j = 0; j < valuesRegion.getLength(); j++) {
084: valuesRegion.setValue(j, new Value(-j));
085: }
086: checkArrayValues(valuesRegion, classID, fieldID);
087: } else if (name.equals("longArray")) {
088: ArrayRegion valuesRegion = new ArrayRegion(
089: JDWPConstants.Tag.LONG_TAG, 10);
090: for (int j = 0; j < valuesRegion.getLength(); j++) {
091: valuesRegion.setValue(j, new Value((long) -j));
092: }
093: checkArrayValues(valuesRegion, classID, fieldID);
094: } else if (name.equals("byteArray")) {
095: ArrayRegion valuesRegion = new ArrayRegion(
096: JDWPConstants.Tag.BYTE_TAG, 10);
097: for (int j = 0; j < valuesRegion.getLength(); j++) {
098: valuesRegion.setValue(j, new Value((byte) -j));
099: }
100: checkArrayValues(valuesRegion, classID, fieldID);
101: }
102: }
103:
104: synchronizer
105: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
106: }
107:
108: private void checkArrayValues(ArrayRegion valuesRegion,
109: long classID, long fieldID) {
110: CommandPacket packet = new CommandPacket(
111: JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
112: JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
113: packet.setNextValueAsReferenceTypeID(classID);
114: packet.setNextValueAsInt(1);
115: packet.setNextValueAsFieldID(fieldID);
116:
117: ReplyPacket reply = debuggeeWrapper.vmMirror
118: .performCommand(packet);
119: checkReplyPacket(reply, "ReferenceType::GetValues command");
120:
121: assertEquals(
122: "GetValuesCommand returned invalid number of values,",
123: 1, reply.getNextValueAsInt());
124: Value value = reply.getNextValueAsValue();
125: //System.err.println("value="+value);
126: long arrayID = value.getLongValue();
127: int length = valuesRegion.getLength();
128:
129: checkArrayRegion(valuesRegion, arrayID, 0, length);
130: }
131:
132: private void checkArrayRegion(ArrayRegion valuesRegion,
133: long arrayID, int firstIndex, int length) {
134: // set values
135: CommandPacket packet = new CommandPacket(
136: JDWPCommands.ArrayReferenceCommandSet.CommandSetID,
137: JDWPCommands.ArrayReferenceCommandSet.SetValuesCommand);
138: packet.setNextValueAsArrayID(arrayID);
139: packet.setNextValueAsInt(firstIndex);
140: packet.setNextValueAsInt(length);
141: for (int i = 0; i < length; i++) {
142: packet
143: .setNextValueAsUntaggedValue(valuesRegion
144: .getValue(i));
145: }
146: packet.setNextValueAsInt(length);
147:
148: ReplyPacket reply = debuggeeWrapper.vmMirror
149: .performCommand(packet);
150: checkReplyPacket(reply, "ArrayReference::SetValues command");
151:
152: // get values
153: packet = new CommandPacket(
154: JDWPCommands.ArrayReferenceCommandSet.CommandSetID,
155: JDWPCommands.ArrayReferenceCommandSet.GetValuesCommand);
156: packet.setNextValueAsArrayID(arrayID);
157: packet.setNextValueAsInt(firstIndex);
158: packet.setNextValueAsInt(length);
159: reply = debuggeeWrapper.vmMirror.performCommand(packet);
160: checkReplyPacket(reply, "ArrayReference::GetValues command");
161:
162: // do not check values for non-array fields
163: ArrayRegion region = reply.getNextValueAsArrayRegion();
164: assertEquals("Invalud returned array length,", length, region
165: .getLength());
166: for (int i = 0; i < region.getLength(); i++) {
167: Value value = region.getValue(i);
168: logWriter.println(value.toString());
169: assertEquals(
170: "ArrayReference::GetValues returned invalid value on index:<"
171: + i + ">", value, valuesRegion.getValue(i));
172: }
173: }
174: }
|