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 package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
20 
21 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
22 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
23 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
24 import org.apache.harmony.jpda.tests.framework.jdwp.Value;
25 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants.Tag;
26 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
27 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
28 
29 public class ForceEarlyReturn006Test extends JDWPSyncTestCase {
30 
31     static final String thisCommandName = "ThreadReference.ForceEarlyReturn command ";
32 
33     static final String testObjSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ThreadReference/TestObject;";
34 
getDebuggeeClassName()35     protected String getDebuggeeClassName() {
36         return "org.apache.harmony.jpda.tests.jdwp.ThreadReference.ForceEarlyReturnDebuggee";
37     }
38 
39     // ForceEarlyReturn needs canForceEarlyReturn VM capability support
isCapability()40     private boolean isCapability() {
41         // check capability, relevant for this test
42         logWriter.println("=> Check capability: canForceEarlyReturn");
43         debuggeeWrapper.vmMirror.capabilities();
44         boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canForceEarlyReturn;
45         return isCapability;
46     }
47 
48     // Get the objectID of test thread
getObjectID()49     private long getObjectID() {
50         // Compose Instances command to get tested thread objectID
51         CommandPacket InstancesCommand = new CommandPacket(
52                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
53                 JDWPCommands.ReferenceTypeCommandSet.InstancesCommand);
54 
55         long testThreadTypeID = getClassIDBySignature(testObjSignature);
56         InstancesCommand.setNextValueAsReferenceTypeID(testThreadTypeID);
57         InstancesCommand.setNextValueAsInt(1);
58 
59         ReplyPacket checkedReply = debuggeeWrapper.vmMirror
60                 .performCommand(InstancesCommand);
61         InstancesCommand = null;
62 
63         // Get the number of instances that returned.
64         int objNum = checkedReply.getNextValueAsInt();
65         // Get the tagged-objectID
66         byte tag = checkedReply.getNextValueAsByte();
67         long objectID = checkedReply.getNextValueAsObjectID();
68         return objectID;
69     }
70 
71     /**
72      * This testcase exercises ThreadReference.ForceEarlyReturn command. <BR>
73      * At first the test starts ForceEarlyReturnDebuggee and send it the thread
74      * name through which to start a specific thread. Then the test performs the
75      * ThreadReference.ForceEarlyReturn command for the tested thread and gets
76      * the returned value of the called method. The returned value should be
77      * equal to the value which is used in ForceEarlyReturn Command. In this
78      * testcase, an Object value is returned.
79      */
testForceEarlyReturn_ReturnObject()80     public void testForceEarlyReturn_ReturnObject() {
81         String thisTestName = "testForceEarlyReturn_ReturnObject";
82         logWriter.println("==> " + thisTestName + " for " + thisCommandName
83                 + ": START...");
84         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
85 
86         if (!isCapability()) {
87             logWriter
88                     .println("##WARNING: this VM dosn't possess capability:canForceEarlyReturn");
89             return;
90         }
91 
92         // Get test object id
93         long testObjID = getObjectID();
94         logWriter.println("==> test object id is: " + testObjID);
95 
96         // Tell debuggee to start a thread named THREAD_OBJECT
97         synchronizer.sendMessage(ForceEarlyReturnDebuggee.THREAD_OBJECT);
98 
99         // Wait until the func_Object is processing.
100         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
101 
102         // Getting ID of the tested thread
103         logWriter.println("==> testedThreadName = "
104                 + ForceEarlyReturnDebuggee.THREAD_OBJECT);
105         logWriter.println("==> Get testedThreadID...");
106         long testedThreadID = debuggeeWrapper.vmMirror
107                 .getThreadID(ForceEarlyReturnDebuggee.THREAD_OBJECT);
108         logWriter.println("==> Get testedThreadID is" + testedThreadID);
109 
110         // Suspend the VM before perform command
111         logWriter.println("==> testedThreadID = " + testedThreadID);
112         logWriter.println("==> suspend testedThread...");
113         debuggeeWrapper.vmMirror.suspendThread(testedThreadID);
114 
115         // Compose the ForceEarlyReturn command
116         CommandPacket forceEarlyReturnPacket = new CommandPacket(
117                 JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
118                 JDWPCommands.ThreadReferenceCommandSet.ForceEarlyReturnCommand);
119         forceEarlyReturnPacket.setNextValueAsThreadID(testedThreadID);
120         forceEarlyReturnPacket.setNextValueAsValue(new Value(Tag.OBJECT_TAG,
121                 testObjID));
122 
123         // Perform the command
124         logWriter.println("==> Perform " + thisCommandName);
125         ReplyPacket forceEarlyReturnReply = debuggeeWrapper.vmMirror
126                 .performCommand(forceEarlyReturnPacket);
127         forceEarlyReturnPacket = null;
128 
129         checkReplyPacket(forceEarlyReturnReply,
130                 "ThreadReference::ForceEarlyReturn command");
131 
132         // Resume the thread
133         logWriter.println("==> testedThreadID = " + testedThreadID);
134         logWriter.println("==> resume testedThread...");
135         debuggeeWrapper.vmMirror.resumeThread(testedThreadID);
136 
137         String isTestObj = synchronizer.receiveMessage();
138         // Check the early returned value
139         if (!isTestObj.equals("TRUE")) {
140             printErrorAndFail(thisCommandName
141                     + "returned value is not test object set by ForceEarlyReturn command");
142         }
143         logWriter
144                 .println("==> CHECK: PASSED: returned value does set by ForceEarlyReturn command.");
145         logWriter.println("==> Returned value: " + "void");
146 
147         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
148     }
149 
150 }
151