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
19package org.apache.harmony.jpda.tests.jdwp.Events;
20
21import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
22import org.apache.harmony.jpda.tests.framework.jdwp.EventPacket;
23import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
24import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
25import org.apache.harmony.jpda.tests.framework.jdwp.Location;
26import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
27import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
28
29/**
30 * Base class for EXCEPTION tests.
31 */
32public abstract class ExceptionBaseTest extends JDWPEventTestCase {
33
34    /**
35     * Waits for EXCEPTION event and checks it is the one we expect and the event
36     * thread's state.
37     */
38    protected ParsedEvent.Event_EXCEPTION receiveAndCheckExceptionEvent(int requestID) {
39        printTestLog("=> receiveEvent()...");
40        EventPacket event = debuggeeWrapper.vmMirror.receiveEvent();
41        printTestLog("Event is received! Check it ...");
42        ParsedEvent[] parsedEvents = ParsedEvent.parseEventPacket(event);
43
44        // assert that event is the expected one
45        printTestLog("parsedEvents.length = " + parsedEvents.length);
46        printTestLog("parsedEvents[0].getEventKind() = " + parsedEvents[0].getEventKind());
47        assertEquals("Invalid number of events,", 1, parsedEvents.length);
48        assertEquals("Invalid event kind,",
49                JDWPConstants.EventKind.EXCEPTION,
50                parsedEvents[0].getEventKind(),
51                JDWPConstants.EventKind.getName(JDWPConstants.EventKind.EXCEPTION),
52                JDWPConstants.EventKind.getName(parsedEvents[0].getEventKind()));
53        assertEquals("Invalid event request ID", requestID, parsedEvents[0].getRequestID());
54
55        ParsedEvent.Event_EXCEPTION exceptionEvent = (ParsedEvent.Event_EXCEPTION) parsedEvents[0];
56
57        long eventThreadID = exceptionEvent.getThreadID();
58        checkThreadState(eventThreadID, JDWPConstants.ThreadStatus.RUNNING,
59                JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED);
60
61        // Remove event request.
62        debuggeeWrapper.vmMirror.clearEvent(JDWPConstants.EventKind.EXCEPTION, requestID);
63
64        return exceptionEvent;
65    }
66
67    /**
68     * Returns the location of the top stack frame of a thread by sending a
69     * ThreadReference.Frames command for only one frame.
70     * @param threadID the thread ID
71     * @return the location of the top stack frame of a thread
72     */
73    protected Location getTopFrameLocation(long threadID) {
74        // getting frames of the thread
75        CommandPacket packet = new CommandPacket(
76                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
77                JDWPCommands.ThreadReferenceCommandSet.FramesCommand);
78        packet.setNextValueAsThreadID(threadID);
79        packet.setNextValueAsInt(0);
80        packet.setNextValueAsInt(1);
81        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
82        debuggeeWrapper.vmMirror.checkReply(reply);
83
84        // assert that only one top frame is returned
85        int framesCount = reply.getNextValueAsInt();
86        assertEquals("Invalid number of top stack frames,", 1, framesCount);
87
88        reply.getNextValueAsFrameID(); // frameID
89        return reply.getNextValueAsLocation();
90    }
91
92    public String dumpLocation(Location location) {
93        StringBuilder builder = new StringBuilder("{");
94        String classSig = "<null>";
95        String methodName = "<null>";
96        if (location.classID != 0 && location.methodID != 0) {
97            classSig = getClassSignature(location.classID);
98            methodName = getMethodName(location.classID, location.methodID);
99        }
100        builder.append(JDWPConstants.TypeTag.getName(location.tag));
101        builder.append(',');
102        builder.append("0x" + Long.toHexString(location.classID));
103        builder.append(" (" + classSig + "),");
104        builder.append("0x" + Long.toHexString(location.methodID));
105        builder.append(" (" + methodName + "),");
106        builder.append("0x" + Long.toHexString(location.index));
107        builder.append('}');
108        return builder.toString();
109    }
110}
111