Home | History | Annotate | Download | only in Events
      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 Anton V. Karnachuk
     21  */
     22 
     23 /**
     24  * Created on 11.04.2005
     25  */
     26 package org.apache.harmony.jpda.tests.jdwp.Events;
     27 
     28 import org.apache.harmony.jpda.tests.framework.jdwp.EventPacket;
     29 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
     30 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
     31 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
     32 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
     33 import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
     34 import org.apache.harmony.jpda.tests.framework.jdwp.TaggedObject;
     35 import org.apache.harmony.jpda.tests.framework.jdwp.Location;
     36 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
     37 
     38 
     39 
     40 /**
     41  * JDWP Unit test for caught EXCEPTION event.
     42  */
     43 public class ExceptionTest extends JDWPEventTestCase {
     44     protected String getDebuggeeClassName() {
     45         return ExceptionDebuggee.class.getName();
     46     }
     47 
     48     /**
     49      * This testcase is for caught EXCEPTION event.
     50      * <BR>It runs ExceptionDebuggee that throws caught DebuggeeException.
     51      * The test verifies that requested EXCEPTION event occurs and reported exception
     52      * object is not null and is instance of expected class with expected tag.
     53      */
     54     public void testExceptionEvent() {
     55         logWriter.println(">> testExceptionEvent: STARTED...");
     56 
     57         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     58 
     59         String exceptionSignature = "Lorg/apache/harmony/jpda/tests/jdwp/Events/DebuggeeException;";
     60         boolean isCatch = true;
     61         boolean isUncatch = true;
     62         logWriter.println("\n>> testExceptionEvent: => setException(...)...");
     63         debuggeeWrapper.vmMirror.setException(exceptionSignature, isCatch, isUncatch);
     64         logWriter.println(">> testExceptionEvent: setException(...) DONE");
     65 
     66         logWriter.println("\n>> testExceptionEvent: send to Debuggee SGNL_CONTINUE...");
     67         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
     68 
     69         logWriter.println("\n>> testExceptionEvent: => receiveEvent()...");
     70         EventPacket event = debuggeeWrapper.vmMirror.receiveEvent();
     71         logWriter.println(">> testExceptionEvent: Event is received! Check it ...");
     72         ParsedEvent[] parsedEvents = ParsedEvent.parseEventPacket(event);
     73 
     74         // assert that event is the expected one
     75         logWriter.println(">> testExceptionEvent: parsedEvents.length = " + parsedEvents.length);
     76         logWriter.println(">> testExceptionEvent: parsedEvents[0].getEventKind() = " + parsedEvents[0].getEventKind());
     77         assertEquals("Invalid number of events,", 1, parsedEvents.length);
     78         assertEquals("Invalid event kind,",
     79                 JDWPConstants.EventKind.EXCEPTION,
     80                 parsedEvents[0].getEventKind(),
     81                 JDWPConstants.EventKind.getName(JDWPConstants.EventKind.EXCEPTION),
     82                 JDWPConstants.EventKind.getName(parsedEvents[0].getEventKind()));
     83         TaggedObject returnedException =((ParsedEvent.Event_EXCEPTION)parsedEvents[0]).getException();
     84 
     85         // assert that exception ObjectID is not null
     86         logWriter.println(">> testExceptionEvent: returnedException.objectID = " + returnedException.objectID);
     87         assertTrue("Returned exception object is null.", returnedException.objectID != 0);
     88 
     89         // assert that exception tag is OBJECT
     90         logWriter.println(">> testExceptionEvent: returnedException.tag = " + returnedException.objectID);
     91         assertEquals("Returned exception tag is not OBJECT.", JDWPConstants.Tag.OBJECT_TAG, returnedException.tag);
     92 
     93         // assert that exception class is the expected one
     94         long typeID = getObjectReferenceType(returnedException.objectID);
     95         String returnedExceptionSignature = getClassSignature(typeID);
     96         logWriter.println(">> testExceptionEvent: returnedExceptionSignature = |" + returnedExceptionSignature+"|");
     97         assertString("Invalid signature of returned exception,", exceptionSignature, returnedExceptionSignature);
     98 
     99         // resume debuggee
    100         logWriter.println("\n>> testExceptionEvent: resume debuggee...");
    101         debuggeeWrapper.vmMirror.resume();
    102     }
    103 
    104     /**
    105      * This testcase is for caught EXCEPTION event and reported location.
    106      * <BR>It runs ExceptionDebuggee that throws caught DebuggeeException.
    107      * Tte test verifies that requested EXCEPTION event occurs, thread is not null,
    108      * reported location is not null and is equal to location of the top stack frame.
    109      */
    110     public void testExceptionEventLocation() {
    111         logWriter.println(">> testExceptionEventLocation: STARTED...");
    112 
    113         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
    114 
    115         String exceptionSignature = "Lorg/apache/harmony/jpda/tests/jdwp/Events/DebuggeeException;";
    116         boolean isCatch = true;
    117         boolean isUncatch = true;
    118         logWriter.println("\n>> testExceptionEventLocation: => setException(...)...");
    119         debuggeeWrapper.vmMirror.setException(exceptionSignature, isCatch, isUncatch);
    120         logWriter.println(">> testExceptionEventLocation: setException(...) DONE");
    121 
    122         logWriter.println("\n>> testExceptionEventLocation: send to Debuggee SGNL_CONTINUE...");
    123         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    124 
    125         logWriter.println("\n>> testExceptionEventLocation: => receiveEvent()...");
    126         EventPacket event = debuggeeWrapper.vmMirror.receiveEvent();
    127         logWriter.println(">> testExceptionEventLocation: Event is received! Check it ...");
    128         ParsedEvent[] parsedEvents = ParsedEvent.parseEventPacket(event);
    129 
    130         // assert that event is the expected one
    131         logWriter.println(">> testExceptionEventLocation: parsedEvents.length = " + parsedEvents.length);
    132         logWriter.println(">> testExceptionEventLocation: parsedEvents[0].getEventKind() = " + parsedEvents[0].getEventKind());
    133         assertEquals("Invalid number of events,", 1, parsedEvents.length);
    134         assertEquals("Invalid event kind,",
    135                 JDWPConstants.EventKind.EXCEPTION,
    136                 parsedEvents[0].getEventKind(),
    137                 JDWPConstants.EventKind.getName(JDWPConstants.EventKind.EXCEPTION),
    138                 JDWPConstants.EventKind.getName(parsedEvents[0].getEventKind()));
    139         long returnedThread = ((ParsedEvent.Event_EXCEPTION)parsedEvents[0]).getThreadID();
    140         Location returnedExceptionLoc =((ParsedEvent.Event_EXCEPTION)parsedEvents[0]).getLocation();
    141 
    142         // assert that exception thread is not null
    143         logWriter.println(">> testExceptionEventLocation: returnedThread = " + returnedThread);
    144         assertTrue("Returned exception ThreadID is null,", returnedThread != 0);
    145 
    146         // assert that exception location is not null
    147         logWriter.println(">> testExceptionEventLocation: returnedExceptionLoc = " + returnedExceptionLoc);
    148         assertTrue("Returned exception location is null,", returnedExceptionLoc != null);
    149 
    150         // assert that top stack frame location is not null
    151         Location topFrameLoc = getTopFrameLocation(returnedThread);
    152         logWriter.println(">> testExceptionEventLocation: topFrameLoc = " + topFrameLoc);
    153         assertTrue("Returned top stack frame location is null,", topFrameLoc != null);
    154 
    155         // assert that locations of exception and top frame are equal
    156         assertEquals("Different exception and top frame location tag,", returnedExceptionLoc.tag, topFrameLoc.tag);
    157         assertEquals("Different exception and top frame location class,", returnedExceptionLoc.classID, topFrameLoc.classID);
    158         assertEquals("Different exception and top frame location method,", returnedExceptionLoc.methodID, topFrameLoc.methodID);
    159         assertEquals("Different exception and top frame location index,", returnedExceptionLoc.index, topFrameLoc.index);
    160 
    161         // resume debuggee
    162         logWriter.println("\n>> testExceptionEventLocation: resume debuggee...");
    163         debuggeeWrapper.vmMirror.resume();
    164     }
    165 
    166     @SuppressWarnings("unused")
    167     private Location getTopFrameLocation(long threadID) {
    168 
    169         // getting frames of the thread
    170         CommandPacket packet = new CommandPacket(
    171                 JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
    172                 JDWPCommands.ThreadReferenceCommandSet.FramesCommand);
    173         packet.setNextValueAsThreadID(threadID);
    174         packet.setNextValueAsInt(0);
    175         packet.setNextValueAsInt(1);
    176         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
    177         debuggeeWrapper.vmMirror.checkReply(reply);
    178 
    179         // assert that only one top frame is returned
    180         int framesCount = reply.getNextValueAsInt();
    181         assertEquals("Invalid number of top stack frames,", 1, framesCount);
    182 
    183         long frameID = reply.getNextValueAsFrameID();
    184         Location loc = reply.getNextValueAsLocation();
    185 
    186         return loc;
    187     }
    188 
    189 }
    190