1 19 package org.netbeans.api.java.source.gen; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.Collections ; 24 import javax.lang.model.element.Modifier; 25 import com.sun.source.tree.*; 26 import org.netbeans.api.java.source.*; 27 import org.netbeans.junit.NbTestSuite; 28 import static org.netbeans.api.java.source.JavaSource.*; 29 30 35 public class MethodParametersTest extends GeneratorTestMDRCompat { 36 37 38 public MethodParametersTest(String testName) { 39 super(testName); 40 } 41 42 public static NbTestSuite suite() { 43 NbTestSuite suite = new NbTestSuite(); 44 suite.addTest(new MethodParametersTest("testAddFirst")); 47 suite.addTest(new MethodParametersTest("testAddToIndex0")); 48 suite.addTest(new MethodParametersTest("testRemoveFirstTwo")); 49 suite.addTest(new MethodParametersTest("testRemoveLast")); 50 suite.addTest(new MethodParametersTest("testRemoveLastTwo")); 51 suite.addTest(new MethodParametersTest("testRemoveMid")); 52 suite.addTest(new MethodParametersTest("testSwap")); 53 suite.addTest(new MethodParametersTest("testRenameInTypePar")); 54 suite.addTest(new MethodParametersTest("testRenameInParameterizedType")); 55 return suite; 56 } 57 58 public void testAddInsertReplaceParameters() throws Exception { 59 testFile = new File (getWorkDir(), "Test.java"); 60 TestUtilities.copyStringToFile(testFile, 61 "package hierbas.del.litoral;\n\n" + 62 "import java.io.File;\n\n" + 63 "public class Test {\n" + 64 " public void taragui(int a, long c, String s) {\n" + 65 " }\n" + 66 "}\n" 67 ); 68 String golden = 69 "package hierbas.del.litoral;\n\n" + 70 "import java.io.File;\n\n" + 71 "public class Test {\n" + 72 " public void taragui(final File elaborada,File marcela, long c, String s,File cedron) {\n" + 73 " }\n" + 74 "}\n"; 75 76 JavaSource src = getJavaSource(testFile); 77 78 CancellableTask task = new CancellableTask<WorkingCopy>() { 79 80 public void run(WorkingCopy workingCopy) throws IOException { 81 workingCopy.toPhase(Phase.RESOLVED); 82 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 83 TreeMaker make = workingCopy.getTreeMaker(); 84 for (Tree typeDecl : cut.getTypeDecls()) { 85 if (Tree.Kind.CLASS == typeDecl.getKind()) { 87 ClassTree clazz = (ClassTree) typeDecl; 88 MethodTree node = (MethodTree) clazz.getMembers().get(1); 89 MethodTree copy = make.insertMethodParameter( 90 node, 0, 91 make.Variable( 92 make.Modifiers( 93 Collections.singleton(Modifier.FINAL), 94 Collections.<AnnotationTree>emptyList() 95 ), 96 "elaborada", 97 make.Identifier("File"), 98 null 99 ) 100 ); 101 copy = make.removeMethodParameter(copy, 1); 102 copy = make.addMethodParameter( 103 copy, 104 make.Variable( 105 make.Modifiers( 106 Collections.<Modifier>emptySet(), 107 Collections.<AnnotationTree>emptyList() 108 ), 109 "cedron", 110 make.Identifier("File"), 111 null 112 ) 113 ); 114 copy = make.insertMethodParameter( 115 copy, 116 1, 117 make.Variable( 118 make.Modifiers( 119 Collections.<Modifier>emptySet(), 120 Collections.<AnnotationTree>emptyList() 121 ), 122 "marcela", 123 make.Identifier("File"), 124 null 125 ) 126 ); 127 workingCopy.rewrite(node, copy); 128 } 129 } 130 } 131 132 public void cancel() { 133 } 134 }; 135 src.runModificationTask(task).commit(); 136 String res = TestUtilities.copyFileToString(testFile); 137 System.err.println(res); 138 assertEquals(golden, res); 139 } 140 141 public void testAddFirst() throws Exception { 142 testFile = new File (getWorkDir(), "Test.java"); 143 TestUtilities.copyStringToFile(testFile, 144 "package hierbas.del.litoral;\n\n" + 145 "import java.io.File;\n\n" + 146 "public class Test {\n" + 147 " public void taragui() {\n" + 148 " }\n" + 149 "}\n" 150 ); 151 String golden = 152 "package hierbas.del.litoral;\n\n" + 153 "import java.io.File;\n\n" + 154 "public class Test {\n" + 155 " public void taragui(final File elaborada) {\n" + 156 " }\n" + 157 "}\n"; 158 159 JavaSource src = getJavaSource(testFile); 160 161 CancellableTask task = new CancellableTask<WorkingCopy>() { 162 163 public void run(WorkingCopy workingCopy) throws IOException { 164 workingCopy.toPhase(Phase.RESOLVED); 165 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 166 TreeMaker make = workingCopy.getTreeMaker(); 167 for (Tree typeDecl : cut.getTypeDecls()) { 168 if (Tree.Kind.CLASS == typeDecl.getKind()) { 170 ClassTree clazz = (ClassTree) typeDecl; 171 MethodTree node = (MethodTree) clazz.getMembers().get(1); 172 MethodTree copy = make.insertMethodParameter( 173 node, 0, 174 make.Variable( 175 make.Modifiers( 176 Collections.singleton(Modifier.FINAL), 177 Collections.<AnnotationTree>emptyList() 178 ), 179 "elaborada", 180 make.Identifier("File"), 181 null 182 ) 183 ); 184 workingCopy.rewrite(node, copy); 185 } 186 } 187 } 188 189 public void cancel() { 190 } 191 }; 192 src.runModificationTask(task).commit(); 193 String res = TestUtilities.copyFileToString(testFile); 194 System.err.println(res); 195 assertEquals(golden, res); 196 } 197 198 public void testAddToIndex0() throws Exception { 199 testFile = new File (getWorkDir(), "Test.java"); 200 TestUtilities.copyStringToFile(testFile, 201 "package hierbas.del.litoral;\n\n" + 202 "import java.io.File;\n\n" + 203 "public class Test {\n" + 204 " public void taragui(final File carqueja) {\n" + 205 " }\n" + 206 "}\n" 207 ); 208 String golden = 209 "package hierbas.del.litoral;\n\n" + 210 "import java.io.File;\n\n" + 211 "public class Test {\n" + 212 " public void taragui(final File elaborada,final File carqueja) {\n" + 213 " }\n" + 214 "}\n"; 215 216 JavaSource src = getJavaSource(testFile); 217 218 CancellableTask task = new CancellableTask<WorkingCopy>() { 219 220 public void run(WorkingCopy workingCopy) throws IOException { 221 workingCopy.toPhase(Phase.RESOLVED); 222 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 223 TreeMaker make = workingCopy.getTreeMaker(); 224 for (Tree typeDecl : cut.getTypeDecls()) { 225 if (Tree.Kind.CLASS == typeDecl.getKind()) { 227 ClassTree clazz = (ClassTree) typeDecl; 228 MethodTree node = (MethodTree) clazz.getMembers().get(1); 229 MethodTree copy = make.insertMethodParameter( 230 node, 0, 231 make.Variable( 232 make.Modifiers( 233 Collections.singleton(Modifier.FINAL), 234 Collections.<AnnotationTree>emptyList() 235 ), 236 "elaborada", 237 make.Identifier("File"), 238 null 239 ) 240 ); 241 workingCopy.rewrite(node, copy); 242 } 243 } 244 } 245 246 public void cancel() { 247 } 248 }; 249 src.runModificationTask(task).commit(); 250 String res = TestUtilities.copyFileToString(testFile); 251 System.err.println(res); 252 assertEquals(golden, res); 253 } 254 255 public void testRemoveLast() throws Exception { 256 testFile = new File (getWorkDir(), "Test.java"); 257 TestUtilities.copyStringToFile(testFile, 258 "package hierbas.del.litoral;\n\n" + 259 "import java.io.File;\n\n" + 260 "public class Test {\n" + 261 " public void taragui(int b) {\n" + 262 " }\n" + 263 "}\n" 264 ); 265 String golden = 266 "package hierbas.del.litoral;\n\n" + 267 "import java.io.File;\n\n" + 268 "public class Test {\n" + 269 " public void taragui() {\n" + 270 " }\n" + 271 "}\n"; 272 273 JavaSource src = getJavaSource(testFile); 274 275 CancellableTask task = new CancellableTask<WorkingCopy>() { 276 277 public void run(WorkingCopy workingCopy) throws IOException { 278 workingCopy.toPhase(Phase.RESOLVED); 279 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 280 TreeMaker make = workingCopy.getTreeMaker(); 281 for (Tree typeDecl : cut.getTypeDecls()) { 282 if (Tree.Kind.CLASS == typeDecl.getKind()) { 284 ClassTree clazz = (ClassTree) typeDecl; 285 MethodTree node = (MethodTree) clazz.getMembers().get(1); 286 MethodTree copy = make.removeMethodParameter( 287 node, 0 288 ); 289 workingCopy.rewrite(node, copy); 290 } 291 } 292 } 293 294 public void cancel() { 295 } 296 }; 297 src.runModificationTask(task).commit(); 298 String res = TestUtilities.copyFileToString(testFile); 299 System.err.println(res); 300 assertEquals(golden, res); 301 } 302 303 public void testRemoveMid() throws Exception { 304 testFile = new File (getWorkDir(), "Test.java"); 305 TestUtilities.copyStringToFile(testFile, 306 "package hierbas.del.litoral;\n\n" + 307 "import java.io.File;\n\n" + 308 "public class Test {\n" + 309 " public void taragui(int para, int empezar, int sugerimos) {\n" + 310 " }\n" + 311 "}\n" 312 ); 313 String golden = 314 "package hierbas.del.litoral;\n\n" + 315 "import java.io.File;\n\n" + 316 "public class Test {\n" + 317 " public void taragui(int para, int sugerimos) {\n" + 318 " }\n" + 319 "}\n"; 320 321 JavaSource src = getJavaSource(testFile); 322 323 CancellableTask task = new CancellableTask<WorkingCopy>() { 324 325 public void run(WorkingCopy workingCopy) throws IOException { 326 workingCopy.toPhase(Phase.RESOLVED); 327 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 328 TreeMaker make = workingCopy.getTreeMaker(); 329 for (Tree typeDecl : cut.getTypeDecls()) { 330 if (Tree.Kind.CLASS == typeDecl.getKind()) { 332 ClassTree clazz = (ClassTree) typeDecl; 333 MethodTree node = (MethodTree) clazz.getMembers().get(1); 334 MethodTree copy = make.removeMethodParameter( 335 node, 1 336 ); 337 workingCopy.rewrite(node, copy); 338 } 339 } 340 } 341 342 public void cancel() { 343 } 344 }; 345 src.runModificationTask(task).commit(); 346 String res = TestUtilities.copyFileToString(testFile); 347 System.err.println(res); 348 assertEquals(golden, res); 349 } 350 351 public void testRemoveLastTwo() throws Exception { 352 testFile = new File (getWorkDir(), "Test.java"); 353 TestUtilities.copyStringToFile(testFile, 354 "package hierbas.del.litoral;\n\n" + 355 "import java.io.File;\n\n" + 356 "public class Test {\n" + 357 " public void taragui(int para, int empezar, int sugerimos) {\n" + 358 " }\n" + 359 "}\n" 360 ); 361 String golden = 362 "package hierbas.del.litoral;\n\n" + 363 "import java.io.File;\n\n" + 364 "public class Test {\n" + 365 " public void taragui(int para) {\n" + 366 " }\n" + 367 "}\n"; 368 369 JavaSource src = getJavaSource(testFile); 370 371 CancellableTask task = new CancellableTask<WorkingCopy>() { 372 373 public void run(WorkingCopy workingCopy) throws IOException { 374 workingCopy.toPhase(Phase.RESOLVED); 375 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 376 TreeMaker make = workingCopy.getTreeMaker(); 377 for (Tree typeDecl : cut.getTypeDecls()) { 378 if (Tree.Kind.CLASS == typeDecl.getKind()) { 380 ClassTree clazz = (ClassTree) typeDecl; 381 MethodTree node = (MethodTree) clazz.getMembers().get(1); 382 MethodTree copy = make.removeMethodParameter(node, 1); 383 copy = make.removeMethodParameter(copy, 1); 384 workingCopy.rewrite(node, copy); 385 } 386 } 387 } 388 389 public void cancel() { 390 } 391 }; 392 src.runModificationTask(task).commit(); 393 String res = TestUtilities.copyFileToString(testFile); 394 System.err.println(res); 395 assertEquals(golden, res); 396 } 397 398 public void testRemoveFirstTwo() throws Exception { 399 testFile = new File (getWorkDir(), "Test.java"); 400 TestUtilities.copyStringToFile(testFile, 401 "package hierbas.del.litoral;\n\n" + 402 "import java.io.File;\n\n" + 403 "public class Test {\n" + 404 " public void taragui(int para, int empezar, int sugerimos) {\n" + 405 " }\n" + 406 "}\n" 407 ); 408 String golden = 409 "package hierbas.del.litoral;\n\n" + 410 "import java.io.File;\n\n" + 411 "public class Test {\n" + 412 " public void taragui( int sugerimos) {\n" + 413 " }\n" + 414 "}\n"; 415 416 JavaSource src = getJavaSource(testFile); 417 418 CancellableTask task = new CancellableTask<WorkingCopy>() { 419 420 public void run(WorkingCopy workingCopy) throws IOException { 421 workingCopy.toPhase(Phase.RESOLVED); 422 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 423 TreeMaker make = workingCopy.getTreeMaker(); 424 for (Tree typeDecl : cut.getTypeDecls()) { 425 if (Tree.Kind.CLASS == typeDecl.getKind()) { 427 ClassTree clazz = (ClassTree) typeDecl; 428 MethodTree node = (MethodTree) clazz.getMembers().get(1); 429 MethodTree copy = make.removeMethodParameter(node, 0); 430 copy = make.removeMethodParameter(copy, 0); 431 workingCopy.rewrite(node, copy); 432 } 433 } 434 } 435 436 public void cancel() { 437 } 438 }; 439 src.runModificationTask(task).commit(); 440 String res = TestUtilities.copyFileToString(testFile); 441 System.err.println(res); 442 assertEquals(golden, res); 443 } 444 445 public void testSwap() throws Exception { 446 testFile = new File (getWorkDir(), "Test.java"); 447 TestUtilities.copyStringToFile(testFile, 448 "package hierbas.del.litoral;\n\n" + 449 "import java.io.File;\n\n" + 450 "public class Test {\n" + 451 " public void taragui(int empezar, int sugerimos) {\n" + 452 " }\n" + 453 "}\n" 454 ); 455 String golden = 456 "package hierbas.del.litoral;\n\n" + 457 "import java.io.File;\n\n" + 458 "public class Test {\n" + 459 " public void taragui( int sugerimos,int empezar) {\n" + 460 " }\n" + 461 "}\n"; 462 463 JavaSource src = getJavaSource(testFile); 464 465 CancellableTask task = new CancellableTask<WorkingCopy>() { 466 467 public void run(WorkingCopy workingCopy) throws IOException { 468 workingCopy.toPhase(Phase.RESOLVED); 469 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 470 TreeMaker make = workingCopy.getTreeMaker(); 471 for (Tree typeDecl : cut.getTypeDecls()) { 472 if (Tree.Kind.CLASS == typeDecl.getKind()) { 474 ClassTree clazz = (ClassTree) typeDecl; 475 MethodTree node = (MethodTree) clazz.getMembers().get(1); 476 VariableTree vt = node.getParameters().get(0); 477 MethodTree copy = make.removeMethodParameter(node, 0); 478 copy = make.addMethodParameter(copy, vt); 479 workingCopy.rewrite(node, copy); 480 } 481 } 482 } 483 484 public void cancel() { 485 } 486 }; 487 src.runModificationTask(task).commit(); 488 String res = TestUtilities.copyFileToString(testFile); 489 System.err.println(res); 490 assertEquals(golden, res); 491 } 492 493 496 public void testRenameInTypePar() throws Exception { 497 testFile = new File (getWorkDir(), "Test.java"); 498 TestUtilities.copyStringToFile(testFile, 499 "package hierbas.del.litoral;\n\n" + 500 "import java.io.File;\n\n" + 501 "public class Test {\n" + 502 " public void taragui(List<Something> empezar, int sugerimos) {\n" + 503 " }\n" + 504 "}\n" 505 ); 506 String golden = 507 "package hierbas.del.litoral;\n\n" + 508 "import java.io.File;\n\n" + 509 "public class Test {\n" + 510 " public void taragui(List<Neco> empezar, int sugerimos) {\n" + 511 " }\n" + 512 "}\n"; 513 514 JavaSource src = getJavaSource(testFile); 515 516 CancellableTask task = new CancellableTask<WorkingCopy>() { 517 518 public void run(WorkingCopy workingCopy) throws IOException { 519 workingCopy.toPhase(Phase.RESOLVED); 520 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 521 TreeMaker make = workingCopy.getTreeMaker(); 522 for (Tree typeDecl : cut.getTypeDecls()) { 523 if (Tree.Kind.CLASS == typeDecl.getKind()) { 525 ClassTree clazz = (ClassTree) typeDecl; 526 MethodTree node = (MethodTree) clazz.getMembers().get(1); 527 VariableTree vt = node.getParameters().get(0); 528 ParameterizedTypeTree ptt = (ParameterizedTypeTree) vt.getType(); 529 IdentifierTree it = (IdentifierTree) ptt.getTypeArguments().get(0); 530 workingCopy.rewrite(it, make.Identifier("Neco")); 531 } 532 } 533 } 534 535 public void cancel() { 536 } 537 }; 538 src.runModificationTask(task).commit(); 539 String res = TestUtilities.copyFileToString(testFile); 540 System.err.println(res); 541 assertEquals(golden, res); 542 } 543 544 547 public void testRenameInParameterizedType() throws Exception { 548 testFile = new File (getWorkDir(), "Test.java"); 549 TestUtilities.copyStringToFile(testFile, 550 "package hierbas.del.litoral;\n\n" + 551 "import java.io.File;\n\n" + 552 "public class Test {\n" + 553 " public void taragui(List<Something> empezar, int sugerimos) {\n" + 554 " }\n" + 555 "}\n" 556 ); 557 String golden = 558 "package hierbas.del.litoral;\n\n" + 559 "import java.io.File;\n\n" + 560 "public class Test {\n" + 561 " public void taragui(Seznam<Something> empezar, int sugerimos) {\n" + 562 " }\n" + 563 "}\n"; 564 565 JavaSource src = getJavaSource(testFile); 566 567 CancellableTask task = new CancellableTask<WorkingCopy>() { 568 569 public void run(WorkingCopy workingCopy) throws IOException { 570 workingCopy.toPhase(Phase.RESOLVED); 571 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 572 TreeMaker make = workingCopy.getTreeMaker(); 573 for (Tree typeDecl : cut.getTypeDecls()) { 574 if (Tree.Kind.CLASS == typeDecl.getKind()) { 576 ClassTree clazz = (ClassTree) typeDecl; 577 MethodTree node = (MethodTree) clazz.getMembers().get(1); 578 VariableTree vt = node.getParameters().get(0); 579 ParameterizedTypeTree ptt = (ParameterizedTypeTree) vt.getType(); 580 workingCopy.rewrite(ptt.getType(), make.Identifier("Seznam")); 581 } 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 |