VMDeathTest.java revision 5f0a23683aa603d8c50b6dd071a565821b76067b
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
27package org.apache.harmony.jpda.tests.jdwp.MultiSession;
28
29import org.apache.harmony.jpda.tests.framework.TestOptions;
30import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
31import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
32import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
33import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
34import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
35import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
36import org.apache.harmony.jpda.tests.jdwp.share.JDWPUnitDebuggeeWrapper;
37import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
38
39
40/**
41 * JDWP Unit test for verifying canceling of requested VM_DEATH event after re-connection.
42 */
43public class VMDeathTest extends JDWPSyncTestCase {
44
45    int requestID = 0;
46
47    static final String DEBUGGEE_CLASS_NAME = "org.apache.harmony.jpda.tests.jdwp.Events.EventDebuggee";
48
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        int autoEvents = 0;
111        int wrongEvents = 0;
112        for (int i = 0; i < eventsCount; i++) {
113            ParsedEvent event = parsedEvents[i];
114            logWriter.println("=> Event #" + i + ";");
115
116            // print event info
117            byte eventSuspendPolicy = event.getSuspendPolicy();
118            logWriter.println("=> SuspendPolicy=" + eventSuspendPolicy + "/"
119                    + JDWPConstants.SuspendPolicy.getName(eventSuspendPolicy));
120            byte eventKind = event.getEventKind();
121            logWriter.println("=> EventKind=" + eventKind + "/"
122                    + JDWPConstants.EventKind.getName(eventKind));
123            int eventRequestID = event.getRequestID();
124            logWriter.println("=> RequestID=" + eventRequestID);
125
126            // check if event is expected
127            if (eventKind == JDWPConstants.EventKind.VM_DEATH) {
128                if (parsedEvents[i].getRequestID() == 0) {
129                    autoEvents++;
130                    logWriter.println("=> found auto VM_DEATH event!");
131                    // for automatical event suspend policy can be changed
132                } else {
133                    logWriter.println("## FAILURE: VM_DEATH event "
134                            + "with unexpected RequestID: " + eventRequestID);
135                    result = 1;
136                }
137            } else {
138                wrongEvents++;
139                logWriter.println("## FAILURE: unexpected event kind: "
140                        + eventKind);
141                result = 2;
142            }
143        }
144
145        if (1 == result)
146            fail("VM_DEATH event with unexpected RequestID");
147        else if (2 == result)
148            fail("Unexpected event kind");
149
150        logWriter.println("==> test PASSED!");
151    }
152
153    protected void beforeDebuggeeStart(JDWPUnitDebuggeeWrapper debuggeeWrapper) {
154        settings.setAttachConnectorKind();
155        if (settings.getTransportAddress() == null) {
156            settings.setTransportAddress(TestOptions.DEFAULT_ATTACHING_ADDRESS);
157        }
158        logWriter.println("ATTACH connector kind");
159        super.beforeDebuggeeStart(debuggeeWrapper);
160    }
161}
162