1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.ClassTree; 22 import com.sun.source.tree.CompilationUnitTree; 23 import com.sun.source.tree.MethodTree; 24 import com.sun.source.tree.Tree; 25 import java.io.File ; 26 import java.io.IOException ; 27 import org.netbeans.api.java.source.CancellableTask; 28 import org.netbeans.api.java.source.JavaSource; 29 import org.netbeans.api.java.source.JavaSource.Phase; 30 import org.netbeans.api.java.source.TestUtilities; 31 import org.netbeans.api.java.source.TreeMaker; 32 import org.netbeans.api.java.source.WorkingCopy; 33 import org.netbeans.junit.NbTestSuite; 34 35 40 public class MethodThrowsTest extends GeneratorTestMDRCompat { 41 42 43 public MethodThrowsTest(String testName) { 44 super(testName); 45 } 46 47 public static NbTestSuite suite() { 48 NbTestSuite suite = new NbTestSuite(); 49 suite.addTest(new MethodThrowsTest("testAddFirst")); 51 suite.addTest(new MethodThrowsTest("testAddFirstToExisting")); 52 suite.addTest(new MethodThrowsTest("testAddFirstTwo")); 53 suite.addTest(new MethodThrowsTest("testAddThirdToExisting")); 54 suite.addTest(new MethodThrowsTest("testRemoveMid")); 56 suite.addTest(new MethodThrowsTest("testRemoveLast")); 58 suite.addTest(new MethodThrowsTest("testRemoveJust")); 59 suite.addTest(new MethodThrowsTest("testStupid")); 60 suite.addTest(new MethodThrowsTest("testTuner")); 61 suite.addTest(new MethodThrowsTest("testAddFirstToBadFormatted")); 62 return suite; 63 } 64 65 public void testAddFirst() throws Exception { 66 testFile = new File (getWorkDir(), "Test.java"); 67 TestUtilities.copyStringToFile(testFile, 68 "package hierbas.del.litoral;\n\n" + 69 "import java.io.*;\n\n" + 70 "public class Test {\n" + 71 " public void taragui() {\n" + 72 " }\n" + 73 "}\n" 74 ); 75 String golden = 76 "package hierbas.del.litoral;\n\n" + 77 "import java.io.*;\n\n" + 78 "public class Test {\n" + 79 " public void taragui() throws IOException {\n" + 80 " }\n" + 81 "}\n"; 82 83 JavaSource src = getJavaSource(testFile); 84 CancellableTask task = new CancellableTask<WorkingCopy>() { 85 86 public void run(WorkingCopy workingCopy) throws IOException { 87 workingCopy.toPhase(Phase.RESOLVED); 88 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 89 TreeMaker make = workingCopy.getTreeMaker(); 90 91 for (Tree typeDecl : cut.getTypeDecls()) { 92 ClassTree clazz = (ClassTree) typeDecl; 94 MethodTree method = (MethodTree) clazz.getMembers().get(1); 95 MethodTree copy = make.addMethodThrows( 96 method, make.Identifier("IOException") 97 ); 98 workingCopy.rewrite(method, copy); 99 } 100 } 101 102 public void cancel() { 103 } 104 }; 105 src.runModificationTask(task).commit(); 106 String res = TestUtilities.copyFileToString(testFile); 107 System.err.println(res); 108 assertEquals(golden, res); 109 } 110 111 public void testAddFirstTwo() throws Exception { 112 testFile = new File (getWorkDir(), "Test.java"); 113 TestUtilities.copyStringToFile(testFile, 114 "package hierbas.del.litoral;\n\n" + 115 "import java.io.*;\n\n" + 116 "public class Test {\n" + 117 " public void taragui() {\n" + 118 " }\n" + 119 "}\n" 120 ); 121 String golden = 122 "package hierbas.del.litoral;\n\n" + 123 "import java.io.*;\n\n" + 124 "public class Test {\n" + 125 " public void taragui() throws IOException, FileNotFoundException {\n" + 126 " }\n" + 127 "}\n"; 128 129 JavaSource src = getJavaSource(testFile); 130 CancellableTask task = new CancellableTask<WorkingCopy>() { 131 132 public void run(WorkingCopy workingCopy) throws IOException { 133 workingCopy.toPhase(Phase.RESOLVED); 134 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 135 TreeMaker make = workingCopy.getTreeMaker(); 136 137 for (Tree typeDecl : cut.getTypeDecls()) { 138 ClassTree clazz = (ClassTree) typeDecl; 140 MethodTree method = (MethodTree) clazz.getMembers().get(1); 141 MethodTree copy = make.addMethodThrows( 142 method, make.Identifier("IOException") 143 ); 144 copy = make.addMethodThrows( 145 copy, make.Identifier("FileNotFoundException") 146 ); 147 workingCopy.rewrite(method, copy); 148 } 149 } 150 151 public void cancel() { 152 } 153 }; 154 src.runModificationTask(task).commit(); 155 String res = TestUtilities.copyFileToString(testFile); 156 assertEquals(golden, res); 157 } 158 159 public void testRemoveAll() throws Exception { 160 testFile = new File (getWorkDir(), "Test.java"); 161 TestUtilities.copyStringToFile(testFile, 162 "package hierbas.del.litoral;\n\n" + 163 "import java.io.*;\n\n" + 164 "public class Test {\n" + 165 " public void taragui() throws IOException, Exception, FileNotFoundException {\n" + 166 " }\n" + 167 "}\n" 168 ); 169 String golden = 170 "package hierbas.del.litoral;\n\n" + 171 "import java.io.*;\n\n" + 172 "public class Test {\n" + 173 " public void taragui() {\n" + 174 " }\n" + 175 "}\n"; 176 177 JavaSource src = getJavaSource(testFile); 178 CancellableTask task = new CancellableTask<WorkingCopy>() { 179 180 public void run(WorkingCopy workingCopy) throws IOException { 181 workingCopy.toPhase(Phase.RESOLVED); 182 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 183 TreeMaker make = workingCopy.getTreeMaker(); 184 185 for (Tree typeDecl : cut.getTypeDecls()) { 186 ClassTree clazz = (ClassTree) typeDecl; 188 MethodTree method = (MethodTree) clazz.getMembers().get(1); 189 MethodTree copy = make.removeMethodThrows(method, 0); 190 copy = make.removeMethodThrows(copy, 0); 191 copy = make.removeMethodThrows(copy, 0); 192 workingCopy.rewrite(method, copy); 193 } 194 } 195 196 public void cancel() { 197 } 198 }; 199 src.runModificationTask(task).commit(); 200 String res = TestUtilities.copyFileToString(testFile); 201 System.err.println(res); 202 assertEquals(golden, res); 203 204 } 205 206 207 public void testAddThirdToExisting() throws Exception { 208 testFile = new File (getWorkDir(), "Test.java"); 209 TestUtilities.copyStringToFile(testFile, 210 "package hierbas.del.litoral;\n\n" + 211 "import java.io.*;\n\n" + 212 "public class Test {\n" + 213 " public void taragui() throws IOException, FileNotFoundException {\n" + 214 " }\n" + 215 "}\n" 216 ); 217 String golden = 218 "package hierbas.del.litoral;\n\n" + 219 "import java.io.*;\n\n" + 220 "public class Test {\n" + 221 " public void taragui() throws IOException, FileNotFoundException, Exception {\n" + 222 " }\n" + 223 "}\n"; 224 225 JavaSource src = getJavaSource(testFile); 226 CancellableTask task = new CancellableTask<WorkingCopy>() { 227 228 public void run(WorkingCopy workingCopy) throws IOException { 229 workingCopy.toPhase(Phase.RESOLVED); 230 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 231 TreeMaker make = workingCopy.getTreeMaker(); 232 233 for (Tree typeDecl : cut.getTypeDecls()) { 234 ClassTree clazz = (ClassTree) typeDecl; 236 MethodTree method = (MethodTree) clazz.getMembers().get(1); 237 MethodTree copy = make.addMethodThrows( 238 method, make.Identifier("Exception") 239 ); 240 workingCopy.rewrite(method, copy); 241 } 242 } 243 244 public void cancel() { 245 } 246 }; 247 src.runModificationTask(task).commit(); 248 String res = TestUtilities.copyFileToString(testFile); 249 System.err.println(res); 250 assertEquals(golden, res); 251 } 252 253 public void testAddFirstToExisting() throws Exception { 254 testFile = new File (getWorkDir(), "Test.java"); 255 TestUtilities.copyStringToFile(testFile, 256 "package hierbas.del.litoral;\n\n" + 257 "import java.io.*;\n\n" + 258 "public class Test {\n" + 259 " public void taragui() throws IOException {\n" + 260 " }\n" + 261 "}\n" 262 ); 263 String golden = 264 "package hierbas.del.litoral;\n\n" + 265 "import java.io.*;\n\n" + 266 "public class Test {\n" + 267 " public void taragui() throws FileNotFoundException, IOException {\n" + 268 " }\n" + 269 "}\n"; 270 271 JavaSource src = getJavaSource(testFile); 272 CancellableTask task = new CancellableTask<WorkingCopy>() { 273 274 public void run(WorkingCopy workingCopy) throws IOException { 275 workingCopy.toPhase(Phase.RESOLVED); 276 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 277 TreeMaker make = workingCopy.getTreeMaker(); 278 279 for (Tree typeDecl : cut.getTypeDecls()) { 280 ClassTree clazz = (ClassTree) typeDecl; 282 MethodTree method = (MethodTree) clazz.getMembers().get(1); 283 MethodTree copy = make.insertMethodThrows( 284 method, 0, make.Identifier("FileNotFoundException") 285 ); 286 workingCopy.rewrite(method, copy); 287 } 288 } 289 290 public void cancel() { 291 } 292 }; 293 src.runModificationTask(task).commit(); 294 String res = TestUtilities.copyFileToString(testFile); 295 System.err.println(res); 296 assertEquals(golden, res); 297 } 298 299 public void testRemoveMid() throws Exception { 300 testFile = new File (getWorkDir(), "Test.java"); 301 TestUtilities.copyStringToFile(testFile, 302 "package hierbas.del.litoral;\n\n" + 303 "import java.io.*;\n\n" + 304 "public class Test {\n" + 305 " public void taragui() throws IOException, FileNotFoundException, Exception {\n" + 306 " }\n" + 307 "}\n" 308 ); 309 String golden = 310 "package hierbas.del.litoral;\n\n" + 311 "import java.io.*;\n\n" + 312 "public class Test {\n" + 313 " public void taragui() throws IOException, Exception {\n" + 314 " }\n" + 315 "}\n"; 316 317 JavaSource src = getJavaSource(testFile); 318 CancellableTask task = new CancellableTask<WorkingCopy>() { 319 320 public void run(WorkingCopy workingCopy) throws IOException { 321 workingCopy.toPhase(Phase.RESOLVED); 322 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 323 TreeMaker make = workingCopy.getTreeMaker(); 324 325 for (Tree typeDecl : cut.getTypeDecls()) { 326 ClassTree clazz = (ClassTree) typeDecl; 328 MethodTree method = (MethodTree) clazz.getMembers().get(1); 329 MethodTree copy = make.removeMethodThrows(method, 1); 330 workingCopy.rewrite(method, copy); 331 } 332 } 333 334 public void cancel() { 335 } 336 }; 337 src.runModificationTask(task).commit(); 338 String res = TestUtilities.copyFileToString(testFile); 339 assertEquals(golden, res); 340 } 341 342 public void testRemoveFirst() throws Exception { 343 testFile = new File (getWorkDir(), "Test.java"); 344 TestUtilities.copyStringToFile(testFile, 345 "package hierbas.del.litoral;\n\n" + 346 "import java.io.*;\n\n" + 347 "public class Test {\n" + 348 " public void taragui() throws IOException {\n" + 349 " }\n" + 350 "}\n" 351 ); 352 String golden = 353 "package hierbas.del.litoral;\n\n" + 354 "import java.io.*;\n\n" + 355 "public class Test {\n" + 356 " public void taragui() {\n" + 357 " }\n" + 358 "}\n"; 359 360 JavaSource src = getJavaSource(testFile); 361 CancellableTask task = new CancellableTask<WorkingCopy>() { 362 363 public void run(WorkingCopy workingCopy) throws IOException { 364 workingCopy.toPhase(Phase.RESOLVED); 365 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 366 TreeMaker make = workingCopy.getTreeMaker(); 367 368 for (Tree typeDecl : cut.getTypeDecls()) { 369 ClassTree clazz = (ClassTree) typeDecl; 371 MethodTree method = (MethodTree) clazz.getMembers().get(1); 372 MethodTree copy = make.removeMethodThrows(method, 0); 373 workingCopy.rewrite(method, copy); 374 } 375 } 376 377 public void cancel() { 378 } 379 }; 380 src.runModificationTask(task).commit(); 381 String res = TestUtilities.copyFileToString(testFile); 382 System.err.println(res); 383 assertEquals(golden, res); 384 } 385 386 public void testRemoveLast() throws Exception { 387 testFile = new File (getWorkDir(), "Test.java"); 388 TestUtilities.copyStringToFile(testFile, 389 "package hierbas.del.litoral;\n\n" + 390 "import java.io.*;\n\n" + 391 "public class Test {\n" + 392 " public void taragui() throws IOException, FileNotFoundException, Exception {\n" + 393 " }\n" + 394 "}\n" 395 ); 396 String golden = 397 "package hierbas.del.litoral;\n\n" + 398 "import java.io.*;\n\n" + 399 "public class Test {\n" + 400 " public void taragui() throws IOException, FileNotFoundException {\n" + 401 " }\n" + 402 "}\n"; 403 404 JavaSource src = getJavaSource(testFile); 405 CancellableTask task = new CancellableTask<WorkingCopy>() { 406 407 public void run(WorkingCopy workingCopy) throws IOException { 408 workingCopy.toPhase(Phase.RESOLVED); 409 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 410 TreeMaker make = workingCopy.getTreeMaker(); 411 412 for (Tree typeDecl : cut.getTypeDecls()) { 413 ClassTree clazz = (ClassTree) typeDecl; 415 MethodTree method = (MethodTree) clazz.getMembers().get(1); 416 MethodTree copy = make.removeMethodThrows(method, 2); 417 workingCopy.rewrite(method, copy); 418 } 419 } 420 421 public void cancel() { 422 } 423 }; 424 src.runModificationTask(task).commit(); 425 String res = TestUtilities.copyFileToString(testFile); 426 System.err.println(res); 427 assertEquals(golden, res); 428 } 429 430 public void testRemoveJust() throws Exception { 431 testFile = new File (getWorkDir(), "Test.java"); 432 TestUtilities.copyStringToFile(testFile, 433 "package hierbas.del.litoral;\n\n" + 434 "import java.io.*;\n\n" + 435 "public class Test {\n" + 436 " public void taragui() throws Exception,IOException,FileNotFoundException {\n" + 437 " }\n" + 438 "}\n" 439 ); 440 String golden = 441 "package hierbas.del.litoral;\n\n" + 442 "import java.io.*;\n\n" + 443 "public class Test {\n" + 444 " public void taragui() throws IOException,FileNotFoundException {\n" + 445 " }\n" + 446 "}\n"; 447 448 JavaSource src = getJavaSource(testFile); 449 CancellableTask task = new CancellableTask<WorkingCopy>() { 450 451 public void run(WorkingCopy workingCopy) throws IOException { 452 workingCopy.toPhase(Phase.RESOLVED); 453 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 454 TreeMaker make = workingCopy.getTreeMaker(); 455 456 for (Tree typeDecl : cut.getTypeDecls()) { 457 ClassTree clazz = (ClassTree) typeDecl; 459 MethodTree method = (MethodTree) clazz.getMembers().get(1); 460 MethodTree copy = make.removeMethodThrows(method, 0); 461 workingCopy.rewrite(method, copy); 462 } 463 } 464 465 public void cancel() { 466 } 467 }; 468 src.runModificationTask(task).commit(); 469 String res = TestUtilities.copyFileToString(testFile); 470 System.err.println(res); 471 assertEquals(golden, res); 472 } 473 474 public void testStupid() throws Exception { 475 testFile = new File (getWorkDir(), "Test.java"); 476 TestUtilities.copyStringToFile(testFile, 477 "package hierbas.del.litoral;\n\n" + 478 "import java.io.*;\n\n" + 479 "public class Test {\n" + 480 " public void taragui() throws Exception,IOException // karel\n" + 481 " {\n" + 482 " }\n" + 483 "}\n" 484 ); 485 String golden = 486 "package hierbas.del.litoral;\n\n" + 487 "import java.io.*;\n\n" + 488 "public class Test {\n" + 489 " public void taragui() throws Exception,IOException, FileNotFoundException // karel\n" + 490 " {\n" + 491 " }\n" + 492 "}\n"; 493 494 JavaSource src = getJavaSource(testFile); 495 CancellableTask task = new CancellableTask<WorkingCopy>() { 496 497 public void run(WorkingCopy workingCopy) throws IOException { 498 workingCopy.toPhase(Phase.RESOLVED); 499 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 500 TreeMaker make = workingCopy.getTreeMaker(); 501 502 for (Tree typeDecl : cut.getTypeDecls()) { 503 ClassTree clazz = (ClassTree) typeDecl; 505 MethodTree method = (MethodTree) clazz.getMembers().get(1); 506 MethodTree copy = make.addMethodThrows(method, make.Identifier("FileNotFoundException")); 507 workingCopy.rewrite(method, copy); 508 } 509 } 510 511 public void cancel() { 512 } 513 }; 514 src.runModificationTask(task).commit(); 515 String res = TestUtilities.copyFileToString(testFile); 516 System.err.println(res); 517 assertEquals(golden, res); 518 } 519 520 public void testTuner() throws Exception { 521 testFile = new File (getWorkDir(), "Test.java"); 522 TestUtilities.copyStringToFile(testFile, 523 "package hierbas.del.litoral;\n\n" + 524 "import java.io.*;\n\n" + 525 "public class Test {\n" + 526 " public void taragui() throws Exception, // karel\n" + 527 " IOException,\n" + 528 " PermissionDeniedException,\n" + 529 " FileNotFoundException // charles\n" + 530 " {\n" + 531 " }\n" + 532 "}\n" 533 ); 534 String golden = 535 "package hierbas.del.litoral;\n\n" + 536 "import java.io.*;\n\n" + 537 "public class Test {\n" + 538 " public void taragui() throws Exception, // karel\n" + 539 " IOException,\n" + 540 " PermissionDeniedException,\n" + 541 " FileNotFoundException, \n" + 542 " NullPointerException // charles\n" + 543 " {\n" + 544 " }\n" + 545 "}\n"; 546 547 JavaSource src = getJavaSource(testFile); 548 CancellableTask task = new CancellableTask<WorkingCopy>() { 549 550 public void run(WorkingCopy workingCopy) throws IOException { 551 workingCopy.toPhase(Phase.RESOLVED); 552 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 553 TreeMaker make = workingCopy.getTreeMaker(); 554 555 for (Tree typeDecl : cut.getTypeDecls()) { 556 ClassTree clazz = (ClassTree) typeDecl; 558 MethodTree method = (MethodTree) clazz.getMembers().get(1); 559 MethodTree copy = make.addMethodThrows(method, make.Identifier("NullPointerException")); 560 workingCopy.rewrite(method, copy); 561 } 562 } 563 564 public void cancel() { 565 } 566 }; 567 src.runModificationTask(task).commit(); 568 String res = TestUtilities.copyFileToString(testFile); 569 System.err.println(res); 570 assertEquals(golden, res); 571 } 572 573 public void testAddFirstToBadFormatted() throws Exception { 574 testFile = new File (getWorkDir(), "Test.java"); 575 TestUtilities.copyStringToFile(testFile, 576 "package hierbas.del.litoral;\n\n" + 577 "import java.io.*;\n\n" + 578 "public class Test {\n" + 579 " public void taragui(){\n" + 580 " }\n" + 581 "}\n" 582 ); 583 String golden = 584 "package hierbas.del.litoral;\n\n" + 585 "import java.io.*;\n\n" + 586 "public class Test {\n" + 587 " public void taragui() throws IOException{\n" + 588 " }\n" + 589 "}\n"; 590 591 JavaSource src = getJavaSource(testFile); 592 CancellableTask task = new CancellableTask<WorkingCopy>() { 593 594 public void run(WorkingCopy workingCopy) throws IOException { 595 workingCopy.toPhase(Phase.RESOLVED); 596 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 597 TreeMaker make = workingCopy.getTreeMaker(); 598 599 for (Tree typeDecl : cut.getTypeDecls()) { 600 ClassTree clazz = (ClassTree) typeDecl; 602 MethodTree method = (MethodTree) clazz.getMembers().get(1); 603 if ("taragui".contentEquals(method.getName())) { 604 MethodTree copy = make.addMethodThrows( 605 method, make.Identifier("IOException") 606 ); 607 workingCopy.rewrite(method, copy); 608 } 609 } 610 } 611 612 public void cancel() { 613 } 614 }; 615 src.runModificationTask(task).commit(); 616 String res = TestUtilities.copyFileToString(testFile); 617 System.err.println(res); 618 assertEquals(golden, res); 619 } 620 621 protected void setUp() throws Exception { 622 super.setUp(); 623 testFile = getFile(getSourceDir(), getSourcePckg() + "Test.java"); 624 } 625 626 String getGoldenPckg() { 627 return ""; 628 } 629 630 String getSourcePckg() { 631 return ""; 632 } 633 634 } 635 | Popular Tags |