Home | History | Annotate | Download | only in MultiSession
      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 Aleksander V. Budniy
     21  */
     22 
     23 /**
     24  * Created on 8.7.2005
     25  */
     26 
     27 package org.apache.harmony.jpda.tests.jdwp.MultiSession;
     28 
     29 import org.apache.harmony.jpda.tests.framework.TestOptions;
     30 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
     31 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
     32 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
     33 import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
     34 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
     35 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
     36 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
     37 
     38 
     39 /**
     40  * JDWP Unit test for verifying canceling of requested VM_DEATH event after re-connection.
     41  */
     42 public class VMDeathTest extends JDWPSyncTestCase {
     43 
     44     int requestID = 0;
     45 
     46     static final String DEBUGGEE_CLASS_NAME = "org.apache.harmony.jpda.tests.jdwp.Events.EventDebuggee";
     47 
     48     @Override
     49     protected String getDebuggeeClassName() {
     50         return DEBUGGEE_CLASS_NAME;
     51     }
     52 
     53     /**
     54      * This testcase verifies canceling of requested VM_DEATH event after re-connection.
     55      * <BR>It runs EventDebuggee, sets request for VM_DEATH event
     56      * and re-connects.
     57      * <BR>It is expected that only auto VM_DEATH event occurs after re-connection,
     58      * but no any other event, including requested VM_DEATH event.
     59      */
     60     public void testVMDeathRequest() {
     61         logWriter.println("==> testVMDeathRequest started");
     62         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     63 
     64         //set request for VM_DEATH event with suspend policy SUSPEND_ALL
     65         byte suspendPolicy = JDWPConstants.SuspendPolicy.ALL;
     66         logWriter.println("=> Create request for VM_DEATH event with suspend policy: "
     67                 + suspendPolicy
     68                 + "/"
     69                 + JDWPConstants.SuspendPolicy.getName(suspendPolicy));
     70         CommandPacket setRequestCommand = new CommandPacket(
     71                 JDWPCommands.EventRequestCommandSet.CommandSetID,
     72                 JDWPCommands.EventRequestCommandSet.SetCommand);
     73 
     74         setRequestCommand.setNextValueAsByte(JDWPConstants.EventKind.VM_DEATH);
     75         setRequestCommand.setNextValueAsByte(JDWPConstants.SuspendPolicy.ALL);
     76         setRequestCommand.setNextValueAsInt(0);
     77 
     78         ReplyPacket setRequestReply = debuggeeWrapper.vmMirror
     79                 .performCommand(setRequestCommand);
     80 
     81         checkReplyPacket(setRequestReply, "Set VM_DEATH event");
     82 
     83         requestID = setRequestReply.getNextValueAsInt();
     84         logWriter.println("=> RequestID = " + requestID);
     85 
     86         assertAllDataRead(setRequestReply);
     87 
     88         logWriter.println("");
     89         logWriter.println("=> CLOSE CONNECTION");
     90         closeConnection();
     91         logWriter.println("=> CONNECTION IS CLOSED");
     92 
     93         logWriter.println("");
     94         logWriter.println("=> OPEN NEW CONNECTION");
     95         openConnection();
     96         logWriter.println("=> CONNECTION IS OPENED");
     97 
     98         //release debuggee
     99         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    100         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    101         //receive and parse event set
    102         logWriter.println("=> Wait for event..");
    103         CommandPacket eventPacket = debuggeeWrapper.vmMirror.receiveEvent();
    104         ParsedEvent[] parsedEvents = ParsedEvent.parseEventPacket(eventPacket);
    105         int eventsCount = parsedEvents.length;
    106         logWriter.println("=> Received event set: count=" + eventsCount);
    107 
    108         //ckeck if received events are expected
    109         int result = 0;
    110         for (int i = 0; i < eventsCount; i++) {
    111             ParsedEvent event = parsedEvents[i];
    112             logWriter.println("=> Event #" + i + ";");
    113 
    114             // print event info
    115             byte eventSuspendPolicy = event.getSuspendPolicy();
    116             logWriter.println("=> SuspendPolicy=" + eventSuspendPolicy + "/"
    117                     + JDWPConstants.SuspendPolicy.getName(eventSuspendPolicy));
    118             byte eventKind = event.getEventKind();
    119             logWriter.println("=> EventKind=" + eventKind + "/"
    120                     + JDWPConstants.EventKind.getName(eventKind));
    121             int eventRequestID = event.getRequestID();
    122             logWriter.println("=> RequestID=" + eventRequestID);
    123 
    124             // check if event is expected
    125             if (eventKind == JDWPConstants.EventKind.VM_DEATH) {
    126                 if (parsedEvents[i].getRequestID() == 0) {
    127                     logWriter.println("=> found auto VM_DEATH event!");
    128                     // for automatical event suspend policy can be changed
    129                 } else {
    130                     logWriter.println("## FAILURE: VM_DEATH event "
    131                             + "with unexpected RequestID: " + eventRequestID);
    132                     result = 1;
    133                 }
    134             } else {
    135                 logWriter.println("## FAILURE: unexpected event kind: "
    136                         + eventKind);
    137                 result = 2;
    138             }
    139         }
    140 
    141         if (1 == result)
    142             fail("VM_DEATH event with unexpected RequestID");
    143         else if (2 == result)
    144             fail("Unexpected event kind");
    145 
    146         logWriter.println("==> test PASSED!");
    147     }
    148 
    149     @Override
    150     protected void beforeConnectionSetUp() {
    151         settings.setAttachConnectorKind();
    152         if (settings.getTransportAddress() == null) {
    153             settings.setTransportAddress(TestOptions.DEFAULT_ATTACHING_ADDRESS);
    154         }
    155         logWriter.println("ATTACH connector kind");
    156         super.beforeConnectionSetUp();
    157     }
    158 }
    159