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 package org.apache.harmony.jpda.tests.jdwp.ClassType;
     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.TaggedObject;
     26 import org.apache.harmony.jpda.tests.framework.jdwp.Value;
     27 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
     28 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
     29 
     30 
     31 /**
     32  * JDWP unit test for ClassType.InvokeMethod command.
     33  */
     34 public class InvokeMethod003Test extends JDWPSyncTestCase {
     35     @Override
     36     protected String getDebuggeeClassName() {
     37         return "org.apache.harmony.jpda.tests.jdwp.ClassType.InvokeMethod003Debuggee";
     38     }
     39 
     40     /**
     41      * This testcase exercises ClassType.InvokeMethod command.
     42      * <BR>The test first starts the debuggee, request METHOD_ENTRY event so the
     43      * application suspends on first invoke.
     44      * <BR>Then sends ClassType.InvokeMethod command for method with null
     45      * argument. Checks that returned value is expected int value and returned
     46      * exception object is null.
     47      * <BR>Finally resume the application.
     48      */
     49     public void testInvokeMethod_null_argument() {
     50         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     51 
     52         // Get debuggee class ID.
     53         String debuggeeClassSig = "Lorg/apache/harmony/jpda/tests/jdwp/ClassType/InvokeMethod003Debuggee;";
     54         long debuggeeTypeID = debuggeeWrapper.vmMirror.getClassID(debuggeeClassSig);
     55         assertTrue("Failed to find debuggee class", debuggeeTypeID != 0);
     56 
     57         // Set METHOD_ENTRY event request so application is suspended.
     58         CommandPacket packet = new CommandPacket(
     59                 JDWPCommands.EventRequestCommandSet.CommandSetID,
     60                 JDWPCommands.EventRequestCommandSet.SetCommand);
     61         packet.setNextValueAsByte(JDWPConstants.EventKind.METHOD_ENTRY);
     62         packet.setNextValueAsByte(JDWPConstants.SuspendPolicy.ALL);
     63         packet.setNextValueAsInt(1);
     64         packet.setNextValueAsByte((byte) 4);  // class-only modifier.
     65         packet.setNextValueAsReferenceTypeID(debuggeeTypeID);
     66         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
     67         checkReplyPacket(reply, "EventRequest::Set command");
     68 
     69         int requestID = reply.getNextValueAsInt();
     70         logWriter.println(" EventRequest.Set: requestID=" + requestID);
     71         assertAllDataRead(reply);
     72         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
     73 
     74         long targetThreadID = 0;
     75         // Wait for METHOD_ENTRY event and collect event thread.
     76         CommandPacket event = debuggeeWrapper.vmMirror.receiveEvent();
     77         byte suspendPolicy = event.getNextValueAsByte();
     78         int events = event.getNextValueAsInt();
     79         logWriter.println(" EVENT_THREAD event: suspendPolicy=" + suspendPolicy + " events=" + events);
     80         for (int i = 0; i < events; i++) {
     81             byte eventKind = event.getNextValueAsByte();
     82             int newRequestID = event.getNextValueAsInt();
     83             long threadID = event.getNextValueAsThreadID();
     84             //Location location =
     85                 event.getNextValueAsLocation();
     86             logWriter.println("  EVENT_THREAD event " + i + ": eventKind="
     87                     + eventKind + " requestID=" + newRequestID + " threadID="
     88                     + threadID);
     89             if (newRequestID == requestID) {
     90                 targetThreadID = threadID;
     91             }
     92         }
     93         assertAllDataRead(event);
     94         assertTrue("Invalid targetThreadID, must be != 0", targetThreadID != 0);
     95 
     96         //  Now we're suspended, clear event request.
     97         packet = new CommandPacket(
     98                 JDWPCommands.EventRequestCommandSet.CommandSetID,
     99                 JDWPCommands.EventRequestCommandSet.ClearCommand);
    100         packet.setNextValueAsByte(JDWPConstants.EventKind.METHOD_ENTRY);
    101         packet.setNextValueAsInt(requestID);
    102         reply = debuggeeWrapper.vmMirror.performCommand(packet);
    103         checkReplyPacket(reply, "EventRequest::Clear command");
    104         assertAllDataRead(reply);
    105 
    106         // Get test method ID.
    107         long targetMethodID = debuggeeWrapper.vmMirror.getMethodID(debuggeeTypeID, "testMethod");
    108         assertTrue("Failed to find method", targetMethodID != 0);
    109 
    110         // Invoke test method with null argument.
    111         Value nullObjectValue = Value.createObjectValue(JDWPConstants.Tag.OBJECT_TAG, 0);
    112         packet = new CommandPacket(
    113                 JDWPCommands.ClassTypeCommandSet.CommandSetID,
    114                 JDWPCommands.ClassTypeCommandSet.InvokeMethodCommand);
    115         packet.setNextValueAsClassID(debuggeeTypeID);
    116         packet.setNextValueAsThreadID(targetThreadID);
    117         packet.setNextValueAsMethodID(targetMethodID);
    118         packet.setNextValueAsInt(1);
    119         packet.setNextValueAsValue(nullObjectValue);
    120         packet.setNextValueAsInt(0);
    121         logWriter.println(" Send ClassType.InvokeMethod without Exception");
    122         reply = debuggeeWrapper.vmMirror.performCommand(packet);
    123         checkReplyPacket(reply, "ClassType::InvokeMethod command");
    124 
    125         Value returnValue = reply.getNextValueAsValue();
    126         assertNotNull("Returned value is null", returnValue);
    127         assertEquals("Invalid returned value,", 123, returnValue.getIntValue());
    128         logWriter.println(" ClassType.InvokeMethod: returnValue.getIntValue()="
    129                 + returnValue.getIntValue());
    130 
    131         TaggedObject exception = reply.getNextValueAsTaggedObject();
    132         assertNotNull("Returned exception is null", exception);
    133         assertTrue("Invalid exception object ID:<" + exception.objectID + ">", exception.objectID == 0);
    134         assertEquals("Invalid exception tag,", JDWPConstants.Tag.OBJECT_TAG, exception.tag
    135                 , JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG)
    136                 , JDWPConstants.Tag.getName(exception.tag));
    137         logWriter.println(" ClassType.InvokeMethod: exception.tag="
    138                 + exception.tag + " exception.objectID=" + exception.objectID);
    139         assertAllDataRead(reply);
    140 
    141         //  Let's resume application
    142         debuggeeWrapper.vmMirror.resume();
    143 
    144         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    145     }
    146 }
    147