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 14.07.2005
25 */
26
27package org.apache.harmony.jpda.tests.jdwp.Events;
28
29import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
30import org.apache.harmony.jpda.tests.framework.jdwp.EventMod;
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.share.JPDADebuggeeSynchronizer;
36
37
38
39/**
40 * JDWP Unit test for SINGLE_STEP event.
41 */
42public class SingleStepTest extends JDWPEventTestCase {
43
44    private String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/Events/SingleStepDebuggee;";
45
46    private String DEBUGGEE_CLASS_NAME = "org.apache.harmony.jpda.tests.jdwp.Events.SingleStepDebuggee";
47
48    protected String getDebuggeeClassName() {
49        return DEBUGGEE_CLASS_NAME;
50    }
51
52    /**
53     * This test case exercises SINGLE_STEP event.<BR>
54     * Runs stepFunction() function four times to checks
55     * SINGLE_STEP event with LINE and OVER steps.
56     *
57     */
58    public void testSingleStep1() {
59        stepFunction(JDWPConstants.StepSize.LINE, JDWPConstants.StepDepth.OVER);
60
61    }
62
63    /**
64     * This test case exercises SINGLE_STEP event.<BR>
65     * Runs stepFunction() function four times to checks
66     * SINGLE_STEP event with LINE and INTO steps.
67     *
68     */
69    public void testSingleStep2() {
70        stepFunction(JDWPConstants.StepSize.LINE, JDWPConstants.StepDepth.INTO);
71    }
72
73    /**
74     * This test case exercises SINGLE_STEP event.<BR>
75     * Runs stepFunction() function four times to checks
76     * SINGLE_STEP event with MIN and OVER steps.
77     *
78     */
79    public void testSingleStep3() {
80        stepFunction(JDWPConstants.StepSize.MIN, JDWPConstants.StepDepth.OVER);
81    }
82
83    /**
84     * This test case exercises SINGLE_STEP event.<BR>
85     * Runs stepFunction() function four times to checks
86     * SINGLE_STEP event with MIN and INTO steps.
87     *
88     */
89    public void testSingleStep4() {
90        stepFunction(JDWPConstants.StepSize.MIN, JDWPConstants.StepDepth.INTO);
91    }
92
93    /**
94     * Runs SingleStepDebuggee and sets breakpoint to its
95     * breakpointTest method, sends a request for single step event, then
96     * verifies that requested SINGLE_STEP event with StepSize and StepDepth
97     * steps occurs.
98     */
99
100    void stepFunction(byte StepSize, byte StepDepth) {
101        logWriter.println("=> testSingleStep started");
102
103        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
104
105        //find checked method
106        long refTypeID = getClassIDBySignature(debuggeeSignature);
107
108        logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
109        logWriter.println("=> referenceTypeID for Debuggee class = " + refTypeID);
110        logWriter.println("=> Send ReferenceType::Methods command and get methodIDs ");
111
112        long requestID = debuggeeWrapper.vmMirror.setBreakpointAtMethodBegin(
113                refTypeID, "breakpointTest");
114        logWriter.println("=> breakpointID = " + requestID);
115        logWriter.println("=> starting thread");
116
117        //execute the breakpoint
118        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
119
120        long breakpointThreadID = debuggeeWrapper.vmMirror
121                .waitForBreakpoint(requestID);
122
123        logWriter.println("=> breakpointThreadID = " + breakpointThreadID);
124
125        // Sending a SINGLE_STEP request
126
127        CommandPacket setRequestCommand = new CommandPacket(
128                JDWPCommands.EventRequestCommandSet.CommandSetID,
129                JDWPCommands.EventRequestCommandSet.SetCommand);
130
131        setRequestCommand
132                .setNextValueAsByte(JDWPConstants.EventKind.SINGLE_STEP);
133        setRequestCommand.setNextValueAsByte(JDWPConstants.SuspendPolicy.ALL);
134        setRequestCommand.setNextValueAsInt(1);
135        setRequestCommand.setNextValueAsByte(EventMod.ModKind.Step);
136        setRequestCommand.setNextValueAsThreadID(breakpointThreadID);
137        setRequestCommand.setNextValueAsInt(StepSize);
138        setRequestCommand.setNextValueAsInt(StepDepth);
139
140        ReplyPacket setRequestReply = debuggeeWrapper.vmMirror
141                .performCommand(setRequestCommand);
142
143        checkReplyPacket(setRequestReply, "Set SINGLE_STEP event");
144
145        requestID = setRequestReply.getNextValueAsInt();
146
147        logWriter.println("=> RequestID = " + requestID);
148        assertAllDataRead(setRequestReply);
149
150        //resume debuggee
151        resumeDebuggee();
152
153        //receive event
154        logWriter.println("==> Wait for SINGLE_STEP event");
155        CommandPacket event = debuggeeWrapper.vmMirror.receiveEvent();
156        ParsedEvent[] parsedEvents = ParsedEvent.parseEventPacket(event);
157
158        //check if received event is expected
159        logWriter.println("==> Received " + parsedEvents.length + " events");
160
161        // trace events
162        for (int i = 0; i < parsedEvents.length; i++) {
163            logWriter.println("");
164            logWriter.println("==> Event #" + i + ";");
165            logWriter.println("==> EventKind: " + parsedEvents[i].getEventKind() + "("
166                    + JDWPConstants.EventKind.getName(parsedEvents[i].getEventKind()) + ")");
167            logWriter.println("==> RequestID: " + parsedEvents[i].getRequestID());
168        }
169
170        // check all
171        assertEquals("Received wrong number of events,", 1, parsedEvents.length);
172        assertEquals("Received wrong event request ID,", requestID, parsedEvents[0].getRequestID());
173        assertEquals("Invalid event kind,", JDWPConstants.EventKind.SINGLE_STEP,
174                parsedEvents[0].getEventKind(),
175                JDWPConstants.EventKind.getName(JDWPConstants.EventKind.SINGLE_STEP),
176                JDWPConstants.EventKind.getName(parsedEvents[0].getEventKind()));
177
178        // clear SINGLE_STEP event
179        logWriter.println("==> Clearing SINGLE_STEP event..");
180        ReplyPacket clearRequestReply =
181            debuggeeWrapper.vmMirror.clearEvent(JDWPConstants.EventKind.SINGLE_STEP, (int) requestID);
182        checkReplyPacket(clearRequestReply, "Clear SINGLE_STEP event");
183        logWriter.println("==> SINGLE_STEP event has been cleared");
184
185        // resuming debuggee
186        logWriter.println("==> Resuming debuggee");
187        resumeDebuggee();
188        logWriter.println("==> Test PASSED!");
189    }
190}
191