1 /******************************************************************************* 2 * Copyright (c) 2014, 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.assertTrue; 17 import static org.junit.Assert.fail; 18 19 import java.util.Hashtable; 20 21 import org.junit.Rule; 22 import org.junit.Test; 23 import org.junit.runner.RunWith; 24 25 import org.eclipse.jdt.testplugin.JavaProjectHelper; 26 27 import org.eclipse.core.runtime.NullProgressMonitor; 28 import org.eclipse.core.runtime.Path; 29 30 import org.eclipse.jface.preference.IPreferenceStore; 31 32 import org.eclipse.ltk.core.refactoring.Refactoring; 33 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 34 35 import org.eclipse.jdt.core.ICompilationUnit; 36 import org.eclipse.jdt.core.IJavaElement; 37 import org.eclipse.jdt.core.IJavaProject; 38 import org.eclipse.jdt.core.IMethod; 39 import org.eclipse.jdt.core.IType; 40 import org.eclipse.jdt.core.JavaCore; 41 42 import org.eclipse.jdt.internal.corext.refactoring.structure.PullUpRefactoringProcessor; 43 44 import org.eclipse.jdt.ui.PreferenceConstants; 45 import org.eclipse.jdt.ui.tests.CustomBaseRunner; 46 import org.eclipse.jdt.ui.tests.IgnoreInheritedTests; 47 import org.eclipse.jdt.ui.tests.core.rules.Java1d8ProjectTestSetup; 48 import org.eclipse.jdt.ui.tests.refactoring.rules.Java1d8Setup; 49 import org.eclipse.jdt.ui.tests.refactoring.rules.RefactoringTestSetup; 50 51 import org.eclipse.jdt.internal.ui.JavaPlugin; 52 53 /** 54 * Those tests are made to run on Java Spider 1.8 . 55 */ 56 @IgnoreInheritedTests 57 @RunWith(CustomBaseRunner.class) 58 public class PullUpTests1d8 extends PullUpTests { 59 @SuppressWarnings("hiding") 60 @Rule 61 public RefactoringTestSetup fts= new Java1d8Setup(); 62 63 @Test test18_1()64 public void test18_1() throws Exception { 65 String[] methodNames= new String[] { "getArea" }; 66 String[][] signatures= new String[][] { new String[] { "QInteger;" } }; 67 JavaProjectHelper.addLibrary((IJavaProject) getPackageP().getAncestor(IJavaElement.JAVA_PROJECT), new Path(Java1d8ProjectTestSetup.getJdtAnnotations20Path())); 68 69 ICompilationUnit cuA= createCUfromTestFile(getPackageP(), "A"); 70 ICompilationUnit cuB= createCUfromTestFile(getPackageP(), "B"); 71 72 73 IType type= getType(cuB, "B"); 74 IMethod[] methods= getMethods(type, methodNames, signatures); 75 76 PullUpRefactoringProcessor processor= createRefactoringProcessor(methods); 77 Refactoring ref= processor.getRefactoring(); 78 79 assertTrue("activation", ref.checkInitialConditions(new NullProgressMonitor()).isOK()); 80 setSuperclassAsTargetClass(processor); 81 82 RefactoringStatus result= performRefactoring(ref); 83 assertTrue("precondition was supposed to pass", result == null || !result.hasError()); 84 85 assertEqualLines("A", getFileContents(getOutputTestFileName("A")), cuA.getSource()); 86 assertEqualLines("B", getFileContents(getOutputTestFileName("B")), cuB.getSource()); 87 } 88 89 // bug 394551 comment 2 90 @Test test18_2()91 public void test18_2() throws Exception { 92 String[] selectedMethodNames= { "m" }; 93 String[][] selectedMethodSignatures= { new String[0] }; 94 String[] selectedFieldNames= {}; 95 String[] namesOfMethodsToPullUp= {}; 96 String[][] signaturesOfMethodsToPullUp= {}; 97 String[] namesOfFieldsToPullUp= {}; 98 String[] namesOfMethodsToDeclareAbstract= selectedMethodNames; 99 String[][] signaturesOfMethodsToDeclareAbstract= selectedMethodSignatures; 100 101 declareAbstractHelper(selectedMethodNames, selectedMethodSignatures, 102 selectedFieldNames, 103 new String[0], namesOfMethodsToPullUp, 104 signaturesOfMethodsToPullUp, 105 namesOfFieldsToPullUp, namesOfMethodsToDeclareAbstract, 106 signaturesOfMethodsToDeclareAbstract, new String[0], true, true, 1); 107 } 108 109 // bug 394551 expect @java.lang.Override 110 @Test test18_3()111 public void test18_3() throws Exception { 112 String[] selectedMethodNames= { "m" }; 113 String[][] selectedMethodSignatures= { new String[0] }; 114 String[] selectedFieldNames= {}; 115 String[] namesOfMethodsToPullUp= {}; 116 String[][] signaturesOfMethodsToPullUp= {}; 117 String[] namesOfFieldsToPullUp= {}; 118 String[] namesOfMethodsToDeclareAbstract= selectedMethodNames; 119 String[][] signaturesOfMethodsToDeclareAbstract= selectedMethodSignatures; 120 121 declareAbstractHelper(selectedMethodNames, selectedMethodSignatures, 122 selectedFieldNames, 123 new String[0], namesOfMethodsToPullUp, 124 signaturesOfMethodsToPullUp, 125 namesOfFieldsToPullUp, namesOfMethodsToDeclareAbstract, 126 signaturesOfMethodsToDeclareAbstract, new String[0], true, true, 1); 127 } 128 129 // bug 394551: no override annotation expected 130 @Test test18_4()131 public void test18_4() throws Exception { 132 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); 133 boolean previousValue= store.getBoolean(PreferenceConstants.CODEGEN_USE_OVERRIDE_ANNOTATION); 134 try { 135 store.setValue(PreferenceConstants.CODEGEN_USE_OVERRIDE_ANNOTATION, false); 136 String[] selectedMethodNames= { "m" }; 137 String[][] selectedMethodSignatures= { new String[0] }; 138 String[] selectedFieldNames= {}; 139 String[] namesOfMethodsToPullUp= {}; 140 String[][] signaturesOfMethodsToPullUp= {}; 141 String[] namesOfFieldsToPullUp= {}; 142 String[] namesOfMethodsToDeclareAbstract= selectedMethodNames; 143 String[][] signaturesOfMethodsToDeclareAbstract= selectedMethodSignatures; 144 145 declareAbstractHelper(selectedMethodNames, selectedMethodSignatures, 146 selectedFieldNames, 147 new String[0], namesOfMethodsToPullUp, 148 signaturesOfMethodsToPullUp, 149 namesOfFieldsToPullUp, namesOfMethodsToDeclareAbstract, 150 signaturesOfMethodsToDeclareAbstract, new String[0], true, true, 1); 151 } finally { 152 store.setValue(PreferenceConstants.CODEGEN_USE_OVERRIDE_ANNOTATION, previousValue); 153 } 154 } 155 156 // bug 394551: no override annotation expected 157 @Test test18_5()158 public void test18_5() throws Exception { 159 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); 160 boolean previousValue= store.getBoolean(PreferenceConstants.CODEGEN_USE_OVERRIDE_ANNOTATION); 161 Hashtable<String, String> options= JavaCore.getOptions(); 162 Hashtable<String, String> changedOptions= new Hashtable<>(options); 163 changedOptions.put(JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION, JavaCore.WARNING); 164 changedOptions.put(JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION_FOR_INTERFACE_METHOD_IMPLEMENTATION, JavaCore.DISABLED); 165 try { 166 JavaCore.setOptions(changedOptions); 167 store.setValue(PreferenceConstants.CODEGEN_USE_OVERRIDE_ANNOTATION, false); 168 String[] selectedMethodNames= { "m" }; 169 String[][] selectedMethodSignatures= { new String[0] }; 170 String[] selectedFieldNames= {}; 171 String[] namesOfMethodsToPullUp= {}; 172 String[][] signaturesOfMethodsToPullUp= {}; 173 String[] namesOfFieldsToPullUp= {}; 174 String[] namesOfMethodsToDeclareAbstract= selectedMethodNames; 175 String[][] signaturesOfMethodsToDeclareAbstract= selectedMethodSignatures; 176 177 declareAbstractHelper(selectedMethodNames, selectedMethodSignatures, 178 selectedFieldNames, 179 new String[0], namesOfMethodsToPullUp, 180 signaturesOfMethodsToPullUp, 181 namesOfFieldsToPullUp, namesOfMethodsToDeclareAbstract, 182 signaturesOfMethodsToDeclareAbstract, new String[0], true, true, 1); 183 } finally { 184 store.setValue(PreferenceConstants.CODEGEN_USE_OVERRIDE_ANNOTATION, previousValue); 185 JavaCore.setOptions(options); 186 } 187 } 188 189 // bug 394551: override annotation expected 190 @Test test18_6()191 public void test18_6() throws Exception { 192 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); 193 boolean previousValue= store.getBoolean(PreferenceConstants.CODEGEN_USE_OVERRIDE_ANNOTATION); 194 Hashtable<String, String> options= JavaCore.getOptions(); 195 Hashtable<String, String> changedOptions= new Hashtable<>(options); 196 changedOptions.put(JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION, JavaCore.WARNING); 197 try { 198 JavaCore.setOptions(changedOptions); 199 store.setValue(PreferenceConstants.CODEGEN_USE_OVERRIDE_ANNOTATION, false); 200 String[] selectedMethodNames= { "m" }; 201 String[][] selectedMethodSignatures= { new String[0] }; 202 String[] selectedFieldNames= {}; 203 String[] namesOfMethodsToPullUp= {}; 204 String[][] signaturesOfMethodsToPullUp= {}; 205 String[] namesOfFieldsToPullUp= {}; 206 String[] namesOfMethodsToDeclareAbstract= selectedMethodNames; 207 String[][] signaturesOfMethodsToDeclareAbstract= selectedMethodSignatures; 208 209 declareAbstractHelper(selectedMethodNames, selectedMethodSignatures, 210 selectedFieldNames, 211 new String[0], namesOfMethodsToPullUp, 212 signaturesOfMethodsToPullUp, 213 namesOfFieldsToPullUp, namesOfMethodsToDeclareAbstract, 214 signaturesOfMethodsToDeclareAbstract, new String[0], true, true, 1); 215 } finally { 216 store.setValue(PreferenceConstants.CODEGEN_USE_OVERRIDE_ANNOTATION, previousValue); 217 JavaCore.setOptions(options); 218 } 219 } 220 221 // bug 497368 222 @Test test54()223 public void test54() throws Exception { 224 ICompilationUnit cuFoo= createCUfromTestFile(getPackageP(), "Foo"); 225 ICompilationUnit cuIFoo= createCUfromTestFile(getPackageP(), "IFoo"); 226 ICompilationUnit cuFooImpl= createCUfromTestFile(getPackageP(), "FooImpl"); 227 228 String[] methodNames= new String[] { "log" }; 229 String[][] signatures= new String[][] { new String[] { "QField;", "QString;" } }; 230 231 IType type= getType(cuFoo, "Foo"); 232 IMethod[] methods= getMethods(type, methodNames, signatures); 233 234 PullUpRefactoringProcessor processor= createRefactoringProcessor(methods); 235 Refactoring ref= processor.getRefactoring(); 236 237 assertTrue("activation", ref.checkInitialConditions(new NullProgressMonitor()).isOK()); 238 setSuperclassAsTargetClass(processor); 239 240 RefactoringStatus result= performRefactoring(ref); 241 assertTrue("precondition was supposed to pass", result == null || !result.hasError()); 242 243 assertEqualLines("IFoo", getFileContents(getOutputTestFileName("IFoo")), cuIFoo.getSource()); 244 assertEqualLines("FooImpl", getFileContents(getOutputTestFileName("FooImpl")), cuFooImpl.getSource()); 245 assertEqualLines("Foo", getFileContents(getOutputTestFileName("Foo")), cuFoo.getSource()); 246 } 247 248 // bug 552377 249 @Test testGenerics16()250 public void testGenerics16() throws Exception { 251 ICompilationUnit cuIGeneric= createCUfromTestFile(getPackageP(), "IGeneric"); 252 ICompilationUnit cuMainImpl= createCUfromTestFile(getPackageP(), "MainImpl"); 253 ICompilationUnit cuOtherImpl= createCUfromTestFile(getPackageP(), "OtherImpl"); 254 255 String[] methodNames= { "genericMethod" }; 256 String[][] signatures= { { "QString;" } }; 257 258 IType typeIGeneric= getType(cuIGeneric, "IGeneric"); 259 IType typeMainImpl= getType(cuMainImpl, "MainImpl"); 260 IMethod[] methods= getMethods(typeMainImpl, methodNames, signatures); 261 262 PullUpRefactoringProcessor processor= createRefactoringProcessor(methods); 263 Refactoring ref= processor.getRefactoring(); 264 265 assertTrue("activation", ref.checkInitialConditions(new NullProgressMonitor()).isOK()); 266 processor.setDestinationType(typeIGeneric); 267 268 processor.setDeletedMethods(getMethods(processor.getMatchingElements(new NullProgressMonitor(), false))); 269 270 RefactoringStatus result= performRefactoring(ref); 271 272 if (result != null && result.hasError()) { 273 fail("No errors should occur: " + result.getMessageMatchingSeverity(RefactoringStatus.ERROR)); 274 } 275 276 assertEqualLines("IGeneric", getFileContents(getOutputTestFileName("IGeneric")), cuIGeneric.getSource()); 277 assertEqualLines("MainImpl", getFileContents(getOutputTestFileName("MainImpl")), cuMainImpl.getSource()); 278 assertEqualLines("OtherImpl", getFileContents(getOutputTestFileName("OtherImpl")), cuOtherImpl.getSource()); 279 } 280 } 281