001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Aleksander V. Budniy
021: * @version $Revision: 1.4 $
022: */
023:
024: /**
025: * Created on 06.04.2005
026: */package org.apache.harmony.jpda.tests.jdwp.Events;
027:
028: import org.apache.harmony.jpda.tests.framework.TestErrorException;
029: import org.apache.harmony.jpda.tests.framework.jdwp.EventPacket;
030: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
031: import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
032: import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
033: import org.apache.harmony.jpda.tests.framework.jdwp.exceptions.TimeoutException;
034: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
035:
036: /**
037: * JDWP Unit test for CLASS_UNLOAD event.
038: */
039: public class ClassUnloadTest extends JDWPEventTestCase {
040:
041: public static final String TESTED_CLASS_NAME = "org.apache.harmony.jpda.tests.jdwp.Events.ClassUnloadTestedClass";
042:
043: public static final String TESTED_CLASS_SIGNATURE = "L"
044: + TESTED_CLASS_NAME.replace('.', '/') + ";";
045:
046: protected String getDebuggeeClassName() {
047: return ClassUnloadDebuggee.class.getName();
048: }
049:
050: public static void main(String[] args) {
051: junit.textui.TestRunner.run(ClassUnloadTest.class);
052: }
053:
054: /**
055: * This testcase is for CLASS_UNLOAD event.
056: */
057: public void testClassUnloadEvent() {
058: logWriter.println("==> testClassUnloadEvent started");
059:
060: synchronizer
061: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
062:
063: ReplyPacket reply = null;
064: int foundClasses = 0;
065:
066: // commented out because it may leave JNI references to the tested class,
067: // which will prevent it from garbage collecting and unloading
068: /*
069: // check that tested class is loaded before unloading it
070: logWriter.println("=> Find tested class by signature: " + TESTED_CLASS_SIGNATURE);
071: reply = debuggeeWrapper.vmMirror.getClassBySignature(TESTED_CLASS_SIGNATURE);
072: foundClasses = reply.getNextValueAsInt();
073: logWriter.println("=> Found clases: " + foundClasses);
074:
075: if (foundClasses <= 0) {
076: fail("Tested class was not found: count=" + foundClasses);
077: }
078: */
079:
080: logWriter.println("=> Set request for ClasUnload event: "
081: + TESTED_CLASS_NAME);
082: reply = debuggeeWrapper.vmMirror
083: .setClassUnload(TESTED_CLASS_NAME);
084: checkReplyPacket(reply, "Set CLASS_UNLOAD event");
085: int requestID = reply.getNextValueAsInt();
086: logWriter
087: .println("=> Created requestID for ClassUnload event: "
088: + requestID);
089:
090: logWriter.println("=> Release debuggee");
091: synchronizer
092: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
093:
094: logWriter.println("=> Wait for class unload event");
095: EventPacket event = null;
096: try {
097: event = debuggeeWrapper.vmMirror.receiveEvent(settings
098: .getTimeout());
099: logWriter.println("=> Event received");
100: } catch (TimeoutException e) {
101: logWriter
102: .println("=> ClassUnload event was not received (class might be not really unloaded)");
103: } catch (Exception e) {
104: logWriter
105: .println("=> Exception during receiving ClassUnload event: "
106: + e);
107: throw new TestErrorException(e);
108: }
109:
110: logWriter.println("=> Clear request for ClassUnload event");
111: reply = debuggeeWrapper.vmMirror.clearEvent(
112: JDWPConstants.EventKind.CLASS_UNLOAD, requestID);
113:
114: logWriter.println("=> Try to find tested class by signature: "
115: + TESTED_CLASS_SIGNATURE);
116: reply = debuggeeWrapper.vmMirror
117: .getClassBySignature(TESTED_CLASS_SIGNATURE);
118: foundClasses = reply.getNextValueAsInt();
119: logWriter.println("=> Found clases: " + foundClasses);
120:
121: logWriter
122: .println("=> Wait for class status message from debuggee");
123: String status = synchronizer.receiveMessage();
124: logWriter.println("=> Debuggee reported class status: "
125: + status);
126:
127: if (event != null) {
128: // check event data
129: ParsedEvent[] parsedEvents = ParsedEvent
130: .parseEventPacket(event);
131:
132: assertEquals("Invalid number of events,", 1,
133: parsedEvents.length);
134: assertEquals(
135: "Invalid event kind,",
136: JDWPConstants.EventKind.CLASS_UNLOAD,
137: parsedEvents[0].getEventKind(),
138: JDWPConstants.EventKind
139: .getName(JDWPConstants.EventKind.CLASS_UNLOAD),
140: JDWPConstants.EventKind.getName(parsedEvents[0]
141: .getEventKind()));
142: assertEquals("Invalid event request,", requestID,
143: parsedEvents[0].getRequestID());
144:
145: // check that unloaded class was not found after event
146: if (foundClasses > 0) {
147: fail("Tested class was found after ClasUnload event: count="
148: + foundClasses);
149: }
150:
151: logWriter.println("=> Resume debuggee on event");
152: debuggeeWrapper.resume();
153: } else {
154: // check if tested class not found without event
155: if (foundClasses <= 0) {
156: fail("No ClassUnload event, but tested class not found: count="
157: + foundClasses);
158: }
159:
160: // check if debuggee reported tested class unloaded without event
161: if ("UNLOADED".equals(status)) {
162: fail("No ClassUnload event, but tested class was unloaded");
163: }
164: }
165:
166: logWriter.println("=> Release debuggee");
167: synchronizer
168: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
169: logWriter.println("==> testClassUnloadEvent ended");
170: }
171: }
|