jdk/test/com/sun/jdi/LocationTest.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2001 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  *  @test
       
    26  *  @bug 4419453
       
    27  *  @summary Test that Method.location() returns the right values
       
    28  *
       
    29  *  @author Robert Field
       
    30  *
       
    31  *  @run build TestScaffold VMConnection TargetListener TargetAdapter
       
    32  *  @run compile -g LocationTest.java
       
    33  *  @run main LocationTest
       
    34  */
       
    35 import com.sun.jdi.*;
       
    36 import com.sun.jdi.event.*;
       
    37 import com.sun.jdi.request.*;
       
    38 
       
    39 import java.util.*;
       
    40 
       
    41     /********** target program **********/
       
    42 
       
    43 abstract class AbstractLocationTarg {
       
    44     abstract void foo();
       
    45 }
       
    46 
       
    47 class LocationTarg extends AbstractLocationTarg {
       
    48     public static void main(String[] args){
       
    49         System.out.println("Howdy!");  // don't change the following 3 lines
       
    50     }
       
    51     void foo() {
       
    52         System.out.println("Never here!");  // must be 3 lines after "Howdy!"
       
    53     }
       
    54 }
       
    55 
       
    56     /********** test program **********/
       
    57 
       
    58 public class LocationTest extends TestScaffold {
       
    59     ReferenceType targetClass;
       
    60     ThreadReference mainThread;
       
    61 
       
    62     LocationTest (String args[]) {
       
    63         super(args);
       
    64     }
       
    65 
       
    66     public static void main(String[] args)      throws Exception {
       
    67         new LocationTest(args).startTests();
       
    68     }
       
    69 
       
    70     /********** test assist **********/
       
    71 
       
    72     Location getLocation(String refName, String methodName) {
       
    73         List refs = vm().classesByName(refName);
       
    74         if (refs.size() != 1) {
       
    75             failure("Test failure: " + refs.size() +
       
    76                     " ReferenceTypes named: " + refName);
       
    77             return null;
       
    78         }
       
    79         ReferenceType refType = (ReferenceType)refs.get(0);
       
    80         List meths = refType.methodsByName(methodName);
       
    81         if (meths.size() != 1) {
       
    82             failure("Test failure: " + meths.size() +
       
    83                     " methods named: " + methodName);
       
    84             return null;
       
    85         }
       
    86         Method meth = (Method)meths.get(0);
       
    87         return meth.location();
       
    88     }
       
    89 
       
    90     /********** test core **********/
       
    91 
       
    92     protected void runTests() throws Exception {
       
    93         Location loc;
       
    94 
       
    95         /*
       
    96          * Get to the top of main() to get everything loaded
       
    97          */
       
    98         startToMain("LocationTarg");
       
    99 
       
   100         /*
       
   101          * Test the values of location()
       
   102          */
       
   103         loc = getLocation("AbstractLocationTarg", "foo");
       
   104         if (loc != null) {
       
   105             failure("location of AbstractLocationTarg.foo() should have " +
       
   106                     "been null, but was: " + loc);
       
   107         }
       
   108 
       
   109         loc = getLocation("java.util.List", "clear");
       
   110         if (loc != null) {
       
   111             failure("location of java.util.List.clear() " +
       
   112                     "should have been null, but was: " + loc);
       
   113         }
       
   114 
       
   115         loc = getLocation("java.lang.Object", "getClass");
       
   116         if (loc == null) {
       
   117             failure("location of Object.getClass() " +
       
   118                     "should have been non-null, but was: " + loc);
       
   119         } else {
       
   120             if (!loc.declaringType().name().equals("java.lang.Object")) {
       
   121                 failure("location.declaringType() of Object.getClass() " +
       
   122                         "should have been java.lang.Object, but was: " +
       
   123                         loc.declaringType());
       
   124             }
       
   125             if (!loc.method().name().equals("getClass")) {
       
   126                 failure("location.method() of Object.getClass() " +
       
   127                         "should have been getClass, but was: " +
       
   128                         loc.method());
       
   129             }
       
   130             if (loc.codeIndex() != -1) {
       
   131                 failure("location.codeIndex() of Object.getClass() " +
       
   132                         "should have been -1, but was: " +
       
   133                         loc.codeIndex());
       
   134             }
       
   135             if (loc.lineNumber() != -1) {
       
   136                 failure("location.lineNumber() of Object.getClass() " +
       
   137                         "should have been -1, but was: " +
       
   138                         loc.lineNumber());
       
   139             }
       
   140         }
       
   141         Location mainLoc = getLocation("LocationTarg", "main");
       
   142         loc = getLocation("LocationTarg", "foo");
       
   143         if (loc == null) {
       
   144             failure("location of LocationTarg.foo() " +
       
   145                     "should have been non-null, but was: " + loc);
       
   146         } else {
       
   147             if (!loc.declaringType().name().equals("LocationTarg")) {
       
   148                 failure("location.declaringType() of LocationTarg.foo() " +
       
   149                         "should have been LocationTarg, but was: " +
       
   150                         loc.declaringType());
       
   151             }
       
   152             if (!loc.method().name().equals("foo")) {
       
   153                 failure("location.method() of LocationTarg.foo() " +
       
   154                         "should have been foo, but was: " +
       
   155                         loc.method());
       
   156             }
       
   157             if (loc.codeIndex() != 0) {  // implementation dependent!!!
       
   158                 failure("location.codeIndex() of LocationTarg.foo() " +
       
   159                         "should have been 0, but was: " +
       
   160                         loc.codeIndex());
       
   161             }
       
   162             if (loc.lineNumber() != (mainLoc.lineNumber() + 3)) {
       
   163                 failure("location.lineNumber() of LocationTarg.foo() " +
       
   164                         "should have been " + (mainLoc.lineNumber() + 3) +
       
   165                         ", but was: " + loc.lineNumber());
       
   166             }
       
   167         }
       
   168 
       
   169 
       
   170         /*
       
   171          * resume until the end
       
   172          */
       
   173         listenUntilVMDisconnect();
       
   174 
       
   175         /*
       
   176          * deal with results of test
       
   177          * if anything has called failure("foo") testFailed will be true
       
   178          */
       
   179         if (!testFailed) {
       
   180             println("LocationTest: passed");
       
   181         } else {
       
   182             throw new Exception("LocationTest: failed");
       
   183         }
       
   184     }
       
   185 }