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 /**
20  * @author Anatoly F. Bondarenko
21  */
22 
23 /**
24  * Created on 18.02.2005
25  */
26 package org.apache.harmony.jpda.tests.jdwp.ReferenceType;
27 
28 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
29 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
30 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
31 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
32 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
33 
34 
35 /**
36  * JDWP Unit test for ReferenceType.Methods command.
37  */
38 public class MethodsTest extends JDWPSyncTestCase {
39 
40     static final int testStatusPassed = 0;
41     static final int testStatusFailed = -1;
42     static final String thisCommandName = "ReferenceType.Methods command";
43     static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/MethodsDebuggee;";
44 
getDebuggeeClassName()45     protected String getDebuggeeClassName() {
46         return "org.apache.harmony.jpda.tests.jdwp.ReferenceType.MethodsDebuggee";
47     }
48 
49     /**
50      * This testcase exercises ReferenceType.Methods command.
51      * <BR>The test starts MethodsDebuggee class, requests referenceTypeId
52      * for this class by VirtualMachine.ClassesBySignature command, then
53      * performs ReferenceType.Methods command and checks that returned
54      * list of methods corresponds to expected list of methods with expected attributes.
55      */
testMethods001()56     public void testMethods001() {
57         String thisTestName = "testMethods001";
58         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
59         int testStatus = testStatusPassed;
60         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
61 
62         long refTypeID = getClassIDBySignature(debuggeeSignature);
63 
64         logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
65         logWriter.println("=> referenceTypeID for Debuggee class = " + refTypeID);
66         logWriter.println("=> CHECK: send " + thisCommandName + " and check reply...");
67 
68         CommandPacket methodsCommand = new CommandPacket(
69                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
70                 JDWPCommands.ReferenceTypeCommandSet.MethodsCommand);
71         methodsCommand.setNextValueAsReferenceTypeID(refTypeID);
72 
73         ReplyPacket methodsReply = debuggeeWrapper.vmMirror.performCommand(methodsCommand);
74         methodsCommand = null;
75         checkReplyPacket(methodsReply, thisCommandName);
76 
77         int returnedMethodsNumber = methodsReply.getNextValueAsInt();
78         logWriter.println("=> Returned methods number = " + returnedMethodsNumber);
79 
80         String methodNames[] = {
81                 "staticTestMethod",
82                 "objectTestMethod",
83                 "run",
84                 "main",
85                 "<init>"
86         };
87 
88         String methodSignatures[] = {
89                 "(J)I",
90                 "(Ljava/lang/Object;)Ljava/lang/Object;",
91                 "()V",
92                 "([Ljava/lang/String;)V",
93                 "()V"
94         };
95 
96         int methodModifiers[] = {
97                 0x8,
98                 0x0,
99                 0x1,
100                 0x9,
101                 0x1
102         };
103 
104         boolean methodFound[] = {
105                 false,
106                 false,
107                 false,
108                 false,
109                 false
110         };
111         int expectedMetodsNumber = methodNames.length;
112         int methodSyntheticFlag = 0xf0000000;
113         String failMessage = "";
114 
115         logWriter.println("=> CHECK for all expected methods...");
116         for (int i = 0; i < returnedMethodsNumber; i++) {
117             long returnedMethodID = methodsReply.getNextValueAsMethodID();
118             String returnedMethodName = methodsReply.getNextValueAsString();
119             String returnedMethodSignature = methodsReply.getNextValueAsString();
120             int returnedMethodModifiers = methodsReply.getNextValueAsInt();
121             logWriter.println("\n=> Method ID = " + returnedMethodID);
122             logWriter.println("=> Method name = " + returnedMethodName);
123             logWriter.println("=> Method signature = " + returnedMethodSignature);
124             logWriter.println("=> Method modifiers = 0x" + Integer.toHexString(returnedMethodModifiers));
125             if ( (returnedMethodModifiers & methodSyntheticFlag) == methodSyntheticFlag ) {
126                 continue; // do not check synthetic methods
127             }
128             int k = 0;
129             for (; k < expectedMetodsNumber; k++) {
130                 if ( ! methodNames[k].equals(returnedMethodName)) {
131                     continue;
132                 }
133                 if ( methodFound[k] ) {
134                     logWriter.println("\n## FAILURE: The method is found out repeatedly in the list");
135                     logWriter.println("## Method name = " + returnedMethodName);
136                     testStatus = testStatusFailed;
137                     failMessage = failMessage +
138                         "The method '" + returnedMethodName +
139                         "' is found repeatedly in the list;\n";
140                     break;
141                 }
142                 methodFound[k] = true;
143                 if ( ! methodSignatures[k].equals(returnedMethodSignature) ) {
144                     logWriter.println("\n## FAILURE: Unexpected method signature is returned:");
145                     logWriter.println("## Method name = " + returnedMethodName);
146                     logWriter.println("## Expected signature = " + methodSignatures[k]);
147                     logWriter.println("## Returned signature = " + returnedMethodSignature);
148                     testStatus = testStatusFailed;
149                     failMessage = failMessage +
150                         "Unexpected signature is returned for method: " +
151                         returnedMethodName +
152                         ", Expected: " + methodSignatures[k] +
153                         ", Returned: " + returnedMethodSignature + ";\n";
154                 }
155                 if ( methodModifiers[k] != returnedMethodModifiers ) {
156                     logWriter.println("\n## FAILURE: Unexpected method modifiers are returned:");
157                     logWriter.println("## Method name = " + returnedMethodName);
158                     logWriter.println
159                     ("## Expected modifiers = 0x" + Integer.toHexString(methodModifiers[k]));
160                     logWriter.println
161                     ("## Returned modifiers = 0x" + Integer.toHexString(returnedMethodModifiers));
162                     testStatus = testStatusFailed;
163                     failMessage = failMessage +
164                         "Unexpected modifiers are returned for method: " +
165                         returnedMethodName +
166                         ", Expected: 0x" + Integer.toHexString(methodModifiers[k]) +
167                         ", Returned: 0x" + Integer.toHexString(returnedMethodModifiers) + ";\n";
168                 }
169                 break;
170             }
171             if ( k == expectedMetodsNumber ) {
172                 // returned method is not found out in the list of expected methods
173                 logWriter.println("\n## FAILURE: It is found out unexpected returned method:");
174                 logWriter.println("## Method name = " + returnedMethodName);
175                 logWriter.println("## Method signature = " + returnedMethodSignature);
176                 logWriter.println
177                 ("## Method modifiers = 0x" + Integer.toHexString(returnedMethodModifiers));
178                 testStatus = testStatusFailed;
179                 failMessage = failMessage +
180                     "Unexpected returned method is found:" +
181                     ", name = " + returnedMethodName +
182                     ", signature = " + returnedMethodSignature +
183                     ", modifiers = 0x" + Integer.toHexString(returnedMethodModifiers) + ";\n";
184             }
185         }
186         for (int k=0; k < expectedMetodsNumber; k++) {
187             if ( ! methodFound[k] ) {
188                 logWriter.println
189                 ("\n## FAILURE: Expected method is NOT found out in the list of retuned methods:");
190                 logWriter.println("## Method name = " + methodNames[k]);
191                 testStatus = testStatusFailed;
192                 failMessage = failMessage +
193                     "Expected method is NOT found in the list of retuned methods:" +
194                     " name = " + methodNames[k];
195             }
196         }
197         if ( testStatus == testStatusPassed ) {
198             logWriter.println
199             ("=> CHECK PASSED: All expected methods are found out and have expected attributes");
200         }
201 
202         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
203         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
204         if (testStatus == testStatusFailed) {
205             fail(failMessage);
206         }
207         assertAllDataRead(methodsReply);
208     }
209 }
210