Home | History | Annotate | Download | only in ArrayType
      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 Viacheslav G. Rybalov
     21  */
     22 
     23 /**
     24  * Created on 10.03.2005
     25  */
     26 package org.apache.harmony.jpda.tests.jdwp.ArrayType;
     27 
     28 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
     29 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
     30 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
     31 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
     32 import org.apache.harmony.jpda.tests.framework.jdwp.TaggedObject;
     33 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
     34 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
     35 
     36 
     37 /**
     38  * JDWP unit test for ArrayType.NewInstance command.
     39  */
     40 
     41 public class NewInstanceTest extends JDWPSyncTestCase {
     42 
     43     /**
     44      * Returns full name of debuggee class which is used by this test.
     45      * @return full name of debuggee class.
     46      */
     47     @Override
     48     protected String getDebuggeeClassName() {
     49         return "org.apache.harmony.jpda.tests.jdwp.ArrayType.NewInstanceDebuggee";
     50     }
     51 
     52     /**
     53      * This testcase exercises ArrayType.NewInstance command.
     54      * <BR>Starts <A HREF="../share/debuggee/HelloWorld.html">HelloWorld</A> debuggee.
     55      * <BR>Creates new instance of array by ArrayType.NewInstance command,
     56      * check it length by ArrayReference.Length command.
     57      */
     58     public void testNewInstance001() {
     59         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     60         String[] testedArrayRefs = {
     61             "[Lorg/apache/harmony/jpda/tests/jdwp/ArrayType/checkClass;",
     62             "[Ljava/lang/String;",
     63             "[I",
     64             "[[Lorg/apache/harmony/jpda/tests/jdwp/ArrayType/checkClass;",
     65             "[[Ljava/lang/String;",
     66             "[[I"
     67         };
     68         for (int i = 0; i < testedArrayRefs.length; i++) {
     69             logWriter.println("Checking reference " + testedArrayRefs[i]);
     70 
     71             // Get referenceTypeID
     72             CommandPacket packet = new CommandPacket(
     73                 JDWPCommands.VirtualMachineCommandSet.CommandSetID,
     74                 JDWPCommands.VirtualMachineCommandSet.ClassesBySignatureCommand);
     75             packet.setNextValueAsString(testedArrayRefs[i]);
     76             ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
     77             checkReplyPacket(reply, "VirtualMachine::ClassesBySignature command");
     78 
     79             int classes = reply.getNextValueAsInt();
     80             if (classes <= 0) {
     81                 logWriter.println("## WARNING: class " + testedArrayRefs[i]+ " is not loadded ");
     82                 continue;
     83             }
     84 
     85             byte refInitTypeTag = reply.getNextValueAsByte();
     86             long typeArrayID = reply.getNextValueAsReferenceTypeID();
     87             int status = reply.getNextValueAsInt();
     88 
     89             assertEquals("VirtualMachine::ClassesBySignature returned invalid TypeTag,",
     90                     JDWPConstants.TypeTag.ARRAY, refInitTypeTag,
     91                     JDWPConstants.TypeTag.getName(JDWPConstants.TypeTag.ARRAY),
     92                     JDWPConstants.TypeTag.getName(refInitTypeTag));
     93 
     94             logWriter.println(" VirtualMachine.ClassesBySignature: classes="
     95                 + classes + " refTypeTag=" + refInitTypeTag + " typeID= "
     96                 + typeArrayID + " status=" + status);
     97 
     98             // Make NewInstance
     99             packet = new CommandPacket(
    100                 JDWPCommands.ArrayTypeCommandSet.CommandSetID,
    101                 JDWPCommands.ArrayTypeCommandSet.NewInstanceCommand);
    102             packet.setNextValueAsReferenceTypeID(typeArrayID);
    103             packet.setNextValueAsInt(10);
    104             reply = debuggeeWrapper.vmMirror.performCommand(packet);
    105             checkReplyPacket(reply, "ArrayType::NewInstance command");
    106 
    107             TaggedObject newArray = reply.getNextValueAsTaggedObject();
    108             assertAllDataRead(reply);
    109             assertNotNull("ArrayType::NewInstance returned null newArray", newArray);
    110 
    111             logWriter.println("ArrayType.NewInstance: newArray.tag="
    112                 + newArray.tag + " newArray.objectID=" + newArray.objectID);
    113 
    114             if (newArray.tag != JDWPConstants.Tag.ARRAY_TAG) {
    115                 logWriter.println("##FAILURE: typeTag is not ARRAY_TAG");
    116                 fail("Returned tag " + JDWPConstants.Tag.getName(newArray.tag)
    117                     + "(" + newArray.tag + ") is not ARRAY_TAG");
    118             }
    119 
    120             // Let's check array length
    121             packet = new CommandPacket(
    122                 JDWPCommands.ArrayReferenceCommandSet.CommandSetID,
    123                 JDWPCommands.ArrayReferenceCommandSet.LengthCommand);
    124             packet.setNextValueAsObjectID(newArray.objectID);
    125             reply = debuggeeWrapper.vmMirror.performCommand(packet);
    126             checkReplyPacket(reply, "ArrayReference::Length command");
    127 
    128             int arrayLength = reply.getNextValueAsInt();
    129             logWriter.println("ArrayReference.Length: arrayLength=" + arrayLength);
    130             assertEquals("ArrayReference::Length command returned ivalid array length,",
    131                     10, arrayLength);
    132             assertAllDataRead(reply);
    133         }
    134         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    135     }
    136 }
    137