|
43 | 43 | import org.eclipse.core.runtime.jobs.Job; |
44 | 44 | import org.eclipse.jdt.core.ICompilationUnit; |
45 | 45 | import org.eclipse.jdt.core.IJavaProject; |
| 46 | +import org.eclipse.jdt.core.IMethod; |
46 | 47 | import org.eclipse.jdt.core.IPackageFragment; |
47 | 48 | import org.eclipse.jdt.core.IPackageFragmentRoot; |
| 49 | +import org.eclipse.jdt.core.IType; |
48 | 50 | import org.eclipse.jdt.core.JavaCore; |
49 | 51 | import org.eclipse.jdt.core.JavaModelException; |
50 | 52 | import org.eclipse.jdt.core.compiler.IProblem; |
@@ -204,6 +206,7 @@ private Preferences mockPreferences() { |
204 | 206 | Mockito.lenient().when(mockPreferences.getIncompleteClasspathSeverity()).thenReturn(Severity.ignore); |
205 | 207 | Mockito.lenient().when(this.preferenceManager.getClientPreferences()).thenReturn(clientPreferences); |
206 | 208 | Mockito.lenient().when(clientPreferences.isSupportedCodeActionKind(CodeActionKind.QuickFix)).thenReturn(true); |
| 209 | + Mockito.lenient().when(preferenceManager.getPreferences().getRenameReferencesEnabled()).thenReturn(true); |
207 | 210 | return mockPreferences; |
208 | 211 | } |
209 | 212 |
|
@@ -699,6 +702,67 @@ public void testNotExpectedPackage() throws Exception { |
699 | 702 | assertEquals("Unexpected number of errors", 0, problems.length); |
700 | 703 | } |
701 | 704 |
|
| 705 | + @Test |
| 706 | + public void testRenameReferences() throws Exception { |
| 707 | + try { |
| 708 | + Mockito.lenient().when(preferenceManager.getPreferences().getRenameReferencesEnabled()).thenReturn(true); |
| 709 | + newDefaultProject(); |
| 710 | + // @formatter:off |
| 711 | + String content = |
| 712 | + "package org;\n" + |
| 713 | + "public class Foo {\n" + |
| 714 | + "\tpublic void test(){\n" + |
| 715 | + "\t}\n" + |
| 716 | + "\n" + |
| 717 | + "\tpublic String getString(){\n" + |
| 718 | + "\t}\n" + |
| 719 | + "}"; |
| 720 | + // @formatter:on |
| 721 | + temp = createTempFolder(); |
| 722 | + File file = createTempFile(temp, "Foo.java", content); |
| 723 | + URI uri = file.toURI(); |
| 724 | + ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri); |
| 725 | + openDocument(cu, cu.getSource(), 1); |
| 726 | + |
| 727 | + // change 1 |
| 728 | + IType[] types = cu.getAllTypes(); |
| 729 | + assertEquals("Unexpected number of types", 1, types.length); |
| 730 | + IMethod[] methods = types[0].getMethods(); |
| 731 | + assertEquals("Unexpected number of methods", 2, methods.length); |
| 732 | + changeDocument(cu, "test1", 2, JDTUtils.toRange(cu, methods[0].getNameRange().getOffset(), methods[0].getNameRange().getLength())); |
| 733 | + IMarker[] markers = cu.getResource().findMarkers(BaseDocumentLifeCycleHandler.RENAME_REFERENCE_MARKER_ID, false, IResource.DEPTH_ONE); |
| 734 | + assertEquals("Unexpected number of markers", 1, markers.length); |
| 735 | + String originalName = (String) markers[0].getAttribute("originalName"); |
| 736 | + assertEquals("Unexpected originalName", "test", originalName); |
| 737 | + String newName = (String) markers[0].getAttribute("newName"); |
| 738 | + assertEquals("Unexpected newName", "test1", newName); |
| 739 | + |
| 740 | + // change 2 |
| 741 | + types = cu.getAllTypes(); |
| 742 | + assertEquals("Unexpected number of types", 1, types.length); |
| 743 | + methods = types[0].getMethods(); |
| 744 | + assertEquals("Unexpected number of methods", 2, methods.length); |
| 745 | + changeDocument(cu, "test12", 3, JDTUtils.toRange(cu, methods[0].getNameRange().getOffset(), methods[0].getNameRange().getLength())); |
| 746 | + markers = cu.getResource().findMarkers(BaseDocumentLifeCycleHandler.RENAME_REFERENCE_MARKER_ID, false, IResource.DEPTH_ONE); |
| 747 | + assertEquals("Unexpected number of markers", 1, markers.length); |
| 748 | + originalName = (String) markers[0].getAttribute("originalName"); |
| 749 | + assertEquals("Unexpected originalName", "test", originalName); |
| 750 | + newName = (String) markers[0].getAttribute("newName"); |
| 751 | + assertEquals("Unexpected newName", "test12", newName); |
| 752 | + |
| 753 | + // rollback change |
| 754 | + types = cu.getAllTypes(); |
| 755 | + assertEquals("Unexpected number of types", 1, types.length); |
| 756 | + methods = types[0].getMethods(); |
| 757 | + assertEquals("Unexpected number of methods", 2, methods.length); |
| 758 | + changeDocument(cu, "test", 4, JDTUtils.toRange(cu, methods[0].getNameRange().getOffset(), methods[0].getNameRange().getLength())); |
| 759 | + markers = cu.getResource().findMarkers(BaseDocumentLifeCycleHandler.RENAME_REFERENCE_MARKER_ID, false, IResource.DEPTH_ONE); |
| 760 | + assertEquals("Unexpected number of markers", 0, markers.length); |
| 761 | + } finally { |
| 762 | + Mockito.lenient().when(preferenceManager.getPreferences().getRenameReferencesEnabled()).thenReturn(false); |
| 763 | + } |
| 764 | + } |
| 765 | + |
702 | 766 | @Test |
703 | 767 | public void testCreateCompilationUnit() throws Exception { |
704 | 768 | IJavaProject javaProject = newEmptyProject(); |
|
0 commit comments