Home | History | Annotate | Download | only in ReferenceType
      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 package org.apache.harmony.jpda.tests.jdwp.ReferenceType;
     20 
     21 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
     22 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
     23 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
     24 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
     25 import org.apache.harmony.jpda.tests.framework.jdwp.Value;
     26 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
     27 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
     28 
     29 public class InstancesTest extends JDWPSyncTestCase {
     30 
     31     static final int testStatusPassed = 0;
     32 
     33     static final int testStatusFailed = -1;
     34 
     35     static int maxInstances;
     36 
     37     static final String thisCommandName = "ReferenceType.Instances command";
     38 
     39     static String thisTestName;
     40 
     41     static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/InstancesDebuggee;";
     42 
     43     static final String mockClassSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/MockClass;";
     44 
     45     static final String stringSignature = "Ljava/lang/String;";
     46 
     47     static final String intArraySignature = "[I";
     48     @Override
     49     protected String getDebuggeeClassName() {
     50         return "org.apache.harmony.jpda.tests.jdwp.ReferenceType.InstancesDebuggee";
     51     }
     52 
     53     /**
     54      * All test cases is based on this process. <BR>
     55      * It starts InstancesDebuggee class, requests referenceTypeId for
     56      * MockClass class by VirtualMachine.ClassesBySignature command, then performs
     57      * ReferenceType.Instances command and checks that returned reachable
     58      * objects are expected ones.
     59      */
     60     private void runTestInstances() {
     61         logWriter.println("==> " + thisTestName + " for " + thisCommandName
     62                 + ": START...");
     63         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     64 
     65         long mockClassRefTypeID = getClassIDBySignature(mockClassSignature);
     66         long debuggeeRefTypeID = getClassIDBySignature(debuggeeSignature);
     67 
     68         //Get the number of reachable objects in debuggee class
     69         long reachableObjNumID = debuggeeWrapper.vmMirror.getFieldID(
     70                 debuggeeRefTypeID, "reachableObjNum");
     71         long[] fieldIDs = new long[1];
     72         fieldIDs[0] = reachableObjNumID;
     73 
     74         Value[] values = debuggeeWrapper.vmMirror.getReferenceTypeValues(
     75                 debuggeeRefTypeID, fieldIDs);
     76         int expectedReachableObjNum = values[0].getIntValue();
     77 
     78         logWriter.println("=> ReachableObjNum in debuggee is: " + expectedReachableObjNum);
     79 
     80         //maxInstances is maximum number of instances to return.
     81         //So expectedReachableObjNum should be less than maxInstances
     82         if (expectedReachableObjNum > maxInstances && maxInstances > 0) {
     83             expectedReachableObjNum = maxInstances;
     84         }
     85 
     86         logWriter.println("=> CHECK: send " + thisCommandName
     87                 + " and check reply for ERROR...");
     88 
     89         CommandPacket InstancesCommand = new CommandPacket(
     90                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
     91                 JDWPCommands.ReferenceTypeCommandSet.InstancesCommand);
     92         InstancesCommand.setNextValueAsReferenceTypeID(mockClassRefTypeID);
     93         InstancesCommand.setNextValueAsInt(maxInstances);
     94 
     95         ReplyPacket checkedReply = debuggeeWrapper.vmMirror
     96                 .performCommand(InstancesCommand);
     97         InstancesCommand = null;
     98 
     99         short errorCode = checkedReply.getErrorCode();
    100         if (errorCode != JDWPConstants.Error.NONE) {
    101             if (errorCode == JDWPConstants.Error.NOT_IMPLEMENTED) {
    102                 logWriter
    103                         .println("=> CHECK PASSED: Expected error (NOT_IMPLEMENTED) is returned");
    104                 return;
    105             }
    106             else if(errorCode == JDWPConstants.Error.ILLEGAL_ARGUMENT) {
    107                 logWriter
    108                         .println("=> CHECK PASSED: Expected error (ILLEGAL_ARGUMENT) is returned");
    109                 return;
    110             }
    111 
    112         }
    113 
    114         //Get the number of instances that returned.
    115         int reachableInstancesNum = checkedReply.getNextValueAsInt();
    116         assertEquals(thisCommandName + "returned instances number is wrong.",
    117                 expectedReachableObjNum, reachableInstancesNum, null, null);
    118 
    119         long mockClassFieldID = debuggeeWrapper.vmMirror.getFieldID(
    120                 mockClassRefTypeID, "isReachable");
    121         for (int i = 0; i < reachableInstancesNum; i++) {
    122             //Get the tagged-objectID
    123             byte tag = checkedReply.getNextValueAsByte();
    124             assertEquals(thisCommandName
    125                     + "returned object tag is invalid.", 'L', tag, null, null);
    126 
    127             long objectID = checkedReply.getNextValueAsObjectID();
    128             logWriter.println("=> ObjectID is: " + objectID);
    129             values = debuggeeWrapper.vmMirror.getObjectReferenceValues(
    130                     objectID, new long[] { mockClassFieldID });
    131             boolean isReachable = values[0].getBooleanValue();
    132             if (!isReachable) {
    133                 printErrorAndFail(thisCommandName
    134                         + "returned object is not reachable.");
    135             }
    136         }
    137         logWriter.println("=> CHECK: PASSED: expected instances are returned:");
    138         logWriter.println("=> Returned reachable instances number is" + expectedReachableObjNum);
    139 
    140         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    141         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
    142         assertAllDataRead(checkedReply);
    143 
    144     }
    145 
    146     /**
    147      * This testcase exercises ReferenceType.Instances command. <BR>
    148      * The test starts InstancesDebuggee class, requests referenceTypeId for
    149      * MockClass class by VirtualMachine.ClassesBySignature command, then performs
    150      * ReferenceType.Instances command and checks that returned reachable
    151      * objects are expected ones. Maximum number of instances is zero, so all instances
    152      * are returned.
    153      */
    154     public void testInstances001() {
    155         thisTestName = "testInstances001";
    156         maxInstances = 0;
    157         runTestInstances();
    158     }
    159 
    160     /**
    161      * This testcase exercises ReferenceType.Instances command. <BR>
    162      * The test starts InstancesDebuggee class, requests referenceTypeId for
    163      * MockClass class by VirtualMachine.ClassesBySignature command, then performs
    164      * ReferenceType.Instances command. Since maximum number of instances is negtive, so
    165      * ILLEGAL_ARGUMENT exception are replied.
    166      */
    167     public void testInstances002() {
    168         thisTestName = "testInstances002";
    169         maxInstances = -1;
    170         runTestInstances();
    171     }
    172 
    173     /**
    174      * This testcase exercises ReferenceType.Instances command. <BR>
    175      * The test starts InstancesDebuggee class, requests referenceTypeId for
    176      * MockClass class by VirtualMachine.ClassesBySignature command, then performs
    177      * ReferenceType.Instances command and checks that returned reachable
    178      * objects are expected ones. Maximum number of instances is more than the reachable
    179      * objects of this reference type, so all instances are returned.
    180      */
    181     public void testInstances003() {
    182         thisTestName = "testInstances003";
    183         maxInstances = 20;
    184         runTestInstances();
    185     }
    186 
    187     /**
    188      * This testcase exercises ReferenceType.Instances command. <BR>
    189      * The test starts InstancesDebuggee class, requests referenceTypeId for
    190      * MockClass class by VirtualMachine.ClassesBySignature command, then performs
    191      * ReferenceType.Instances command and checks that returned reachable
    192      * objects are expected ones. Maximum number of instances is less than the reachable
    193      * objects of this reference type, so maximum number of instances are returned.
    194      */
    195     public void testInstances004() {
    196         thisTestName = "testInstances004";
    197         maxInstances = 1;
    198         runTestInstances();
    199     }
    200 
    201     /**
    202      * It starts InstancesDebuggee class, requests referenceTypeId for String
    203      * class by VirtualMachine.ClassesBySignature command, then performs
    204      * ReferenceType.Instances command and checks that returned reachable String
    205      * objects are expected.
    206      */
    207     public void testInstances_String() {
    208         String thisTestName = "testInstances_String";
    209 
    210         logWriter.println("==> " + thisTestName + " for " + thisCommandName
    211                 + ": START...");
    212         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
    213 
    214         long stringRefTypeID = getClassIDBySignature(stringSignature);
    215         maxInstances = 10;
    216 
    217         logWriter.println("=> CHECK: send " + thisCommandName
    218                 + " and check reply for ERROR...");
    219 
    220         CommandPacket InstancesCommand = new CommandPacket(
    221                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
    222                 JDWPCommands.ReferenceTypeCommandSet.InstancesCommand);
    223         InstancesCommand.setNextValueAsReferenceTypeID(stringRefTypeID);
    224         InstancesCommand.setNextValueAsInt(maxInstances);
    225 
    226         ReplyPacket checkedReply = debuggeeWrapper.vmMirror
    227                 .performCommand(InstancesCommand);
    228         InstancesCommand = null;
    229         checkReplyPacket(checkedReply, thisTestName);
    230 
    231         // Get the number of instances that returned.
    232         int reachableInstancesNum = checkedReply.getNextValueAsInt();
    233 
    234         for (int i = 0; i < reachableInstancesNum; i++) {
    235             // Get the tagged-objectID
    236             byte tag = checkedReply.getNextValueAsByte();
    237             assertEquals(thisCommandName + "returned String tag is invalid.",
    238                     's', tag, null, null);
    239             long objectID = checkedReply.getNextValueAsObjectID();
    240             logWriter.println("=> ObjectID is: " + objectID);
    241 
    242         }
    243         logWriter.println("=> CHECK: PASSED: expected instances are returned:");
    244         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    245         logWriter.println("==> " + thisTestName + " for " + thisCommandName
    246                 + ": FINISH");
    247         assertAllDataRead(checkedReply);
    248 
    249     }
    250 
    251     /**
    252      * It starts InstancesDebuggee class, requests referenceTypeId for Array
    253      * class by VirtualMachine.ClassesBySignature command, then performs
    254      * ReferenceType.Instances command and checks that returned reachable Array
    255      * objects are expected.
    256      */
    257     public void testInstances_Array() {
    258         String thisTestName = "testInstances_Array";
    259 
    260         logWriter.println("==> " + thisTestName + " for " + thisCommandName
    261                 + ": START...");
    262         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
    263 
    264         long intArrayRefTypeID = getClassIDBySignature(intArraySignature);
    265         maxInstances = 10;
    266 
    267         logWriter.println("=> CHECK: send " + thisCommandName
    268                 + " and check reply for ERROR...");
    269 
    270         CommandPacket InstancesCommand = new CommandPacket(
    271                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
    272                 JDWPCommands.ReferenceTypeCommandSet.InstancesCommand);
    273         InstancesCommand.setNextValueAsReferenceTypeID(intArrayRefTypeID);
    274         InstancesCommand.setNextValueAsInt(maxInstances);
    275 
    276         ReplyPacket checkedReply = debuggeeWrapper.vmMirror
    277                 .performCommand(InstancesCommand);
    278         InstancesCommand = null;
    279         checkReplyPacket(checkedReply, thisTestName);
    280 
    281         // Get the number of instances that returned.
    282         int reachableInstancesNum = checkedReply.getNextValueAsInt();
    283 
    284         for (int i = 0; i < reachableInstancesNum; i++) {
    285             // Get the tagged-objectID
    286             byte tag = checkedReply.getNextValueAsByte();
    287             assertEquals(thisCommandName + "returned Array tag is invalid.",
    288                     '[', tag, null, null);
    289             long objectID = checkedReply.getNextValueAsObjectID();
    290             logWriter.println("=> ObjectID is: " + objectID);
    291 
    292         }
    293         logWriter.println("=> CHECK: PASSED: expected instances are returned:");
    294         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    295         logWriter.println("==> " + thisTestName + " for " + thisCommandName
    296                 + ": FINISH");
    297         assertAllDataRead(checkedReply);
    298     }
    299 }
    300