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