mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
show conflicts unification;
encapsulate fields: rise conflicts; tests
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* User: anna
|
||||
* Date: 20-Aug-2008
|
||||
*/
|
||||
package com.intellij.refactoring;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.util.PropertyUtil;
|
||||
import com.intellij.refactoring.encapsulateFields.EncapsulateFieldsDescriptor;
|
||||
import com.intellij.refactoring.encapsulateFields.EncapsulateFieldsProcessor;
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class EncapsulateFieldsTest extends MultiFileTestCase{
|
||||
public void testAlreadyExist() throws Exception {
|
||||
doTest("i" , null);
|
||||
}
|
||||
|
||||
public void testDiffWithReturnTypeOnly() throws Exception {
|
||||
doTest("i", "There already is a method <b><code>Test setI(int)</code></b> which differs from setter <b><code>setI</code></b> by return type only.");
|
||||
}
|
||||
|
||||
public void testDiffWithReturnTypeOnlyInHierarchy() throws Exception {
|
||||
doTest("i", "There already is a method <b><code>Super setI(int)</code></b> which differs from setter <b><code>setI</code></b> by return type only.");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return JavaTestUtil.getJavaTestDataPath();
|
||||
}
|
||||
|
||||
protected String getTestRoot() {
|
||||
return "/refactoring/encapsulateFields/";
|
||||
}
|
||||
|
||||
|
||||
private void doTest(final String fieldName, final String conflicts) throws Exception {
|
||||
doTest(new PerformAction() {
|
||||
public void performAction(final VirtualFile rootDir, final VirtualFile rootAfter) throws Exception {
|
||||
PsiClass aClass = myJavaFacade.findClass("Test", GlobalSearchScope.projectScope(myProject));
|
||||
|
||||
assertNotNull("Class Test not found", aClass);
|
||||
|
||||
|
||||
doTest(aClass, aClass.findFieldByName(fieldName, false), conflicts, true, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static void doTest(final PsiClass aClass, final PsiField field, final String conflicts,
|
||||
final boolean generateGetters, final boolean generateSetters) {
|
||||
try {
|
||||
final Project project = aClass.getProject();
|
||||
EncapsulateFieldsProcessor processor = new EncapsulateFieldsProcessor(project, new EncapsulateFieldsDescriptor() {
|
||||
public PsiField[] getSelectedFields() {
|
||||
return new PsiField[]{field};
|
||||
}
|
||||
|
||||
public String[] getGetterNames() {
|
||||
return new String[]{PropertyUtil.suggestGetterName(project, field)};
|
||||
}
|
||||
|
||||
public String[] getSetterNames() {
|
||||
return new String[]{PropertyUtil.suggestSetterName(project, field)};
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public PsiMethod[] getGetterPrototypes() {
|
||||
return isToEncapsulateGet() ? new PsiMethod[]{PropertyUtil.generateGetterPrototype(field)} : null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public PsiMethod[] getSetterPrototypes() {
|
||||
return isToEncapsulateSet() ? new PsiMethod[]{PropertyUtil.generateSetterPrototype(field)} : null;
|
||||
}
|
||||
|
||||
public boolean isToEncapsulateGet() {
|
||||
return generateGetters;
|
||||
}
|
||||
|
||||
public boolean isToEncapsulateSet() {
|
||||
return generateSetters;
|
||||
}
|
||||
|
||||
public boolean isToUseAccessorsWhenAccessible() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Modifier
|
||||
public String getFieldsVisibility() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Modifier
|
||||
public String getAccessorsVisibility() {
|
||||
return PsiModifier.PUBLIC;
|
||||
}
|
||||
});
|
||||
processor.run();
|
||||
LocalFileSystem.getInstance().refresh(false);
|
||||
FileDocumentManager.getInstance().saveAllDocuments();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (conflicts != null) {
|
||||
Assert.assertEquals(conflicts, e.getMessage());
|
||||
return;
|
||||
} else {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
if (conflicts != null) {
|
||||
fail("Conflicts were not detected: " + conflicts);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,12 +4,12 @@
|
||||
*/
|
||||
package com.intellij.refactoring;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.refactoring.inlineSuperClass.InlineSuperClassRefactoringProcessor;
|
||||
import com.intellij.JavaTestUtil;
|
||||
|
||||
public class InlineSuperClassTest extends MultiFileTestCase {
|
||||
protected String getTestRoot() {
|
||||
|
||||
@@ -31,12 +31,28 @@ public class IntroduceParameterTest extends LightCodeInsightTestCase {
|
||||
protected String getTestDataPath() {
|
||||
return JavaTestUtil.getJavaTestDataPath();
|
||||
}
|
||||
|
||||
|
||||
private void doTest(int replaceFieldsWithGetters, boolean removeUnusedParameters, boolean searchForSuper, boolean declareFinal,
|
||||
final boolean generateDelegate) throws Exception {
|
||||
configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java");
|
||||
perform(true, replaceFieldsWithGetters, "anObject", searchForSuper, declareFinal, removeUnusedParameters, generateDelegate);
|
||||
checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java");
|
||||
doTest(replaceFieldsWithGetters, removeUnusedParameters, searchForSuper, declareFinal, generateDelegate, null);
|
||||
}
|
||||
|
||||
private void doTest(int replaceFieldsWithGetters, boolean removeUnusedParameters, boolean searchForSuper, boolean declareFinal, final boolean generateDelegate,
|
||||
String conflict) throws Exception {
|
||||
try {
|
||||
configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java");
|
||||
perform(true, replaceFieldsWithGetters, "anObject", searchForSuper, declareFinal, removeUnusedParameters, generateDelegate);
|
||||
checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java");
|
||||
if (conflict != null) {
|
||||
fail("Conflict expected");
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (conflict == null) {
|
||||
throw e;
|
||||
}
|
||||
assertEquals(conflict, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testNoUsages() throws Exception {
|
||||
@@ -60,7 +76,7 @@ public class IntroduceParameterTest extends LightCodeInsightTestCase {
|
||||
}
|
||||
|
||||
public void testThisSubstitutionInQualifier() throws Exception {
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false);
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false, "field <b><code>Test.i</code></b> is not accesible from method <b><code>XTest.n()</code></b>. Value for introduced parameter in that method call will be incorrect.");
|
||||
}
|
||||
|
||||
public void testFieldAccess() throws Exception {
|
||||
@@ -88,7 +104,7 @@ public class IntroduceParameterTest extends LightCodeInsightTestCase {
|
||||
}
|
||||
|
||||
public void testSuperInExpression() throws Exception {
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false);
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false, "Parameter initializer contains <b><code>super</code></b>, but not all calls to method are in its class.");
|
||||
}
|
||||
|
||||
public void testNull() throws Exception {
|
||||
@@ -124,7 +140,7 @@ public class IntroduceParameterTest extends LightCodeInsightTestCase {
|
||||
}
|
||||
|
||||
public void testSuperWithSideEffect() throws Exception {
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false);
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false, "Parameter initializer contains <b><code>super</code></b>, but not all calls to method are in its class.");
|
||||
}
|
||||
|
||||
public void testConflictingField() throws Exception {
|
||||
|
||||
@@ -54,7 +54,13 @@ public class MoveClassTest extends CodeInsightTestCase {
|
||||
}
|
||||
|
||||
public void testClassAndSecondary() throws Exception{
|
||||
doTest("classAndSecondary", new String[]{"pack1.Class1", "pack1.Class2"}, "pack2");
|
||||
try {
|
||||
doTest("classAndSecondary", new String[]{"pack1.Class1", "pack1.Class2"}, "pack2");
|
||||
fail("Conflicts expected");
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
assertEquals("A package-local class <b><code>Class2</code></b> will no longer be accessible from field <b><code>User.class2</code></b>", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testIdeadev27996() throws Exception {
|
||||
|
||||
@@ -10,7 +10,6 @@ import com.intellij.psi.PsiModifier;
|
||||
import com.intellij.psi.search.ProjectScope;
|
||||
import com.intellij.refactoring.move.moveMembers.MockMoveMembersOptions;
|
||||
import com.intellij.refactoring.move.moveMembers.MoveMembersProcessor;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
@@ -95,7 +94,7 @@ public class MoveMembersTest extends MultiFileTestCase {
|
||||
fail("conflict expected");
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertEquals(e.getMessage(), "Found conflicts: Field <b><code>B.ONE</code></b> has write access but is moved to an interface");
|
||||
assertEquals("Field <b><code>B.ONE</code></b> has write access but is moved to an interface", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,15 +138,7 @@ public class MoveMembersTest extends MultiFileTestCase {
|
||||
|
||||
MockMoveMembersOptions options = new MockMoveMembersOptions(targetClass.getQualifiedName(), memberSet);
|
||||
options.setMemberVisibility(null);
|
||||
new MoveMembersProcessor(myProject, null, options){
|
||||
@Override
|
||||
protected boolean showConflicts(MultiMap<PsiElement, String> conflicts) {
|
||||
if (!conflicts.isEmpty()) {
|
||||
throw new RuntimeException("Found conflicts: " + conflicts.values().iterator().next());
|
||||
}
|
||||
return super.showConflicts(conflicts);
|
||||
}
|
||||
}.run();
|
||||
new MoveMembersProcessor(myProject, null, options).run();
|
||||
FileDocumentManager.getInstance().saveAllDocuments();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ public class PushDownTest extends LightCodeInsightTestCase {
|
||||
if (failure ? conflicts.isEmpty() : !conflicts.isEmpty()) {
|
||||
fail(failure ? "Conflict was not detected" : "False conflict was detected");
|
||||
}
|
||||
return super.showConflicts(conflicts);
|
||||
return true;
|
||||
}
|
||||
}.run();
|
||||
|
||||
@@ -92,4 +92,4 @@ public class PushDownTest extends LightCodeInsightTestCase {
|
||||
public void testThisRefInAnonymous() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,11 +30,7 @@ public class RemoveMiddleManTest extends MultiFileTestCase{
|
||||
return "/refactoring/removemiddleman/";
|
||||
}
|
||||
|
||||
private void doTest() throws Exception {
|
||||
doTest(true);
|
||||
}
|
||||
|
||||
private void doTest(final boolean delete) throws Exception {
|
||||
private void doTest(final String conflict) throws Exception {
|
||||
doTest(new PerformAction() {
|
||||
public void performAction(final VirtualFile rootDir, final VirtualFile rootAfter) throws Exception {
|
||||
PsiClass aClass = myJavaFacade.findClass("Test", GlobalSearchScope.allScope(getProject()));
|
||||
@@ -48,35 +44,42 @@ public class RemoveMiddleManTest extends MultiFileTestCase{
|
||||
for (PsiMethod method : methods) {
|
||||
final MemberInfo info = new MemberInfo(method);
|
||||
info.setChecked(true);
|
||||
info.setToAbstract(delete);
|
||||
info.setToAbstract(true);
|
||||
infos.add(info);
|
||||
}
|
||||
RemoveMiddlemanProcessor processor = new RemoveMiddlemanProcessor(field, infos);
|
||||
processor.run();
|
||||
LocalFileSystem.getInstance().refresh(false);
|
||||
FileDocumentManager.getInstance().saveAllDocuments();
|
||||
try {
|
||||
RemoveMiddlemanProcessor processor = new RemoveMiddlemanProcessor(field, infos);
|
||||
processor.run();
|
||||
LocalFileSystem.getInstance().refresh(false);
|
||||
FileDocumentManager.getInstance().saveAllDocuments();
|
||||
if (conflict != null) fail("Conflict expected");
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (conflict == null) throw e;
|
||||
assertEquals(conflict, e.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void testNoGetter() throws Exception {
|
||||
doTest();
|
||||
doTest((String)null);
|
||||
}
|
||||
|
||||
public void testSiblings() throws Exception {
|
||||
doTest();
|
||||
doTest("foo() will be deleted. Hierarchy will be broken");
|
||||
}
|
||||
|
||||
|
||||
public void testInterface() throws Exception {
|
||||
doTest();
|
||||
doTest("foo() will be deleted. Hierarchy will be broken");
|
||||
}
|
||||
|
||||
public void testPresentGetter() throws Exception {
|
||||
doTest();
|
||||
doTest("foo() will be deleted. Hierarchy will be broken");
|
||||
}
|
||||
|
||||
public void testInterfaceDelegation() throws Exception {
|
||||
doTest();
|
||||
doTest("foo() will be deleted. Hierarchy will be broken");
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -5,6 +5,7 @@ import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.util.VisibilityUtil;
|
||||
|
||||
/**
|
||||
* @author dsl
|
||||
@@ -36,7 +37,7 @@ public class ConvertToInstanceMethodTest extends LightCodeInsightTestCase {
|
||||
assertTrue("<caret> is not on method name", targetElement instanceof PsiMethod);
|
||||
PsiMethod method = (PsiMethod) targetElement;
|
||||
new ConvertToInstanceMethodProcessor(getProject(),
|
||||
method, method.getParameterList().getParameters()[targetParameter], null).run();
|
||||
method, method.getParameterList().getParameters()[targetParameter], VisibilityUtil.ESCALATE_VISIBILITY).run();
|
||||
checkResultByFile(filePath + ".after");
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user