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