1 19 package org.netbeans.modules.editor.java; 20 import java.beans.PropertyVetoException ; 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import javax.lang.model.element.Element; 25 import javax.lang.model.element.ElementKind; 26 import javax.lang.model.element.ExecutableElement; 27 import javax.lang.model.element.TypeElement; 28 import javax.lang.model.type.DeclaredType; 29 import javax.lang.model.type.TypeKind; 30 import javax.lang.model.type.TypeMirror; 31 import javax.swing.text.Document ; 32 import org.netbeans.api.java.lexer.JavaTokenId; 33 import org.netbeans.api.java.source.ClasspathInfo; 34 import org.netbeans.api.java.source.SourceUtilsTestUtil; 35 import org.netbeans.api.lexer.Language; 36 import org.netbeans.junit.NbTestCase; 37 import org.netbeans.modules.editor.java.GoToSupport.UiUtilsCaller; 38 import org.openide.cookies.EditorCookie; 39 import org.openide.filesystems.FileLock; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileUtil; 42 import org.openide.filesystems.LocalFileSystem; 43 import org.openide.filesystems.Repository; 44 import org.openide.loaders.DataObject; 45 46 50 public class GoToSupportTest extends NbTestCase { 51 52 53 public GoToSupportTest(String name) { 54 super(name); 55 } 56 57 protected void setUp() throws Exception { 58 SourceUtilsTestUtil.prepareTest(new String [] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object [0]); 59 } 60 61 public void testGoToMethod() throws Exception { 62 final boolean[] wasCalled = new boolean[1]; 63 64 performTest("package test; public class Test { public void test() {} public static void main(String[] args) {test();}}", 97, new UiUtilsCaller() { 65 public void open(FileObject fo, int pos) { 66 assertTrue(source == fo); 67 assertEquals(34, pos); 68 wasCalled[0] = true; 69 } 70 public void beep() { 71 fail("Should not be called."); 72 } 73 public void open(ClasspathInfo info, Element el) { 74 fail("Should not be called."); 75 } 76 }, false); 77 78 assertTrue(wasCalled[0]); 79 } 80 81 public void testGoToClass() throws Exception { 82 final boolean[] wasCalled = new boolean[1]; 83 84 performTest("package test; public class Test { public static void main(String[] args) {TT tt} } class TT { }", 75, new UiUtilsCaller() { 85 public void open(FileObject fo, int pos) { 86 assertTrue(source == fo); 87 assertEquals(83, pos); 88 wasCalled[0] = true; 89 } 90 public void beep() { 91 fail("Should not be called."); 92 } 93 public void open(ClasspathInfo info, Element el) { 94 fail("Should not be called."); 95 } 96 }, false); 97 98 assertTrue(wasCalled[0]); 99 } 100 101 public void testGoToConstructor() throws Exception { 102 final boolean[] wasCalled = new boolean[1]; 103 104 performTest("package test; public class Test { public Test() {} public static void main(String[] args) {new Test();}}", 97, new UiUtilsCaller() { 105 public void open(FileObject fo, int pos) { 106 assertTrue(source == fo); 107 assertEquals(34, pos); 108 wasCalled[0] = true; 109 } 110 public void beep() { 111 fail("Should not be called."); 112 } 113 public void open(ClasspathInfo info, Element el) { 114 fail("Should not be called."); 115 } 116 }, false); 117 118 assertTrue(wasCalled[0]); 119 } 120 121 public void testGoToGenerifiedConstructor() throws Exception { 122 final boolean[] wasCalled = new boolean[1]; 123 124 performTest("package test; public class Test<T> { public Test() {} public static void main(String[] args) {new Test<String>();}}", 100, new UiUtilsCaller() { 125 public void open(FileObject fo, int pos) { 126 assertTrue(source == fo); 127 assertEquals(37, pos); 128 wasCalled[0] = true; 129 } 130 public void beep() { 131 fail("Should not be called."); 132 } 133 public void open(ClasspathInfo info, Element el) { 134 fail("Should not be called."); 135 } 136 }, false); 137 138 assertTrue(wasCalled[0]); 139 } 140 141 public void testGoToSuperConstructor() throws Exception { 142 final boolean[] wasCalled = new boolean[1]; 143 144 performTest("package test; public class Test extends Base { public Test() {super(1);} } class Base {public Base() {} public Base(int i) {}}", 64, new UiUtilsCaller() { 145 public void open(FileObject fo, int pos) { 146 assertTrue(source == fo); 147 assertEquals(104, pos); 148 wasCalled[0] = true; 149 } 150 public void beep() { 151 fail("Should not be called."); 152 } 153 public void open(ClasspathInfo info, Element el) { 154 fail("Should not be called."); 155 } 156 }, false); 157 158 assertTrue(wasCalled[0]); 159 } 160 161 public void testGoToThisConstructor() throws Exception { 162 final boolean[] wasCalled = new boolean[1]; 163 164 performTest("package test; public class Test { public Test() {this(1);} public Test(int i) {}}", 50, new UiUtilsCaller() { 165 public void open(FileObject fo, int pos) { 166 assertTrue(source == fo); 167 assertEquals(59, pos); 168 wasCalled[0] = true; 169 } 170 public void beep() { 171 fail("Should not be called."); 172 } 173 public void open(ClasspathInfo info, Element el) { 174 fail("Should not be called."); 175 } 176 }, false); 177 178 assertTrue(wasCalled[0]); 179 } 180 181 public void testGoToSuperMethod1() throws Exception { 182 final boolean[] wasCalled = new boolean[1]; 184 185 performTest("package test; public class Test extends Base { public void test() {super.methodInParent();} } class Base {public void methodInParent() {}}", 75, new UiUtilsCaller() { 186 public void open(FileObject fo, int pos) { 187 assertTrue(source == fo); 188 assertEquals(106, pos); 189 wasCalled[0] = true; 190 } 191 public void beep() { 192 fail("Should not be called."); 193 } 194 public void open(ClasspathInfo info, Element el) { 195 fail("Should not be called."); 196 } 197 }, false); 198 199 assertTrue(wasCalled[0]); 200 } 201 202 public void testGoToSuperMethod2() throws Exception { 203 final boolean[] wasCalled = new boolean[1]; 205 206 performTest("package test; public class Test extends Base { public void test() {super.methodInParent();} } class Base {public void methodInParent() {}}", 70, new UiUtilsCaller() { 207 public void open(FileObject fo, int pos) { 208 fail("Should not be called."); 209 } 210 public void beep() { 211 wasCalled[0] = true; 212 } 213 public void open(ClasspathInfo info, Element el) { 214 fail("Should not be called."); 215 } 216 }, false); 217 218 assertTrue(wasCalled[0]); 219 } 220 221 public void testGoToGarbage() throws Exception { 222 final boolean[] wasCalled = new boolean[1]; 223 224 performTest("package test; public class Test {ddddddddd public void test() {super.methodInParent();} }", 36, new UiUtilsCaller() { 225 public void open(FileObject fo, int pos) { 226 fail("Should not be called."); 227 } 228 public void beep() { 229 wasCalled[0] = true; 230 } 231 public void open(ClasspathInfo info, Element el) { 232 fail("Should not be called."); 233 } 234 }, false); 235 236 assertTrue(wasCalled[0]); 237 } 238 239 public void testTooltipForGarbage() throws Exception { 240 String tooltip = performTest("package test; public class Test {ddddddddd public void test() {super.methodInParent();} }", 36, new UiUtilsCaller() { 241 public void open(FileObject fo, int pos) { 242 fail("Should not be called."); 243 } 244 public void beep() { 245 fail("Should not be called."); 246 } 247 public void open(ClasspathInfo info, Element el) { 248 fail("Should not be called."); 249 } 250 }, true); 251 } 252 253 public void testGoToIntoAnnonymous() throws Exception { 254 final boolean[] wasCalled = new boolean[1]; 255 256 performTest("package test; public class Test {public void test() {new Runnable() {int var; public void run() {var = 0;}};} }", 99, new UiUtilsCaller() { 257 public void open(FileObject fo, int pos) { 258 wasCalled[0] = true; 259 assertTrue(source == fo); 260 assertEquals(69, pos); 261 } 262 public void beep() { 263 fail("Should not be called."); 264 } 265 public void open(ClasspathInfo info, Element el) { 266 fail("Should not be called."); 267 } 268 }, false); 269 270 assertTrue(wasCalled[0]); 271 } 272 273 public void testGoToString() throws Exception { 274 final boolean[] wasCalled = new boolean[1]; 275 276 performTest("package test; public class Test {public void test() {String s = null;} }", 56, new UiUtilsCaller() { 277 public void open(FileObject fo, int pos) { 278 fail("Should not be called."); 279 } 280 public void beep() { 281 fail("Should not be called."); 282 } 283 public void open(ClasspathInfo info, Element el) { 284 assertEquals(ElementKind.CLASS, el.getKind()); 285 assertEquals("java.lang.String", ((TypeElement) el).getQualifiedName().toString()); 286 wasCalled[0] = true; 287 } 288 }, false); 289 290 assertTrue(wasCalled[0]); 291 } 292 293 public void testGoToAnnonymousInnerClass() throws Exception { 294 final boolean[] wasCalled = new boolean[1]; 295 296 performTest("package test; public class Test {public void test() {new Runnable() {public void run(){}};} }", 61, new UiUtilsCaller() { 297 public void open(FileObject fo, int pos) { 298 fail("Should not be called."); 299 } 300 public void beep() { 301 fail("Should not be called."); 302 } 303 public void open(ClasspathInfo info, Element el) { 304 assertEquals(ElementKind.INTERFACE, el.getKind()); 305 assertEquals("java.lang.Runnable", ((TypeElement) el).getQualifiedName().toString()); 306 wasCalled[0] = true; 307 } 308 }, false); 309 310 assertTrue(wasCalled[0]); 311 } 312 313 public void testGoToAnnonymousInnerClass2() throws Exception { 314 final boolean[] wasCalled = new boolean[1]; 315 316 performTest("package test; public class Test {public void test() {new java.util.ArrayList(c) {public void run(){}};} java.util.Collection c;}", 70, new UiUtilsCaller() { 317 public void open(FileObject fo, int pos) { 318 fail("Should not be called."); 319 } 320 public void beep() { 321 fail("Should not be called."); 322 } 323 public void open(ClasspathInfo info, Element el) { 324 assertEquals(ElementKind.CONSTRUCTOR, el.getKind()); 325 assertEquals("java.util.ArrayList", ((TypeElement) el.getEnclosingElement()).getQualifiedName().toString()); 326 327 ExecutableElement ee = (ExecutableElement) el; 328 329 assertEquals(1, ee.getParameters().size()); 330 331 TypeMirror paramType = ee.getParameters().get(0).asType(); 332 333 assertEquals(TypeKind.DECLARED, paramType.getKind()); 334 assertEquals("java.util.Collection", ((TypeElement) ((DeclaredType) paramType).asElement()).getQualifiedName().toString()); 335 336 wasCalled[0] = true; 337 } 338 }, false); 339 340 assertTrue(wasCalled[0]); 341 } 342 343 public void testGoToAnnonymousInnerClass3() throws Exception { 344 final boolean[] wasCalled = new boolean[1]; 345 346 performTest("package test; public class Test {java.util.List l = new java.util.ArrayList() {public void run(){}};}", 70, new UiUtilsCaller() { 347 public void open(FileObject fo, int pos) { 348 fail("Should not be called."); 349 } 350 public void beep() { 351 fail("Should not be called."); 352 } 353 public void open(ClasspathInfo info, Element el) { 354 assertEquals(ElementKind.CONSTRUCTOR, el.getKind()); 355 assertEquals("java.util.ArrayList", ((TypeElement) el.getEnclosingElement()).getQualifiedName().toString()); 356 357 ExecutableElement ee = (ExecutableElement) el; 358 359 assertEquals(0, ee.getParameters().size()); 360 361 wasCalled[0] = true; 362 } 363 }, false); 364 365 assertTrue(wasCalled[0]); 366 } 367 368 public void testGoToParameter() throws Exception { 369 final boolean[] wasCalled = new boolean[1]; 370 371 performTest("package test; public class Test {public void test(int xx) {xx = 0;}}", 60, new UiUtilsCaller() { 372 public void open(FileObject fo, int pos) { 373 assertTrue(source == fo); 374 assertEquals(50, pos); 375 wasCalled[0] = true; 376 } 377 public void beep() { 378 fail("Should not be called."); 379 } 380 public void open(ClasspathInfo info, Element el) { 381 fail("Should not be called."); 382 } 383 }, false); 384 385 assertTrue(wasCalled[0]); 386 } 387 388 public void testGoToLocalVariable() throws Exception { 389 final boolean[] wasCalled = new boolean[1]; 390 391 performTest("package test; public class Test {public void test() {int xx;xx = 0;}}", 61, new UiUtilsCaller() { 392 public void open(FileObject fo, int pos) { 393 assertTrue(source == fo); 394 assertEquals(53, pos); 395 wasCalled[0] = true; 396 } 397 public void beep() { 398 fail("Should not be called."); 399 } 400 public void open(ClasspathInfo info, Element el) { 401 fail("Should not be called."); 402 } 403 }, false); 404 405 assertTrue(wasCalled[0]); 406 } 407 408 public void testGoToTypeVariable() throws Exception { 409 final boolean[] wasCalled = new boolean[1]; 410 411 performTest("package test; public class Test<TTT> {public void test() {TTT t;}}", 60, new UiUtilsCaller() { 412 public void open(FileObject fo, int pos) { 413 assertTrue(source == fo); 414 assertEquals(32, pos); 415 wasCalled[0] = true; 416 } 417 public void beep() { 418 fail("Should not be called."); 419 } 420 public void open(ClasspathInfo info, Element el) { 421 fail("Should not be called."); 422 } 423 }, false); 424 425 assertTrue(wasCalled[0]); 426 } 427 428 public void testGoToSynteticConstructorInDifferentClass() throws Exception { 429 final boolean[] wasCalled = new boolean[1]; 430 431 performTest("package test; public class Test {public void test() {new Auxiliary();}}", 62, new UiUtilsCaller() { 432 public void open(FileObject fo, int pos) { 433 fail("Should not be called."); 434 } 435 public void beep() { 436 fail("Should not be called."); 437 } 438 public void open(ClasspathInfo info, Element el) { 439 assertEquals(ElementKind.CLASS, el.getKind()); 440 assertEquals("test.Auxiliary", ((TypeElement) el).getQualifiedName().toString()); 441 wasCalled[0] = true; 442 } 443 }, false); 444 445 assertTrue(wasCalled[0]); 446 } 447 448 public void testGoToCArray90875() throws Exception { 449 final boolean[] wasCalled = new boolean[1]; 450 451 performTest("package test; public class Test {public void test() {int ar[][] = null; System.err.println(ar);}}", 92, new UiUtilsCaller() { 452 public void open(FileObject fo, int pos) { 453 assertTrue(source == fo); 454 assertEquals(53, pos); 455 wasCalled[0] = true; 456 } 457 public void beep() { 458 fail("Should not be called."); 459 } 460 public void open(ClasspathInfo info, Element el) { 461 fail("Should not be called."); 462 } 463 }, false); 464 465 assertTrue(wasCalled[0]); 466 } 467 468 public void testNewClass91637() throws Exception { 469 final boolean[] wasCalled = new boolean[1]; 470 471 performTest("package test; public class Test {public Test(int x){} public void test() {int ii = 0; new Test(ii);}}", 96, new UiUtilsCaller() { 472 public void open(FileObject fo, int pos) { 473 assertTrue(source == fo); 474 assertEquals(74, pos); 475 wasCalled[0] = true; 476 } 477 public void beep() { 478 fail("Should not be called."); 479 } 480 public void open(ClasspathInfo info, Element el) { 481 fail("Should not be called."); 482 } 483 }, false); 484 485 assertTrue(wasCalled[0]); 486 487 wasCalled[0] = false; 488 489 performTest("package test; public class Test<T> {public Test(int x){} public void test() {int ii = 0; new Test<Object>(ii);}}", 107, new UiUtilsCaller() { 490 public void open(FileObject fo, int pos) { 491 assertTrue(source == fo); 492 assertEquals(77, pos); 493 wasCalled[0] = true; 494 } 495 public void beep() { 496 fail("Should not be called."); 497 } 498 public void open(ClasspathInfo info, Element el) { 499 fail("Should not be called."); 500 } 501 }, false); 502 503 assertTrue(wasCalled[0]); 504 505 wasCalled[0] = false; 506 507 performTest("package test; public class Test<T> {public Test(int x){} public void test() {int ii = 0; new Test<Object>(ii);}}", 100, new UiUtilsCaller() { 508 public void open(FileObject fo, int pos) { 509 fail("Should not be called."); 510 } 511 public void beep() { 512 fail("Should not be called."); 513 } 514 public void open(ClasspathInfo info, Element el) { 515 assertEquals(ElementKind.CLASS, el.getKind()); 516 assertEquals("java.lang.Object", ((TypeElement) el).getQualifiedName().toString()); 517 wasCalled[0] = true; 518 } 519 }, false); 520 521 assertTrue(wasCalled[0]); 522 } 523 524 public void testNewClass91769() throws Exception { 525 final boolean[] wasCalled = new boolean[1]; 526 527 performTest("package test; public class Test {public void test() {new AB(name);} private static class AB {public AB(String n){}}}", 58, new UiUtilsCaller() { 528 public void open(FileObject fo, int pos) { 529 assertTrue(source == fo); 530 assertEquals(68, pos); 531 wasCalled[0] = true; 532 } 533 public void beep() { 534 fail("Should not be called."); 535 } 536 public void open(ClasspathInfo info, Element el) { 537 fail("Should not be called."); 538 } 539 }, false); 540 541 assertTrue(wasCalled[0]); 542 543 wasCalled[0] = false; 544 545 performTest("package test; public class Test {public void test() {new AB<Object>(name);} private static class AB<T> {public AB(String n){}}}", 58, new UiUtilsCaller() { 546 public void open(FileObject fo, int pos) { 547 assertTrue(source == fo); 548 assertEquals(76, pos); 549 wasCalled[0] = true; 550 } 551 public void beep() { 552 fail("Should not be called."); 553 } 554 public void open(ClasspathInfo info, Element el) { 555 fail("Should not be called."); 556 } 557 }, false); 558 559 assertTrue(wasCalled[0]); 560 561 wasCalled[0] = false; 562 563 performTest("package test; public class Test {public void test() {new AB(name);} private static class AB {public AB(String n){}}}", 62, new UiUtilsCaller() { 564 public void open(FileObject fo, int pos) { 565 fail("Should not be called."); 566 } 567 public void beep() { 568 wasCalled[0] = true; 569 } 570 public void open(ClasspathInfo info, Element el) { 571 fail("Should not be called."); 572 } 573 }, false); 574 575 assertTrue(wasCalled[0]); 576 577 wasCalled[0] = false; 578 579 performTest("package test; public class Test {public void test() {new AB<Object>(name);} private static class AB<T> {public AB(String n){}}}", 63, new UiUtilsCaller() { 580 public void open(FileObject fo, int pos) { 581 fail("Should not be called."); 582 } 583 public void beep() { 584 fail("Should not be called."); 585 } 586 public void open(ClasspathInfo info, Element el) { 587 assertEquals(ElementKind.CLASS, el.getKind()); 588 assertEquals("java.lang.Object", ((TypeElement) el).getQualifiedName().toString()); 589 wasCalled[0] = true; 590 } 591 }, false); 592 593 assertTrue(wasCalled[0]); 594 } 595 596 public void testBeepOnDeclarations() throws Exception { 597 String code = "package test; public class Test {public void test(String s) {} public String test2(String s) {} public void test3() {} private static class AB {} private String FIELD; private void test4(String name1, String name2) {}}"; 598 final boolean[] wasCalled = new boolean[1]; 599 600 for (final int pos : new int[] {53, 71, 103, 134, 165, 187, 220, 234}) { 601 performTest(code, pos - 24, new UiUtilsCaller() { 602 public void open(FileObject fo, int pos) { 603 fail("Should not be called, position= " + pos + "."); 604 } 605 public void beep() { 606 wasCalled[0] = true; 607 } 608 public void open(ClasspathInfo info, Element el) { 609 fail("Should not be called, position= " + pos + "."); 610 } 611 }, false); 612 613 assertTrue(wasCalled[0]); 614 615 wasCalled[0] = false; 616 } 617 618 for (final int pos : new int[] {77, 97, 109, 181, 214, 228}) { 619 performTest(code, pos - 24, new UiUtilsCaller() { 620 public void open(FileObject fo, int pos) { 621 fail("Should not be called, position= " + pos + "."); 622 } 623 public void beep() { 624 fail("Should not be called, position= " + pos + "."); 625 } 626 public void open(ClasspathInfo info, Element el) { 627 assertEquals(ElementKind.CLASS, el.getKind()); 628 assertEquals("java.lang.String", ((TypeElement) el).getQualifiedName().toString()); 629 wasCalled[0] = true; 630 } 631 }, false); 632 633 assertTrue(wasCalled[0]); 634 635 wasCalled[0] = false; 636 } 637 } 638 639 private void writeIntoFile(FileObject file, String what) throws Exception { 640 FileLock lock = file.lock(); 641 OutputStream out = file.getOutputStream(lock); 642 643 try { 644 out.write(what.getBytes()); 645 } finally { 646 out.close(); 647 lock.releaseLock(); 648 } 649 } 650 651 private FileObject source; 652 653 private String performTest(String sourceCode, final int offset, final UiUtilsCaller validator, boolean tooltip) throws Exception { 654 GoToSupport.CALLER = validator; 655 656 FileObject root = makeScratchDir(this); 657 658 FileObject sourceDir = root.createFolder("src"); 659 FileObject buildDir = root.createFolder("build"); 660 FileObject cacheDir = root.createFolder("cache"); 661 FileObject testDir = sourceDir.createFolder("test"); 662 663 source = testDir.createData("Test.java"); 664 665 FileObject auxiliarySource = testDir.createData("Auxiliary.java"); 666 667 writeIntoFile(source, sourceCode); 668 writeIntoFile(auxiliarySource, "package test; public class Auxiliary {}"); 670 SourceUtilsTestUtil.prepareTest(sourceDir, buildDir, cacheDir, new FileObject[0]); 671 SourceUtilsTestUtil.compileRecursively(sourceDir); 672 673 DataObject od = DataObject.find(source); 674 EditorCookie ec = od.getCookie(EditorCookie.class); 675 Document doc = ec.openDocument(); 676 677 doc.putProperty(Language.class, JavaTokenId.language()); 678 679 if (tooltip) 680 return GoToSupport.getGoToElementTooltip(doc, offset, false); 681 else 682 GoToSupport.goTo(doc, offset, false); 683 684 return null; 685 } 686 687 692 public static FileObject makeScratchDir(NbTestCase test) throws IOException { 693 test.clearWorkDir(); 694 File root = test.getWorkDir(); 695 assert root.isDirectory() && root.list().length == 0; 696 FileObject fo = FileUtil.toFileObject(root); 697 if (fo != null) { 698 return fo; 700 } else { 701 LocalFileSystem lfs = new LocalFileSystem(); 703 try { 704 lfs.setRootDirectory(root); 705 } catch (PropertyVetoException e) { 706 assert false : e; 707 } 708 Repository.getDefault().addFileSystem(lfs); 709 return lfs.getRoot(); 710 } 711 } 712 713 } 714 | Popular Tags |