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