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; 15 16 import static org.junit.Assert.assertFalse; 17 import static org.junit.Assert.assertNotNull; 18 import static org.junit.Assert.assertNull; 19 import static org.junit.Assert.assertTrue; 20 21 import org.junit.Rule; 22 import org.junit.Test; 23 24 import org.eclipse.core.runtime.NullProgressMonitor; 25 26 import org.eclipse.ltk.core.refactoring.RefactoringCore; 27 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 28 import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring; 29 30 import org.eclipse.jdt.core.ICompilationUnit; 31 import org.eclipse.jdt.core.IMethod; 32 import org.eclipse.jdt.core.IType; 33 import org.eclipse.jdt.core.ITypeParameter; 34 35 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameTypeParameterProcessor; 36 37 import org.eclipse.jdt.ui.tests.refactoring.rules.Java15Setup; 38 import org.eclipse.jdt.ui.tests.refactoring.rules.RefactoringTestSetup; 39 40 public class RenameTypeParameterTests extends GenericRefactoringTest { 41 42 private static final String REFACTORING_PATH= "RenameTypeParameter/"; 43 44 @Rule 45 public RefactoringTestSetup fts= new Java15Setup(); 46 47 @Override getRefactoringPath()48 protected String getRefactoringPath() { 49 return REFACTORING_PATH; 50 } 51 helper1(String parameterName, String newParameterName, String typeName, boolean references)52 private void helper1(String parameterName, String newParameterName, String typeName, boolean references) throws Exception { 53 IType declaringType= getType(createCUfromTestFile(getPackageP(), "A"), typeName); 54 RenameTypeParameterProcessor processor= new RenameTypeParameterProcessor(declaringType.getTypeParameter(parameterName)); 55 RenameRefactoring refactoring= new RenameRefactoring(processor); 56 processor.setNewElementName(newParameterName); 57 processor.setUpdateReferences(references); 58 RefactoringStatus result= performRefactoring(refactoring); 59 assertNotNull("precondition was supposed to fail", result); 60 } 61 helper1(String parameterName, String newParameterName, String typeName, String methodName, String[] methodSignature, boolean references)62 private void helper1(String parameterName, String newParameterName, String typeName, String methodName, String[] methodSignature, boolean references) throws Exception { 63 IType declaringType= getType(createCUfromTestFile(getPackageP(), "A"), typeName); 64 IMethod[] declaringMethods= getMethods(declaringType, new String[] { methodName}, new String[][] { methodSignature}); 65 RenameTypeParameterProcessor processor= new RenameTypeParameterProcessor(declaringMethods[0].getTypeParameter(parameterName)); 66 RenameRefactoring refactoring= new RenameRefactoring(processor); 67 processor.setNewElementName(newParameterName); 68 processor.setUpdateReferences(references); 69 RefactoringStatus result= performRefactoring(refactoring); 70 assertNotNull("precondition was supposed to fail", result); 71 } 72 helper2(String parameterName, String newParameterName, String typeName, boolean references)73 private void helper2(String parameterName, String newParameterName, String typeName, boolean references) throws Exception { 74 ParticipantTesting.reset(); 75 ICompilationUnit cu= createCUfromTestFile(getPackageP(), "A"); 76 IType declaringType= getType(cu, typeName); 77 ITypeParameter typeParameter= declaringType.getTypeParameter(parameterName); 78 RenameTypeParameterProcessor processor= new RenameTypeParameterProcessor(typeParameter); 79 RenameRefactoring refactoring= new RenameRefactoring(processor); 80 processor.setNewElementName(newParameterName); 81 processor.setUpdateReferences(references); 82 83 RefactoringStatus result= performRefactoring(refactoring); 84 assertNull("was supposed to pass", result); 85 assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName("A")), cu.getSource()); 86 87 assertTrue("anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo()); 88 assertFalse("! anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo()); 89 90 RefactoringCore.getUndoManager().performUndo(null, new NullProgressMonitor()); 91 assertEqualLines("invalid undo", getFileContents(getInputTestFileName("A")), cu.getSource()); 92 93 assertFalse("! anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo()); 94 assertTrue("anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo()); 95 96 RefactoringCore.getUndoManager().performRedo(null, new NullProgressMonitor()); 97 assertEqualLines("invalid redo", getFileContents(getOutputTestFileName("A")), cu.getSource()); 98 } 99 helper2(String parameterName, String newParameterName, String typeName, String methodName, String[] methodSignature, boolean references)100 private void helper2(String parameterName, String newParameterName, String typeName, String methodName, String[] methodSignature, boolean references) throws Exception { 101 ParticipantTesting.reset(); 102 ICompilationUnit cu= createCUfromTestFile(getPackageP(), "A"); 103 IType declaringType= getType(cu, typeName); 104 IMethod[] declaringMethods= getMethods(declaringType, new String[] { methodName}, new String[][] { methodSignature}); 105 ITypeParameter typeParameter= declaringMethods[0].getTypeParameter(parameterName); 106 RenameTypeParameterProcessor processor= new RenameTypeParameterProcessor(typeParameter); 107 RenameRefactoring refactoring= new RenameRefactoring(processor); 108 processor.setNewElementName(newParameterName); 109 processor.setUpdateReferences(references); 110 111 RefactoringStatus result= performRefactoring(refactoring); 112 assertNull("was supposed to pass", result); 113 assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName("A")), cu.getSource()); 114 115 assertTrue("anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo()); 116 assertFalse("! anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo()); 117 118 RefactoringCore.getUndoManager().performUndo(null, new NullProgressMonitor()); 119 assertEqualLines("invalid undo", getFileContents(getInputTestFileName("A")), cu.getSource()); 120 121 assertFalse("! anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo()); 122 assertTrue("anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo()); 123 124 RefactoringCore.getUndoManager().performRedo(null, new NullProgressMonitor()); 125 assertEqualLines("invalid redo", getFileContents(getOutputTestFileName("A")), cu.getSource()); 126 } 127 128 @Test test0()129 public void test0() throws Exception { 130 helper2("T", "S", "A", true); 131 } 132 133 @Test test1()134 public void test1() throws Exception { 135 helper2("T", "S", "A", true); 136 } 137 138 @Test test2()139 public void test2() throws Exception { 140 helper2("T", "S", "A", false); 141 } 142 143 @Test test3()144 public void test3() throws Exception { 145 helper2("T", "S", "A", true); 146 } 147 148 @Test test4()149 public void test4() throws Exception { 150 helper2("T", "S", "A", false); 151 } 152 153 @Test test5()154 public void test5() throws Exception { 155 helper2("T", "S", "A", true); 156 } 157 158 @Test test6()159 public void test6() throws Exception { 160 helper2("S", "T", "A", true); 161 } 162 163 @Test test7()164 public void test7() throws Exception { 165 helper2("T", "S", "A", false); 166 } 167 168 @Test test8()169 public void test8() throws Exception { 170 helper2("S", "T", "A", false); 171 } 172 173 @Test test9()174 public void test9() throws Exception { 175 helper2("T", "S", "A", "f", new String[] { "QT;"}, true); 176 } 177 178 @Test test10()179 public void test10() throws Exception { 180 helper2("T", "S", "B", "f", new String[] { "QT;"}, true); 181 } 182 183 @Test test11()184 public void test11() throws Exception { 185 helper2("T", "S", "A", "f", new String[] { "QT;"}, false); 186 } 187 188 @Test test12()189 public void test12() throws Exception { 190 helper2("T", "S", "B", "f", new String[] { "QT;"}, false); 191 } 192 193 @Test test13()194 public void test13() throws Exception { 195 helper2("T", "S", "A", true); 196 } 197 198 @Test test14()199 public void test14() throws Exception { 200 helper2("ELEMENT", "E", "A", true); 201 } 202 203 @Test test15()204 public void test15() throws Exception { 205 helper2("T", "S", "A", true); 206 } 207 208 // ------------------------------------------------ 209 210 @Test testFail0()211 public void testFail0() throws Exception { 212 helper1("T", "S", "A", true); 213 } 214 215 @Test testFail1()216 public void testFail1() throws Exception { 217 helper1("T", "S", "A", true); 218 } 219 220 @Test testFail2()221 public void testFail2() throws Exception { 222 helper1("T", "S", "A", true); 223 } 224 225 @Test testFail3()226 public void testFail3() throws Exception { 227 helper1("T", "S", "A", true); 228 } 229 230 @Test testFail4()231 public void testFail4() throws Exception { 232 helper1("T", "S", "A", true); 233 } 234 235 @Test testFail5()236 public void testFail5() throws Exception { 237 helper1("T", "S", "B", "f", new String[] { "QT;"}, true); 238 } 239 } 240