|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2013 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* This is an implementation of an early-draft specification developed under the Java Community Process (JCP) and |
| 9 |
* is made available for testing and evaluation purposes only. |
| 10 |
* The code is not compatible with any specification of the JCP. |
| 11 |
* |
| 12 |
* Contributors: |
| 13 |
* IBM Corporation - initial API and implementation |
| 14 |
*******************************************************************************/ |
| 15 |
package org.eclipse.jdt.ui.tests.refactoring; |
| 16 |
|
| 17 |
import java.util.Arrays; |
| 18 |
import java.util.Hashtable; |
| 19 |
import java.util.List; |
| 20 |
|
| 21 |
import junit.framework.Test; |
| 22 |
import junit.framework.TestSuite; |
| 23 |
|
| 24 |
import org.eclipse.jdt.testplugin.TestOptions; |
| 25 |
|
| 26 |
import org.eclipse.core.runtime.Assert; |
| 27 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 28 |
|
| 29 |
import org.eclipse.ltk.core.refactoring.Refactoring; |
| 30 |
import org.eclipse.ltk.core.refactoring.RefactoringStatus; |
| 31 |
import org.eclipse.ltk.core.refactoring.participants.MoveRefactoring; |
| 32 |
import org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring; |
| 33 |
|
| 34 |
import org.eclipse.jdt.core.ICompilationUnit; |
| 35 |
import org.eclipse.jdt.core.IField; |
| 36 |
import org.eclipse.jdt.core.IJavaElement; |
| 37 |
import org.eclipse.jdt.core.IJavaProject; |
| 38 |
import org.eclipse.jdt.core.IMember; |
| 39 |
import org.eclipse.jdt.core.IMethod; |
| 40 |
import org.eclipse.jdt.core.IPackageFragment; |
| 41 |
import org.eclipse.jdt.core.IType; |
| 42 |
import org.eclipse.jdt.core.JavaCore; |
| 43 |
import org.eclipse.jdt.core.JavaModelException; |
| 44 |
import org.eclipse.jdt.core.dom.IVariableBinding; |
| 45 |
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; |
| 46 |
|
| 47 |
import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; |
| 48 |
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; |
| 49 |
import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; |
| 50 |
import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractInterfaceProcessor; |
| 51 |
import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractSupertypeProcessor; |
| 52 |
import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring; |
| 53 |
import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodProcessor; |
| 54 |
import org.eclipse.jdt.internal.corext.refactoring.structure.PullUpRefactoringProcessor; |
| 55 |
import org.eclipse.jdt.internal.corext.refactoring.structure.PushDownRefactoringProcessor; |
| 56 |
import org.eclipse.jdt.internal.corext.refactoring.structure.PushDownRefactoringProcessor.MemberActionInfo; |
| 57 |
import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil; |
| 58 |
import org.eclipse.jdt.internal.corext.template.java.CodeTemplateContextType; |
| 59 |
|
| 60 |
import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; |
| 61 |
|
| 62 |
public class ReceiverParameterTests18 extends RefactoringTest { |
| 63 |
|
| 64 |
private static final Class clazz= ReceiverParameterTests18.class; |
| 65 |
|
| 66 |
private static final String REFACTORING_PATH= "ReceiverParameter/"; |
| 67 |
|
| 68 |
private Hashtable fOldOptions; |
| 69 |
|
| 70 |
public ReceiverParameterTests18(String name) { |
| 71 |
super(name); |
| 72 |
} |
| 73 |
|
| 74 |
public static Test suite() { |
| 75 |
return new Java18Setup(new TestSuite(clazz)); |
| 76 |
} |
| 77 |
|
| 78 |
public static Test setUpTest(Test someTest) { |
| 79 |
return new Java18Setup(someTest); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
protected void setUp() throws Exception { |
| 84 |
super.setUp(); |
| 85 |
StubUtility.setCodeTemplate(CodeTemplateContextType.NEWTYPE_ID, |
| 86 |
"${package_declaration}" + |
| 87 |
System.getProperty("line.separator", "\n") + |
| 88 |
"${" + CodeTemplateContextType.TYPE_COMMENT + "}" + |
| 89 |
System.getProperty("line.separator", "\n") + |
| 90 |
"${type_declaration}", null); |
| 91 |
|
| 92 |
StubUtility.setCodeTemplate(CodeTemplateContextType.TYPECOMMENT_ID, "/** typecomment template*/", null); |
| 93 |
|
| 94 |
fOldOptions= JavaCore.getOptions(); |
| 95 |
|
| 96 |
Hashtable options= TestOptions.getDefaultOptions(); |
| 97 |
options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ASSIGNMENT_OPERATOR, DefaultCodeFormatterConstants.TRUE); |
| 98 |
options.put(DefaultCodeFormatterConstants.FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE, "1"); |
| 99 |
options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.TAB); |
| 100 |
|
| 101 |
JavaCore.setOptions(options); |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
protected void tearDown() throws Exception { |
| 106 |
super.tearDown(); |
| 107 |
JavaCore.setOptions(fOldOptions); |
| 108 |
fOldOptions= null; |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
protected String getRefactoringPath() { |
| 113 |
return REFACTORING_PATH; |
| 114 |
} |
| 115 |
|
| 116 |
private static PullUpRefactoringProcessor createPullUpRefactoringProcessor(IMember[] methods) throws JavaModelException { |
| 117 |
IJavaProject project= null; |
| 118 |
if (methods != null && methods.length > 0) |
| 119 |
project= methods[0].getJavaProject(); |
| 120 |
if (RefactoringAvailabilityTester.isPullUpAvailable(methods)) { |
| 121 |
PullUpRefactoringProcessor processor= new PullUpRefactoringProcessor(methods, JavaPreferencesSettings.getCodeGenerationSettings(project)); |
| 122 |
new ProcessorBasedRefactoring(processor); |
| 123 |
return processor; |
| 124 |
} |
| 125 |
return null; |
| 126 |
} |
| 127 |
|
| 128 |
private static ExtractSupertypeProcessor createExtractSuperclassRefactoringProcessor(IMember[] members) throws JavaModelException { |
| 129 |
IJavaProject project= null; |
| 130 |
if (members != null && members.length > 0) |
| 131 |
project= members[0].getJavaProject(); |
| 132 |
if (RefactoringAvailabilityTester.isExtractSupertypeAvailable(members)) { |
| 133 |
final CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(project); |
| 134 |
settings.createComments= false; |
| 135 |
ExtractSupertypeProcessor processor= new ExtractSupertypeProcessor(members, settings); |
| 136 |
new ProcessorBasedRefactoring(processor); |
| 137 |
return processor; |
| 138 |
} |
| 139 |
return null; |
| 140 |
} |
| 141 |
|
| 142 |
private IPackageFragment getPackage(String name) throws JavaModelException { |
| 143 |
if ("p".equals(name)) |
| 144 |
return getPackageP(); |
| 145 |
IPackageFragment pack= getRoot().getPackageFragment(name); |
| 146 |
if (pack.exists()) |
| 147 |
return pack; |
| 148 |
return getRoot().createPackageFragment(name, false, new NullProgressMonitor()); |
| 149 |
} |
| 150 |
|
| 151 |
private static String getTopLevelTypeName(String typeQualifiedTyperName) { |
| 152 |
int dotIndex= typeQualifiedTyperName.indexOf('.'); |
| 153 |
if (dotIndex == -1) |
| 154 |
return typeQualifiedTyperName; |
| 155 |
return typeQualifiedTyperName.substring(0, dotIndex); |
| 156 |
} |
| 157 |
|
| 158 |
private void prepareForInputCheck(PushDownRefactoringProcessor processor, IMethod[] selectedMethods, IField[] selectedFields, String[] namesOfMethodsToPullUp, |
| 159 |
String[][] signaturesOfMethodsToPullUp, String[] namesOfFieldsToPullUp, String[] namesOfMethodsToDeclareAbstract, String[][] signaturesOfMethodsToDeclareAbstract) { |
| 160 |
IMethod[] methodsToPushDown= findMethods(selectedMethods, namesOfMethodsToPullUp, signaturesOfMethodsToPullUp); |
| 161 |
IField[] fieldsToPushDown= findFields(selectedFields, namesOfFieldsToPullUp); |
| 162 |
List membersToPushDown= Arrays.asList(merge(methodsToPushDown, fieldsToPushDown)); |
| 163 |
List methodsToDeclareAbstract= Arrays.asList(findMethods(selectedMethods, namesOfMethodsToDeclareAbstract, signaturesOfMethodsToDeclareAbstract)); |
| 164 |
|
| 165 |
MemberActionInfo[] infos= processor.getMemberActionInfos(); |
| 166 |
for (int i= 0; i < infos.length; i++) { |
| 167 |
if (membersToPushDown.contains(infos[i].getMember())) { |
| 168 |
infos[i].setAction(MemberActionInfo.PUSH_DOWN_ACTION); |
| 169 |
assertTrue(!methodsToDeclareAbstract.contains(infos[i].getMember())); |
| 170 |
} |
| 171 |
if (methodsToDeclareAbstract.contains(infos[i].getMember())) { |
| 172 |
infos[i].setAction(MemberActionInfo.PUSH_ABSTRACT_ACTION); |
| 173 |
assertTrue(!membersToPushDown.contains(infos[i].getMember())); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Tests "Pull Up" method refactoring involving receiver parameter. |
| 180 |
* |
| 181 |
* @throws Exception any exception thrown from this test case |
| 182 |
*/ |
| 183 |
public void testPullUp() throws Exception { |
| 184 |
String[] methodNames= new String[] { "foo1", "foo2" }; |
| 185 |
String[][] signatures= new String[][] { new String[0], new String[] { "QString;" } }; |
| 186 |
boolean deleteAllInSourceType= true; |
| 187 |
boolean deleteAllMatchingMethods= false; |
| 188 |
ICompilationUnit cu= createCUfromTestFile(getPackageP(), "A"); |
| 189 |
IType type= getType(cu, "B"); |
| 190 |
IMethod[] methods= getMethods(type, methodNames, signatures); |
| 191 |
|
| 192 |
PullUpRefactoringProcessor processor= createPullUpRefactoringProcessor(methods); |
| 193 |
Refactoring ref= processor.getRefactoring(); |
| 194 |
|
| 195 |
assertTrue("activation", ref.checkInitialConditions(new NullProgressMonitor()).isOK()); |
| 196 |
|
| 197 |
IType[] possibleClasses= processor.getCandidateTypes(new RefactoringStatus(), new NullProgressMonitor()); |
| 198 |
assertTrue("No possible class found!", possibleClasses.length > 0); |
| 199 |
processor.setDestinationType(processor.getCandidateTypes(new RefactoringStatus(), new NullProgressMonitor())[possibleClasses.length - 1 - 0]); |
| 200 |
|
| 201 |
if (deleteAllInSourceType) |
| 202 |
processor.setDeletedMethods(methods); |
| 203 |
if (deleteAllMatchingMethods) { |
| 204 |
List l= Arrays.asList(JavaElementUtil.getElementsOfType(processor.getMatchingElements(new NullProgressMonitor(), false), IJavaElement.METHOD)); |
| 205 |
processor.setDeletedMethods((IMethod[])l.toArray(new IMethod[l.size()])); |
| 206 |
} |
| 207 |
|
| 208 |
RefactoringStatus checkInputResult= ref.checkFinalConditions(new NullProgressMonitor()); |
| 209 |
assertTrue("precondition was supposed to pass", !checkInputResult.hasError()); |
| 210 |
performChange(ref, false); |
| 211 |
|
| 212 |
String expected= getFileContents(getOutputTestFileName("A")); |
| 213 |
String actual= cu.getSource(); |
| 214 |
assertEqualLines(expected, actual); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Tests "Push Down" method refactoring involving receiver parameter. |
| 219 |
* |
| 220 |
* @throws Exception any exception thrown from this test case |
| 221 |
*/ |
| 222 |
public void testPushDown() throws Exception { |
| 223 |
String[] namesOfMethodsToPushDown= { "foo1", "foo2" }; |
| 224 |
String[][] signaturesOfMethodsToPushDown= { new String[0], new String[] { "QString;" } }; |
| 225 |
String[] selectedFieldNames= {}; |
| 226 |
String[] namesOfFieldsToPushDown= {}; |
| 227 |
String[] namesOfMethodsToDeclareAbstract= {}; |
| 228 |
String[][] signaturesOfMethodsToDeclareAbstract= {}; |
| 229 |
|
| 230 |
|
| 231 |
ICompilationUnit cuA= createCUfromTestFile(getPackageP(), "A"); |
| 232 |
|
| 233 |
IType type= getType(cuA, "A"); |
| 234 |
IMethod[] selectedMethods= getMethods(type, namesOfMethodsToPushDown, signaturesOfMethodsToPushDown); |
| 235 |
IField[] selectedFields= getFields(type, selectedFieldNames); |
| 236 |
IMember[] selectedMembers= merge(selectedFields, selectedMethods); |
| 237 |
|
| 238 |
assertTrue(RefactoringAvailabilityTester.isPushDownAvailable(selectedMembers)); |
| 239 |
PushDownRefactoringProcessor processor= new PushDownRefactoringProcessor(selectedMembers); |
| 240 |
Refactoring ref= new ProcessorBasedRefactoring(processor); |
| 241 |
|
| 242 |
assertTrue("activation", ref.checkInitialConditions(new NullProgressMonitor()).isOK()); |
| 243 |
|
| 244 |
prepareForInputCheck(processor, selectedMethods, selectedFields, namesOfMethodsToPushDown, signaturesOfMethodsToPushDown, namesOfFieldsToPushDown, namesOfMethodsToDeclareAbstract, |
| 245 |
signaturesOfMethodsToDeclareAbstract); |
| 246 |
|
| 247 |
RefactoringStatus checkInputResult= ref.checkFinalConditions(new NullProgressMonitor()); |
| 248 |
assertTrue("precondition was supposed to pass but got " + checkInputResult.toString(), !checkInputResult.hasError()); |
| 249 |
performChange(ref, false); |
| 250 |
|
| 251 |
String expected= getFileContents(getOutputTestFileName("A")); |
| 252 |
String actual= cuA.getSource(); |
| 253 |
assertEqualLines("A.java", expected, actual); |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Tests "Move" method refactoring involving receiver parameter. |
| 259 |
* |
| 260 |
* @throws Exception any exception thrown from this test case |
| 261 |
*/ |
| 262 |
public void testMove() throws Exception { |
| 263 |
String[] cuQNames= new String[] { "p1.A", "p2.B" }; |
| 264 |
String selectionCuQName= "p1.A"; |
| 265 |
boolean inlineDelegator= false; |
| 266 |
boolean removeDelegator= false; |
| 267 |
int selectionCuIndex= -1; |
| 268 |
for (int i= 0; i < cuQNames.length; i++) |
| 269 |
if (cuQNames[i] == null || selectionCuQName.equals(cuQNames[i])) |
| 270 |
selectionCuIndex= i; |
| 271 |
Assert.isTrue(selectionCuIndex != -1, "parameter selectionCuQName must match some String in cuQNames."); |
| 272 |
ICompilationUnit cuA= createCUfromTestFile(getPackage("p1"), "A"); |
| 273 |
ICompilationUnit cuB= createCUfromTestFile(getPackage("p1"), "B"); |
| 274 |
|
| 275 |
int offset= cuA.getSource().indexOf("mA1(@NonNull A this, B b)"); |
| 276 |
IJavaElement[] codeSelect= cuA.codeSelect(offset, 3); |
| 277 |
assertTrue(codeSelect.length > 0); |
| 278 |
assertTrue(codeSelect[0] instanceof IMethod); |
| 279 |
IMethod method= (IMethod)codeSelect[0]; |
| 280 |
MoveInstanceMethodProcessor processor= new MoveInstanceMethodProcessor(method, JavaPreferencesSettings.getCodeGenerationSettings(cuA.getJavaProject())); |
| 281 |
Refactoring ref= new MoveRefactoring(processor); |
| 282 |
|
| 283 |
assertNotNull("refactoring should be created", ref); |
| 284 |
RefactoringStatus preconditionResult= ref.checkInitialConditions(new NullProgressMonitor()); |
| 285 |
|
| 286 |
assertTrue("activation was supposed to be successful", preconditionResult.isOK()); |
| 287 |
|
| 288 |
IVariableBinding target= null; |
| 289 |
IVariableBinding[] targets= processor.getPossibleTargets(); |
| 290 |
for (int i= 0; i < targets.length; i++) { |
| 291 |
IVariableBinding candidate= targets[i]; |
| 292 |
if (candidate.getName().equals("b")) { |
| 293 |
target= candidate; |
| 294 |
break; |
| 295 |
} |
| 296 |
} |
| 297 |
assertNotNull("Expected new target not available.", target); |
| 298 |
processor.setTarget(target); |
| 299 |
|
| 300 |
processor.setInlineDelegator(inlineDelegator); |
| 301 |
processor.setRemoveDelegator(removeDelegator); |
| 302 |
processor.setDeprecateDelegates(false); |
| 303 |
processor.setMethodName("mA1Moved"); |
| 304 |
|
| 305 |
preconditionResult.merge(ref.checkFinalConditions(new NullProgressMonitor())); |
| 306 |
|
| 307 |
assertTrue("precondition was supposed to pass", !preconditionResult.hasError()); |
| 308 |
|
| 309 |
performChange(ref, false); |
| 310 |
|
| 311 |
String outputTestFileName= getOutputTestFileName("A"); |
| 312 |
assertEqualLines("Incorrect inline in " + outputTestFileName, getFileContents(outputTestFileName), cuA.getSource()); |
| 313 |
|
| 314 |
|
| 315 |
outputTestFileName= getOutputTestFileName("B"); |
| 316 |
assertEqualLines("Incorrect inline in " + outputTestFileName, getFileContents(outputTestFileName), cuB.getSource()); |
| 317 |
|
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Tests "Move Type to New File" refactoring involving receiver parameter. |
| 322 |
* |
| 323 |
* @throws Exception any exception thrown from this test case |
| 324 |
*/ |
| 325 |
public void testMoveType2File() throws Exception { |
| 326 |
String parentClassName= "A"; |
| 327 |
String enclosingInstanceName= "Inner"; |
| 328 |
String[] cuNames= new String[] { "A" }; |
| 329 |
String[] packageNames= new String[] { "p" }; |
| 330 |
String packageName= "p"; |
| 331 |
IType parentClas= getType(createCUfromTestFile(getPackage(packageName), parentClassName), parentClassName); |
| 332 |
IType clas= parentClas.getType(enclosingInstanceName); |
| 333 |
|
| 334 |
assertTrue("should be enabled", RefactoringAvailabilityTester.isMoveInnerAvailable(clas)); |
| 335 |
MoveInnerToTopRefactoring ref= ((RefactoringAvailabilityTester.isMoveInnerAvailable(clas)) ? new MoveInnerToTopRefactoring(clas, JavaPreferencesSettings.getCodeGenerationSettings(clas |
| 336 |
.getJavaProject())) : null); |
| 337 |
RefactoringStatus preconditionResult= ref.checkInitialConditions(new NullProgressMonitor()); |
| 338 |
assertTrue("activation was supposed to be successful" + preconditionResult.toString(), preconditionResult.isOK()); |
| 339 |
|
| 340 |
assertEquals("reference creation possible", true, ref.isCreatingInstanceFieldPossible()); |
| 341 |
assertEquals("reference creation mandatory", false, ref.isCreatingInstanceFieldMandatory()); |
| 342 |
if (ref.isCreatingInstanceFieldPossible() && !ref.isCreatingInstanceFieldMandatory()) |
| 343 |
ref.setCreateInstanceField(false); |
| 344 |
ref.setEnclosingInstanceName(enclosingInstanceName); |
| 345 |
assertTrue("name should be ok ", ref.checkEnclosingInstanceName(enclosingInstanceName).isOK()); |
| 346 |
ref.setMarkInstanceFieldAsFinal(false); |
| 347 |
ICompilationUnit[] cus= new ICompilationUnit[cuNames.length]; |
| 348 |
for (int i= 0; i < cuNames.length; i++) { |
| 349 |
if (cuNames[i].equals(clas.getCompilationUnit().findPrimaryType().getElementName())) |
| 350 |
cus[i]= clas.getCompilationUnit(); |
| 351 |
else |
| 352 |
cus[i]= createCUfromTestFile(getPackage(packageNames[i]), cuNames[i]); |
| 353 |
} |
| 354 |
|
| 355 |
RefactoringStatus checkInputResult= ref.checkFinalConditions(new NullProgressMonitor()); |
| 356 |
assertTrue("precondition was supposed to pass", !checkInputResult.hasError()); |
| 357 |
performChange(ref, false); |
| 358 |
|
| 359 |
for (int i= 0; i < cus.length; i++) { |
| 360 |
String actual= cus[i].getSource(); |
| 361 |
String expected= getFileContents(getOutputTestFileName(cuNames[i])); |
| 362 |
assertEqualLines(cus[i].getElementName(), expected, actual); |
| 363 |
} |
| 364 |
ICompilationUnit newCu= clas.getPackageFragment().getCompilationUnit(enclosingInstanceName + ".java"); |
| 365 |
String expected= getFileContents(getOutputTestFileName(enclosingInstanceName)); |
| 366 |
String actual= newCu.getSource(); |
| 367 |
assertEqualLines("new Cu:", expected, actual); |
| 368 |
|
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Tests "Extract Interface" refactoring involving receiver parameter. |
| 373 |
* |
| 374 |
* @throws Exception any exception thrown from this test case |
| 375 |
*/ |
| 376 |
public void testExtractInterface() throws Exception { |
| 377 |
String className= "A"; |
| 378 |
String newInterfaceName= "I"; |
| 379 |
|
| 380 |
IType clas= getType(createCUfromTestFile(getPackageP(), getTopLevelTypeName(className)), className); |
| 381 |
ICompilationUnit cu= clas.getCompilationUnit(); |
| 382 |
IPackageFragment pack= (IPackageFragment)cu.getParent(); |
| 383 |
|
| 384 |
ExtractInterfaceProcessor processor= new ExtractInterfaceProcessor(clas, JavaPreferencesSettings.getCodeGenerationSettings(clas.getJavaProject())); |
| 385 |
Refactoring ref= new ProcessorBasedRefactoring(processor); |
| 386 |
|
| 387 |
processor.setTypeName(newInterfaceName); |
| 388 |
assertEquals("interface name should be accepted", RefactoringStatus.OK, processor.checkTypeName(newInterfaceName).getSeverity()); |
| 389 |
|
| 390 |
processor.setExtractedMembers(processor.getExtractableMembers()); |
| 391 |
processor.setReplace(true); |
| 392 |
processor.setAnnotations(false); |
| 393 |
assertEquals("was supposed to pass", null, performRefactoring(ref)); |
| 394 |
assertEqualLines("incorrect changes in " + className, |
| 395 |
getFileContents(getOutputTestFileName(className)), |
| 396 |
cu.getSource()); |
| 397 |
|
| 398 |
ICompilationUnit interfaceCu= pack.getCompilationUnit(newInterfaceName + ".java"); |
| 399 |
assertEqualLines("incorrect interface created", |
| 400 |
getFileContents(getOutputTestFileName(newInterfaceName)), |
| 401 |
interfaceCu.getSource()); |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Tests "Extract Superclass" refactoring involving receiver parameter. |
| 406 |
* |
| 407 |
* @throws Exception any exception thrown from this test case |
| 408 |
*/ |
| 409 |
public void testExtractSuperclass() throws Exception { |
| 410 |
String[] methodNames= new String[] { "foo1", "foo2" }; |
| 411 |
String[][] signatures= new String[][] { new String[0], new String[] { "QString;" } }; |
| 412 |
boolean replaceOccurences= true; |
| 413 |
|
| 414 |
|
| 415 |
ICompilationUnit cu= createCUfromTestFile(getPackageP(), "A"); |
| 416 |
IType type= getType(cu, "A"); |
| 417 |
IMethod[] methods= getMethods(type, methodNames, signatures); |
| 418 |
|
| 419 |
ExtractSupertypeProcessor processor= createExtractSuperclassRefactoringProcessor(methods); |
| 420 |
Refactoring refactoring= processor.getRefactoring(); |
| 421 |
processor.setMembersToMove(methods); |
| 422 |
|
| 423 |
assertTrue("activation", refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()); |
| 424 |
|
| 425 |
processor.setTypesToExtract(new IType[] { type }); |
| 426 |
processor.setTypeName("B"); |
| 427 |
processor.setCreateMethodStubs(true); |
| 428 |
processor.setInstanceOf(false); |
| 429 |
processor.setReplace(replaceOccurences); |
| 430 |
processor.setDeletedMethods(methods); |
| 431 |
|
| 432 |
RefactoringStatus status= refactoring.checkFinalConditions(new NullProgressMonitor()); |
| 433 |
assertTrue("precondition was supposed to pass", !status.hasError()); |
| 434 |
performChange(refactoring, false); |
| 435 |
|
| 436 |
String expected= getFileContents(getOutputTestFileName("A")); |
| 437 |
String actual= cu.getSource(); |
| 438 |
assertEqualLines(expected, actual); |
| 439 |
|
| 440 |
expected= getFileContents(getOutputTestFileName("B")); |
| 441 |
ICompilationUnit unit= getPackageP().getCompilationUnit("B.java"); |
| 442 |
if (!unit.exists()) |
| 443 |
assertTrue("extracted compilation unit does not exist", false); |
| 444 |
actual= unit.getBuffer().getContents(); |
| 445 |
assertEqualLines(expected, actual); |
| 446 |
|
| 447 |
|
| 448 |
} |
| 449 |
|
| 450 |
} |