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.framework.jdwp.JDWPConstants.Tag;
26import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
27import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
28
29public class ForceEarlyReturn005Test extends JDWPSyncTestCase {
30
31    static final String thisCommandName = "ThreadReference.ForceEarlyReturn command ";
32
33    protected String getDebuggeeClassName() {
34        return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.ForceEarlyReturnDebuggee";
35    }
36
37    // ForceEarlyReturn needs canForceEarlyReturn VM capability support
38    private boolean isCapability() {
39        // check capability, relevant for this test
40        logWriter.println("=> Check capability: canForceEarlyReturn");
41        debuggeeWrapper.vmMirror.capabilities();
42        boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canForceEarlyReturn;
43        return isCapability;
44    }
45
46    /**
47     * This testcase exercises ThreadReference.ForceEarlyReturn command. <BR>
48     * At first the test starts ForceEarlyReturnDebuggee and send it the thread
49     * name through which to start a specific thread. Then the test performs the
50     * ThreadReference.ForceEarlyReturn command for the tested thread and gets
51     * the returned value of the called method. The returned value should be
52     * equal to the value which is used in ForceEarlyReturn Command. In this
53     * testcase, an Void value is returned.
54     */
55    public void testForceEarlyReturn_ReturnVoid() {
56        String thisTestName = "testForceEarlyReturn_ReturnVoid";
57        logWriter.println("==> " + thisTestName + " for " + thisCommandName
58                + ": START...");
59        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
60
61        if (!isCapability()) {
62            logWriter
63                    .println("##WARNING: this VM dosn't possess capability:canForceEarlyReturn");
64            return;
65        }
66
67        // Tell debuggee to start a thread named THREAD_VOID
68        synchronizer.sendMessage(ForceEarlyReturnDebuggee.THREAD_VOID);
69
70        // Wait until the func_Void is processing.
71        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
72
73        // Getting ID of the tested thread
74        logWriter.println("==> testedThreadName = "
75                + ForceEarlyReturnDebuggee.THREAD_VOID);
76        logWriter.println("==> Get testedThreadID...");
77        long testedThreadID = debuggeeWrapper.vmMirror
78                .getThreadID(ForceEarlyReturnDebuggee.THREAD_VOID);
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(Tag.VOID_TAG, 0));
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 isBreak = synchronizer.receiveMessage();
108        // Check the early returned value
109        if (!isBreak.equals("TRUE")) {
110            printErrorAndFail(thisCommandName
111                    + "returned value is not void set by ForceEarlyReturn command");
112        }
113        logWriter
114                .println("==> CHECK: PASSED: returned value does set by ForceEarlyReturn command.");
115        logWriter.println("==> Returned value: " + "void");
116
117        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
118    }
119}
120