1 /**
   2  * @test
   3  * @bug 4390869
   4  * @bug 4460328
   5  * @summary Test the new SourceDebugExtension facility
   6  * @author Robert Field
   7  *
   8  * @library ..
   9  *
  10  * @run build TestScaffold VMConnection TargetListener TargetAdapter InstallSDE
  11  * @run compile MangleTest.java
  12  * @run compile -g onion/pickle/Mangle.java
  13  * @run driver MangleTest
  14  */
  15 import com.sun.jdi.*;
  16 import com.sun.jdi.event.*;
  17 import com.sun.jdi.request.*;
  18 
  19 import java.util.*;
  20 import java.io.File;
  21 
  22 public class MangleTest extends TestScaffold {
  23     static final String op = "onion" + File.separator + "pickle" + File.separator;
  24     ReferenceType targetClass;
  25 
  26     MangleTest (String args[]) {
  27         super(args);
  28     }
  29 
  30     public static void main(String[] args)      throws Exception {
  31         testSetUp();
  32         new MangleTest(args).startTests();
  33     }
  34 
  35     /********** test set-up **********/
  36 
  37     static void testSetUp() throws Exception {
  38         InstallSDE.install(new File(System.getProperty("test.classes", "."),
  39                                     op + "Mangle.class"),
  40                            new File(System.getProperty("test.src", "."),
  41                                     "Mangle.sde"));
  42     }
  43 
  44     /********** test assist **********/
  45 
  46     void checkLocation(Location loc, String label,
  47                        String expectedSourceName,
  48                        String expectedSourcePath,
  49                        int expectedLinenumber) throws Exception {
  50         String sourceName = loc.sourceName();
  51         if (sourceName.equals(expectedSourceName)) {
  52             println(label + " sourceName: " + sourceName);
  53         } else {
  54             failure("FAIL: " + label +
  55                     " expected sourceName " + expectedSourceName +
  56                     " got - " + sourceName);
  57         }
  58 
  59         String sourcePath = loc.sourcePath();
  60         if (sourcePath.equals(expectedSourcePath)) {
  61             println(label + " sourcePath: " + sourcePath);
  62         } else {
  63             failure("FAIL: " + label +
  64                     " expected sourcePath " + expectedSourcePath +
  65                     " got - " + sourcePath);
  66         }
  67 
  68         int ln = loc.lineNumber();
  69         if (ln == expectedLinenumber) {
  70             println(label + " line number: " + ln);
  71         } else {
  72             failure("FAIL: " + label +
  73                     " expected line number " + expectedLinenumber +
  74                     " got - " + ln);
  75         }
  76     }
  77 
  78     void checkLocation(String stratum, Location loc, String label,
  79                        String expectedSourceName,
  80                        String expectedSourcePath,
  81                        int expectedLinenumber) throws Exception {
  82         String sourceName = loc.sourceName(stratum);
  83         if (sourceName.equals(expectedSourceName)) {
  84             println(label + "(" + stratum + ")" +
  85                     " sourceName: " + sourceName);
  86         } else {
  87             failure("FAIL: " + label + "(" + stratum + ")" +
  88                     " expected sourceName " + expectedSourceName +
  89                     " got " + sourceName);
  90         }
  91 
  92         String sourcePath = loc.sourcePath(stratum);
  93         if (sourcePath.equals(expectedSourcePath)) {
  94             println(label + "(" + stratum + ")" +
  95                     " sourcePath: " + sourcePath);
  96         } else {
  97             failure("FAIL: " + label + "(" + stratum + ")" +
  98                     " expected sourcePath " + expectedSourcePath +
  99                     " got " + sourcePath);
 100         }
 101 
 102         int ln = loc.lineNumber(stratum);
 103         if (ln == expectedLinenumber) {
 104             println(label + "(" + stratum + ")" +
 105                     " line number: " + ln);
 106         } else {
 107             failure("FAIL: " + label + "(" + stratum + ")" +
 108                     " expected line number " + expectedLinenumber +
 109                     " got " + ln);
 110         }
 111     }
 112 
 113     Location getLoc(int index, List locList) {
 114         return ((Location)(locList.get(index)));
 115     }
 116 
 117     void lineMatch(int index, String stratum, Location loc, int line) {
 118         int gotLine = loc.lineNumber(stratum);
 119         if (gotLine != line) {
 120             failure("FAIL: index=" + index +
 121                     " " + stratum + " line=" + gotLine +
 122                     " expected: " + line);
 123         }
 124     }
 125 
 126     void lineMatch(int index, Location loc,
 127                    int javaLine, int xyzLine, int ratsLine) {
 128         lineMatch(index, "Java", loc, javaLine);
 129         lineMatch(index, "XYZ", loc, xyzLine);
 130         lineMatch(index, "Rats", loc, ratsLine);
 131     }
 132 
 133     List listWith(String s1) {
 134         List result = new ArrayList();
 135         result.add(s1);
 136         return result;
 137     }
 138 
 139     List listWith(String s1, String s2) {
 140         List result = new ArrayList();
 141         result.add(s1);
 142         result.add(s2);
 143         return result;
 144     }
 145 
 146 
 147     /********** test core **********/
 148 
 149     protected void runTests() throws Exception {
 150         /*
 151          * Get to the top of main()
 152          * to determine targetClass
 153          */
 154         BreakpointEvent bpe = startToMain("onion.pickle.Mangle");
 155         targetClass = bpe.location().declaringType();
 156 
 157         // ref type source name
 158         String sourceName = targetClass.sourceName();
 159         if (sourceName.equals("Mangle.xyz")) {
 160             println("ref type sourceName: " + sourceName);
 161         } else {
 162             failure("FAIL: unexpected ref type sourceName - " + sourceName);
 163         }
 164 
 165         // ref type source names /paths
 166         List sourceNames;
 167         sourceNames = targetClass.sourceNames("Java");
 168         if (sourceNames.equals(listWith("Mangle.java"))) {
 169             println("ref type Java sourceNames: " + sourceNames);
 170         } else {
 171             failure("FAIL: unexpected ref type Java sourceNames - " +
 172                     sourceNames);
 173         }
 174         sourceNames = targetClass.sourceNames("XYZ");
 175         if (sourceNames.equals(listWith("Mangle.xyz", "Incl.xyz"))) {
 176             println("ref type XYZ sourceNames: " + sourceNames);
 177         } else {
 178             failure("FAIL: unexpected ref type XYZ sourceNames - " +
 179                     sourceNames);
 180         }
 181         sourceNames = targetClass.sourceNames(null);
 182         if (sourceNames.equals(listWith("Mangle.xyz", "Incl.xyz"))) {
 183             println("ref type null sourceNames: " + sourceNames);
 184         } else {
 185             failure("FAIL: unexpected ref type null sourceNames - " +
 186                     sourceNames);
 187         }
 188         sourceNames = targetClass.sourceNames("Rats");
 189         if (sourceNames.equals(listWith("Mangle.rats", "Incl.rats"))) {
 190             println("ref type Rats sourceNames: " + sourceNames);
 191         } else {
 192             failure("FAIL: unexpected ref type Rats sourceNames - " +
 193                     sourceNames);
 194         }
 195         List sourcePaths;
 196         sourcePaths = targetClass.sourcePaths("Java");
 197         if (sourcePaths.equals(listWith(op + "Mangle.java"))) {
 198             println("ref type Java sourcePaths: " + sourcePaths);
 199         } else {
 200             failure("FAIL: unexpected ref type Java sourcePaths - " +
 201                     sourcePaths);
 202         }
 203         sourcePaths = targetClass.sourcePaths("XYZ");
 204         if (sourcePaths.equals(listWith("database14", op + "Incl.xyz"))) {
 205             println("ref type XYZ sourcePaths: " + sourcePaths);
 206         } else {
 207             failure("FAIL: unexpected ref type XYZ sourcePaths - " +
 208                     sourcePaths);
 209         }
 210         sourcePaths = targetClass.sourcePaths(null);
 211         if (sourcePaths.equals(listWith("database14", op + "Incl.xyz"))) {
 212             println("ref type null sourcePaths: " + sourcePaths);
 213         } else {
 214             failure("FAIL: unexpected ref type null sourcePaths - " +
 215                     sourcePaths);
 216         }
 217         sourcePaths = targetClass.sourcePaths("Rats");
 218         if (sourcePaths.equals(listWith(op + "Mangle.rats",
 219                                         "bleep:bleep:Incl.rats"))) {
 220             println("ref type Rats sourcePaths: " + sourcePaths);
 221         } else {
 222             failure("FAIL: unexpected ref type Rats sourcePaths - " +
 223                     sourcePaths);
 224         }
 225 
 226         Method main = findMethod(targetClass, "main",
 227                                  "([Ljava/lang/String;)V");
 228         List allLines = main.allLineLocations();
 229         List javaLines = main.allLineLocations("Java", null);
 230         List bogusLines = main.allLineLocations("bogus", null);
 231         List nullLines = main.allLineLocations(null, null);
 232         List xyzLines = main.allLineLocations("XYZ", null);
 233         List ratsLines = main.allLineLocations("Rats", null);
 234 
 235         List tl = new ArrayList(allLines);
 236         tl.removeAll(xyzLines);
 237         if (tl.isEmpty() && allLines.size() == xyzLines.size()) {
 238             println("allLineLocations() is OK");
 239         } else {
 240             failure("FAIL: allLineLocations() wrong - " + allLines);
 241         }
 242 
 243         tl = new ArrayList(bogusLines);
 244         tl.removeAll(xyzLines);
 245         if (tl.isEmpty() && bogusLines.size() == xyzLines.size()) {
 246             println("allLineLocations(\"bogus\") is OK");
 247         } else {
 248             failure("FAIL: allLineLocations(\"bogus\") wrong - " + bogusLines);
 249         }
 250 
 251         tl = new ArrayList(nullLines);
 252         tl.removeAll(xyzLines);
 253         if (tl.isEmpty() && nullLines.size() == xyzLines.size()) {
 254             println("allLineLocations(null) is OK");
 255         } else {
 256             failure("FAIL: allLineLocations(null) wrong - " + nullLines);
 257         }
 258 
 259         if (!javaLines.get(0).equals(ratsLines.get(0))) {
 260             failure("FAIL: locations should match - " + javaLines.get(0));
 261         }
 262         if (javaLines.get(0).equals(xyzLines.get(0))) {
 263             failure("FAIL: locations should not match - " +
 264                     javaLines.get(0));
 265         }
 266         if (!javaLines.get(1).equals(ratsLines.get(1))) {
 267             failure("FAIL: locations should match - " + javaLines.get(1));
 268         }
 269         if (!javaLines.get(1).equals(xyzLines.get(0))) {
 270             failure("FAIL: locations should match - " + javaLines.get(1));
 271         }
 272         if (javaLines.get(2).equals(ratsLines.get(1))) {
 273             failure("FAIL: locations should not match - " +
 274                     javaLines.get(1));
 275         }
 276         if (xyzLines.contains(javaLines.get(0))) {
 277             failure("FAIL: xyz locations should not match - " +
 278                     javaLines.get(0));
 279         }
 280         if (xyzLines.contains(javaLines.get(2))) {
 281             failure("FAIL: xyz locations should not match - " +
 282                     javaLines.get(2));
 283         }
 284         if (xyzLines.contains(javaLines.get(6))) {
 285             failure("FAIL: xyz locations should not match - " +
 286                     javaLines.get(6));
 287         }
 288 
 289         if (ratsLines.contains(javaLines.get(2))) {
 290             failure("FAIL: rats locations should not match - " +
 291                     javaLines.get(2));
 292         }
 293         if (ratsLines.contains(javaLines.get(4))) {
 294             failure("FAIL: rats locations should not match - " +
 295                     javaLines.get(4));
 296         }
 297         if (ratsLines.contains(javaLines.get(5))) {
 298             failure("FAIL: rats locations should not match - " +
 299                     javaLines.get(5));
 300         }
 301 
 302         println("*** Java");
 303         for (Iterator it = javaLines.iterator(); it.hasNext(); ) {
 304             Location loc = (Location)it.next();
 305             print("" + loc.lineNumber("Java") + " - ");
 306             print(loc.sourceName("XYZ") + " : ");
 307             print("" + loc.lineNumber("XYZ") + " ... ");
 308             print(loc.sourceName("Rats") + " : ");
 309             println("" + loc.lineNumber("Rats"));
 310         }
 311 
 312         println("*** XYZ");
 313         for (Iterator it = xyzLines.iterator(); it.hasNext(); ) {
 314             Location loc = (Location)it.next();
 315             print("" + loc.lineNumber("Java") + " - ");
 316             print(loc.sourceName("XYZ") + " : ");
 317             print("" + loc.lineNumber("XYZ") + " ... ");
 318             print(loc.sourceName("Rats") + " : ");
 319             println("" + loc.lineNumber("Rats"));
 320         }
 321 
 322         println("*** Rats");
 323         for (Iterator it = ratsLines.iterator(); it.hasNext(); ) {
 324             Location loc = (Location)it.next();
 325             print("" + loc.lineNumber("Java") + " - ");
 326             print(loc.sourceName("XYZ") + " : ");
 327             print("" + loc.lineNumber("XYZ") + " ... ");
 328             print(loc.sourceName("Rats") + " : ");
 329             println("" + loc.lineNumber("Rats"));
 330         }
 331 
 332         checkLocation(getLoc(0, javaLines), "0",
 333                       "Incl.xyz",
 334                       op + "Incl.xyz", 200);
 335         checkLocation(null, getLoc(0, javaLines), "0",
 336                       "Incl.xyz",
 337                       op + "Incl.xyz", 200);
 338         checkLocation("bogus", getLoc(0, javaLines), "0",
 339                       "Incl.xyz",
 340                       op + "Incl.xyz", 200);
 341         checkLocation("Java", getLoc(0, javaLines), "0",
 342                       "Mangle.java",
 343                       op + "Mangle.java", 4);
 344         checkLocation("XYZ", getLoc(0, javaLines), "0",
 345                       "Incl.xyz",
 346                       op + "Incl.xyz", 200);
 347         checkLocation("Rats", getLoc(0, javaLines), "0",
 348                       "Mangle.rats",
 349                       op + "Mangle.rats", 1000);
 350 
 351         checkLocation(getLoc(3, javaLines), "3",
 352                       "Mangle.xyz",
 353                       "database14", 210);
 354         checkLocation(null, getLoc(3, javaLines), "3",
 355                       "Mangle.xyz",
 356                       "database14", 210);
 357         checkLocation("bogus", getLoc(3, javaLines), "3",
 358                       "Mangle.xyz",
 359                       "database14", 210);
 360         checkLocation("Java", getLoc(3, javaLines), "3",
 361                       "Mangle.java",
 362                       op + "Mangle.java", 7);
 363         checkLocation("XYZ", getLoc(3, javaLines), "3",
 364                       "Mangle.xyz",
 365                       "database14", 210);
 366         checkLocation("Rats", getLoc(3, javaLines), "3",
 367                       "Incl.rats",
 368                       "bleep:bleep:Incl.rats", 1112);
 369 
 370         checkLocation(getLoc(6, javaLines), "6",
 371                       "Mangle.xyz",
 372                       "database14", 218);
 373         checkLocation(null, getLoc(6, javaLines), "6",
 374                       "Mangle.xyz",
 375                       "database14", 218);
 376         checkLocation("bogus", getLoc(6, javaLines), "6",
 377                       "Mangle.xyz",
 378                       "database14", 218);
 379         checkLocation("Java", getLoc(6, javaLines), "6",
 380                       "Mangle.java",
 381                       op + "Mangle.java", 10);
 382         checkLocation("XYZ", getLoc(6, javaLines), "6",
 383                       "Mangle.xyz",
 384                       "database14", 218);
 385         checkLocation("Rats", getLoc(6, javaLines), "6",
 386                       "Incl.rats",
 387                       "bleep:bleep:Incl.rats", 1112);
 388 
 389         lineMatch(0, getLoc(0, javaLines), 4, 200, 1000);
 390         lineMatch(1, getLoc(1, javaLines), 5, 200, 1111);
 391         lineMatch(2, getLoc(2, javaLines), 6, 200, 1111);
 392         lineMatch(3, getLoc(3, javaLines), 7, 210, 1112);
 393         lineMatch(4, getLoc(4, javaLines), 8, 217, 1112);
 394         lineMatch(5, getLoc(5, javaLines), 9, 218, 1112);
 395         lineMatch(6, getLoc(6, javaLines), 10, 218, 1112);
 396 
 397         /*
 398          * resume the target listening for events
 399          */
 400         listenUntilVMDisconnect();
 401 
 402         /*
 403          * deal with results of test
 404          * if anything has called failure("foo") testFailed will be true
 405          */
 406         if (!testFailed) {
 407             println("MangleTest: passed");
 408         } else {
 409             throw new Exception("MangleTest: failed");
 410         }
 411     }
 412 }