diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/overriding/GroovyOverrideImplementTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/overriding/GroovyOverrideImplementTest.java index 37467b6b71e1..675145bb6f6b 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/overriding/GroovyOverrideImplementTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/overriding/GroovyOverrideImplementTest.java @@ -1,262 +1,253 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jetbrains.plugins.groovy.lang.overriding +// 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.lang.overriding; -import com.intellij.codeInsight.generation.OverrideImplementUtil -import com.intellij.openapi.application.impl.NonBlockingReadActionImpl -import com.intellij.openapi.command.WriteCommandAction -import com.intellij.psi.JavaPsiFacade -import com.intellij.psi.PsiClassOwner -import com.intellij.psi.PsiMethod -import com.intellij.psi.impl.source.PostprocessReformattingAspect -import com.intellij.psi.search.GlobalSearchScope -import org.jetbrains.plugins.groovy.LightGroovyTestCase -import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition -class GroovyOverrideImplementTest extends LightGroovyTestCase { +import com.intellij.codeInsight.generation.OverrideImplementUtil; +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.openapi.application.impl.NonBlockingReadActionImpl; +import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.psi.JavaPsiFacade; +import com.intellij.psi.PsiClassOwner; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.impl.source.PostprocessReformattingAspect; +import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.testFramework.UsefulTestCase; +import org.jetbrains.plugins.groovy.LightGroovyTestCase; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition; - void testInEmptyBraces() throws Exception { - myFixture.configureByText "a.groovy", """ -class Test {} -""" - generateImplementation(findMethod(Object.name, "equals")) - myFixture.checkResult """ -class Test { - @Override - boolean equals(Object obj) { - return super.equals(obj) - } -} -""" +import java.util.List; + +public class GroovyOverrideImplementTest extends LightGroovyTestCase { + public void testInEmptyBraces() throws Exception { + myFixture.configureByText("a.groovy", """ + class Test {} + """); + generateImplementation(findMethod(Object.class.getName(), "equals")); + myFixture.checkResult(""" + class Test { + @Override + boolean equals(Object obj) { + return super.equals(obj) + } + } + """); } - void testConstructor() throws Exception { - myFixture.configureByText "a.groovy", """ -class Test {} -""" - generateImplementation(findMethod(Object.name, "Object")) - myFixture.checkResult """ -class Test { - Test() { - super() - } -} -""" + public void testConstructor() throws Exception { + myFixture.configureByText("a.groovy", """ + class Test {} + """); + generateImplementation(findMethod(Object.class.getName(), "Object")); + myFixture.checkResult(""" + class Test { + Test() { + super() + } + } + """); } - void testNoSuperReturnType() throws Exception { + public void testNoSuperReturnType() throws Exception { myFixture.addFileToProject("Foo.groovy", """ - class Foo { - def foo() { - true + class Foo { + def foo() { + true + } } - }""") + """); - myFixture.configureByText "a.groovy", """ -class Test {} -""" - generateImplementation(findMethod("Foo", "foo")) - myFixture.checkResult """ -class Test { - @Override - def foo() { - return super.foo() - } -} -""" + myFixture.configureByText("a.groovy", """ + class Test {} + """); + generateImplementation(findMethod("Foo", "foo")); + myFixture.checkResult(""" + class Test { + @Override + def foo() { + return super.foo() + } + } + """); } - void testMethodTypeParameters() { - myFixture.addFileToProject "v.java", """ -class Base { - public T[] toArray(T[] t) {return (T[])new Object[0];} -} -""" - myFixture.configureByText "a.groovy", """ -class Test extends Base {} -""" - generateImplementation(findMethod("Base", "toArray")) - myFixture.checkResult """ -class Test extends Base { - @Override - def T1[] toArray(T1[] t) { - return super.toArray(t) - } -} -""" + public void testMethodTypeParameters() { + myFixture.addFileToProject("v.java", """ + class Base { + public T[] toArray(T[] t) {return (T[])new Object[0];} + } + """); + myFixture.configureByText("a.groovy", """ + class Test extends Base {} + """); + generateImplementation(findMethod("Base", "toArray")); + myFixture.checkResult(""" + class Test extends Base { + @Override + def T1[] toArray(T1[] t) { + return super.toArray(t) + } + } + """); } - void testThrowsList() { - assertImplement('''\ -class X implements I { - -} - -interface I { - void foo() throws RuntimeException -} -''', 'I', 'foo', '''\ -class X implements I { - - @Override - void foo() throws RuntimeException { - - } -} - -interface I { - void foo() throws RuntimeException -} -''') + public void testThrowsList() { + assertImplement(""" + class X implements I { + + } + + interface I { + void foo() throws RuntimeException + } + """, "I", "foo", """ + class X implements I { + + @Override + void foo() throws RuntimeException { + + } + } + + interface I { + void foo() throws RuntimeException + } + """); } private void assertImplement(String textBefore, String clazz, String name, String textAfter) { - myFixture.configureByText('a.groovy', textBefore) - generateImplementation(findMethod(clazz, name)) - myFixture.checkResult(textAfter) + myFixture.configureByText("a.groovy", textBefore); + generateImplementation(findMethod(clazz, name)); + myFixture.checkResult(textAfter); } - void testThrowsListWithImport() { - myFixture.addClass('''\ -package pack; -public class Exc extends RuntimeException {} -''') + public void testThrowsListWithImport() { + myFixture.addClass(""" + package pack; + public class Exc extends RuntimeException {} + """); - myFixture.addClass('''\ -import pack.Exc; + myFixture.addClass(""" + import pack.Exc; + + interface I { + void foo() throws Exc; + } + """); -interface I { - void foo() throws Exc; -} -''') + myFixture.configureByText("a.groovy", """ + class X implements I { + + } + """); - myFixture.configureByText('a.groovy', '''\ -class X implements I { - -} -''') + generateImplementation(findMethod("I", "foo")); - generateImplementation(findMethod('I', 'foo')) - - myFixture.checkResult('''\ -import pack.Exc - -class X implements I { - - @Override - void foo() throws Exc { - - } -} -''') + myFixture.checkResult(""" + import pack.Exc + + class X implements I { + + @Override + void foo() throws Exc { + + } + } + """); } - void testNullableParameter() { - myFixture.addClass(''' -package org.jetbrains.annotations; -public @interface Nullable{} -''') + public void testNullableParameter() { + myFixture.addClass(""" + package org.jetbrains.annotations; + public @interface Nullable{} + """); - assertImplement(''' -import org.jetbrains.annotations.Nullable - -class Inheritor implements I { - -} - -interface I { - def foo(@Nullable p) -} -''', 'I', 'foo', ''' -import org.jetbrains.annotations.Nullable - -class Inheritor implements I { - - @Override - def foo(@Nullable Object p) { - return null - } -} - -interface I { - def foo(@Nullable p) -} -''') + assertImplement(""" + import org.jetbrains.annotations.Nullable + + class Inheritor implements I { + + } + + interface I { + def foo(@Nullable p) + } + """, "I", "foo", """ + import org.jetbrains.annotations.Nullable + + class Inheritor implements I { + + @Override + def foo(@Nullable Object p) { + return null + } + } + + interface I { + def foo(@Nullable p) + } + """); } - void _testImplementIntention() { - myFixture.configureByText('a.groovy', ''' -class Base { - public E foo(E e){} -} + public void _testImplementIntention() { + myFixture.configureByText("a.groovy", """ + class Base { + public E foo(E e){} + } + + class Test extends Base { + } + """); -class Test extends Base { -} -''') + List fixes = myFixture.getAvailableIntentions(); + UsefulTestCase.assertSize(1, fixes); - def fixes = myFixture.getAvailableIntentions() - assertSize(1, fixes) - - def fix = fixes[0] - fix.invoke(myFixture.project, myFixture.editor, myFixture.file) + IntentionAction fix = fixes.get(0); + fix.invoke(myFixture.getProject(), myFixture.getEditor(), myFixture.getFile()); } - void 'test abstract final trait properties'() { - myFixture.addFileToProject('T.groovy', '''\ -trait T { - abstract foo - abstract final bar -} -''') - myFixture.configureByText('classes.groovy', '''\ -class A implements T { -} -''') - myFixture.launchAction myFixture.findSingleIntention('Implement methods') - NonBlockingReadActionImpl.waitForAsyncTaskCompletion() - myFixture.checkResult('''\ -class A implements T { - @Override - Object getFoo() { - return null - } - - @Override - void setFoo(Object foo) { - - } - - @Override - Object getBar() { - return null - } -} -''') + public void test_abstract_final_trait_properties() { + myFixture.addFileToProject("T.groovy", """ + trait T { + abstract foo + abstract final bar + } + """); + myFixture.configureByText("classes.groovy", """ + class A implements T { + } + """); + myFixture.launchAction(myFixture.findSingleIntention("Implement methods")); + NonBlockingReadActionImpl.waitForAsyncTaskCompletion(); + myFixture.checkResult(""" + class A implements T { + @Override + Object getFoo() { + return null + } + + @Override + void setFoo(Object foo) { + + } + + @Override + Object getBar() { + return null + } + } + """); } - private def generateImplementation(PsiMethod method) { - WriteCommandAction.runWriteCommandAction project, { - GrTypeDefinition clazz = (myFixture.file as PsiClassOwner).classes[0] as GrTypeDefinition - OverrideImplementUtil.overrideOrImplement(clazz, method) - PostprocessReformattingAspect.getInstance(myFixture.project).doPostponedFormatting() - } - myFixture.editor.selectionModel.removeSelection() + private void generateImplementation(final PsiMethod method) { + WriteCommandAction.runWriteCommandAction(getProject(), () -> { + GrTypeDefinition clazz = (GrTypeDefinition)((PsiClassOwner)myFixture.getFile()).getClasses()[0]; + OverrideImplementUtil.overrideOrImplement(clazz, method); + PostprocessReformattingAspect.getInstance(myFixture.getProject()).doPostponedFormatting(); + }); + myFixture.getEditor().getSelectionModel().removeSelection(); } - PsiMethod findMethod(String className, String methodName) { - return JavaPsiFacade.getInstance(project).findClass(className, GlobalSearchScope.allScope(project)).findMethodsByName(methodName, false)[0] + public PsiMethod findMethod(String className, String methodName) { + return JavaPsiFacade.getInstance(getProject()).findClass(className, GlobalSearchScope.allScope(getProject())) + .findMethodsByName(methodName, false)[0]; } - }