Home | History | Annotate | Download | only in ThreadReference
      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.ThreadReference;
     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.ReplyPacket;
     24 import org.apache.harmony.jpda.tests.framework.jdwp.Value;
     25 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants.Tag;
     26 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
     27 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
     28 
     29 public class ForceEarlyReturn006Test extends JDWPSyncTestCase {
     30 
     31     static final String thisCommandName = "ThreadReference.ForceEarlyReturn command ";
     32 
     33     static final String testObjSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ThreadReference/TestObject;";
     34 
     35     @Override
     36     protected String getDebuggeeClassName() {
     37         return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.ForceEarlyReturnDebuggee";
     38     }
     39 
     40     // Get the objectID of test thread
     41     private long getObjectID() {
     42         // Compose Instances command to get tested thread objectID
     43         CommandPacket InstancesCommand = new CommandPacket(
     44                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
     45                 JDWPCommands.ReferenceTypeCommandSet.InstancesCommand);
     46 
     47         long testThreadTypeID = getClassIDBySignature(testObjSignature);
     48         InstancesCommand.setNextValueAsReferenceTypeID(testThreadTypeID);
     49         InstancesCommand.setNextValueAsInt(1);
     50 
     51         ReplyPacket checkedReply = debuggeeWrapper.vmMirror
     52                 .performCommand(InstancesCommand);
     53         InstancesCommand = null;
     54 
     55         // Get the number of instances that returned.
     56         int objNum = checkedReply.getNextValueAsInt();
     57         // Get the tagged-objectID
     58         byte tag = checkedReply.getNextValueAsByte();
     59         long objectID = checkedReply.getNextValueAsObjectID();
     60         return objectID;
     61     }
     62 
     63     /**
     64      * This testcase exercises ThreadReference.ForceEarlyReturn command. <BR>
     65      * At first the test starts ForceEarlyReturnDebuggee and send it the thread
     66      * name through which to start a specific thread. Then the test performs the
     67      * ThreadReference.ForceEarlyReturn command for the tested thread and gets
     68      * the returned value of the called method. The returned value should be
     69      * equal to the value which is used in ForceEarlyReturn Command. In this
     70      * testcase, an Object value is returned.
     71      */
     72     public void testForceEarlyReturn_ReturnObject() {
     73         String thisTestName = "testForceEarlyReturn_ReturnObject";
     74         logWriter.println("==> " + thisTestName + " for " + thisCommandName
     75                 + ": START...");
     76         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     77 
     78         // ForceEarlyReturn needs canForceEarlyReturn VM capability support
     79         if (!debuggeeWrapper.vmMirror.canForceEarlyReturn()) {
     80             logWriter.println("##WARNING: this VM dosn't possess capability:canForceEarlyReturn");
     81             return;
     82         }
     83 
     84         // Get test object id
     85         long testObjID = getObjectID();
     86         logWriter.println("==> test object id is: " + testObjID);
     87 
     88         // Tell debuggee to start a thread named THREAD_OBJECT
     89         synchronizer.sendMessage(ForceEarlyReturnDebuggee.THREAD_OBJECT);
     90 
     91         // Wait until the func_Object is processing.
     92         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     93 
     94         // Getting ID of the tested thread
     95         logWriter.println("==> testedThreadName = "
     96                 + ForceEarlyReturnDebuggee.THREAD_OBJECT);
     97         logWriter.println("==> Get testedThreadID...");
     98         long testedThreadID = debuggeeWrapper.vmMirror
     99                 .getThreadID(ForceEarlyReturnDebuggee.THREAD_OBJECT);
    100         logWriter.println("==> Get testedThreadID is" + testedThreadID);
    101 
    102         // Suspend the VM before perform command
    103         logWriter.println("==> testedThreadID = " + testedThreadID);
    104         logWriter.println("==> suspend testedThread...");
    105         debuggeeWrapper.vmMirror.suspendThread(testedThreadID);
    106 
    107         // Compose the ForceEarlyReturn command
    108         CommandPacket forceEarlyReturnPacket = new CommandPacket(
    109                 JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
    110                 JDWPCommands.ThreadReferenceCommandSet.ForceEarlyReturnCommand);
    111         forceEarlyReturnPacket.setNextValueAsThreadID(testedThreadID);
    112         forceEarlyReturnPacket
    113                 .setNextValueAsValue(Value.createObjectValue(Tag.OBJECT_TAG, testObjID));
    114 
    115         // Perform the command
    116         logWriter.println("==> Perform " + thisCommandName);
    117         ReplyPacket forceEarlyReturnReply = debuggeeWrapper.vmMirror
    118                 .performCommand(forceEarlyReturnPacket);
    119         forceEarlyReturnPacket = null;
    120 
    121         checkReplyPacket(forceEarlyReturnReply,
    122                 "ThreadReference::ForceEarlyReturn command");
    123 
    124         // Resume the thread
    125         logWriter.println("==> testedThreadID = " + testedThreadID);
    126         logWriter.println("==> resume testedThread...");
    127         debuggeeWrapper.vmMirror.resumeThread(testedThreadID);
    128 
    129         String isTestObj = synchronizer.receiveMessage();
    130         // Check the early returned value
    131         if (!isTestObj.equals("TRUE")) {
    132             printErrorAndFail(thisCommandName
    133                     + "returned value is not test object set by ForceEarlyReturn command");
    134         }
    135         logWriter
    136                 .println("==> CHECK: PASSED: returned value does set by ForceEarlyReturn command.");
    137         logWriter.println("==> Returned value: " + "void");
    138 
    139         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    140     }
    141 
    142 }
    143