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.Events;
20 
21 import org.apache.harmony.jpda.tests.framework.TestErrorException;
22 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
23 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
24 import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
25 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
26 import org.apache.harmony.jpda.tests.framework.jdwp.Value;
27 import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent.EventThread;
28 import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent.Event_METHOD_EXIT;
29 import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent.Event_METHOD_EXIT_WITH_RETURN_VALUE;
30 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
31 
32 public class MethodExitWithReturnValueTest extends JDWPEventTestCase {
33 
getDebuggeeClassName()34     protected String getDebuggeeClassName() {
35         return MethodExitWithReturnValueDebuggee.class.getName();
36     }
37 
38     /**
39      * This testcase is for METHOD_EXIT_WITH_RETURN_VALUE event. <BR>
40      * It runs MethodExitWithReturnValueDebuggee that executed own exceptionMethod.
41      * It then verify that requested METHOD_EXIT_WITH_RETURN_VALUE event occurs
42      */
testMethodExitWithReturnValueException()43     public void testMethodExitWithReturnValueException() {
44         logWriter.println("==> Start testMethodExitWithReturnValue which method will throw IOException.");
45         String methodExitClassNameRegexp = "org.apache.harmony.jpda.tests.jdwp.Events.MethodExitWithReturnValueDebuggee";
46         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
47 
48         // Set event request for MethodExitWithReturnValue
49         ReplyPacket reply = debuggeeWrapper.vmMirror
50                 .setMethodExit(methodExitClassNameRegexp);
51         checkReplyPacket(reply, "Set METHOD_EXIT_WITH_RETURN_VALUE event");
52 
53         // Inform debuggee desired return method
54         synchronizer.sendMessage(MethodExitWithReturnValueDebuggee.EXCEPTION_TYPE);
55 
56         // Receive MethodExitWithReturnValue event
57         CommandPacket receiveEvent = null;
58         try {
59             receiveEvent = debuggeeWrapper.vmMirror.receiveEvent();
60         } catch (TestErrorException e) {
61             printErrorAndFail("There is no event received.");
62         }
63         ParsedEvent[] parsedEvents = ParsedEvent.parseEventPacket(receiveEvent);
64         Event_METHOD_EXIT event = (Event_METHOD_EXIT)parsedEvents[0];
65 
66         assertEquals("Invalid number of events,", 1, parsedEvents.length);
67         assertEquals("Invalid event kind,",
68                 JDWPConstants.EventKind.METHOD_EXIT,
69                 event.getEventKind(),
70                 JDWPConstants.EventKind.getName(JDWPConstants.EventKind.METHOD_EXIT),
71                 JDWPConstants.EventKind.getName(event.getEventKind()));
72 
73         long refID = event.getLocation().classID;
74         String expectedSignature = "Lorg/apache/harmony/jpda/tests/jdwp/Events/MethodExitWithReturnValueDebuggee;";
75         String actualSignature =  debuggeeWrapper.vmMirror.getReferenceTypeSignature(refID);
76         assertEquals("Invalid class signature of method caller,",expectedSignature,actualSignature);
77     }
78 
79     /**
80      * This testcase is for METHOD_EXIT_WITH_RETURN_VALUE event. <BR>
81      * It runs MethodExitWithReturnValueDebuggee that executed own booleanMethod.
82      * It then verify that requested METHOD_EXIT_WITH_RETURN_VALUE event occurs
83      * and the returned value is as expected boolean value.
84      */
testMethodExitWithReturnValueOfBoolean()85     public void testMethodExitWithReturnValueOfBoolean() {
86         runMethodExitWithReturn(MethodExitWithReturnValueDebuggee.BOOLEAN_TYPE);
87     }
88 
89     /**
90      * This testcase is for METHOD_EXIT_WITH_RETURN_VALUE event. <BR>
91      * It runs MethodExitWithReturnValueDebuggee that executed own shortMethod.
92      * It then verify that requested METHOD_EXIT_WITH_RETURN_VALUE event occurs
93      * and the returned value is as expected short value.
94      */
testMethodExitWithReturnValueOfShort()95     public void testMethodExitWithReturnValueOfShort() {
96         runMethodExitWithReturn(MethodExitWithReturnValueDebuggee.SHORT_TYPE);
97     }
98 
99     /**
100      * This testcase is for METHOD_EXIT_WITH_RETURN_VALUE event. <BR>
101      * It runs MethodExitWithReturnValueDebuggee that executed own charMethod.
102      * It then verify that requested METHOD_EXIT_WITH_RETURN_VALUE event occurs
103      * and the returned value is as expected char value.
104      */
testMethodExitWithReturnValueOfChar()105     public void testMethodExitWithReturnValueOfChar() {
106         runMethodExitWithReturn(MethodExitWithReturnValueDebuggee.CHAR_TYPE);
107     }
108 
109     /**
110      * This testcase is for METHOD_EXIT_WITH_RETURN_VALUE event. <BR>
111      * It runs MethodExitWithReturnValueDebuggee that executed own intMethod.
112      * It then verify that requested METHOD_EXIT_WITH_RETURN_VALUE event occurs
113      * and the returned value is as expected integer value.
114      */
testMethodExitWithReturnValueOfInt()115     public void testMethodExitWithReturnValueOfInt() {
116         runMethodExitWithReturn(MethodExitWithReturnValueDebuggee.INT_TYPE);
117     }
118 
119     /**
120      * This testcase is for METHOD_EXIT_WITH_RETURN_VALUE event. <BR>
121      * It runs MethodExitWithReturnValueDebuggee that executed own longMethod.
122      * It then verify that requested METHOD_EXIT_WITH_RETURN_VALUE event occurs
123      * and the returned value is as expected long value.
124      */
testMethodExitWithReturnValueOfLong()125     public void testMethodExitWithReturnValueOfLong() {
126         runMethodExitWithReturn(MethodExitWithReturnValueDebuggee.LONG_TYPE);
127     }
128 
129     /**
130      * This testcase is for METHOD_EXIT_WITH_RETURN_VALUE event. <BR>
131      * It runs MethodExitWithReturnValueDebuggee that executed own doubleMethod.
132      * It then verify that requested METHOD_EXIT_WITH_RETURN_VALUE event occurs
133      * and the returned value is as expected double value.
134      */
testMethodExitWithReturnValueOfDouble()135     public void testMethodExitWithReturnValueOfDouble() {
136         runMethodExitWithReturn(MethodExitWithReturnValueDebuggee.DOUBLE_TYPE);
137     }
138 
139     /**
140      * This testcase is for METHOD_EXIT_WITH_RETURN_VALUE event. <BR>
141      * It runs MethodExitWithReturnValueDebuggee that executed own voidMethod.
142      * It then verify that requested METHOD_EXIT_WITH_RETURN_VALUE event occurs
143      * and there is no returned value.
144      */
testMethodExitWithReturnValueOfVoid()145     public void testMethodExitWithReturnValueOfVoid() {
146         runMethodExitWithReturn(MethodExitWithReturnValueDebuggee.VOID_TYPE);
147     }
148 
149 
150     /*
151      * Internal function used to receive MethodExitWithReturnValue event.
152      * Test the returned value according to the parameter's value
153      */
runMethodExitWithReturn(String type)154     private void runMethodExitWithReturn(String type){
155         logWriter.println("==> Start testMethodExitWithReturnValue with return type of " + type);
156         String methodExitClassNameRegexp = "org.apache.harmony.jpda.tests.jdwp.Events.MethodExitWithReturnValueDebuggee";
157         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
158 
159         // Set event request for MethodExitWithReturnValue
160         ReplyPacket reply = debuggeeWrapper.vmMirror
161                 .setMethodExitWithReturnValue(methodExitClassNameRegexp);
162         checkReplyPacket(reply, "Set METHOD_EXIT_WITH_RETURN_VALUE event");
163 
164         // Inform debuggee desired return method
165         synchronizer.sendMessage(type.toString());
166 
167         // Receive MethodExitWithReturnValue event
168 
169         CommandPacket receiveEvent = null;
170         try {
171             receiveEvent = debuggeeWrapper.vmMirror.receiveEvent();
172         } catch (TestErrorException e) {
173             printErrorAndFail("There is no event received.");
174         }
175         ParsedEvent[] parsedEvents = ParsedEvent.parseEventPacket(receiveEvent);
176 
177         // Check received event's basic information
178         assertEquals("Invalid number of events,", 1, parsedEvents.length);
179         logWriter.println("==> CHECK: receive 1 event");
180 
181         assertEquals(
182                 "Invalid event kind,",
183                 JDWPConstants.EventKind.METHOD_EXIT_WITH_RETURN_VALUE,
184                 parsedEvents[0].getEventKind(),
185                 JDWPConstants.EventKind
186                         .getName(JDWPConstants.EventKind.METHOD_EXIT_WITH_RETURN_VALUE),
187                 JDWPConstants.EventKind.getName(parsedEvents[0].getEventKind()));
188         logWriter.println("==> CHECK: received event's type is " + JDWPConstants.EventKind.METHOD_EXIT_WITH_RETURN_VALUE);
189 
190         long eventThreadID = ((EventThread) parsedEvents[0]).getThreadID();
191         checkThreadState(eventThreadID, JDWPConstants.ThreadStatus.RUNNING,
192                 JDWPConstants.SuspendStatus.SUSPEND_STATUS_SUSPENDED);
193 
194         // Check return value according to it's type
195         Value value = ((Event_METHOD_EXIT_WITH_RETURN_VALUE) parsedEvents[0])
196                 .getReturnValue();
197 
198         if(type.equals(MethodExitWithReturnValueDebuggee.BOOLEAN_TYPE)){
199             boolean b = value.getBooleanValue();
200             logWriter.println("==> CHECK: booleanMethod() is invoked, return value:" + b);
201             assertEquals("Invalid return value,",
202                     MethodExitWithReturnValueDebuggee.EXPECTED_BOOLEAN, b);
203         }else if(type.equals(MethodExitWithReturnValueDebuggee.SHORT_TYPE)){
204             short s = value.getShortValue();
205             logWriter.println("==> CHECK: shortMethod() is invoked, return value:" + s);
206             assertEquals("Invalid return value,",
207                     MethodExitWithReturnValueDebuggee.EXPECTED_SHORT, s);
208         }else if(type.equals(MethodExitWithReturnValueDebuggee.CHAR_TYPE)){
209             char c = value.getCharValue();
210             logWriter.println("==> CHECK: charMethod() is invoked, return value:" + c);
211             assertEquals("Invalid return value,",
212                     MethodExitWithReturnValueDebuggee.EXPECTED_CHAR, c);
213         }else if(type.equals(MethodExitWithReturnValueDebuggee.INT_TYPE)){
214             int i = value.getIntValue();
215             logWriter.println("==> CHECK: intMethod() is invoked, return value:" + i);
216             assertEquals("Invalid return value,",
217                     MethodExitWithReturnValueDebuggee.EXPECTED_INT, i);
218         }else if(type.equals(MethodExitWithReturnValueDebuggee.LONG_TYPE)){
219             long l = value.getLongValue();
220             logWriter.println("==> CHECK: longMethod() is invoked, return value:" + l);
221             assertEquals("Invalid return value,",
222                     MethodExitWithReturnValueDebuggee.EXPECTED_LONG, l);
223         }else if(type.equals(MethodExitWithReturnValueDebuggee.DOUBLE_TYPE)){
224             double d = value.getDoubleValue();
225             logWriter.println("==> CHECK: doubleMethod() is invoked, return value:" + d);
226             assertEquals("Invalid return value,",
227                     MethodExitWithReturnValueDebuggee.EXPECTED_DOUBLE, d);
228         } else if (type.equals(MethodExitWithReturnValueDebuggee.VOID_TYPE)) {
229             logWriter.println("==> CHECK: voidMethod() is invoked");
230             assertEquals("Invalid return value,", null, value);
231         }
232     }
233 }
234