1 /******************************************************************************* 2 * Copyright (c) 2000, 2020 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.jdt.ui.tests.refactoring.ccp; 15 16 import static org.junit.Assert.assertEquals; 17 import static org.junit.Assert.assertFalse; 18 import static org.junit.Assert.assertNotNull; 19 import static org.junit.Assert.assertNull; 20 import static org.junit.Assert.assertTrue; 21 22 import java.io.IOException; 23 import java.util.Map; 24 25 import org.junit.Rule; 26 import org.junit.Test; 27 28 import org.eclipse.jdt.testplugin.JavaProjectHelper; 29 30 import org.eclipse.core.runtime.NullProgressMonitor; 31 import org.eclipse.core.runtime.OperationCanceledException; 32 33 import org.eclipse.core.resources.IContainer; 34 import org.eclipse.core.resources.IFile; 35 import org.eclipse.core.resources.IFolder; 36 import org.eclipse.core.resources.IProject; 37 import org.eclipse.core.resources.IResource; 38 import org.eclipse.core.resources.ResourcesPlugin; 39 40 import org.eclipse.ui.PlatformUI; 41 42 import org.eclipse.ltk.core.refactoring.Refactoring; 43 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 44 import org.eclipse.ltk.core.refactoring.participants.MoveArguments; 45 import org.eclipse.ltk.core.refactoring.participants.MoveRefactoring; 46 47 import org.eclipse.jdt.core.ICompilationUnit; 48 import org.eclipse.jdt.core.IField; 49 import org.eclipse.jdt.core.IInitializer; 50 import org.eclipse.jdt.core.IJavaElement; 51 import org.eclipse.jdt.core.IJavaProject; 52 import org.eclipse.jdt.core.IMethod; 53 import org.eclipse.jdt.core.IPackageFragment; 54 import org.eclipse.jdt.core.IPackageFragmentRoot; 55 import org.eclipse.jdt.core.IType; 56 import org.eclipse.jdt.core.JavaCore; 57 import org.eclipse.jdt.core.JavaModelException; 58 59 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 60 import org.eclipse.jdt.internal.corext.refactoring.reorg.IConfirmQuery; 61 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgDestination; 62 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy; 63 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgQueries; 64 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaMoveProcessor; 65 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgDestinationFactory; 66 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgPolicyFactory; 67 68 import org.eclipse.jdt.ui.tests.refactoring.GenericRefactoringTest; 69 import org.eclipse.jdt.ui.tests.refactoring.ParticipantTesting; 70 import org.eclipse.jdt.ui.tests.refactoring.rules.RefactoringTestSetup; 71 72 import org.eclipse.jdt.internal.ui.refactoring.reorg.CreateTargetQueries; 73 74 public class MoveTest extends GenericRefactoringTest { 75 private static final class ConfirmAllQuery implements IReorgQueries { 76 @Override createSkipQuery(String queryTitle, int queryID)77 public IConfirmQuery createSkipQuery(String queryTitle, int queryID) { 78 return new IConfirmQuery() { 79 @Override 80 public boolean confirm(String question) throws OperationCanceledException { 81 return false; 82 } 83 @Override 84 public boolean confirm(String question, Object[] elements) throws OperationCanceledException { 85 return false; 86 } 87 }; 88 } 89 90 @Override createYesNoQuery(String queryTitle, boolean allowCancel, int queryID)91 public IConfirmQuery createYesNoQuery(String queryTitle, boolean allowCancel, int queryID) { 92 return new IConfirmQuery() { 93 @Override 94 public boolean confirm(String question) throws OperationCanceledException { 95 return true; 96 } 97 @Override 98 public boolean confirm(String question, Object[] elements) throws OperationCanceledException { 99 return true; 100 } 101 }; 102 } 103 104 @Override 105 public IConfirmQuery createYesYesToAllNoNoToAllQuery(String queryTitle, boolean allowCancel, int queryID) { 106 return new IConfirmQuery() { 107 @Override 108 public boolean confirm(String question) throws OperationCanceledException { 109 return true; 110 } 111 @Override 112 public boolean confirm(String question, Object[] elements) throws OperationCanceledException { 113 return true; 114 } 115 }; 116 } 117 } 118 119 private static final class ConfirmNoneQuery implements IReorgQueries { 120 @Override 121 public IConfirmQuery createSkipQuery(String queryTitle, int queryID) { 122 return new IConfirmQuery() { 123 @Override 124 public boolean confirm(String question) throws OperationCanceledException { 125 return false; 126 } 127 @Override 128 public boolean confirm(String question, Object[] elements) throws OperationCanceledException { 129 return false; 130 } 131 }; 132 } 133 134 @Override 135 public IConfirmQuery createYesNoQuery(String queryTitle, boolean allowCancel, int queryID) { 136 return new IConfirmQuery() { 137 @Override 138 public boolean confirm(String question) throws OperationCanceledException { 139 return false; 140 } 141 @Override 142 public boolean confirm(String question, Object[] elements) throws OperationCanceledException { 143 return false; 144 } 145 }; 146 } 147 148 @Override 149 public IConfirmQuery createYesYesToAllNoNoToAllQuery(String queryTitle, boolean allowCancel, int queryID) { 150 return new IConfirmQuery() { 151 @Override 152 public boolean confirm(String question) throws OperationCanceledException { 153 return false; 154 } 155 @Override 156 public boolean confirm(String question, Object[] elements) throws OperationCanceledException { 157 return false; 158 } 159 }; 160 } 161 } 162 163 private static final String REFACTORING_PATH= "Move/"; 164 165 @Rule 166 public RefactoringTestSetup fts= new RefactoringTestSetup(); 167 168 @Override 169 public void genericbefore() throws Exception { 170 super.genericbefore(); 171 fIsPreDeltaTest= true; 172 } 173 174 @Override 175 protected String getRefactoringPath() { 176 return REFACTORING_PATH; 177 } 178 179 private IReorgQueries createReorgQueries(){ 180 return new MockReorgQueries(); 181 } 182 183 private RefactoringStatus performRefactoring(JavaMoveProcessor processor, boolean providesUndo) throws Exception { 184 return performRefactoring(new MoveRefactoring(processor), providesUndo); 185 } 186 187 private void verifyDisabled(IResource[] resources, IJavaElement[] javaElements) throws JavaModelException { 188 assertFalse("move should be disabled", RefactoringAvailabilityTester.isMoveAvailable(resources, javaElements)); 189 IMovePolicy policy= ReorgPolicyFactory.createMovePolicy(resources, javaElements); 190 JavaMoveProcessor processor= policy.canEnable() ? new JavaMoveProcessor(policy) : null; 191 assertNull(processor); 192 } 193 194 private JavaMoveProcessor verifyEnabled(IResource[] resources, IJavaElement[] javaElements, IReorgQueries reorgQueries) throws JavaModelException { 195 assertTrue("move should be enabled", RefactoringAvailabilityTester.isMoveAvailable(resources, javaElements)); 196 IMovePolicy policy= ReorgPolicyFactory.createMovePolicy(resources, javaElements); 197 JavaMoveProcessor processor= policy.canEnable() ? new JavaMoveProcessor(policy) : null; 198 if (reorgQueries != null) 199 processor.setReorgQueries(reorgQueries); 200 assertNotNull(processor); 201 return processor; 202 } 203 204 private void verifyValidDestination(JavaMoveProcessor ref, Object destination) throws Exception { 205 RefactoringStatus status= ref.setDestination(ReorgDestinationFactory.createDestination(destination)); 206 207 int severity= status.getSeverity(); 208 if (severity == RefactoringStatus.INFO) // see ReorgPolicyFactory.MoveFilesFoldersAndCusPolicy.verifyDestination(..) 209 return; 210 211 assertEquals("destination was expected to be valid: " + status.getMessageMatchingSeverity(severity), RefactoringStatus.OK, severity); 212 } 213 214 private void verifyInvalidDestination(JavaMoveProcessor ref, Object destination) throws Exception { 215 RefactoringStatus status= ref.setDestination(ReorgDestinationFactory.createDestination(destination)); 216 217 assertEquals("destination was expected to be not valid", RefactoringStatus.FATAL, status.getSeverity()); 218 } 219 220 @Test 221 public void testDisabled_empty() throws Exception { 222 IJavaElement[] javaElements= {}; 223 IResource[] resources= {}; 224 verifyDisabled(resources, javaElements); 225 } 226 227 @Test 228 public void testDisabled_null_element() throws Exception { 229 IJavaElement[] javaElements= {null}; 230 IResource[] resources= {}; 231 verifyDisabled(resources, javaElements); 232 } 233 234 @Test 235 public void testDisabled_null_resource() throws Exception { 236 IJavaElement[] javaElements= {}; 237 IResource[] resources= {null}; 238 verifyDisabled(resources, javaElements); 239 } 240 241 @Test 242 public void testDisabled_javaProject() throws Exception { 243 IJavaElement[] javaElements= {RefactoringTestSetup.getProject()}; 244 IResource[] resources= {}; 245 verifyDisabled(resources, javaElements); 246 } 247 248 @Test 249 public void testDisabled_defaultPackage() throws Exception { 250 IPackageFragment defaultPackage= getRoot().getPackageFragment(""); 251 assertTrue(defaultPackage.exists()); 252 IJavaElement[] javaElements= {defaultPackage}; 253 IResource[] resources= {}; 254 verifyDisabled(resources, javaElements); 255 } 256 257 @Test 258 public void testDisabled_project() throws Exception { 259 IJavaElement[] javaElements= {}; 260 IResource[] resources= {RefactoringTestSetup.getProject().getProject()}; 261 verifyDisabled(resources, javaElements); 262 } 263 264 @Test 265 public void testDisabled_notExistingElement() throws Exception { 266 ICompilationUnit notExistingCu= getPackageP().getCompilationUnit("NotMe.java"); 267 assertFalse(notExistingCu.exists()); 268 IJavaElement[] javaElements= {notExistingCu}; 269 IResource[] resources= {}; 270 verifyDisabled(resources, javaElements); 271 } 272 273 @Test 274 public void testDisabled_notExistingResource() throws Exception { 275 IFolder folder= (IFolder)getPackageP().getResource(); 276 IFile notExistingFile= folder.getFile("a.txt"); 277 278 IJavaElement[] javaElements= {}; 279 IResource[] resources= {notExistingFile}; 280 verifyDisabled(resources, javaElements); 281 } 282 283 @Test 284 public void testDisabled_noCommonParent0() throws Exception { 285 IJavaElement[] javaElements= {getPackageP(), getRoot()}; 286 IResource[] resources= {}; 287 verifyDisabled(resources, javaElements); 288 } 289 290 @Test 291 public void testDisabled_noCommonParent1() throws Exception { 292 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}}", false, new NullProgressMonitor()); 293 IType classA= cu.getType("A"); 294 IMethod methodFoo= classA.getMethod("foo", new String[0]); 295 IJavaElement[] javaElements= { classA, methodFoo }; 296 IResource[] resources= {}; 297 verifyDisabled(resources, javaElements); 298 } 299 300 // public void testDisabled_noCommonParent2() throws Exception { 301 // ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}}", false, new NullProgressMonitor()); 302 // IType classA= cu.getType("A"); 303 // IJavaElement[] javaElements= { classA, cu}; 304 // IResource[] resources= {}; 305 // verifyDisabled(resources, javaElements); 306 // } 307 308 @Test 309 public void testDisabled_noCommonParent3() throws Exception { 310 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}}", false, new NullProgressMonitor()); 311 IJavaElement[] javaElements= {cu, getPackageP()}; 312 IResource[] resources= {}; 313 verifyDisabled(resources, javaElements); 314 } 315 316 @Test 317 public void testDisabled_noCommonParent5() throws Exception { 318 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}}", false, new NullProgressMonitor()); 319 IJavaElement[] javaElements= {cu, getRoot()}; 320 IResource[] resources= {}; 321 verifyDisabled(resources, javaElements); 322 } 323 324 @Test 325 public void testDisabled_noCommonParent6() throws Exception { 326 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}}", false, new NullProgressMonitor()); 327 IJavaElement[] javaElements= {cu, getRoot()}; 328 IResource[] resources= {}; 329 verifyDisabled(resources, javaElements); 330 } 331 332 @Test 333 public void testDisabled_noCommonParent7() throws Exception { 334 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{class Inner{}}", false, new NullProgressMonitor()); 335 IType classA= cu.getType("A"); 336 IType classInner= classA.getType("Inner"); 337 IJavaElement[] javaElements= { classA, classInner}; 338 IResource[] resources= {}; 339 verifyDisabled(resources, javaElements); 340 } 341 342 @Test 343 public void testDisabled_noCommonParent8() throws Exception { 344 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 345 IType classA= cu.getType("A"); 346 IMethod methodFoo= classA.getMethod("foo", new String[0]); 347 IJavaElement[] javaElements= { methodFoo, classA}; 348 IResource[] resources= {}; 349 verifyDisabled(resources, javaElements); 350 } 351 352 @Test 353 public void testDestination_no_fileToItself() throws Exception { 354 IFolder superFolder= (IFolder)getPackageP().getResource(); 355 IFile file= superFolder.getFile("a.txt"); 356 file.create(getStream("123"), true, null); 357 358 IJavaElement[] javaElements= {}; 359 IResource[] resources= {file}; 360 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 361 362 Object destination= file; 363 verifyInvalidDestination(ref, destination); 364 } 365 366 @Test 367 public void testDestination_no_fileToSiblingFile() throws Exception { 368 IFolder superFolder= (IFolder)getPackageP().getResource(); 369 IFile file1= superFolder.getFile("a.txt"); 370 file1.create(getStream("123"), true, null); 371 IFile file2= superFolder.getFile("b.txt"); 372 file2.create(getStream("123"), true, null); 373 374 IJavaElement[] javaElements= {}; 375 IResource[] resources= {file1}; 376 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 377 378 Object destination= file2; 379 verifyInvalidDestination(ref, destination); 380 } 381 382 @Test 383 public void testDestination_no_folderToItsef() throws Exception { 384 IFolder superFolder= (IFolder)getPackageP().getResource(); 385 IFolder folder= superFolder.getFolder("folder"); 386 folder.create(true, true, null); 387 IJavaElement[] javaElements= {}; 388 IResource[] resources= {folder}; 389 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 390 391 Object destination= folder; 392 verifyInvalidDestination(ref, destination); 393 } 394 395 @Test 396 public void testDestination_no_cuToItsef() throws Exception { 397 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 398 IJavaElement[] javaElements= {cu}; 399 IResource[] resources= {}; 400 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 401 402 Object destination= cu; 403 verifyInvalidDestination(ref, destination); 404 } 405 406 @Test 407 public void testDestination_no_cuToSiblingCu() throws Exception { 408 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 409 ICompilationUnit cu1= getPackageP().createCompilationUnit("B.java", "package p;class A{}", false, new NullProgressMonitor()); 410 IJavaElement[] javaElements= {cu}; 411 IResource[] resources= {}; 412 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 413 414 Object destination= cu1; 415 verifyInvalidDestination(ref, destination); 416 } 417 418 @Test 419 public void testDestination_no_cuToSiblingFile() throws Exception { 420 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 421 422 IFolder superFolder= (IFolder)getPackageP().getResource(); 423 IFile file1= superFolder.getFile("a.txt"); 424 file1.create(getStream("123"), true, null); 425 426 IJavaElement[] javaElements= {cu}; 427 IResource[] resources= {}; 428 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 429 430 Object destination= file1; 431 verifyInvalidDestination(ref, destination); 432 } 433 434 @Test 435 public void testDestination_no_packageToItsef() throws Exception { 436 IJavaElement[] javaElements= {getPackageP()}; 437 IResource[] resources= {}; 438 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 439 440 Object destination= getPackageP(); 441 verifyInvalidDestination(ref, destination); 442 } 443 444 @Test 445 public void testDestination_no_sourceFolderToItsef() throws Exception { 446 IJavaElement[] javaElements= {getRoot()}; 447 IResource[] resources= {}; 448 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 449 450 Object destination= getRoot(); 451 verifyInvalidDestination(ref, destination); 452 } 453 454 @Test 455 public void testDestination_no_methodToItsef() throws Exception { 456 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 457 IMethod method= cu.getType("A").getMethod("foo", new String[0]); 458 IJavaElement[] javaElements= {method}; 459 IResource[] resources= {}; 460 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 461 462 Object destination= method; 463 verifyInvalidDestination(ref, destination); 464 } 465 466 @Test 467 public void testDestination_no_fileToParentFolder() throws Exception { 468 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 469 IFolder folder= superFolder.getFolder("folder"); 470 folder.create(true, true, null); 471 IFile file= folder.getFile("a.txt"); 472 file.create(getStream("123"), true, null); 473 474 IJavaElement[] javaElements= {}; 475 IResource[] resources= {file}; 476 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 477 478 Object destination= folder; 479 verifyInvalidDestination(ref, destination); 480 } 481 482 @Test 483 public void testDestination_no_fileToParentPackage() throws Exception { 484 IFolder superFolder= (IFolder)getPackageP().getResource(); 485 IFile file= superFolder.getFile("a.txt"); 486 file.create(getStream("123"), true, null); 487 488 IJavaElement[] javaElements= {}; 489 IResource[] resources= {file}; 490 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 491 492 Object destination= getPackageP(); 493 verifyInvalidDestination(ref, destination); 494 } 495 496 @Test 497 public void testDestination_no_fileToParentSourceFolder() throws Exception { 498 IFolder superFolder= (IFolder)getRoot().getResource(); 499 IFile file= superFolder.getFile("a.txt"); 500 file.create(getStream("123"), true, null); 501 502 IJavaElement[] javaElements= {}; 503 IResource[] resources= {file}; 504 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 505 506 Object destination= getRoot(); 507 verifyInvalidDestination(ref, destination); 508 } 509 510 @Test 511 public void testDestination_no_fileToParentDefaultPackage() throws Exception { 512 IPackageFragment defaultPackage= getRoot().getPackageFragment(""); 513 assertTrue(defaultPackage.exists()); 514 IFolder superFolder= (IFolder)defaultPackage.getResource(); 515 IFile file= superFolder.getFile("a.txt"); 516 file.create(getStream("123"), true, null); 517 518 IJavaElement[] javaElements= {}; 519 IResource[] resources= { file }; 520 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 521 Object destination= defaultPackage; 522 verifyInvalidDestination(ref, destination); 523 } 524 525 @Test 526 public void testDestination_no_fileToParentDefaultPackage2() throws Exception { 527 IPackageFragment defaultPackage= getRoot().getPackageFragment(""); 528 assertTrue(defaultPackage.exists()); 529 ICompilationUnit cu= defaultPackage.createCompilationUnit("A.java", "class A{}", false, new NullProgressMonitor()); 530 IFolder superFolder= (IFolder)defaultPackage.getResource(); 531 IFile file= superFolder.getFile("a.txt"); 532 file.create(getStream("123"), true, null); 533 534 IJavaElement[] javaElements= {}; 535 IResource[] resources= { file }; 536 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 537 Object destination= cu; 538 verifyInvalidDestination(ref, destination); 539 } 540 541 @Test 542 public void testDestination_no_fileToParentSourceFolder2() throws Exception { 543 IPackageFragmentRoot root= getRoot(); 544 assertTrue(root.exists()); 545 IFolder superFolder= (IFolder)root.getPackageFragment("").getResource(); 546 IFile file= superFolder.getFile("a.txt"); 547 file.create(getStream("123"), true, null); 548 549 IJavaElement[] javaElements= {}; 550 IResource[] resources= { file }; 551 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 552 Object destination= root; 553 verifyInvalidDestination(ref, destination); 554 } 555 556 @Test 557 public void testDestination_no_folderToParentFolder() throws Exception { 558 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 559 560 IFolder parentFolder= superFolder.getFolder("folder"); 561 parentFolder.create(true, true, null); 562 IFolder folder= parentFolder.getFolder("subfolder"); 563 folder.create(true, true, null); 564 565 IJavaElement[] javaElements= {}; 566 IResource[] resources= {parentFolder}; 567 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 568 569 Object destination= parentFolder; 570 verifyInvalidDestination(ref, destination); 571 } 572 573 @Test 574 public void testDestination_no_cuToParentPackage() throws Exception { 575 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 576 IJavaElement[] javaElements= {cu}; 577 IResource[] resources= {}; 578 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 579 580 Object destination= cu.getParent(); 581 verifyInvalidDestination(ref, destination); 582 } 583 584 @Test 585 public void testDestination_no_packageToParentSourceFolder() throws Exception { 586 IJavaElement[] javaElements= {getPackageP()}; 587 IResource[] resources= {}; 588 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 589 590 Object destination= getRoot(); 591 verifyInvalidDestination(ref, destination); 592 } 593 594 @Test 595 public void testDestination_no_sourceFolderToParentProject() throws Exception { 596 IJavaElement[] javaElements= {getRoot()}; 597 IResource[] resources= {}; 598 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 599 600 Object destination= getRoot().getParent(); 601 verifyInvalidDestination(ref, destination); 602 } 603 604 @Test 605 public void testDestination_no_methodToParentType() throws Exception { 606 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 607 IMethod method= cu.getType("A").getMethod("foo", new String[0]); 608 IJavaElement[] javaElements= {method}; 609 IResource[] resources= {}; 610 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 611 612 Object destination= cu.getType("A"); 613 verifyInvalidDestination(ref, destination); 614 } 615 616 @Test 617 public void testDestination_no_cuToMethod() throws Exception { 618 IPackageFragment pack1= getRoot().createPackageFragment("q", true, new NullProgressMonitor()); 619 ICompilationUnit cu= pack1.createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 620 ICompilationUnit cu1= getPackageP().createCompilationUnit("B.java", "package p;class B{}", false, new NullProgressMonitor()); 621 IMethod method= cu.getType("A").getMethod("foo", new String[0]); 622 IJavaElement[] javaElements= {cu1}; 623 IResource[] resources= {}; 624 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 625 626 Object destination= method; 627 verifyInvalidDestination(ref, destination); 628 } 629 630 @Test 631 public void testDestination_no_packageToCu() throws Exception { 632 IPackageFragment pack1= getRoot().createPackageFragment("q", true, new NullProgressMonitor()); 633 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 634 IJavaElement[] javaElements= {pack1}; 635 IResource[] resources= {}; 636 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 637 638 Object destination= cu; 639 verifyInvalidDestination(ref, destination); 640 } 641 642 @Test 643 public void testDestination_no_packageToFile() throws Exception { 644 IPackageFragment pack1= getRoot().createPackageFragment("q", true, new NullProgressMonitor()); 645 IFolder superFolder= (IFolder)getRoot().getResource(); 646 IFile file= superFolder.getFile("a.txt"); 647 file.create(getStream("123"), true, null); 648 649 IJavaElement[] javaElements= {pack1}; 650 IResource[] resources= {}; 651 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 652 653 Object destination= file; 654 verifyInvalidDestination(ref, destination); 655 } 656 657 @Test 658 public void testDestination_no_packageToFolder() throws Exception { 659 IPackageFragment pack1= getRoot().createPackageFragment("q", true, new NullProgressMonitor()); 660 IFolder superFolder= (IFolder)getRoot().getResource(); 661 IFolder folder= superFolder.getFolder("folder"); 662 folder.create(true, true, null); 663 664 IJavaElement[] javaElements= {pack1}; 665 IResource[] resources= {}; 666 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 667 668 Object destination= folder; 669 verifyValidDestination(ref, destination); 670 } 671 672 @Test 673 public void testDestination_no_packageToSimpleProject() throws Exception { 674 IProject simpleProject= ResourcesPlugin.getWorkspace().getRoot().getProject("mySImpleProject"); 675 simpleProject.create(null); 676 simpleProject.open(null); 677 678 IPackageFragment pack1= getRoot().createPackageFragment("q", true, new NullProgressMonitor()); 679 try{ 680 IJavaElement[] javaElements= {pack1}; 681 IResource[] resources= {}; 682 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 683 684 Object destination= simpleProject; 685 verifyValidDestination(ref, destination); 686 } finally{ 687 JavaProjectHelper.delete(simpleProject); 688 } 689 } 690 691 // public void testDestination_no_packageToJavaProjectWithNoSourceFolders() throws Exception { 692 // IJavaProject otherProject= JavaProjectHelper.createJavaProject("otherProject", null); 693 // JavaProjectHelper.addSourceContainer(otherProject, null); 694 // IPackageFragment pack1= getRoot().createPackageFragment("q", true, new NullProgressMonitor()); 695 // try{ 696 // IJavaElement[] javaElements= {pack1}; 697 // IResource[] resources= {}; 698 // JavaMoveProcessor2 ref= verifyEnabled(resources, javaElements, createReorgQueries()); 699 // 700 // Object destination= otherProject; 701 // verifyInvalidDestination(ref, destination); 702 // } finally{ 703 // JavaProjectHelper.delete(otherProject); 704 // } 705 // } 706 707 @Test 708 public void testDestination_no_packageToSiblingPackage() throws Exception { 709 IPackageFragment pack1= getRoot().createPackageFragment("q", true, new NullProgressMonitor()); 710 IJavaElement[] javaElements= {getPackageP()}; 711 IResource[] resources= {}; 712 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 713 714 Object destination= pack1; 715 verifyInvalidDestination(ref, destination); 716 } 717 718 @Test 719 public void testDestination_no_sourceFolderToCu() throws Exception { 720 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 721 IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(RefactoringTestSetup.getProject(), "src2"); 722 IJavaElement[] javaElements= {sourceFolder}; 723 IResource[] resources= {}; 724 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 725 726 Object destination= cu; 727 verifyInvalidDestination(ref, destination); 728 } 729 730 @Test 731 public void testDestination_no_sourceFolderToPackage() throws Exception { 732 IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(RefactoringTestSetup.getProject(), "src2"); 733 IJavaElement[] javaElements= {sourceFolder}; 734 IResource[] resources= {}; 735 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 736 737 Object destination= getPackageP(); 738 verifyInvalidDestination(ref, destination); 739 } 740 741 @Test 742 public void testDestination_no_sourceFolderToFile() throws Exception { 743 IFolder superFolder= (IFolder)getRoot().getResource(); 744 IFile file= superFolder.getFile("a.txt"); 745 file.create(getStream("123"), true, null); 746 747 IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(RefactoringTestSetup.getProject(), "src2"); 748 IJavaElement[] javaElements= {sourceFolder}; 749 IResource[] resources= {}; 750 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 751 752 Object destination= file; 753 verifyInvalidDestination(ref, destination); 754 } 755 756 @Test 757 public void testDestination_no_sourceFolderToFolder() throws Exception { 758 IFolder superFolder= (IFolder)getRoot().getResource(); 759 IFolder folder= superFolder.getFolder("folder"); 760 folder.create(true, true, null); 761 762 IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(RefactoringTestSetup.getProject(), "src2"); 763 IJavaElement[] javaElements= {sourceFolder}; 764 IResource[] resources= {}; 765 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 766 767 Object destination= folder; 768 verifyValidDestination(ref, destination); 769 } 770 771 @Test 772 public void testDestination_no_sourceFolderToSourceFolder() throws Exception { 773 IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(RefactoringTestSetup.getProject(), "src2"); 774 IJavaElement[] javaElements= {sourceFolder}; 775 IResource[] resources= {}; 776 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 777 778 Object destination= getRoot(); 779 verifyInvalidDestination(ref, destination); 780 } 781 782 @Test 783 public void testDestination_no_sourceFolderToSimpleProject() throws Exception { 784 IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(RefactoringTestSetup.getProject(), "src2"); 785 IProject simpleProject= ResourcesPlugin.getWorkspace().getRoot().getProject("mySImpleProject"); 786 simpleProject.create(null); 787 simpleProject.open(null); 788 789 try{ 790 IJavaElement[] javaElements= {sourceFolder}; 791 IResource[] resources= {}; 792 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 793 794 Object destination= simpleProject; 795 verifyValidDestination(ref, destination); 796 } finally{ 797 JavaProjectHelper.delete(simpleProject); 798 } 799 } 800 801 @Test 802 public void testDestination_no_sourceFolderToJavaProjecteWithNoSourceFolder() throws Exception { 803 IJavaProject otherProject= JavaProjectHelper.createJavaProject("otherProject", null); 804 JavaProjectHelper.addSourceContainer(otherProject, null); 805 IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(RefactoringTestSetup.getProject(), "src2"); 806 807 try{ 808 IJavaElement[] javaElements= {sourceFolder}; 809 IResource[] resources= {}; 810 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 811 812 Object destination= otherProject; 813 verifyInvalidDestination(ref, destination); 814 }finally{ 815 JavaProjectHelper.delete(otherProject); 816 } 817 } 818 819 @Test 820 public void testDestination_yes_methodToCu() throws Exception { 821 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){/*impl*/}}", false, new NullProgressMonitor()); 822 ICompilationUnit cu1= getPackageP().createCompilationUnit("B.java", "package p;class B{}", false, new NullProgressMonitor()); 823 IMethod method= cu.getType("A").getMethod("foo", new String[0]); 824 IJavaElement[] javaElements= {method}; 825 IResource[] resources= {}; 826 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 827 828 Object destination= cu1; 829 verifyValidDestination(ref, destination); 830 831 RefactoringStatus status= performRefactoring(ref, true); 832 assertNull(status); 833 834 assertFalse("source method not moved", method.exists()); 835 836 IType typeB= cu1.getType("B"); 837 IMethod methodBfoo= typeB.getMethod("foo", new String[0]); 838 assertTrue("method does not exist after", methodBfoo.exists()); 839 840 assertEquals("void foo(){/*impl*/}", methodBfoo.getSource()); 841 842 } 843 844 @Test 845 public void testDestination_no_methodToFile() throws Exception { 846 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 847 IFolder superFolder= (IFolder)getRoot().getResource(); 848 IFile file= superFolder.getFile("a.txt"); 849 file.create(getStream("123"), true, null); 850 851 IMethod method= cu.getType("A").getMethod("foo", new String[0]); 852 IJavaElement[] javaElements= {method}; 853 IResource[] resources= {}; 854 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 855 856 Object destination= file; 857 verifyInvalidDestination(ref, destination); 858 } 859 860 @Test 861 public void testDestination_no_methodToFolder() throws Exception { 862 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 863 IFolder superFolder= (IFolder)getRoot().getResource(); 864 IFolder folder= superFolder.getFolder("folder"); 865 folder.create(true, true, null); 866 867 IMethod method= cu.getType("A").getMethod("foo", new String[0]); 868 IJavaElement[] javaElements= {method}; 869 IResource[] resources= {}; 870 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 871 872 Object destination= folder; 873 verifyInvalidDestination(ref, destination); 874 } 875 876 @Test 877 public void testDestination_no_methodToPackage() throws Exception { 878 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 879 IMethod method= cu.getType("A").getMethod("foo", new String[0]); 880 IJavaElement[] javaElements= {method}; 881 IResource[] resources= {}; 882 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 883 884 Object destination= getPackageP(); 885 verifyInvalidDestination(ref, destination); 886 } 887 888 @Test 889 public void testDestination_no_methodToSourceFolder() throws Exception { 890 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 891 IMethod method= cu.getType("A").getMethod("foo", new String[0]); 892 IJavaElement[] javaElements= {method}; 893 IResource[] resources= {}; 894 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 895 896 Object destination= getRoot(); 897 verifyInvalidDestination(ref, destination); 898 } 899 900 @Test 901 public void testDestination_no_methodToJavaProject() throws Exception { 902 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 903 IMethod method= cu.getType("A").getMethod("foo", new String[0]); 904 IJavaElement[] javaElements= {method}; 905 IResource[] resources= {}; 906 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 907 908 Object destination= RefactoringTestSetup.getProject(); 909 verifyInvalidDestination(ref, destination); 910 } 911 912 @Test 913 public void testDestination_no_methodToSimpleProject() throws Exception { 914 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 915 IProject simpleProject= ResourcesPlugin.getWorkspace().getRoot().getProject("mySImpleProject"); 916 simpleProject.create(null); 917 simpleProject.open(null); 918 try{ 919 IMethod method= cu.getType("A").getMethod("foo", new String[0]); 920 IJavaElement[] javaElements= {method}; 921 IResource[] resources= {}; 922 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 923 924 Object destination= simpleProject; 925 verifyInvalidDestination(ref, destination); 926 } finally{ 927 JavaProjectHelper.delete(simpleProject); 928 } 929 } 930 931 @Test 932 public void testDestination_no_cuToItself() throws Exception { 933 ICompilationUnit cu1= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 934 IJavaElement[] javaElements= { cu1}; 935 IResource[] resources= {}; 936 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 937 Object destination= cu1; 938 verifyInvalidDestination(ref, destination); 939 } 940 941 @Test 942 public void testDestination_yes_cuToOtherPackage() throws Exception { 943 IPackageFragment otherPackage= getRoot().createPackageFragment("otherPackage", true, new NullProgressMonitor()); 944 String oldSource= "package p;class A{void foo(){}class Inner{}}"; 945 String newSource= "package otherPackage;class A{void foo(){}class Inner{}}"; 946 ICompilationUnit cu1= getPackageP().createCompilationUnit("A.java", oldSource, false, new NullProgressMonitor()); 947 ParticipantTesting.reset(); 948 IJavaElement[] javaElements= { cu1}; 949 IResource[] resources= {}; 950 String[] handles= ParticipantTesting.createHandles(new Object[] {cu1, cu1.getTypes()[0], cu1.getResource()}); 951 JavaMoveProcessor processor= verifyEnabled(resources, javaElements, createReorgQueries()); 952 953 Object destination= otherPackage; 954 verifyValidDestination(processor, destination); 955 956 assertTrue("source file does not exist before moving", cu1.exists()); 957 RefactoringStatus status= performRefactoring(processor, true); 958 assertNull(status); 959 assertFalse("source file exists after moving", cu1.exists()); 960 ICompilationUnit newCu= otherPackage.getCompilationUnit(cu1.getElementName()); 961 assertTrue("new file does not exist after moving", newCu.exists()); 962 assertEqualLines("source differs", newSource, newCu.getSource()); 963 ParticipantTesting.testMove( 964 handles, 965 new MoveArguments[] { 966 new MoveArguments(otherPackage, processor.getUpdateReferences()), 967 new MoveArguments(otherPackage, processor.getUpdateReferences()), 968 new MoveArguments(otherPackage.getResource(), processor.getUpdateReferences())}); 969 } 970 971 @Test 972 public void testDestination_yes_cuToOtherPackageBug549674() throws Exception { 973 ParticipantTesting.reset(); 974 975 StringBuffer buf= new StringBuffer(); 976 buf.append("package p;\n"); 977 buf.append("\n"); 978 buf.append("import q.Class1;\n"); 979 buf.append("import q.Class3;\n"); 980 buf.append("import q.Class3.InnerClass3;\n"); 981 buf.append("\n"); 982 buf.append("public class Class2 {\n"); 983 buf.append(" Class1 c;\n"); 984 buf.append(" Class3 c3;\n"); 985 buf.append(" InnerClass3 ic3;\n"); 986 buf.append("}\n"); 987 ICompilationUnit toMove= getPackageP().createCompilationUnit("Class2.java", buf.toString(), false, new NullProgressMonitor()); 988 989 buf= new StringBuffer(); 990 buf.append("package q;\n"); 991 buf.append("public class Class1 {\n"); 992 buf.append("}\n"); 993 getPackageQ().createCompilationUnit("Class1.java", buf.toString(), false, new NullProgressMonitor()); 994 995 buf= new StringBuffer(); 996 buf.append("package q;\n"); 997 buf.append("public class Class3 {\n"); 998 buf.append(" public interface InnerClass3 {\n"); 999 buf.append(" }\n"); 1000 buf.append("{\n"); 1001 getPackageQ().createCompilationUnit("Class3.java", buf.toString(), false, new NullProgressMonitor()); 1002 1003 String[] handles= ParticipantTesting.createHandles(new Object[] { toMove, toMove.getTypes()[0], toMove.getResource() }); 1004 JavaMoveProcessor processor= verifyEnabled(new IResource[] {}, new IJavaElement[] { toMove }, createReorgQueries()); 1005 1006 verifyValidDestination(processor, getPackageQ()); 1007 1008 assertTrue("source file does not exist before moving", toMove.exists()); 1009 RefactoringStatus status= performRefactoring(processor, true); 1010 assertNull(status); 1011 assertFalse("source file exists after moving", toMove.exists()); 1012 ICompilationUnit newCu= getPackageQ().getCompilationUnit(toMove.getElementName()); 1013 assertTrue("new file does not exist after moving", newCu.exists()); 1014 1015 buf= new StringBuffer(); 1016 buf.append("package q;\n"); 1017 buf.append("\n"); 1018 buf.append("import q.Class3.InnerClass3;\n"); 1019 buf.append("\n"); 1020 buf.append("public class Class2 {\n"); 1021 buf.append(" Class1 c;\n"); 1022 buf.append(" Class3 c3;\n"); 1023 buf.append(" InnerClass3 ic3;\n"); 1024 buf.append("}\n"); 1025 assertEqualLines(buf.toString(), newCu.getSource()); 1026 1027 ParticipantTesting.testMove(handles, 1028 new MoveArguments[] { 1029 new MoveArguments(getPackageQ(), processor.getUpdateReferences()), 1030 new MoveArguments(getPackageQ(), processor.getUpdateReferences()), 1031 new MoveArguments(getPackageQ().getResource(), processor.getUpdateReferences()) 1032 }); 1033 } 1034 1035 @Test 1036 public void testDestination_yes_cuToOtherPackageBug21008() throws Exception { 1037 ParticipantTesting.reset(); 1038 1039 StringBuffer buf= new StringBuffer(); 1040 buf.append("package p;\n"); 1041 buf.append("\n"); 1042 buf.append("import q.*;\n"); 1043 buf.append("\n"); 1044 buf.append("public class Class2 {\n"); 1045 buf.append(" Class1 c;\n"); 1046 buf.append(" Class3 c3;\n"); 1047 buf.append(" InnerClass3 ic3;\n"); 1048 buf.append("}\n"); 1049 ICompilationUnit toMove= getPackageP().createCompilationUnit("Class2.java", buf.toString(), false, new NullProgressMonitor()); 1050 1051 buf= new StringBuffer(); 1052 buf.append("package q;\n"); 1053 buf.append("public class Class1 {\n"); 1054 buf.append("}\n"); 1055 getPackageQ().createCompilationUnit("Class1.java", buf.toString(), false, new NullProgressMonitor()); 1056 1057 buf= new StringBuffer(); 1058 buf.append("package q;\n"); 1059 buf.append("public class Class3 {\n"); 1060 buf.append(" public interface InnerClass3 {\n"); 1061 buf.append(" }\n"); 1062 buf.append("{\n"); 1063 getPackageQ().createCompilationUnit("Class3.java", buf.toString(), false, new NullProgressMonitor()); 1064 1065 String[] handles= ParticipantTesting.createHandles(new Object[] { toMove, toMove.getTypes()[0], toMove.getResource() }); 1066 JavaMoveProcessor processor= verifyEnabled(new IResource[] {}, new IJavaElement[] { toMove }, createReorgQueries()); 1067 1068 verifyValidDestination(processor, getPackageQ()); 1069 1070 assertTrue("source file does not exist before moving", toMove.exists()); 1071 RefactoringStatus status= performRefactoring(processor, true); 1072 assertNull(status); 1073 assertFalse("source file exists after moving", toMove.exists()); 1074 ICompilationUnit newCu= getPackageQ().getCompilationUnit(toMove.getElementName()); 1075 assertTrue("new file does not exist after moving", newCu.exists()); 1076 1077 buf= new StringBuffer(); 1078 buf.append("package q;\n"); 1079 buf.append("\n"); 1080 buf.append("public class Class2 {\n"); 1081 buf.append(" Class1 c;\n"); 1082 buf.append(" Class3 c3;\n"); 1083 buf.append(" InnerClass3 ic3;\n"); 1084 buf.append("}\n"); 1085 assertEqualLines(buf.toString(), newCu.getSource()); 1086 1087 ParticipantTesting.testMove(handles, 1088 new MoveArguments[] { 1089 new MoveArguments(getPackageQ(), processor.getUpdateReferences()), 1090 new MoveArguments(getPackageQ(), processor.getUpdateReferences()), 1091 new MoveArguments(getPackageQ().getResource(), processor.getUpdateReferences()) 1092 }); 1093 } 1094 1095 @Test 1096 public void testDestination_yes_cuToOtherPackageWithMultiRoot() throws Exception { 1097 ParticipantTesting.reset(); 1098 //regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=47788 1099 IPackageFragment otherPackage= getRoot().createPackageFragment("otherPackage", true, new NullProgressMonitor()); 1100 String oldA= "package p;public class A{}"; 1101 String newA= "package otherPackage;public class A{}"; 1102 ICompilationUnit cuA= getPackageP().createCompilationUnit("A.java", oldA, false, new NullProgressMonitor()); 1103 1104 IPackageFragmentRoot testSrc= JavaProjectHelper.addSourceContainer(RefactoringTestSetup.getProject(), "testSrc"); 1105 IPackageFragment testP= testSrc.createPackageFragment("p", true, new NullProgressMonitor()); 1106 String oldRef= "package p;\npublic class Ref { A t = new A(); }"; 1107 String newRef= "package p;\n\nimport otherPackage.A;\n\npublic class Ref { A t = new A(); }"; 1108 ICompilationUnit cuRef= testP.createCompilationUnit("Ref.java", oldRef, false, new NullProgressMonitor()); 1109 IJavaElement[] javaElements= {cuA}; 1110 IResource[] resources= {}; 1111 String[] handles= ParticipantTesting.createHandles(new Object[] {cuA, cuA.getTypes()[0], cuA.getResource()}); 1112 JavaMoveProcessor processor= verifyEnabled(resources, javaElements, createReorgQueries()); 1113 1114 Object destination= otherPackage; 1115 verifyValidDestination(processor, destination); 1116 1117 assertTrue("source file does not exist before moving", cuA.exists()); 1118 RefactoringStatus status= performRefactoring(processor, true); 1119 assertNull(status); 1120 assertFalse("source file exists after moving", cuA.exists()); 1121 ICompilationUnit newCu= otherPackage.getCompilationUnit(cuA.getElementName()); 1122 assertTrue("new file does not exist after moving", newCu.exists()); 1123 assertEqualLines("source differs", newA, newCu.getSource()); 1124 assertEqualLines("Ref differs", newRef, cuRef.getSource()); 1125 1126 ParticipantTesting.testMove( 1127 handles, 1128 new MoveArguments[] { 1129 new MoveArguments(otherPackage, processor.getUpdateReferences()), 1130 new MoveArguments(otherPackage, processor.getUpdateReferences()), 1131 new MoveArguments(otherPackage.getResource(), processor.getUpdateReferences())}); 1132 } 1133 1134 @Test 1135 public void testDestination_yes_cuToOtherPackageWithMultiRootBug109145() throws Exception { 1136 ParticipantTesting.reset(); 1137 1138 StringBuffer buf= new StringBuffer(); 1139 buf.append("package p;\n"); 1140 buf.append("public class Class2 {\n"); 1141 buf.append(" Class1 c;\n"); 1142 buf.append("}\n"); 1143 ICompilationUnit toMove= getPackageP().createCompilationUnit("Class2.java", buf.toString(), false, new NullProgressMonitor()); 1144 1145 IPackageFragmentRoot testSrc= JavaProjectHelper.addSourceContainer(RefactoringTestSetup.getProject(), "testSrc"); 1146 IPackageFragment testP= testSrc.createPackageFragment("p", true, new NullProgressMonitor()); 1147 buf= new StringBuffer(); 1148 buf.append("package p;\n"); 1149 buf.append("public class Class1 {\n"); 1150 buf.append("}\n"); 1151 ICompilationUnit reference= testP.createCompilationUnit("Class1.java", buf.toString(), false, new NullProgressMonitor()); 1152 IPackageFragment destination= testSrc.createPackageFragment("p2", true, new NullProgressMonitor()); 1153 1154 String[] handles= ParticipantTesting.createHandles(new Object[] { toMove, toMove.getTypes()[0], toMove.getResource() }); 1155 JavaMoveProcessor processor= verifyEnabled(new IResource[] {}, new IJavaElement[] { toMove }, createReorgQueries()); 1156 1157 verifyValidDestination(processor, destination); 1158 1159 assertTrue("source file does not exist before moving", toMove.exists()); 1160 RefactoringStatus status= performRefactoring(processor, true); 1161 assertNull(status); 1162 assertFalse("source file exists after moving", toMove.exists()); 1163 ICompilationUnit newCu= destination.getCompilationUnit(toMove.getElementName()); 1164 assertTrue("new file does not exist after moving", newCu.exists()); 1165 1166 buf= new StringBuffer(); 1167 buf.append("package p2;\n"); 1168 buf.append("\n"); 1169 buf.append("import p.Class1;\n"); 1170 buf.append("\n"); 1171 buf.append("public class Class2 {\n"); 1172 buf.append(" Class1 c;\n"); 1173 buf.append("}\n"); 1174 assertEqualLines(buf.toString(), newCu.getSource()); 1175 1176 buf= new StringBuffer(); 1177 buf.append("package p;\n"); 1178 buf.append("public class Class1 {\n"); 1179 buf.append("}\n"); 1180 assertEqualLines(buf.toString(), reference.getSource()); 1181 1182 ParticipantTesting.testMove(handles, new MoveArguments[] { new MoveArguments(destination, processor.getUpdateReferences()), 1183 new MoveArguments(destination, processor.getUpdateReferences()), new MoveArguments(destination.getResource(), processor.getUpdateReferences()) }); 1184 } 1185 1186 @Test 1187 public void testDestination_yes_cuToRoot() throws Exception { 1188 ParticipantTesting.reset(); 1189 String newSource= "package p;class A{void foo(){}class Inner{}}"; 1190 String oldSource= "package p;class A{void foo(){}class Inner{}}"; 1191 ICompilationUnit cu1= getPackageP().createCompilationUnit("A.java", oldSource, false, new NullProgressMonitor()); 1192 IPackageFragmentRoot destination= JavaProjectHelper.addSourceContainer(getRoot().getJavaProject(), "src2"); 1193 IJavaElement[] javaElements= { cu1}; 1194 IResource[] resources= {}; 1195 String[] handles= ParticipantTesting.createHandles(new Object[] {cu1, cu1.getTypes()[0], cu1.getResource()}); 1196 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1197 1198 verifyValidDestination(ref, destination); 1199 1200 assertTrue("source file does not exist before moving", cu1.exists()); 1201 RefactoringStatus status= performRefactoring(ref, true); 1202 assertNull(status); 1203 assertFalse("source file exists after moving", cu1.exists()); 1204 ICompilationUnit newCu= destination.getPackageFragment("p").getCompilationUnit(cu1.getElementName()); 1205 assertTrue("new file does not exist after moving", newCu.exists()); 1206 assertEqualLines("source differs", newSource, newCu.getSource()); 1207 1208 ParticipantTesting.testMove( 1209 handles, 1210 new MoveArguments[] { 1211 new MoveArguments(destination.getPackageFragment("p"), ref.getUpdateReferences()), 1212 new MoveArguments(destination.getPackageFragment("p"), ref.getUpdateReferences()), 1213 new MoveArguments(destination.getPackageFragment("p").getResource(), ref.getUpdateReferences()) }); 1214 1215 } 1216 1217 @Test 1218 public void testDestination_yes_cuFromRoot() throws Exception { 1219 ParticipantTesting.reset(); 1220 1221 //import statement with type from default package - only <= java 1.3 1222 IJavaProject javaProject= getRoot().getJavaProject(); 1223 Map<String, String> originalOptions= javaProject.getOptions(false); 1224 Map<String, String> newOptions= javaProject.getOptions(false); 1225 newOptions.put(JavaCore.COMPILER_COMPLIANCE, "1.3"); 1226 newOptions.put(JavaCore.COMPILER_SOURCE, "1.3"); 1227 javaProject.setOptions(newOptions); 1228 1229 String oldD= "import org.test.Reference;public class Default {Reference ref;}"; 1230 String oldRef= "package org.test;import Default;public class Reference{Default d;}"; 1231 String newD= "package org;\nimport org.test.Reference;public class Default {Reference ref;}"; 1232 String newRef= "package org.test;import org.Default;public class Reference{Default d;}"; 1233 ICompilationUnit cuD= getRoot().getPackageFragment("").createCompilationUnit("Default.java", oldD, false, new NullProgressMonitor()); 1234 IPackageFragment orgTest= getRoot().createPackageFragment("org.test", false, new NullProgressMonitor()); 1235 ICompilationUnit cuRef= orgTest.createCompilationUnit("Reference.java", oldRef, false, new NullProgressMonitor()); 1236 IPackageFragment org= getRoot().getPackageFragment("org"); 1237 ICompilationUnit newCuD= org.getCompilationUnit(cuD.getElementName()); 1238 try{ 1239 IJavaElement[] javaElements= { cuD }; 1240 IResource[] resources= {}; 1241 String[] handles= ParticipantTesting.createHandles(new Object[] {cuD, cuD.getTypes()[0], cuD.getResource()}); 1242 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1243 1244 verifyValidDestination(ref, org); 1245 1246 assertTrue("source file Default.java does not exist before moving", cuD.exists()); 1247 assertTrue("source file Reference.java does not exist before moving", cuRef.exists()); 1248 RefactoringStatus status= performRefactoring(ref, true); 1249 assertNull(status); 1250 assertFalse("source file Default.java exists after moving", cuD.exists()); 1251 assertTrue("new file Default.java does not exist after moving", newCuD.exists()); 1252 assertTrue("source file Reference.java does not exist after moving", cuRef.exists()); 1253 assertEqualLines("Default.java differs", newD, newCuD.getSource()); 1254 assertEqualLines("Reference.java differs", newRef, cuRef.getSource()); 1255 1256 ParticipantTesting.testMove( 1257 handles, 1258 new MoveArguments[] { 1259 new MoveArguments(org, ref.getUpdateReferences()), 1260 new MoveArguments(org, ref.getUpdateReferences()), 1261 new MoveArguments(org.getResource(), ref.getUpdateReferences())}); 1262 }finally{ 1263 javaProject.setOptions(originalOptions); 1264 } 1265 } 1266 1267 @Test 1268 public void testDestination_no_cuFromRoot() throws Exception { 1269 //import statement with type from default package - only <= java 1.3 1270 IJavaProject javaProject= getRoot().getJavaProject(); 1271 Map<String, String> originalOptions= javaProject.getOptions(false); 1272 Map<String, String> newOptions= javaProject.getOptions(false); 1273 newOptions.put(JavaCore.COMPILER_COMPLIANCE, "1.4"); //will cause error (potential match) 1274 newOptions.put(JavaCore.COMPILER_SOURCE, "1.4"); //will cause error (potential match) 1275 javaProject.setOptions(newOptions); 1276 1277 String oldD= "import org.test.Reference;public class Default {Reference ref;}"; 1278 String oldRef= "package org.test;import Default;public class Reference{Default d;}"; 1279 String newD= "package org;\nimport org.test.Reference;public class Default {Reference ref;}"; 1280 String newRef= "package org.test;import org.Default;public class Reference{Default d;}"; 1281 ICompilationUnit cuD= getRoot().getPackageFragment("").createCompilationUnit("Default.java", oldD, false, new NullProgressMonitor()); 1282 IPackageFragment orgTest= getRoot().createPackageFragment("org.test", false, new NullProgressMonitor()); 1283 ICompilationUnit cuRef= orgTest.createCompilationUnit("Reference.java", oldRef, false, new NullProgressMonitor()); 1284 IPackageFragment org= getRoot().getPackageFragment("org"); 1285 ICompilationUnit newCuD= org.getCompilationUnit(cuD.getElementName()); 1286 try{ 1287 IJavaElement[] javaElements= { cuD }; 1288 IResource[] resources= {}; 1289 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1290 1291 verifyValidDestination(ref, org); 1292 1293 assertTrue("source file Default.java does not exist before moving", cuD.exists()); 1294 assertTrue("source file Reference.java does not exist before moving", cuRef.exists()); 1295 RefactoringStatus status= performRefactoring(ref, false); 1296 assertEquals(RefactoringStatus.ERROR, status.getSeverity()); 1297 assertFalse("source file Default.java exists after moving", cuD.exists()); 1298 assertTrue("new file Default.java does not exist after moving", newCuD.exists()); 1299 assertTrue("source file Reference.java does not exist after moving", cuRef.exists()); 1300 assertEqualLines("Default.java differs", newD, newCuD.getSource()); 1301 assertEqualLines("Reference.java differs", newRef, cuRef.getSource()); 1302 1303 }finally{ 1304 javaProject.setOptions(originalOptions); 1305 } 1306 } 1307 1308 @Test 1309 public void testDestination_yes_cuToProject() throws Exception { 1310 ParticipantTesting.reset(); 1311 String oldSource= "package p;class A{void foo(){}class Inner{}}"; 1312 String newSource= oldSource; 1313 ICompilationUnit cu1= getPackageP().createCompilationUnit("A.java", oldSource, false, new NullProgressMonitor()); 1314 IJavaElement[] javaElements= { cu1}; 1315 IResource[] resources= {}; 1316 String[] handles= ParticipantTesting.createHandles(new Object[] {cu1, cu1.getTypes()[0], cu1.getResource()}); 1317 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1318 1319 IJavaProject project= RefactoringTestSetup.getProject(); 1320 Object destination= project; 1321 verifyValidDestination(ref, destination); 1322 1323 assertTrue("source file does not exist before moving", cu1.exists()); 1324 RefactoringStatus status= performRefactoring(ref, true); 1325 assertNull(status); 1326 assertFalse("source file exists after moving", cu1.exists()); 1327 IFile newFile= project.getProject().getFile(cu1.getElementName()); 1328 assertEqualLines("source differs", newSource, getContents(newFile)); 1329 1330 ParticipantTesting.testMove( 1331 handles, 1332 new MoveArguments[] { 1333 new MoveArguments(project.getProject(), ref.getUpdateReferences()), 1334 new MoveArguments(project.getProject(), ref.getUpdateReferences()), 1335 new MoveArguments(project.getResource(), ref.getUpdateReferences())}); 1336 } 1337 1338 @Test 1339 public void testDestination_yes_cuToSimpleProject() throws Exception { 1340 ParticipantTesting.reset(); 1341 String oldSource= "package p;class A{void foo(){}class Inner{}}"; 1342 String newSource= oldSource; 1343 ICompilationUnit cu1= getPackageP().createCompilationUnit("A.java", oldSource, false, new NullProgressMonitor()); 1344 IProject simpleProject= ResourcesPlugin.getWorkspace().getRoot().getProject("mySImpleProject"); 1345 simpleProject.create(null); 1346 simpleProject.open(null); 1347 try{ 1348 IJavaElement[] javaElements= { cu1}; 1349 IResource[] resources= {}; 1350 String[] handles= ParticipantTesting.createHandles(new Object[] {cu1, cu1.getTypes()[0], cu1.getResource()}); 1351 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1352 1353 Object destination= simpleProject; 1354 verifyValidDestination(ref, destination); 1355 1356 assertTrue("source file does not exist before moving", cu1.exists()); 1357 RefactoringStatus status= performRefactoring(ref, true); 1358 assertNull(status); 1359 assertFalse("source file exists after moving", cu1.exists()); 1360 IFile newFile= simpleProject.getFile(cu1.getElementName()); 1361 assertEqualLines("source differs", newSource, getContents(newFile)); 1362 1363 ParticipantTesting.testMove( 1364 handles, 1365 new MoveArguments[] { 1366 new MoveArguments(simpleProject, ref.getUpdateReferences()), 1367 new MoveArguments(simpleProject, ref.getUpdateReferences()), 1368 new MoveArguments(simpleProject, ref.getUpdateReferences())}); 1369 } finally { 1370 JavaProjectHelper.delete(simpleProject); 1371 } 1372 } 1373 1374 @Test 1375 public void testDestination_yes_cuToFileInDifferentPackage() throws Exception { 1376 ParticipantTesting.reset(); 1377 ICompilationUnit cu1= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 1378 IPackageFragment otherPackage= getRoot().createPackageFragment("other", true, new NullProgressMonitor()); 1379 IFolder superFolder= (IFolder) otherPackage.getResource(); 1380 IFile file= superFolder.getFile("a.txt"); 1381 file.create(getStream("123"), true, null); 1382 1383 IJavaElement[] javaElements= { cu1}; 1384 IResource[] resources= {}; 1385 String[] handles= ParticipantTesting.createHandles(new Object[] {cu1, cu1.getTypes()[0], cu1.getResource()}); 1386 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1387 1388 Object destination= file; 1389 verifyValidDestination(ref, destination); 1390 1391 assertTrue("source file does not exist before", cu1.exists()); 1392 1393 RefactoringStatus status= performRefactoring(ref, true); 1394 assertNull(status); 1395 1396 assertFalse("source file not moved", cu1.exists()); 1397 1398 ICompilationUnit newCu= otherPackage.getCompilationUnit(cu1.getElementName()); 1399 assertTrue("new file does not exist after", newCu.exists()); 1400 1401 String expectedSource= "package other;class A{void foo(){}class Inner{}}"; 1402 assertEqualLines("source compare failed", expectedSource, newCu.getSource()); 1403 1404 ParticipantTesting.testMove( 1405 handles, 1406 new MoveArguments[] { 1407 new MoveArguments(otherPackage, ref.getUpdateReferences()), 1408 new MoveArguments(otherPackage, ref.getUpdateReferences()), 1409 new MoveArguments(otherPackage.getResource(), ref.getUpdateReferences())}); 1410 } 1411 1412 @Test 1413 public void testDestination_yes_cuToFolder() throws Exception { 1414 ParticipantTesting.reset(); 1415 ICompilationUnit cu1= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 1416 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1417 IFolder folder= superFolder.getFolder("folder"); 1418 folder.create(true, true, null); 1419 1420 IJavaElement[] javaElements= { cu1}; 1421 IResource[] resources= {}; 1422 String[] handles= ParticipantTesting.createHandles(new Object[] {cu1, cu1.getTypes()[0], cu1.getResource()}); 1423 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1424 1425 Object destination= folder; 1426 verifyValidDestination(ref, destination); 1427 1428 assertTrue("source file does not exist before", cu1.exists()); 1429 String expectedSource= cu1.getSource(); 1430 1431 RefactoringStatus status= performRefactoring(ref, true); 1432 assertNull(status); 1433 1434 assertFalse("source file not moved", cu1.exists()); 1435 1436 IFile newFile= folder.getFile(cu1.getElementName()); 1437 assertTrue("new file does not exist after", newFile.exists()); 1438 1439 assertEqualLines("source compare failed", expectedSource, getContents(newFile)); 1440 1441 ParticipantTesting.testMove( 1442 handles, 1443 new MoveArguments[] { 1444 new MoveArguments(destination, ref.getUpdateReferences()), 1445 new MoveArguments(destination, ref.getUpdateReferences()), 1446 new MoveArguments(folder, ref.getUpdateReferences())}); 1447 } 1448 1449 @Test 1450 public void testDestination_yes_fileToSiblingFolder() throws Exception { 1451 ParticipantTesting.reset(); 1452 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1453 IFile file= superFolder.getFile("a.txt"); 1454 file.create(getStream("123"), true, null); 1455 1456 IFolder folder= superFolder.getFolder("folder"); 1457 folder.create(true, true, null); 1458 1459 IJavaElement[] javaElements= {}; 1460 IResource[] resources= {file}; 1461 String[] handles= ParticipantTesting.createHandles(new Object[] {file}); 1462 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1463 1464 Object destination= folder; 1465 verifyValidDestination(ref, destination); 1466 1467 assertTrue("source file does not exist before", file.exists()); 1468 1469 RefactoringStatus status= performRefactoring(ref, true); 1470 assertNull(status); 1471 1472 assertFalse("source file not moved", file.exists()); 1473 1474 IFile newFile= folder.getFile(file.getName()); 1475 assertTrue("new file does not exist after", newFile.exists()); 1476 1477 ParticipantTesting.testMove( 1478 handles, 1479 new MoveArguments[] { 1480 new MoveArguments(folder, ref.getUpdateReferences())}); 1481 } 1482 1483 @Test 1484 public void testDestination_yes_fileToCu() throws Exception { 1485 ParticipantTesting.reset(); 1486 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1487 IFile file= superFolder.getFile("a.txt"); 1488 file.create(getStream("123"), true, null); 1489 1490 ICompilationUnit cu1= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 1491 IJavaElement[] javaElements= {}; 1492 IResource[] resources= {file}; 1493 String[] handles= ParticipantTesting.createHandles(new Object[] {file}); 1494 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1495 1496 Object destination= cu1; 1497 verifyValidDestination(ref, destination); 1498 1499 assertTrue("source file does not exist before", file.exists()); 1500 1501 RefactoringStatus status= performRefactoring(ref, true); 1502 assertNull(status); 1503 1504 assertFalse("source file not moved", file.exists()); 1505 1506 IFile newFile= ((IFolder)cu1.getParent().getResource()).getFile(file.getName()); 1507 assertTrue("new file does not exist after", newFile.exists()); 1508 1509 ParticipantTesting.testMove( 1510 handles, 1511 new MoveArguments[] { 1512 new MoveArguments(getPackageP().getResource(), ref.getUpdateReferences())}); 1513 } 1514 1515 @Test 1516 public void testDestination_yes_fileToPackage() throws Exception { 1517 ParticipantTesting.reset(); 1518 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1519 IFile file= superFolder.getFile("a.txt"); 1520 file.create(getStream("123"), true, null); 1521 1522 IJavaElement[] javaElements= {}; 1523 IResource[] resources= {file}; 1524 String[] handles= ParticipantTesting.createHandles(new Object[] {file}); 1525 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1526 1527 Object destination= getPackageP(); 1528 verifyValidDestination(ref, destination); 1529 1530 assertTrue("source file does not exist before", file.exists()); 1531 1532 RefactoringStatus status= performRefactoring(ref, true); 1533 assertNull(status); 1534 1535 assertFalse("source file not moved", file.exists()); 1536 1537 IFile newFile= ((IFolder)getPackageP().getResource()).getFile(file.getName()); 1538 assertTrue("new file does not exist after", newFile.exists()); 1539 1540 ParticipantTesting.testMove( 1541 handles, 1542 new MoveArguments[] { 1543 new MoveArguments(getPackageP().getResource(), ref.getUpdateReferences())}); 1544 } 1545 1546 @Test 1547 public void testDestination_no_fileToMethod() throws Exception { 1548 IFolder superFolder= (IFolder)getRoot().getResource(); 1549 IFile file= superFolder.getFile("a.txt"); 1550 file.create(getStream("123"), true, null); 1551 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 1552 IMethod method= cu.getType("A").getMethod("foo", new String[0]); 1553 IJavaElement[] javaElements= {}; 1554 IResource[] resources= {file}; 1555 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1556 1557 Object destination= method; 1558 verifyInvalidDestination(ref, destination); 1559 } 1560 1561 @Test 1562 public void testDestination_yes_fileToRoot() throws Exception { 1563 ParticipantTesting.reset(); 1564 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1565 IFile file= superFolder.getFile("a.txt"); 1566 file.create(getStream("123"), true, null); 1567 1568 IJavaElement[] javaElements= {}; 1569 IResource[] resources= {file}; 1570 String[] handles= ParticipantTesting.createHandles(new Object[] {file}); 1571 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1572 1573 Object destination= getRoot(); 1574 verifyValidDestination(ref, destination); 1575 1576 assertTrue("source file does not exist before", file.exists()); 1577 1578 RefactoringStatus status= performRefactoring(ref, true); 1579 assertNull(status); 1580 1581 assertFalse("source file not moved", file.exists()); 1582 1583 IFile newFile= ((IFolder)getRoot().getResource()).getFile(file.getName()); 1584 assertTrue("new file does not exist after", newFile.exists()); 1585 ParticipantTesting.testMove( 1586 handles, 1587 new MoveArguments[] { 1588 new MoveArguments(getRoot().getResource(), ref.getUpdateReferences())}); 1589 } 1590 1591 @Test 1592 public void testDestination_no_fileToParentProject() throws Exception { 1593 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1594 IFile file= superFolder.getFile("a.txt"); 1595 file.create(getStream("123"), true, null); 1596 1597 IJavaElement[] javaElements= {}; 1598 IResource[] resources= {file}; 1599 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1600 1601 Object destination= RefactoringTestSetup.getProject(); 1602 verifyInvalidDestination(ref, destination); 1603 } 1604 1605 @Test 1606 public void testDestination_yes_folderToSiblingFolder() throws Exception { 1607 ParticipantTesting.reset(); 1608 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1609 IFolder folder= superFolder.getFolder("folder"); 1610 folder.create(true, true, null); 1611 1612 IFolder otherFolder= superFolder.getFolder("otherfolder"); 1613 otherFolder.create(true, true, null); 1614 1615 IJavaElement[] javaElements= {}; 1616 IResource[] resources= {folder}; 1617 String[] handles= ParticipantTesting.createHandles(new Object[] {folder}); 1618 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1619 1620 Object destination= otherFolder; 1621 verifyValidDestination(ref, destination); 1622 1623 assertTrue("folder does not exist before", folder.exists()); 1624 RefactoringStatus status= performRefactoring(ref, true); 1625 assertNull(status); 1626 assertFalse("folder not moved", folder.exists()); 1627 IFolder newFolder= otherFolder.getFolder(folder.getName()); 1628 assertTrue("new folder does not exist after", newFolder.exists()); 1629 ParticipantTesting.testMove( 1630 handles, 1631 new MoveArguments[] { 1632 new MoveArguments(destination, ref.getUpdateReferences())}); 1633 } 1634 1635 @Test 1636 public void testDestination_no_folderToParentProject() throws Exception { 1637 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1638 IFolder folder= superFolder.getFolder("folder"); 1639 folder.create(true, true, null); 1640 1641 IJavaElement[] javaElements= {}; 1642 IResource[] resources= {folder}; 1643 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1644 1645 Object destination= RefactoringTestSetup.getProject(); 1646 verifyInvalidDestination(ref, destination); 1647 } 1648 1649 @Test 1650 public void testDestination_yes_folderToSiblingRoot() throws Exception { 1651 ParticipantTesting.reset(); 1652 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1653 IFolder folder= superFolder.getFolder("folder"); 1654 folder.create(true, true, null); 1655 1656 IJavaElement[] javaElements= {}; 1657 IResource[] resources= {folder}; 1658 String[] handles= ParticipantTesting.createHandles(new Object[] {folder}); 1659 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1660 1661 Object destination= getRoot(); 1662 verifyValidDestination(ref, destination); 1663 1664 assertTrue("folder does not exist before", folder.exists()); 1665 RefactoringStatus status= performRefactoring(ref, true); 1666 assertNull(status); 1667 assertFalse("folder not moved", folder.exists()); 1668 IPackageFragment newPackage= getRoot().getPackageFragment(folder.getName()); 1669 assertTrue("new folder does not exist after", newPackage.exists()); 1670 ParticipantTesting.testMove( 1671 handles, 1672 new MoveArguments[] { 1673 new MoveArguments(getRoot().getResource(), ref.getUpdateReferences())}); 1674 } 1675 1676 @Test 1677 public void testDestination_yes_folderToPackage() throws Exception { 1678 ParticipantTesting.reset(); 1679 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1680 IFolder folder= superFolder.getFolder("folder"); 1681 folder.create(true, true, null); 1682 1683 IJavaElement[] javaElements= {}; 1684 IResource[] resources= {folder}; 1685 String[] handles= ParticipantTesting.createHandles(new Object[] {folder}); 1686 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1687 1688 Object destination= getPackageP(); 1689 verifyValidDestination(ref, destination); 1690 1691 assertTrue("folder does not exist before", folder.exists()); 1692 RefactoringStatus status= performRefactoring(ref, true); 1693 assertNull(status); 1694 assertFalse("folder not moved", folder.exists()); 1695 IPackageFragment newPackage= getRoot().getPackageFragment(getPackageP().getElementName() + "." + folder.getName()); 1696 assertTrue("new package does not exist after", newPackage.exists()); 1697 ParticipantTesting.testMove( 1698 handles, 1699 new MoveArguments[] { 1700 new MoveArguments(getPackageP().getResource(), ref.getUpdateReferences())}); 1701 } 1702 1703 @Test 1704 public void testDestination_yes_folderToFileInAnotherFolder() throws Exception { 1705 ParticipantTesting.reset(); 1706 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1707 IFolder folder= superFolder.getFolder("folder"); 1708 folder.create(true, true, null); 1709 1710 IFolder otherFolder= superFolder.getFolder("otherfolder"); 1711 otherFolder.create(true, true, null); 1712 IFile fileInAnotherFolder= otherFolder.getFile("f.tex"); 1713 fileInAnotherFolder.create(getStream("123"), true, null); 1714 1715 IJavaElement[] javaElements= {}; 1716 IResource[] resources= {folder}; 1717 String[] handles= ParticipantTesting.createHandles(new Object[] {folder}); 1718 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1719 1720 Object destination= fileInAnotherFolder; 1721 verifyValidDestination(ref, destination); 1722 1723 assertTrue("folder does not exist before", folder.exists()); 1724 RefactoringStatus status= performRefactoring(ref, true); 1725 assertNull(status); 1726 assertFalse("folder not moved", folder.exists()); 1727 IFolder newFolder= otherFolder.getFolder(folder.getName()); 1728 assertTrue("new folder does not exist after", newFolder.exists()); 1729 ParticipantTesting.testMove( 1730 handles, 1731 new MoveArguments[] { 1732 new MoveArguments(otherFolder, ref.getUpdateReferences())}); 1733 } 1734 1735 @Test 1736 public void testDestination_yes_folderToCu() throws Exception { 1737 ParticipantTesting.reset(); 1738 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1739 IFolder folder= superFolder.getFolder("folder"); 1740 folder.create(true, true, null); 1741 1742 ICompilationUnit cu= getPackageP().createCompilationUnit("A.java", "package p;class A{void foo(){}class Inner{}}", false, new NullProgressMonitor()); 1743 1744 IJavaElement[] javaElements= {}; 1745 IResource[] resources= {folder}; 1746 String[] handles= ParticipantTesting.createHandles(new Object[] {folder}); 1747 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1748 1749 Object destination= cu; 1750 verifyValidDestination(ref, destination); 1751 1752 assertTrue("folder does not exist before", folder.exists()); 1753 RefactoringStatus status= performRefactoring(ref, true); 1754 assertNull(status); 1755 assertFalse("folder not moved", folder.exists()); 1756 IPackageFragment newPackage= getRoot().getPackageFragment(getPackageP().getElementName() + "." + folder.getName()); 1757 assertTrue("new package does not exist after", newPackage.exists()); 1758 ParticipantTesting.testMove( 1759 handles, 1760 new MoveArguments[] { 1761 new MoveArguments(cu.getParent().getResource(), ref.getUpdateReferences())}); 1762 } 1763 1764 @Test 1765 public void testDestination_yes_folderToSimpleProject() throws Exception { 1766 ParticipantTesting.reset(); 1767 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1768 IFolder folder= superFolder.getFolder("folder"); 1769 folder.create(true, true, null); 1770 1771 IProject simpleProject= ResourcesPlugin.getWorkspace().getRoot().getProject("mySImpleProject"); 1772 simpleProject.create(null); 1773 simpleProject.open(null); 1774 1775 try { 1776 IJavaElement[] javaElements= {}; 1777 IResource[] resources= {folder}; 1778 String[] handles= ParticipantTesting.createHandles(new Object[] {folder}); 1779 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1780 1781 Object destination= simpleProject; 1782 verifyValidDestination(ref, destination); 1783 1784 assertTrue("folder does not exist before", folder.exists()); 1785 RefactoringStatus status= performRefactoring(ref, true); 1786 assertNull(status); 1787 assertFalse("folder not moved", folder.exists()); 1788 IFolder newFolder= simpleProject.getFolder(folder.getName()); 1789 assertTrue("new folder does not exist after", newFolder.exists()); 1790 ParticipantTesting.testMove( 1791 handles, 1792 new MoveArguments[] { 1793 new MoveArguments(simpleProject, ref.getUpdateReferences())}); 1794 } finally { 1795 JavaProjectHelper.delete(simpleProject); 1796 } 1797 } 1798 1799 @Test 1800 public void testDestination_yes_sourceFolderToOtherProject() throws Exception { 1801 ParticipantTesting.reset(); 1802 IJavaProject otherJavaProject= JavaProjectHelper.createJavaProject("other", "bin"); 1803 1804 IPackageFragmentRoot oldRoot= JavaProjectHelper.addSourceContainer(RefactoringTestSetup.getProject(), "newSrc"); 1805 try { 1806 IJavaElement[] javaElements= { oldRoot }; 1807 IResource[] resources= {}; 1808 String[] handles= ParticipantTesting.createHandles(new Object[] {oldRoot, oldRoot.getResource()}); 1809 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1810 1811 Object destination= otherJavaProject; 1812 verifyValidDestination(ref, destination); 1813 1814 assertTrue("folder does not exist before", oldRoot.exists()); 1815 RefactoringStatus status= performRefactoring(ref, false); 1816 assertNull(status); 1817 assertFalse("folder not moved", oldRoot.exists()); 1818 IPackageFragmentRoot newRoot= getSourceFolder(otherJavaProject, oldRoot.getElementName()); 1819 assertTrue("new folder does not exist after", newRoot.exists()); 1820 ParticipantTesting.testMove( 1821 handles, 1822 new MoveArguments[] { 1823 new MoveArguments(otherJavaProject, ref.getUpdateReferences()), 1824 new MoveArguments(otherJavaProject.getResource(), ref.getUpdateReferences())}); 1825 } finally { 1826 JavaProjectHelper.delete(otherJavaProject); 1827 } 1828 } 1829 1830 @Test 1831 public void testDestination_no_methodToItself() throws Exception { 1832 ICompilationUnit cu= createCUfromTestFile(getPackageP(), "A"); 1833 IMethod method= cu.getType("A").getMethod("foo", new String[0]); 1834 IJavaElement[] javaElements= { method }; 1835 IResource[] resources= {}; 1836 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1837 Object destination= method; 1838 verifyInvalidDestination(ref, destination); 1839 } 1840 1841 @Test 1842 public void testDestination_yes_methodToOtherType() throws Exception { 1843 ParticipantTesting.reset(); 1844 ICompilationUnit cu= createCUfromTestFile(getPackageP(), "A"); 1845 IMethod method= cu.getType("A").getMethod("foo", new String[0]); 1846 IJavaElement[] javaElements= { method }; 1847 IResource[] resources= {}; 1848 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1849 IType otherType= cu.getType("B"); 1850 Object destination= otherType; 1851 verifyValidDestination(ref, destination); 1852 RefactoringStatus status= performRefactoring(ref, true); 1853 assertNull(status); 1854 1855 String expected= getFileContents(getOutputTestFileName(removeExtension(cu.getElementName()))); 1856 assertEqualLines("source differs", expected, cu.getSource()); 1857 ParticipantTesting.testMove(new String[] {},new MoveArguments[] {} ); 1858 } 1859 1860 @Test 1861 public void testDestination_yes_fieldToOtherType() throws Exception { 1862 ParticipantTesting.reset(); 1863 ICompilationUnit cu= createCUfromTestFile(getPackageP(), "A"); 1864 IField field= cu.getType("A").getField("f"); 1865 IJavaElement[] javaElements= { field }; 1866 IResource[] resources= {}; 1867 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1868 IType otherType= cu.getType("B"); 1869 Object destination= otherType; 1870 verifyValidDestination(ref, destination); 1871 RefactoringStatus status= performRefactoring(ref, true); 1872 assertNull(status); 1873 1874 String expected= getFileContents(getOutputTestFileName(removeExtension(cu.getElementName()))); 1875 assertEqualLines("source differs", expected, cu.getSource()); 1876 ParticipantTesting.testMove(new String[] {},new MoveArguments[] {} ); 1877 } 1878 1879 @Test 1880 public void testDestination_yes_initializerToOtherType() throws Exception { 1881 ParticipantTesting.reset(); 1882 ICompilationUnit cu= createCUfromTestFile(getPackageP(), "A"); 1883 IInitializer initializer= cu.getType("A").getInitializer(1); 1884 IJavaElement[] javaElements= { initializer }; 1885 IResource[] resources= {}; 1886 JavaMoveProcessor ref= verifyEnabled(resources, javaElements, createReorgQueries()); 1887 IType otherType= cu.getType("B"); 1888 Object destination= otherType; 1889 verifyValidDestination(ref, destination); 1890 RefactoringStatus status= performRefactoring(ref, true); 1891 assertNull(status); 1892 1893 String expected= getFileContents(getOutputTestFileName(removeExtension(cu.getElementName()))); 1894 assertEqualLines("source differs", expected, cu.getSource()); 1895 ParticipantTesting.testMove(new String[] {},new MoveArguments[] {} ); 1896 } 1897 1898 @Test 1899 public void testDestination_bug79318() throws Exception { 1900 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1901 IFolder folder= superFolder.getFolder("bar"); 1902 folder.create(true, true, null); 1903 IFile file= folder.getFile("bar"); 1904 file.create(getStream("123"), true, null); 1905 1906 IJavaElement[] javaElements= {}; 1907 IResource[] resources= {file}; 1908 1909 move(javaElements, resources, superFolder, null, IReorgDestination.LOCATION_ON, true, true); 1910 1911 assertIsParent(folder, file); 1912 assertIsParent(superFolder, folder); 1913 } 1914 1915 @Test 1916 public void testDestination_bug196303() throws Exception { 1917 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1918 IFolder folder1= superFolder.getFolder("bar"); 1919 folder1.create(true, true, null); 1920 1921 IFolder folder= superFolder.getFolder("foo"); 1922 folder.create(true, true, null); 1923 IFile file= folder.getFile("bar"); 1924 file.create(getStream("123"), true, null); 1925 1926 IJavaElement[] javaElements= {}; 1927 IResource[] resources= {file}; 1928 1929 move(javaElements, resources, superFolder, null, IReorgDestination.LOCATION_ON, false, true); 1930 1931 assertIsParent(folder, file); 1932 assertIsParent(superFolder, folder); 1933 } 1934 1935 @Test 1936 public void testDestination_fieldWithImport() throws Exception { 1937 ICompilationUnit cuA= createCUfromTestFile(getPackageP(), "A"); 1938 IType typeA= cuA.getType("A"); 1939 IJavaElement fieldF= typeA.getField("f"); 1940 IJavaElement fieldG= typeA.getField("g"); 1941 1942 move(new IJavaElement[] {fieldF} , new IResource[0], null, fieldG, IReorgDestination.LOCATION_AFTER, true, true); 1943 1944 compareContents("A"); 1945 } 1946 1947 @Test 1948 public void testDestination_fieldWithImport_back() throws Exception { 1949 ICompilationUnit cuA= createCUfromTestFile(getPackageP(), "A"); 1950 IType typeA= cuA.getType("A"); 1951 IJavaElement fieldF= typeA.getField("f"); 1952 IJavaElement fieldG= typeA.getField("g"); 1953 1954 move(new IJavaElement[] {fieldF} , new IResource[0], null, fieldG, IReorgDestination.LOCATION_BEFORE, true, true); 1955 1956 compareContents("A"); 1957 } 1958 1959 @Test 1960 public void testDestination_fieldWithImportMoveAcross() throws Exception { 1961 ICompilationUnit cuA= createCUfromTestFile(getPackageP(), "A"); 1962 ICompilationUnit cuB= createCUfromTestFile(getPackageP(), "B"); 1963 IType typeA= cuA.getType("A"); 1964 IJavaElement fieldF= typeA.getField("f"); 1965 1966 IType typeB= cuB.getType("B"); 1967 1968 move(new IJavaElement[] {fieldF} , new IResource[0], null, typeB, IReorgDestination.LOCATION_ON, true, true); 1969 1970 compareContents("A"); 1971 compareContents("B"); 1972 } 1973 1974 @Test 1975 public void testDestination_bug31125() throws Exception { 1976 IProject superFolder= RefactoringTestSetup.getProject().getProject(); 1977 IFolder destination= superFolder.getFolder("folder"); 1978 destination.create(true, true, null); 1979 1980 IFile file= superFolder.getFile("archive.jar"); 1981 file.create(getStream("123"), true, null); 1982 1983 IPackageFragmentRoot source= JavaProjectHelper.addLibrary(RefactoringTestSetup.getProject(), file.getFullPath()); 1984 1985 move(new IJavaElement[] {source} , new IResource[] {}, destination, null, IReorgDestination.LOCATION_ON, true, false); 1986 1987 assertTrue(destination.findMember(file.getName()).exists()); 1988 } 1989 1990 private static void assertIsParent(IContainer parent, IResource child) { 1991 assertTrue(child.getParent().equals(parent)); 1992 } 1993 1994 public void move(IJavaElement[] javaElements, IResource[] resources, IResource destination, IJavaElement javaDestination, int location, boolean confirmAll, boolean providesUndo) throws Exception { 1995 assertNotNull(javaElements); 1996 assertNotNull(resources); 1997 assertTrue((destination != null || javaDestination != null) && (destination == null || javaDestination == null)); 1998 1999 if (javaDestination != null) { 2000 assertTrue(javaDestination.exists()); 2001 } else { 2002 assertTrue(destination.exists()); 2003 } 2004 for (IResource resource : resources) { 2005 assertTrue(resource.exists()); 2006 } 2007 2008 IMovePolicy policy= ReorgPolicyFactory.createMovePolicy(resources, javaElements); 2009 assertTrue(policy.canEnable()); 2010 2011 JavaMoveProcessor processor= new JavaMoveProcessor(policy); 2012 if (javaDestination != null) { 2013 assertTrue(processor.setDestination(ReorgDestinationFactory.createDestination(javaDestination, location)).isOK()); 2014 } else { 2015 RefactoringStatus status= processor.setDestination(ReorgDestinationFactory.createDestination(destination, location)); 2016 assertTrue(status.getSeverity() <= RefactoringStatus.INFO); 2017 } 2018 2019 Refactoring ref= new MoveRefactoring(processor); 2020 2021 processor.setCreateTargetQueries(new CreateTargetQueries(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell())); 2022 if (confirmAll) { 2023 processor.setReorgQueries(new ConfirmAllQuery()); 2024 } else { 2025 processor.setReorgQueries(new ConfirmNoneQuery()); 2026 } 2027 2028 performRefactoring(ref, providesUndo); 2029 } 2030 2031 private void compareContents(String cuName) throws JavaModelException, IOException { 2032 assertEqualLines(cuName, getFileContents(getOutputTestFileName(cuName)), getPackageP().getCompilationUnit(cuName + ".java").getSource()); 2033 } 2034 } 2035