[Groovy] convert IntroduceParameterTest to Java (IDEA-327339)

GitOrigin-RevId: 850198b2bee49834fd5a0f70af68a15cc90ac6c1
This commit is contained in:
Bas Leijdekkers
2024-10-07 17:17:27 +00:00
committed by intellij-monorepo-bot
parent 7469ab3a0f
commit 92a84afeb5
@@ -1,195 +1,196 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.plugins.groovy.refactoring.introduceParameter;
package org.jetbrains.plugins.groovy.refactoring.introduceParameter
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.*
import com.intellij.psi.impl.source.PostprocessReformattingAspect
import com.intellij.refactoring.HelpID
import com.intellij.refactoring.IntroduceParameterRefactoring
import com.intellij.refactoring.introduceField.ElementToWorkOn
import com.intellij.refactoring.introduceParameter.IntroduceParameterProcessor
import com.intellij.refactoring.introduceParameter.Util
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase
import it.unimi.dsi.fastutil.ints.IntArrayList
import org.jetbrains.annotations.NonNls
import org.jetbrains.annotations.Nullable
import org.jetbrains.plugins.groovy.util.TestUtils
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
import com.intellij.refactoring.HelpID;
import com.intellij.refactoring.IntroduceParameterRefactoring;
import com.intellij.refactoring.introduceField.ElementToWorkOn;
import com.intellij.refactoring.introduceParameter.IntroduceParameterProcessor;
import com.intellij.refactoring.introduceParameter.Util;
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.util.TestUtils;
/**
* @author Maxim.Medvedev
*/
class IntroduceParameterTest extends LightJavaCodeInsightFixtureTestCase {
final String getBasePath() {
return TestUtils.testDataPath + 'refactoring/introduceParameter/' + getTestName(true) + '/'
public class IntroduceParameterTest extends LightJavaCodeInsightFixtureTestCase {
@Override
public final String getBasePath() {
return TestUtils.getTestDataPath() + "refactoring/introduceParameter/" + getTestName(true) + "/";
}
private void doTest(int replaceFieldsWithGetters, boolean removeUnusedParameters, boolean searchForSuper, boolean declareFinal, @Nullable String conflicts = null) {
final String beforeGroovy = getTestName(false)+"Before.groovy"
final String afterGroovy = getTestName(false) + "After.groovy"
final String javaClass = getTestName(false) + "MyClass.java"
myFixture.configureByFiles(javaClass, beforeGroovy)
executeRefactoring(IntroduceVariableBase.JavaReplaceChoice.ALL, replaceFieldsWithGetters, "anObject", searchForSuper, declareFinal, removeUnusedParameters, conflicts)
PostprocessReformattingAspect.getInstance(project).doPostponedFormatting()
myFixture.checkResultByFile(beforeGroovy, afterGroovy, true)
private void doTest(int replaceFieldsWithGetters,
boolean removeUnusedParameters,
boolean searchForSuper,
boolean declareFinal,
@Nullable String conflicts) {
final String beforeGroovy = getTestName(false) + "Before.groovy";
final String afterGroovy = getTestName(false) + "After.groovy";
final String javaClass = getTestName(false) + "MyClass.java";
myFixture.configureByFiles(javaClass, beforeGroovy);
executeRefactoring(replaceFieldsWithGetters, searchForSuper, declareFinal, removeUnusedParameters, conflicts);
PostprocessReformattingAspect.getInstance(getProject()).doPostponedFormatting();
myFixture.checkResultByFile(beforeGroovy, afterGroovy, true);
}
private void doTest(int replaceFieldsWithGetters, boolean removeUnusedParameters, boolean searchForSuper, boolean declareFinal) {
doTest(replaceFieldsWithGetters, removeUnusedParameters, searchForSuper, declareFinal, null);
}
private boolean executeRefactoring(IntroduceVariableBase.JavaReplaceChoice replaceAllOccurrences,
int replaceFieldsWithGetters,
@NonNls String parameterName,
boolean searchForSuper,
boolean declareFinal,
final boolean removeUnusedParameters,
final String conflicts) {
boolean generateDelegate = false
Editor editor = myFixture.editor
private void executeRefactoring(int replaceFieldsWithGetters, boolean searchForSuper, boolean declareFinal,
boolean removeUnusedParameters, String conflicts) {
boolean generateDelegate = false;
Editor editor = myFixture.getEditor();
final PsiFile myFile = myFixture.file
final ElementToWorkOn[] elementToWorkOn = new ElementToWorkOn[1]
ElementToWorkOn.processElementToWorkOn(editor, myFile, "INtr param", HelpID.INTRODUCE_PARAMETER, project, new ElementToWorkOn.ElementsProcessor<com.intellij.refactoring.introduceField.ElementToWorkOn>() {
final PsiFile myFile = myFixture.getFile();
final ElementToWorkOn[] elementToWorkOn = new ElementToWorkOn[1];
ElementToWorkOn.processElementToWorkOn(editor, myFile, "INtr param", HelpID.INTRODUCE_PARAMETER, getProject(),
new ElementToWorkOn.ElementsProcessor<>() {
@Override
public boolean accept(ElementToWorkOn el) {
return true;
}
@Override
boolean accept(ElementToWorkOn el) {
return true
}
@Override
public void pass(final ElementToWorkOn e) {
if (e == null) return;
elementToWorkOn[0] = e;
}
});
@Override
void pass(final ElementToWorkOn e) {
if (e == null) return
final PsiExpression expr = elementToWorkOn[0].getExpression();
final PsiLocalVariable localVar = elementToWorkOn[0].getLocalVariable();
elementToWorkOn[0] = e
}
})
PsiElement context = expr == null ? localVar : expr;
PsiMethod method = Util.getContainingMethod(context);
if (method == null) return;
final PsiExpression expr = elementToWorkOn[0].expression
final PsiLocalVariable localVar = elementToWorkOn[0].localVariable
PsiElement context = expr == null ? localVar : expr
PsiMethod method = Util.getContainingMethod(context)
if (method == null) return false
final PsiMethod methodToSearchFor
final PsiMethod methodToSearchFor;
if (searchForSuper) {
final PsiMethod[] superMethods = method.findDeepestSuperMethods()
methodToSearchFor = superMethods.length > 0 ? superMethods[0] : method //findDeepestSuperMethod();
final PsiMethod[] superMethods = method.findDeepestSuperMethods();
methodToSearchFor = superMethods.length > 0 ? superMethods[0] : method;//findDeepestSuperMethod();
}
else {
methodToSearchFor = method
methodToSearchFor = method;
}
PsiExpression initializer = expr == null ? localVar.initializer : expr
assert initializer != null
IntArrayList parametersToRemove = removeUnusedParameters ? new IntArrayList(Util.findParametersToRemove(method, initializer, null)) : new IntArrayList()
final Project project = myFixture.project
PsiExpression initializer = expr == null ? localVar.getInitializer() : expr;
assert initializer != null;
IntList parametersToRemove = removeUnusedParameters ? Util.findParametersToRemove(method, initializer, null) : new IntArrayList();
final Project project = myFixture.getProject();
final IntroduceParameterProcessor processor =
new IntroduceParameterProcessor(project, method, methodToSearchFor, initializer, expr, localVar, true, parameterName,
replaceAllOccurrences, replaceFieldsWithGetters, declareFinal, generateDelegate, false, null,
parametersToRemove)
new IntroduceParameterProcessor(project, method, methodToSearchFor, initializer, expr, localVar, true, "anObject",
IntroduceVariableBase.JavaReplaceChoice.ALL, replaceFieldsWithGetters, declareFinal, generateDelegate,
false, null, parametersToRemove);
try {
processor.run()
if (conflicts != null) fail("Conflicts were expected")
processor.run();
if (conflicts != null) fail("Conflicts were expected");
}
catch (Exception e) {
if (conflicts == null){
e.printStackTrace()
fail("Conflicts were not expected")
if (conflicts == null) {
e.printStackTrace();
fail("Conflicts were not expected");
}
assertEquals(conflicts, e.message)
assertEquals(conflicts, e.getMessage());
}
editor.selectionModel.removeSelection()
return true
editor.getSelectionModel().removeSelection();
}
void testSimpleOverridedMethod() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false)
public void testSimpleOverridedMethod() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false);
}
void testOverridedMethodWithRemoveUnusedParameters() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false)
public void testOverridedMethodWithRemoveUnusedParameters() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false);
}
void testSimpleUsage() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false)
public void testSimpleUsage() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false);
}
void testMethodWithoutParams() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false)
public void testMethodWithoutParams() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false);
}
void testParameterSubstitution() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false)
public void testParameterSubstitution() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false);
}
void testThisSubstitution() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false)
public void testThisSubstitution() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false);
}
void testThisSubstitutionInQualifier() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, "field <b><code>Test.i</code></b> is not accessible from method <b><code>XTest.n()</code></b>. Value for introduced parameter in that method call will be incorrect.")
public void testThisSubstitutionInQualifier() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false,
"field <b><code>Test.i</code></b> is not accessible from method <b><code>XTest.n()</code></b>. Value for introduced parameter in that method call will be incorrect.");
}
void testFieldAccess() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false)
public void testFieldAccess() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false);
}
void testMethodAccess() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false)
public void testMethodAccess() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false);
}
void testStaticFieldAccess() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false)
public void testStaticFieldAccess() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false);
}
void testFieldWithGetterReplacement() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, false, false, false)
public void testFieldWithGetterReplacement() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, false, false, false);
}
void testFieldWithInaccessibleGetterReplacement() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false)
public void testFieldWithInaccessibleGetterReplacement() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false);
}
void testWeirdQualifier() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false)
public void testWeirdQualifier() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false);
}
void testSuperInExpression() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, "Parameter initializer contains <b><code>super</code></b>, but not all calls to method are in its class")
public void testSuperInExpression() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false,
"Parameter initializer contains <b><code>super</code></b>, but not all calls to method are in its class");
}
void testWeirdQualifierAndParameter() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false)
public void testWeirdQualifierAndParameter() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false);
}
void testImplicitSuperCall() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false)
public void testImplicitSuperCall() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false);
}
void testImplicitDefaultConstructor() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false)
public void testImplicitDefaultConstructor() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false);
}
void testInternalSideEffect() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false)
public void testInternalSideEffect() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false);
}
/* public void testAnonymousClass() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false);
}*/
void testSuperWithSideEffect() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, "Parameter initializer contains <b><code>super</code></b>, but not all calls to method are in its class")
public void testSuperWithSideEffect() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false,
"Parameter initializer contains <b><code>super</code></b>, but not all calls to method are in its class");
}
void testConflictingField() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, true, false)
public void testConflictingField() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, true, false);
}
/*public void testParameterJavaDoc1() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, true);
}
@@ -206,25 +207,23 @@ class IntroduceParameterTest extends LightJavaCodeInsightFixtureTestCase {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, true);
}*/
void testRemoveParameterInHierarchy() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false)
public void testRemoveParameterInHierarchy() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false);
}
/*public void testRemoveParameterWithJavadoc() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false);
}*/
void testVarargs() { // IDEADEV-16828
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false)
public void testVarargs() { // IDEADEV-16828
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false);
}
void testMethodUsageInThisMethodInheritor() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false)
public void testMethodUsageInThisMethodInheritor() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false);
}
void testIntroduceConstantField() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false)
public void testIntroduceConstantField() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false);
}
}
}