Home | History | Annotate | Download | only in ClassType
      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 Anatoly F. Bondarenko
     21  */
     22 
     23 /**
     24  * Created on 05.07.2005
     25  */
     26 package org.apache.harmony.jpda.tests.jdwp.ClassType;
     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.Value;
     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 ClassType.SetValues command with incorrect types of values.
     39  */
     40 public class SetValues002Test extends JDWPSyncTestCase {
     41 
     42     static final int testStatusPassed = 0;
     43     static final int testStatusFailed = -1;
     44     static final String thisCommandName = "ClassType::SetValues command";
     45     static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ClassType/SetValues002Debuggee;";
     46 
     47     @Override
     48     protected String getDebuggeeClassName() {
     49         return "org.apache.harmony.jpda.tests.jdwp.ClassType.SetValues002Debuggee";
     50     }
     51 
     52     /**
     53      * The test checks ClassType.SetValues command for
     54      * field of Debuggee class with value which has other
     55      * referenceType than field to set.
     56      * The test expects the field should not be set.
     57      */
     58     public void testSetValues002() {
     59         String thisTestName = "testSetValues002";
     60         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
     61         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     62 
     63         CommandPacket classesBySignatureCommand = new CommandPacket(
     64                 JDWPCommands.VirtualMachineCommandSet.CommandSetID,
     65                 JDWPCommands.VirtualMachineCommandSet.ClassesBySignatureCommand);
     66         classesBySignatureCommand.setNextValueAsString(debuggeeSignature);
     67         ReplyPacket classesBySignatureReply =
     68             debuggeeWrapper.vmMirror.performCommand(classesBySignatureCommand);
     69         classesBySignatureCommand = null;
     70         checkReplyPacket(classesBySignatureReply, "VirtualMachine::ClassesBySignature command");
     71 
     72         classesBySignatureReply.getNextValueAsInt();
     73         // Number of returned reference types - is NOt used here
     74 
     75         classesBySignatureReply.getNextValueAsByte();
     76         // refTypeTag of class - is NOt used here
     77 
     78         long refTypeID = classesBySignatureReply.getNextValueAsReferenceTypeID();
     79         classesBySignatureReply = null;
     80 
     81         logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
     82         logWriter.println("=> referenceTypeID for Debuggee class = " + refTypeID);
     83 
     84         String checkedFieldNames[] = {
     85             "SetValues002DebuggeeObject",
     86             "objectField",
     87         };
     88         long checkedFieldIDs[] = checkFields(refTypeID, checkedFieldNames);
     89         int checkedFieldsNumber = checkedFieldNames.length;
     90 
     91         logWriter.println
     92             ("=> Send ReferenceType::GetValues command and get ObjectID for value to set...");
     93 
     94         CommandPacket getValuesCommand = new CommandPacket(
     95                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
     96                 JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
     97         getValuesCommand.setNextValueAsReferenceTypeID(refTypeID);
     98         getValuesCommand.setNextValueAsInt(1);
     99         getValuesCommand.setNextValueAsFieldID(checkedFieldIDs[0]);
    100         ReplyPacket getValuesReply =
    101             debuggeeWrapper.vmMirror.performCommand(getValuesCommand);
    102         getValuesCommand = null;
    103         checkReplyPacket(getValuesReply, "ReferenceType::GetValues command");
    104 
    105         int returnedValuesNumber = getValuesReply.getNextValueAsInt();
    106         logWriter.println("=> Returned values number = " + returnedValuesNumber);
    107         assertEquals("ReferenceType::GetValues returned invalid values number,",
    108                 1, returnedValuesNumber);
    109 
    110         Value objectFieldValueToSet = getValuesReply.getNextValueAsValue();
    111         byte objectFieldValueToSetTag = objectFieldValueToSet.getTag();
    112         logWriter.println
    113             ("=> Returned field value tag for checked object= " + objectFieldValueToSetTag
    114             + "(" + JDWPConstants.Tag.getName(objectFieldValueToSetTag) + ")");
    115         assertEquals("ReferenceType::GetValues returned invalid value tag,",
    116                 JDWPConstants.Tag.OBJECT_TAG, objectFieldValueToSetTag,
    117                 JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG),
    118                 JDWPConstants.Tag.getName(objectFieldValueToSetTag));
    119 
    120         long objectFieldID = objectFieldValueToSet.getLongValue();
    121         logWriter.println("=> Returned ObjectID = " + objectFieldID);
    122         logWriter.println
    123             ("=> CHECK: send " + thisCommandName
    124             + " for Debuggee class with value which has other referenceType than field to set...");
    125 
    126         CommandPacket checkedCommand = new CommandPacket(
    127                 JDWPCommands.ClassTypeCommandSet.CommandSetID,
    128                 JDWPCommands.ClassTypeCommandSet.SetValuesCommand);
    129         checkedCommand.setNextValueAsClassID(refTypeID);
    130         checkedCommand.setNextValueAsInt(checkedFieldsNumber-1);
    131         int fieldIndex = 1;
    132         for (; fieldIndex < checkedFieldsNumber; fieldIndex++) {
    133             checkedCommand.setNextValueAsFieldID(checkedFieldIDs[fieldIndex]);
    134             switch ( fieldIndex ) {
    135             case 1: // objectField
    136                 checkedCommand.setNextValueAsObjectID(objectFieldID);
    137                 break;
    138             }
    139         }
    140         ReplyPacket checkedReply =
    141             debuggeeWrapper.vmMirror.performCommand(checkedCommand);
    142         checkedCommand = null;
    143 
    144         short errorCode = checkedReply.getErrorCode();
    145         if ( errorCode == JDWPConstants.Error.NONE ) {
    146             logWriter.println("=> " +  thisCommandName
    147                     + " run without any ERROR!");
    148         } else {
    149             logWriter.println("=> " +  thisCommandName
    150                     + " returns ERROR = " + errorCode
    151                     + "(" + JDWPConstants.Error.getName(errorCode) + ")");
    152         }
    153 
    154         logWriter.println("=> Wait for Debuggee's status about check for set field...");
    155         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    156         boolean debuggeeStatus = synchronizer.receiveMessage("PASSED");
    157         if ( ! debuggeeStatus ) {
    158             logWriter.println("## " + thisTestName + ": Debuggee returned status FAILED");
    159             fail("Debuggee returned status FAILED");
    160         } else {
    161             logWriter.println("=> " + thisTestName + ": Debuggee returned status PASSED");
    162         }
    163 
    164         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": OK");
    165     }
    166 }
    167