/* * Copyright 2000-2017 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 com.intellij.codeInsight import com.intellij.JavaTestUtil import com.intellij.ide.actions.CopyReferenceAction import com.intellij.openapi.actionSystem.ActionManager import com.intellij.openapi.actionSystem.IdeActions import com.intellij.openapi.ide.CopyPasteManager import com.intellij.psi.PsiFile import com.intellij.testFramework.LightProjectDescriptor import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase class CopyReferenceActionTest extends LightCodeInsightFixtureTestCase { private int oldSetting @Override protected LightProjectDescriptor getProjectDescriptor() { return JAVA_9 } @Override protected String getBasePath() { return JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInsight/copyReference" } @Override protected void setUp() { super.setUp() CodeInsightSettings settings = CodeInsightSettings.getInstance() oldSetting = settings.ADD_IMPORTS_ON_PASTE settings.ADD_IMPORTS_ON_PASTE = CodeInsightSettings.YES } @Override protected void tearDown() { CodeInsightSettings settings = CodeInsightSettings.getInstance() settings.ADD_IMPORTS_ON_PASTE = oldSetting super.tearDown() } void testConstructor() { doTest() } void testDefaultConstructor() { doTest() } void testIdentifierSeparator() { doTest() } void testMethodFromAnonymousClass() { doTest() } void testSameClassNames() { myFixture.addClass("package p; public class Foo {}") myFixture.configureByText("Foo.java", "package p1; public class Foo {}") performCopy() myFixture.configureByText("a.java", "import p.Foo; class Bar { }") performPaste() myFixture.checkResult """import p.Foo; class Bar { p1.Foo}""" } void testAddImport() { myFixture.addClass("package foo; public class Foo {}") myFixture.configureByText "a.java", "import foo.Foo;" performCopy() myFixture.configureByText "b.java", "class Goo { }" performPaste() myFixture.checkResult """import foo.Foo; class Goo { Foo }""" } void "test paste correct signature to javadoc"() { myFixture.configureByText "a.java", """ class Foo { void foo(int a) {} void foo(byte a) {} } """ performCopy() myFixture.configureByText "b.java", "/** */" performPaste() myFixture.checkResult "/** Foo#foo(byte) */" } void "test paste correct generic signature to javadoc"() { myFixture.configureByText "a.java", """ class Foo { void foo(java.util.List a) {} } """ performCopy() myFixture.configureByText "b.java", "/** */" performPaste() myFixture.checkResult "/** Foo#foo(java.util.List) */" } void "test paste overloaded signature to a comment"() { myFixture.configureByText "a.java", """ class Foo { void foo(int a) {} // void foo(int a, int b) {} } """ performCopy() myFixture.editor.caretModel.moveToOffset(myFixture.editor.document.text.indexOf('//') + 2) performPaste() myFixture.checkResult """ class Foo { void foo(int a) {} //Foo.foo(int) void foo(int a, int b) {} } """ } void testFqnInImport() { myFixture.addClass("package foo; public class Foo {}") myFixture.configureByText "a.java", "import foo.Foo;" performCopy() myFixture.configureByText "b.java", "import " performPaste() myFixture.checkResult """import foo.Foo""" } void testCopyFile() { PsiFile psiFile = myFixture.addFileToProject("x/x.txt", "") assertTrue(CopyReferenceAction.doCopy(psiFile, getProject())) String name = getTestName(false) myFixture.configureByFile(name + "_dst.java") performPaste() myFixture.checkResultByFile(name + "_after.java") } void testCopyLineNumber() { myFixture.configureByText 'a.java', ''' class Foo { }''' performCopy() myFixture.configureByText 'a.txt', '' performPaste() myFixture.checkResult "a.java:2" } void testMethodOverloadCopy() { myFixture.configureByText 'a.java', ''' class Koo { public void foo(int a) { } public void foo(boolean a) { } { foo(true); } }''' performCopy() myFixture.configureByText 'b.java', ''' /** * */ class Koo2 { } ''' performPaste() myFixture.checkResult ''' /** * Koo#foo(boolean) */ class Koo2 { } ''' } void testModuleNameCopy() { myFixture.configureByText 'module-info.java', 'module M16 { }' performCopy() assert CopyPasteManager.getInstance().getContents(CopyReferenceAction.ourFlavor) == 'M16' } void testModuleReferenceCopy() { myFixture.configureByText 'module-info.java', 'module M16 { requires M16; }' performCopy() assert CopyPasteManager.getInstance().getContents(CopyReferenceAction.ourFlavor) == 'M16' } private void doTest() { String name = getTestName(false) myFixture.configureByFile(name + ".java") performCopy() myFixture.configureByFile(name + "_dst.java") performPaste() myFixture.checkResultByFile(name + "_after.java") } private void performCopy() { myFixture.testAction(ActionManager.getInstance().getAction(IdeActions.ACTION_COPY_REFERENCE)) } private void performPaste() { myFixture.performEditorAction(IdeActions.ACTION_EDITOR_PASTE) } }