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 HelloWorld
  11  * @run compile TemperatureTableTest.java
  12  * @run compile -g TemperatureTableServlet.java
  13  * @run driver TemperatureTableTest
  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 TemperatureTableTest extends TestScaffold {
  23     ReferenceType targetClass;
  24 
  25     TemperatureTableTest (String args[]) {
  26         super(args);
  27     }
  28 
  29     public static void main(String[] args)      throws Exception {
  30         testSetUp();
  31         new TemperatureTableTest(args).startTests();
  32     }
  33 
  34     /********** test set-up **********/
  35 
  36     static void testSetUp() throws Exception {
  37         InstallSDE.install(new File(System.getProperty("test.classes", "."),
  38                                     "TemperatureTableServlet.class"),
  39                            new File(System.getProperty("test.src", "."),
  40                                     "TemperatureTable.sde"));
  41     }
  42 
  43     /********** test assist **********/
  44 
  45     void checkLocation(Location loc, String label,
  46                        String expectedSourceName,
  47                        String expectedSourcePath,
  48                        int expectedLinenumber) throws Exception {
  49         String sourceName = loc.sourceName();
  50         if (sourceName.equals(expectedSourceName)) {
  51             println(label + " sourceName: " + sourceName);
  52         } else {
  53             failure("FAIL: " + label +
  54                     " expected sourceName " + expectedSourceName +
  55                     " got - " + sourceName);
  56         }
  57 
  58         String sourcePath = loc.sourcePath();
  59         if (sourcePath.equals(expectedSourcePath)) {
  60             println(label + " sourcePath: " + sourcePath);
  61         } else {
  62             failure("FAIL: " + label +
  63                     " expected sourcePath " + expectedSourcePath +
  64                     " got - " + sourcePath);
  65         }
  66 
  67         int ln = loc.lineNumber();
  68         if (ln == expectedLinenumber) {
  69             println(label + " line number: " + ln);
  70         } else {
  71             failure("FAIL: " + label +
  72                     " expected line number " + expectedLinenumber +
  73                     " got - " + ln);
  74         }
  75     }
  76 
  77     void checkLocation(String stratum, Location loc, String label,
  78                        String expectedSourceName,
  79                        String expectedSourcePath,
  80                        int expectedLinenumber) throws Exception {
  81         String sourceName = loc.sourceName(stratum);
  82         if (sourceName.equals(expectedSourceName)) {
  83             println(label + "(" + stratum + ")" +
  84                     " sourceName: " + sourceName);
  85         } else {
  86             failure("FAIL: " + label + "(" + stratum + ")" +
  87                     " expected sourceName " + expectedSourceName +
  88                     " got " + sourceName);
  89         }
  90 
  91         String sourcePath = loc.sourcePath(stratum);
  92         if (sourcePath.equals(expectedSourcePath)) {
  93             println(label + "(" + stratum + ")" +
  94                     " sourcePath: " + sourcePath);
  95         } else {
  96             failure("FAIL: " + label + "(" + stratum + ")" +
  97                     " expected sourcePath " + expectedSourcePath +
  98                     " got " + sourcePath);
  99         }
 100 
 101         int ln = loc.lineNumber(stratum);
 102         if (ln == expectedLinenumber) {
 103             println(label + "(" + stratum + ")" +
 104                     " line number: " + ln);
 105         } else {
 106             failure("FAIL: " + label + "(" + stratum + ")" +
 107                     " expected line number " + expectedLinenumber +
 108                     " got " + ln);
 109         }
 110     }
 111 
 112     /********** test core **********/
 113 
 114     protected void runTests() throws Exception {
 115         /*
 116          * Get to the top of main()
 117          * to determine targetClass
 118          */
 119         BreakpointEvent bpe = startToMain("TemperatureTableServlet");
 120         targetClass = bpe.location().declaringType();
 121 
 122         if (!vm().canGetSourceDebugExtension()) {
 123             failure("FAIL: canGetSourceDebugExtension() is false");
 124         } else {
 125             println("canGetSourceDebugExtension() is true");
 126         }
 127 
 128         checkLocation(bpe.location(), "main BP",
 129                       "TemperatureTable.jsp",
 130                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 131 
 132         checkLocation("JSP", bpe.location(), "main BP",
 133                       "TemperatureTable.jsp",
 134                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 135 
 136         checkLocation("bogus", bpe.location(), "main BP",
 137                       "TemperatureTable.jsp",
 138                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 139 
 140         checkLocation(null, bpe.location(), "main BP",
 141                       "TemperatureTable.jsp",
 142                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 143 
 144         checkLocation("Java", bpe.location(), "main BP",
 145                       "TemperatureTableServlet.java",
 146                       "TemperatureTableServlet.java", 11);
 147 
 148         // ref type source name
 149         String sourceName = targetClass.sourceName();
 150         if (sourceName.equals("TemperatureTable.jsp")) {
 151             println("ref type sourceName: " + sourceName);
 152         } else {
 153             failure("FAIL: unexpected ref type sourceName - " + sourceName);
 154         }
 155 
 156         List allLines = targetClass.allLineLocations();
 157         for (Iterator it = allLines.iterator(); it.hasNext(); ) {
 158             Location loc = (Location)it.next();
 159             println("Location: " + loc);
 160         }
 161 
 162         List locs = targetClass.locationsOfLine(7);
 163         if (locs.size() != 1) {
 164             failure("FAIL: expect on elocation, got " + locs.size());
 165         }
 166         Location loc7 = (Location)locs.get(0);
 167 
 168         checkLocation(loc7, "line7",
 169                       "TemperatureTable.jsp",
 170                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 171 
 172         checkLocation("JSP", loc7, "line7",
 173                       "TemperatureTable.jsp",
 174                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 175 
 176         checkLocation("bogus", loc7, "line7",
 177                       "TemperatureTable.jsp",
 178                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 179 
 180         checkLocation(null, loc7, "line7",
 181                       "TemperatureTable.jsp",
 182                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 183 
 184         checkLocation("Java", loc7, "line7",
 185                       "TemperatureTableServlet.java",
 186                       "TemperatureTableServlet.java", 28);
 187 
 188         List availSt = targetClass.availableStrata();
 189         List avail = new ArrayList(availSt);
 190         if (avail.size() == 2 &&
 191             avail.remove("JSP") &&
 192             avail.remove("Java") &&
 193             avail.size() == 0) {
 194             println("availableStrata: " + availSt);
 195         } else {
 196             failure("FAIL: unexpected availableStrata - " + availSt);
 197         }
 198 
 199         String def = targetClass.defaultStratum();
 200         if (def.equals("JSP")) {
 201             println("defaultStratum: " + def);
 202         } else {
 203             failure("FAIL: unexpected defaultStratum - " + def);
 204         }
 205 
 206         // Test HelloWorld
 207         BreakpointEvent bpHello = resumeTo("HelloWorld", "main",
 208                                        "([Ljava/lang/String;)V");
 209         Location hello = bpHello.location();
 210 
 211         checkLocation(hello, "hello BP",
 212                       "HelloWorld.java",
 213                       "HelloWorld.java", 3);
 214 
 215         checkLocation("JSP", hello, "hello BP",
 216                       "HelloWorld.java",
 217                       "HelloWorld.java", 3);
 218 
 219         checkLocation("bogus", hello, "hello BP",
 220                       "HelloWorld.java",
 221                       "HelloWorld.java", 3);
 222 
 223         checkLocation(null, hello, "hello BP",
 224                       "HelloWorld.java",
 225                       "HelloWorld.java", 3);
 226 
 227         checkLocation("Java", hello, "hello BP",
 228                       "HelloWorld.java",
 229                       "HelloWorld.java", 3);
 230 
 231         /******** test VM default *************/
 232 
 233         vm().setDefaultStratum("Java");
 234         println("VM default set to Java");
 235 
 236         checkLocation(bpe.location(), "main BP",
 237                       "TemperatureTableServlet.java",
 238                       "TemperatureTableServlet.java", 11);
 239 
 240         checkLocation("JSP", bpe.location(), "main BP",
 241                       "TemperatureTable.jsp",
 242                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 243 
 244         checkLocation("bogus", bpe.location(), "main BP",
 245                       "TemperatureTable.jsp",
 246                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 247 
 248         checkLocation(null, bpe.location(), "main BP",
 249                       "TemperatureTable.jsp",
 250                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 251 
 252         checkLocation("Java", bpe.location(), "main BP",
 253                       "TemperatureTableServlet.java",
 254                       "TemperatureTableServlet.java", 11);
 255 
 256         checkLocation(loc7, "line7",
 257                       "TemperatureTableServlet.java",
 258                       "TemperatureTableServlet.java", 28);
 259 
 260         checkLocation("JSP", loc7, "line7",
 261                       "TemperatureTable.jsp",
 262                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 263 
 264         checkLocation("bogus", loc7, "line7",
 265                       "TemperatureTable.jsp",
 266                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 267 
 268         checkLocation(null, loc7, "line7",
 269                       "TemperatureTable.jsp",
 270                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 271 
 272         checkLocation("Java", loc7, "line7",
 273                       "TemperatureTableServlet.java",
 274                       "TemperatureTableServlet.java", 28);
 275 
 276         checkLocation(hello, "hello BP",
 277                       "HelloWorld.java",
 278                       "HelloWorld.java", 3);
 279 
 280         checkLocation("JSP", hello, "hello BP",
 281                       "HelloWorld.java",
 282                       "HelloWorld.java", 3);
 283 
 284         checkLocation("bogus", hello, "hello BP",
 285                       "HelloWorld.java",
 286                       "HelloWorld.java", 3);
 287 
 288         checkLocation(null, hello, "hello BP",
 289                       "HelloWorld.java",
 290                       "HelloWorld.java", 3);
 291 
 292         checkLocation("Java", hello, "hello BP",
 293                       "HelloWorld.java",
 294                       "HelloWorld.java", 3);
 295 
 296         vm().setDefaultStratum(null);
 297         println("VM default set to null");
 298 
 299         checkLocation(bpe.location(), "main BP",
 300                       "TemperatureTable.jsp",
 301                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 302 
 303         checkLocation("JSP", bpe.location(), "main BP",
 304                       "TemperatureTable.jsp",
 305                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 306 
 307         checkLocation("bogus", bpe.location(), "main BP",
 308                       "TemperatureTable.jsp",
 309                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 310 
 311         checkLocation(null, bpe.location(), "main BP",
 312                       "TemperatureTable.jsp",
 313                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 314 
 315         checkLocation("Java", bpe.location(), "main BP",
 316                       "TemperatureTableServlet.java",
 317                       "TemperatureTableServlet.java", 11);
 318 
 319         checkLocation(loc7, "line7",
 320                       "TemperatureTable.jsp",
 321                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 322 
 323         checkLocation("JSP", loc7, "line7",
 324                       "TemperatureTable.jsp",
 325                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 326 
 327         checkLocation("bogus", loc7, "line7",
 328                       "TemperatureTable.jsp",
 329                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 330 
 331         checkLocation(null, loc7, "line7",
 332                       "TemperatureTable.jsp",
 333                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 334 
 335         checkLocation("Java", loc7, "line7",
 336                       "TemperatureTableServlet.java",
 337                       "TemperatureTableServlet.java", 28);
 338 
 339         checkLocation(hello, "hello BP",
 340                       "HelloWorld.java",
 341                       "HelloWorld.java", 3);
 342 
 343         checkLocation("JSP", hello, "hello BP",
 344                       "HelloWorld.java",
 345                       "HelloWorld.java", 3);
 346 
 347         checkLocation("bogus", hello, "hello BP",
 348                       "HelloWorld.java",
 349                       "HelloWorld.java", 3);
 350 
 351         checkLocation(null, hello, "hello BP",
 352                       "HelloWorld.java",
 353                       "HelloWorld.java", 3);
 354 
 355         checkLocation("Java", hello, "hello BP",
 356                       "HelloWorld.java",
 357                       "HelloWorld.java", 3);
 358 
 359 
 360         vm().setDefaultStratum("bogus");
 361         println("VM default set to bogus");
 362 
 363         checkLocation(bpe.location(), "main BP",
 364                       "TemperatureTable.jsp",
 365                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 366 
 367         checkLocation("JSP", bpe.location(), "main BP",
 368                       "TemperatureTable.jsp",
 369                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 370 
 371         checkLocation("bogus", bpe.location(), "main BP",
 372                       "TemperatureTable.jsp",
 373                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 374 
 375         checkLocation(null, bpe.location(), "main BP",
 376                       "TemperatureTable.jsp",
 377                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 378 
 379         checkLocation("Java", bpe.location(), "main BP",
 380                       "TemperatureTableServlet.java",
 381                       "TemperatureTableServlet.java", 11);
 382 
 383         checkLocation(loc7, "line7",
 384                       "TemperatureTable.jsp",
 385                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 386 
 387         checkLocation("JSP", loc7, "line7",
 388                       "TemperatureTable.jsp",
 389                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 390 
 391         checkLocation("bogus", loc7, "line7",
 392                       "TemperatureTable.jsp",
 393                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 394 
 395         checkLocation(null, loc7, "line7",
 396                       "TemperatureTable.jsp",
 397                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 398 
 399         checkLocation("Java", loc7, "line7",
 400                       "TemperatureTableServlet.java",
 401                       "TemperatureTableServlet.java", 28);
 402 
 403 
 404         checkLocation(hello, "hello BP",
 405                       "HelloWorld.java",
 406                       "HelloWorld.java", 3);
 407 
 408         checkLocation("JSP", hello, "hello BP",
 409                       "HelloWorld.java",
 410                       "HelloWorld.java", 3);
 411 
 412         checkLocation("bogus", hello, "hello BP",
 413                       "HelloWorld.java",
 414                       "HelloWorld.java", 3);
 415 
 416         checkLocation(null, hello, "hello BP",
 417                       "HelloWorld.java",
 418                       "HelloWorld.java", 3);
 419 
 420         checkLocation("Java", hello, "hello BP",
 421                       "HelloWorld.java",
 422                       "HelloWorld.java", 3);
 423 
 424         vm().setDefaultStratum("JSP");
 425         println("VM default set to JSP");
 426 
 427         checkLocation(bpe.location(), "main BP",
 428                       "TemperatureTable.jsp",
 429                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 430 
 431         checkLocation("JSP", bpe.location(), "main BP",
 432                       "TemperatureTable.jsp",
 433                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 434 
 435         checkLocation("bogus", bpe.location(), "main BP",
 436                       "TemperatureTable.jsp",
 437                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 438 
 439         checkLocation(null, bpe.location(), "main BP",
 440                       "TemperatureTable.jsp",
 441                       "tst" + File.separatorChar + "TemperatureTable.jsp", 1);
 442 
 443         checkLocation("Java", bpe.location(), "main BP",
 444                       "TemperatureTableServlet.java",
 445                       "TemperatureTableServlet.java", 11);
 446 
 447         checkLocation(loc7, "line7",
 448                       "TemperatureTable.jsp",
 449                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 450 
 451         checkLocation("JSP", loc7, "line7",
 452                       "TemperatureTable.jsp",
 453                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 454 
 455         checkLocation("bogus", loc7, "line7",
 456                       "TemperatureTable.jsp",
 457                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 458 
 459         checkLocation(null, loc7, "line7",
 460                       "TemperatureTable.jsp",
 461                       "tst" + File.separatorChar + "TemperatureTable.jsp", 7);
 462 
 463         checkLocation("Java", loc7, "line7",
 464                       "TemperatureTableServlet.java",
 465                       "TemperatureTableServlet.java", 28);
 466 
 467         checkLocation(hello, "hello BP",
 468                       "HelloWorld.java",
 469                       "HelloWorld.java", 3);
 470 
 471         checkLocation("JSP", hello, "hello BP",
 472                       "HelloWorld.java",
 473                       "HelloWorld.java", 3);
 474 
 475         checkLocation("bogus", hello, "hello BP",
 476                       "HelloWorld.java",
 477                       "HelloWorld.java", 3);
 478 
 479         checkLocation(null, hello, "hello BP",
 480                       "HelloWorld.java",
 481                       "HelloWorld.java", 3);
 482 
 483         checkLocation("Java", hello, "hello BP",
 484                       "HelloWorld.java",
 485                       "HelloWorld.java", 3);
 486 
 487         /*
 488          * resume the target listening for events
 489          */
 490         listenUntilVMDisconnect();
 491 
 492         /*
 493          * deal with results of test
 494          * if anything has called failure("foo") testFailed will be true
 495          */
 496         if (!testFailed) {
 497             println("TemperatureTableTest: passed");
 498         } else {
 499             throw new Exception("TemperatureTableTest: failed");
 500         }
 501     }
 502 }