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