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 Anton V. Karnachuk
021: * @version $Revision: 1.4 $
022: */
023:
024: /**
025: * Created on 11.04.2005
026: */package org.apache.harmony.jpda.tests.jdwp.Events;
027:
028: import org.apache.harmony.jpda.tests.framework.jdwp.EventPacket;
029: import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
030: import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
031: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
032: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
033: import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
034: import org.apache.harmony.jpda.tests.framework.jdwp.TaggedObject;
035: import org.apache.harmony.jpda.tests.framework.jdwp.Location;
036: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
037:
038: /**
039: * JDWP Unit test for caught EXCEPTION event.
040: */
041: public class ExceptionTest extends JDWPEventTestCase {
042:
043: public static void main(String[] args) {
044: junit.textui.TestRunner.run(ExceptionTest.class);
045: }
046:
047: protected String getDebuggeeClassName() {
048: return ExceptionDebuggee.class.getName();
049: }
050:
051: /**
052: * This testcase is for caught EXCEPTION event.
053: * <BR>It runs ExceptionDebuggee that throws caught DebuggeeException.
054: * The test verifies that requested EXCEPTION event occurs and reported exception
055: * object is not null and is instance of expected class with expected tag.
056: */
057: public void testExceptionEvent() {
058: logWriter.println(">> testExceptionEvent: STARTED...");
059:
060: synchronizer
061: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
062:
063: String exceptionSignature = "Lorg/apache/harmony/jpda/tests/jdwp/Events/DebuggeeException;";
064: boolean isCatch = true;
065: boolean isUncatch = true;
066: logWriter
067: .println("\n>> testExceptionEvent: => setException(...)...");
068: debuggeeWrapper.vmMirror.setException(exceptionSignature,
069: isCatch, isUncatch);
070: logWriter
071: .println(">> testExceptionEvent: setException(...) DONE");
072:
073: logWriter
074: .println("\n>> testExceptionEvent: send to Debuggee SGNL_CONTINUE...");
075: synchronizer
076: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
077:
078: logWriter
079: .println("\n>> testExceptionEvent: => receiveEvent()...");
080: EventPacket event = debuggeeWrapper.vmMirror.receiveEvent();
081: logWriter
082: .println(">> testExceptionEvent: Event is received! Check it ...");
083: ParsedEvent[] parsedEvents = ParsedEvent
084: .parseEventPacket(event);
085:
086: // assert that event is the expected one
087: logWriter
088: .println(">> testExceptionEvent: parsedEvents.length = "
089: + parsedEvents.length);
090: logWriter
091: .println(">> testExceptionEvent: parsedEvents[0].getEventKind() = "
092: + parsedEvents[0].getEventKind());
093: assertEquals("Invalid number of events,", 1,
094: parsedEvents.length);
095: assertEquals("Invalid event kind,",
096: JDWPConstants.EventKind.EXCEPTION, parsedEvents[0]
097: .getEventKind(), JDWPConstants.EventKind
098: .getName(JDWPConstants.EventKind.EXCEPTION),
099: JDWPConstants.EventKind.getName(parsedEvents[0]
100: .getEventKind()));
101: TaggedObject returnedException = ((ParsedEvent.Event_EXCEPTION) parsedEvents[0])
102: .getException();
103:
104: // assert that exception ObjectID is not null
105: logWriter
106: .println(">> testExceptionEvent: returnedException.objectID = "
107: + returnedException.objectID);
108: assertTrue("Returned exception object is null.",
109: returnedException.objectID != 0);
110:
111: // assert that exception tag is OBJECT
112: logWriter
113: .println(">> testExceptionEvent: returnedException.tag = "
114: + returnedException.objectID);
115: assertEquals("Returned exception tag is not OBJECT.",
116: JDWPConstants.Tag.OBJECT_TAG, returnedException.tag);
117:
118: // assert that exception class is the expected one
119: long typeID = getObjectReferenceType(returnedException.objectID);
120: String returnedExceptionSignature = getClassSignature(typeID);
121: logWriter
122: .println(">> testExceptionEvent: returnedExceptionSignature = |"
123: + returnedExceptionSignature + "|");
124: assertString("Invalid signature of returned exception,",
125: exceptionSignature, returnedExceptionSignature);
126:
127: // resume debuggee
128: logWriter
129: .println("\n>> testExceptionEvent: resume debuggee...");
130: debuggeeWrapper.vmMirror.resume();
131: }
132:
133: /**
134: * This testcase is for caught EXCEPTION event and reported location.
135: * <BR>It runs ExceptionDebuggee that throws caught DebuggeeException.
136: * Tte test verifies that requested EXCEPTION event occurs, thread is not null,
137: * reported location is not null and is equal to location of the top stack frame.
138: */
139: public void testExceptionEventLocation() {
140: logWriter.println(">> testExceptionEventLocation: STARTED...");
141:
142: synchronizer
143: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
144:
145: String exceptionSignature = "Lorg/apache/harmony/jpda/tests/jdwp/Events/DebuggeeException;";
146: boolean isCatch = true;
147: boolean isUncatch = true;
148: logWriter
149: .println("\n>> testExceptionEventLocation: => setException(...)...");
150: debuggeeWrapper.vmMirror.setException(exceptionSignature,
151: isCatch, isUncatch);
152: logWriter
153: .println(">> testExceptionEventLocation: setException(...) DONE");
154:
155: logWriter
156: .println("\n>> testExceptionEventLocation: send to Debuggee SGNL_CONTINUE...");
157: synchronizer
158: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
159:
160: logWriter
161: .println("\n>> testExceptionEventLocation: => receiveEvent()...");
162: EventPacket event = debuggeeWrapper.vmMirror.receiveEvent();
163: logWriter
164: .println(">> testExceptionEventLocation: Event is received! Check it ...");
165: ParsedEvent[] parsedEvents = ParsedEvent
166: .parseEventPacket(event);
167:
168: // assert that event is the expected one
169: logWriter
170: .println(">> testExceptionEventLocation: parsedEvents.length = "
171: + parsedEvents.length);
172: logWriter
173: .println(">> testExceptionEventLocation: parsedEvents[0].getEventKind() = "
174: + parsedEvents[0].getEventKind());
175: assertEquals("Invalid number of events,", 1,
176: parsedEvents.length);
177: assertEquals("Invalid event kind,",
178: JDWPConstants.EventKind.EXCEPTION, parsedEvents[0]
179: .getEventKind(), JDWPConstants.EventKind
180: .getName(JDWPConstants.EventKind.EXCEPTION),
181: JDWPConstants.EventKind.getName(parsedEvents[0]
182: .getEventKind()));
183: long returnedThread = ((ParsedEvent.Event_EXCEPTION) parsedEvents[0])
184: .getThreadID();
185: Location returnedExceptionLoc = ((ParsedEvent.Event_EXCEPTION) parsedEvents[0])
186: .getLocation();
187:
188: // assert that exception thread is not null
189: logWriter
190: .println(">> testExceptionEventLocation: returnedThread = "
191: + returnedThread);
192: assertTrue("Returned exception ThreadID is null,",
193: returnedThread != 0);
194:
195: // assert that exception location is not null
196: logWriter
197: .println(">> testExceptionEventLocation: returnedExceptionLoc = "
198: + returnedExceptionLoc);
199: assertTrue("Returned exception location is null,",
200: returnedExceptionLoc != null);
201:
202: // assert that top stack frame location is not null
203: Location topFrameLoc = getTopFrameLocation(returnedThread);
204: logWriter
205: .println(">> testExceptionEventLocation: topFrameLoc = "
206: + topFrameLoc);
207: assertTrue("Returned top stack frame location is null,",
208: topFrameLoc != null);
209:
210: // assert that locations of exception and top frame are equal
211: assertEquals("Different exception and top frame location tag,",
212: returnedExceptionLoc.tag, topFrameLoc.tag);
213: assertEquals(
214: "Different exception and top frame location class,",
215: returnedExceptionLoc.classID, topFrameLoc.classID);
216: assertEquals(
217: "Different exception and top frame location method,",
218: returnedExceptionLoc.methodID, topFrameLoc.methodID);
219: assertEquals(
220: "Different exception and top frame location index,",
221: returnedExceptionLoc.index, topFrameLoc.index);
222:
223: // resume debuggee
224: logWriter
225: .println("\n>> testExceptionEventLocation: resume debuggee...");
226: debuggeeWrapper.vmMirror.resume();
227: }
228:
229: @SuppressWarnings("unused")
230: private Location getTopFrameLocation(long threadID) {
231:
232: // getting frames of the thread
233: CommandPacket packet = new CommandPacket(
234: JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
235: JDWPCommands.ThreadReferenceCommandSet.FramesCommand);
236: packet.setNextValueAsThreadID(threadID);
237: packet.setNextValueAsInt(0);
238: packet.setNextValueAsInt(1);
239: ReplyPacket reply = debuggeeWrapper.vmMirror
240: .performCommand(packet);
241: debuggeeWrapper.vmMirror.checkReply(reply);
242:
243: // assert that only one top frame is returned
244: int framesCount = reply.getNextValueAsInt();
245: assertEquals("Invalid number of top stack frames,", 1,
246: framesCount);
247:
248: long frameID = reply.getNextValueAsFrameID();
249: Location loc = reply.getNextValueAsLocation();
250:
251: return loc;
252: }
253:
254: }
|