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.ThreadReference;
20
21import org.apache.harmony.jpda.tests.framework.Breakpoint;
22import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
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.ParsedEvent;
26import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
27import org.apache.harmony.jpda.tests.framework.jdwp.Value;
28import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
29import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
30
31public class ForceEarlyReturn002Test extends JDWPSyncTestCase {
32
33    static final String thisCommandName = "ThreadReference.ForceEarlyReturn command ";
34
35    static final long EXPECTED_LONG = 100;
36
37    protected String getDebuggeeClassName() {
38        return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.ForceEarlyReturnDebuggee";
39    }
40
41    // ForceEarlyReturn needs canForceEarlyReturn VM capability support
42    private boolean isCapability() {
43        // check capability, relevant for this test
44        logWriter.println("=> Check capability: canForceEarlyReturn");
45        debuggeeWrapper.vmMirror.capabilities();
46        boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canForceEarlyReturn;
47        return isCapability;
48    }
49
50    /**
51     * This testcase exercises ThreadReference.ForceEarlyReturn command. <BR>
52     * At first the test starts ForceEarlyReturnDebuggee and send it the thread
53     * name through which to start a specific thread. Then the test performs the
54     * ThreadReference.ForceEarlyReturn command for the tested thread and gets
55     * the returned value of the called method. The returned value should be
56     * equal to the value which is used in ForceEarlyReturn Command. In this
57     * testcase, an Long value is returned.
58     */
59    public void testForceEarlyReturn_ReturnLong() {
60        String thisTestName = "testForceEarlyReturn_ReturnLong";
61        logWriter.println("==> " + thisTestName + " for " + thisCommandName
62                + ": START...");
63        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
64
65        if (!isCapability()) {
66            logWriter
67                    .println("##WARNING: this VM dosn't possess capability:canForceEarlyReturn");
68            return;
69        }
70        // Tell debuggee to start a thread named THREAD_LONG
71        synchronizer.sendMessage(ForceEarlyReturnDebuggee.THREAD_LONG);
72
73        // The thread is ready
74        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
75
76        // Getting ID of the tested thread
77        logWriter.println("==> testedThreadName = "
78                + ForceEarlyReturnDebuggee.THREAD_LONG);
79        logWriter.println("==> Get testedThreadID...");
80        long testedThreadID = debuggeeWrapper.vmMirror
81                .getThreadID(ForceEarlyReturnDebuggee.THREAD_LONG);
82        logWriter.println("==> Get testedThreadID is" + testedThreadID);
83
84
85        // Compose the ForceEarlyReturn command
86        CommandPacket forceEarlyReturnPacket = new CommandPacket(
87                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
88                JDWPCommands.ThreadReferenceCommandSet.ForceEarlyReturnCommand);
89        forceEarlyReturnPacket.setNextValueAsThreadID(testedThreadID);
90        forceEarlyReturnPacket.setNextValueAsValue(new Value(EXPECTED_LONG));
91
92        // Perform the command
93        logWriter.println("==> Perform " + thisCommandName);
94        ReplyPacket forceEarlyReturnReply = debuggeeWrapper.vmMirror
95                .performCommand(forceEarlyReturnPacket);
96        forceEarlyReturnPacket = null;
97
98        checkReplyPacket(forceEarlyReturnReply,
99                "ThreadReference::ForceEarlyReturn command");
100
101        // Resume the thread
102        logWriter.println("==> testedThreadID = " + testedThreadID);
103        logWriter.println("==> resume testedThread...");
104        debuggeeWrapper.vmMirror.resumeThread(testedThreadID);
105
106        String actualValue = synchronizer.receiveMessage();
107        // Check the early returned value
108        if (!actualValue.equals(new Long(EXPECTED_LONG).toString())) {
109            printErrorAndFail(thisCommandName
110                    + "returned value is not set by ForceEarlyReturn command"
111                    + " expected:<" + EXPECTED_LONG + "> but was:<"
112                    + actualValue + ">");
113        }
114        logWriter
115                .println("==> CHECK: PASSED: returned value does set by ForceEarlyReturn command.");
116        logWriter.println("==> Returned value: " + actualValue);
117
118        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
119    }
120}
121