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