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 package org.apache.harmony.jpda.tests.jdwp.MultiSession;
27 
28 import org.apache.harmony.jpda.tests.framework.TestOptions;
29 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
30 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
31 import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
32 import org.apache.harmony.jpda.tests.jdwp.Events.ExceptionCaughtDebuggee;
33 import org.apache.harmony.jpda.tests.jdwp.share.JDWPUnitDebuggeeWrapper;
34 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
35 
36 
37 /**
38  * JDWP Unit test for verifying canceling of EXCEPTION event after re-connection.
39  */
40 public class ExceptionTest extends JDWPEventTestCase {
41 
getDebuggeeClassName()42     protected String getDebuggeeClassName() {
43         return ExceptionCaughtDebuggee.class.getName();
44     }
45 
46     /**
47      * This testcase verifies canceling of EXCEPTION event after re-connection.
48      * <BR>It runs ExceptionDebuggee, sets request for EXCEPTION event
49      * and re-connects.
50      * <BR>It is expected that only auto VM_DEATH event occurs after re-connection,
51      * but no any other event, including EXCEPTION event.
52      */
testException001()53     public void testException001() {
54         logWriter.println(">> testException001: STARTED...");
55 
56         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
57 
58         String exceptionSignature = "Lorg/apache/harmony/jpda/tests/jdwp/Events/DebuggeeException;";
59         boolean isCatch = true;
60         boolean isUncatch = true;
61         logWriter.println("\n>> testExceptionEvent: => setException(...)...");
62         debuggeeWrapper.vmMirror.setException(exceptionSignature, isCatch,
63                 isUncatch);
64         logWriter.println(">> testExceptionEvent: setException(...) DONE");
65 
66         logWriter.println("");
67         logWriter.println("=> CLOSE CONNECTION");
68         closeConnection();
69         logWriter.println("=> CONNECTION CLOSED");
70 
71         logWriter.println("");
72         logWriter.println("=> OPEN NEW CONNECTION");
73         openConnection();
74         logWriter.println("=> CONNECTION OPENED");
75 
76         logWriter.println("=> Resuming debuggee");
77 
78         // start the thread
79         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
80         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
81 
82         // receive event
83         logWriter.println("=> Wait for event..");
84         CommandPacket eventPacket = debuggeeWrapper.vmMirror.receiveEvent();
85         ParsedEvent[] parsedEvents = ParsedEvent.parseEventPacket(eventPacket);
86         int eventsCount = parsedEvents.length;
87         logWriter.println("=> Received event set: count=" + eventsCount);
88 
89         // ckeck if received events are expected
90         int result = 0;
91         int autoEvents = 0;
92         int wrongEvents = 0;
93         for (int i = 0; i < eventsCount; i++) {
94             ParsedEvent event = parsedEvents[i];
95             logWriter.println("=> Event #" + i + ";");
96 
97             // print event info
98             byte eventSuspendPolicy = event.getSuspendPolicy();
99             logWriter.println("=> SuspendPolicy=" + eventSuspendPolicy + "/"
100                     + JDWPConstants.SuspendPolicy.getName(eventSuspendPolicy));
101             byte eventKind = event.getEventKind();
102             logWriter.println("=> EventKind=" + eventKind + "/"
103                     + JDWPConstants.EventKind.getName(eventKind));
104             int eventRequestID = event.getRequestID();
105             logWriter.println("=> RequestID=" + eventRequestID);
106 
107             // check if event is expected
108             if (eventKind == JDWPConstants.EventKind.VM_DEATH) {
109                 if (parsedEvents[i].getRequestID() == 0) {
110                     autoEvents++;
111                     logWriter.println("=> found auto VM_DEATH event!");
112                     // for automatical event suspend policy can be changed
113                 } else {
114                     logWriter.println("## FAILURE: VM_DEATH event "
115                             + "with unexpected RequestID: " + eventRequestID);
116                     result = 1;
117                 }
118             } else {
119                 wrongEvents++;
120                 logWriter.println("## FAILURE: unexpected event kind: "
121                         + eventKind);
122                 result = 2;
123             }
124         }
125 
126         if (1 == result)
127             fail("VM_DEATH event with unexpected RequestID");
128         else if (2 == result)
129             fail("Unexpected event kind");
130 
131         logWriter.println("==> testException001 PASSED!");
132     }
133 
beforeDebuggeeStart(JDWPUnitDebuggeeWrapper debuggeeWrapper)134     protected void beforeDebuggeeStart(JDWPUnitDebuggeeWrapper debuggeeWrapper) {
135         settings.setAttachConnectorKind();
136         if (settings.getTransportAddress() == null) {
137             settings.setTransportAddress(TestOptions.DEFAULT_ATTACHING_ADDRESS);
138         }
139         logWriter.println("ATTACH connector kind");
140         super.beforeDebuggeeStart(debuggeeWrapper);
141     }
142 }
143