From aff615f3df883780973d0323a35c128d410f3bef Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Tue, 11 Oct 2016 16:02:36 +0300 Subject: [PATCH] refactor QuickFixTestCase: encapsulate action hint in separate object; unify error reporting --- .../DelegateWithDefaultParamValueTest.java | 8 +- .../EnableOptimizeImportsOnTheFlyTest.java | 24 +++- .../LightQuickFixAvailabilityTestCase.java | 23 +++- .../daemon/quickFix/OrderEntryTest.java | 41 +++--- .../ConvertParameterToLocalVariableTest.java | 5 +- .../InlineSameParameterValueTest.java | 21 ++- .../daemon/quickFix/ActionHint.java | 124 ++++++++++++++++++ .../quickFix/LightQuickFixTestCase.java | 87 +++--------- .../daemon/quickFix/QuickFixTestCase.java | 7 +- .../FetchExternalResourcesFixTest.java | 25 +++- .../IgnoreExternalResourcesFixTest.java | 23 +++- ...ManuallySetupExternalResourcesFixTest.java | 23 +++- 12 files changed, 277 insertions(+), 134 deletions(-) create mode 100644 java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/ActionHint.java diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/DelegateWithDefaultParamValueTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/DelegateWithDefaultParamValueTest.java index e8787afacbc0..85d5652e7d5e 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/DelegateWithDefaultParamValueTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/DelegateWithDefaultParamValueTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * 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. @@ -24,12 +24,12 @@ import org.jetbrains.annotations.NotNull; */ public class DelegateWithDefaultParamValueTest extends LightQuickFixParameterizedTestCase { @Override - protected void doAction(@NotNull String text, boolean actionShouldBeAvailable, String testFullPath, String testName) + protected void doAction(@NotNull ActionHint actionHint, String testFullPath, String testName) throws Exception { TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); - super.doAction(text, actionShouldBeAvailable, testFullPath, testName); + super.doAction(actionHint, testFullPath, testName); - if (actionShouldBeAvailable) { + if (actionHint.shouldPresent()) { TemplateState state = TemplateManagerImpl.getTemplateState(getEditor()); if (state != null) { state.gotoEnd(false); diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/EnableOptimizeImportsOnTheFlyTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/EnableOptimizeImportsOnTheFlyTest.java index 9b51a247cd01..83285e780ab0 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/EnableOptimizeImportsOnTheFlyTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/EnableOptimizeImportsOnTheFlyTest.java @@ -1,3 +1,18 @@ +/* + * 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 com.intellij.codeInsight.daemon.quickFix; import com.intellij.codeInsight.CodeInsightSettings; @@ -17,17 +32,14 @@ public class EnableOptimizeImportsOnTheFlyTest extends LightQuickFixParameterize public void test() throws Exception { doAllTests(); } @Override - protected void doAction(@NotNull final String text, final boolean actionShouldBeAvailable, final String testFullPath, final String testName) + protected void doAction(@NotNull final ActionHint actionHint, final String testFullPath, final String testName) throws Exception { boolean old = CodeInsightSettings.getInstance().OPTIMIZE_IMPORTS_ON_THE_FLY; try { CodeInsightSettings.getInstance().OPTIMIZE_IMPORTS_ON_THE_FLY = false; - IntentionAction action = findActionWithText(text); - if (action == null && actionShouldBeAvailable) { - fail("Action with text '" + text + "' is not available in test " + testFullPath); - } - if (action != null && actionShouldBeAvailable) { + IntentionAction action = findActionAndCheck(actionHint, testFullPath); + if (action != null) { action.invoke(getProject(), getEditor(), getFile()); assertTrue(CodeInsightSettings.getInstance().OPTIMIZE_IMPORTS_ON_THE_FLY); } diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/LightQuickFixAvailabilityTestCase.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/LightQuickFixAvailabilityTestCase.java index c45ad28631c4..5a3f9fdf17bd 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/LightQuickFixAvailabilityTestCase.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/LightQuickFixAvailabilityTestCase.java @@ -1,6 +1,20 @@ +/* + * 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 com.intellij.codeInsight.daemon.quickFix; -import com.intellij.codeInsight.intention.IntentionAction; import org.jetbrains.annotations.NotNull; /** @@ -9,11 +23,8 @@ import org.jetbrains.annotations.NotNull; */ public abstract class LightQuickFixAvailabilityTestCase extends LightQuickFixParameterizedTestCase { @Override - protected void doAction(@NotNull final String text, final boolean actionShouldBeAvailable, final String testFullPath, final String testName) + protected void doAction(@NotNull final ActionHint actionHint, final String testFullPath, final String testName) throws Exception { - IntentionAction action = findActionWithText(text); - assertTrue("Action with text '" + text + "' is " + (action == null ? "not " :"") + - "available in test " + testFullPath, - (action != null) == actionShouldBeAvailable); + findActionAndCheck(actionHint, testFullPath); } } diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/OrderEntryTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/OrderEntryTest.java index d2b1dbc31612..edd2fb8c5d08 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/OrderEntryTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/OrderEntryTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * 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. @@ -28,7 +28,6 @@ import com.intellij.openapi.roots.ModifiableRootModel; import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.roots.ModuleRootModificationUtil; import com.intellij.openapi.roots.OrderEntry; -import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.testFramework.PsiTestUtil; @@ -63,8 +62,12 @@ public class OrderEntryTest extends DaemonAnalyzerTestCase { @Override protected void tearDown() throws Exception { - removeLibs(); - super.tearDown(); + try { + removeLibs(); + } + finally { + super.tearDown(); + } } private void doTest(String fileName) throws Exception { @@ -73,40 +76,32 @@ public class OrderEntryTest extends DaemonAnalyzerTestCase { VirtualFile root = ModuleRootManager.getInstance(myModule).getContentRoots()[0].getParent(); VirtualFile virtualFile = root.findFileByRelativePath(fileName); configureByExistingFile(virtualFile); - Pair pair = LightQuickFixTestCase.parseActionHint(getFile(), getFile().getText()); - final String text = pair.getFirst(); - final boolean actionShouldBeAvailable = pair.getSecond().booleanValue(); + ActionHint actionHint = ActionHint.parse(getFile(), getFile().getText()); Collection infosBefore = highlightErrors(); - final IntentionAction action = findActionWithText(text, infosBefore); + final IntentionAction action = findActionAndCheck(actionHint, infosBefore); - if (action == null) { - if (actionShouldBeAvailable) { - fail("Action with text '" + text + "' is not available in test " + testFullPath + "." + - "\nAvailable actions are: " + LightQuickFixTestCase.getAvailableActions(getEditor(), getFile()) - + "\nInfos are: " + infosBefore - ); - } - } - else { - if (!actionShouldBeAvailable) { - fail("Action '" + text + "' is available in test " + testFullPath); - } + if(action != null) { WriteCommandAction.runWriteCommandAction(null, () -> action.invoke(getProject(), getEditor(), getFile())); Collection infosAfter = highlightErrors(); - final IntentionAction afterAction = findActionWithText(text, infosAfter); + final IntentionAction afterAction = findActionWithText(action.getText()); if (afterAction != null) { - fail("Action '" + text + "' is still available after its invocation in test " + testFullPath); + fail("Action '" + action.getText() + "' is still available after its invocation in test " + testFullPath); } assertEquals(infosBefore.size() - 1, infosAfter.size()); } } - private IntentionAction findActionWithText(final String actionText, final Collection infos) { + private IntentionAction findActionWithText(final String actionText) { List actions = LightQuickFixTestCase.getAvailableActions(getEditor(), getFile()); return LightQuickFixTestCase.findActionWithText(actions, actionText); } + private IntentionAction findActionAndCheck(final ActionHint actionHint, Collection infosBefore) { + List actions = LightQuickFixTestCase.getAvailableActions(getEditor(), getFile()); + return actionHint.findAndCheck(actions, () -> "Infos: " + infosBefore); + } + public void testAddDependency() throws Exception { doTest("B/src/y/AddDependency.java"); } diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/ConvertParameterToLocalVariableTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/ConvertParameterToLocalVariableTest.java index 8d5bae2c20d5..0393734b62c9 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/ConvertParameterToLocalVariableTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/ConvertParameterToLocalVariableTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * 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. @@ -21,6 +21,7 @@ package com.intellij.codeInspection; import com.intellij.JavaTestUtil; +import com.intellij.codeInsight.daemon.quickFix.ActionHint; import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; import com.intellij.codeInspection.varScopeCanBeNarrowed.ParameterCanBeLocalInspection; import com.intellij.psi.PsiElement; @@ -39,7 +40,7 @@ public class ConvertParameterToLocalVariableTest extends LightQuickFixParameteri } @Override - protected void doAction(@NotNull final String text, final boolean actionShouldBeAvailable, final String testFullPath, final String testName) + protected void doAction(@NotNull final ActionHint actionHint, final String testFullPath, final String testName) throws Exception { final LocalQuickFix fix = new ParameterCanBeLocalInspection.ConvertParameterToLocalQuickFix(); diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/InlineSameParameterValueTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/InlineSameParameterValueTest.java index eb0bf2f6ae66..d6f874f46243 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/InlineSameParameterValueTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/InlineSameParameterValueTest.java @@ -1,3 +1,19 @@ +/* + * 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. + */ + /* * User: anna * Date: 16-May-2007 @@ -5,6 +21,7 @@ package com.intellij.codeInspection; import com.intellij.JavaTestUtil; +import com.intellij.codeInsight.daemon.quickFix.ActionHint; import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; import com.intellij.codeInspection.sameParameterValue.SameParameterValueInspection; import com.intellij.psi.PsiElement; @@ -23,9 +40,9 @@ public class InlineSameParameterValueTest extends LightQuickFixParameterizedTest } @Override - protected void doAction(@NotNull final String text, final boolean actionShouldBeAvailable, final String testFullPath, final String testName) + protected void doAction(@NotNull final ActionHint actionHint, final String testFullPath, final String testName) throws Exception { - final LocalQuickFix fix = (LocalQuickFix)new SameParameterValueInspection().getQuickFix(text); + final LocalQuickFix fix = (LocalQuickFix)new SameParameterValueInspection().getQuickFix(actionHint.getExpectedText()); assert fix != null; final int offset = getEditor().getCaretModel().getOffset(); final PsiElement psiElement = getFile().findElementAt(offset); diff --git a/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/ActionHint.java b/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/ActionHint.java new file mode 100644 index 000000000000..bece7a6071da --- /dev/null +++ b/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/ActionHint.java @@ -0,0 +1,124 @@ +/* + * 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 com.intellij.codeInsight.daemon.quickFix; + +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.lang.Commenter; +import com.intellij.lang.LanguageCommenters; +import com.intellij.lang.injection.InjectedLanguageManager; +import com.intellij.psi.PsiFile; +import junit.framework.TestCase; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collection; +import java.util.function.Supplier; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +import static org.junit.Assert.fail; + +/** + * An object representing a wanted assertion of given quick-fix test file. + * + * @author Tagir Valeev + */ +public class ActionHint { + final String myExpectedText; + final boolean myShouldPresent; + + private ActionHint(String expectedText, boolean shouldPresent) { + myExpectedText = expectedText; + myShouldPresent = shouldPresent; + } + + /** + * Returns an expected text of the action. + *

+ * Usage of this method is discouraged: it's not guaranteed that ActionHint actually looks for text. + *

+ * + * @return an expected action text. May throw an {@link IllegalStateException} if this ActionHint expects something else + * (e.g. quick-fix of specific type, etc.) + */ + public String getExpectedText() { + return myExpectedText; + } + + /** + * @return true if this ActionHint checks that some action should be present + * or false if it checks that some action should be absent + */ + public boolean shouldPresent() { + return myShouldPresent; + } + + /** + * Finds the action which matches this ActionHint and returns it or returns null + * if this ActionHint asserts that no action should be present. + * + * @param actions actions collection to search inside + * @param infoSupplier a supplier which provides additional info which will be appended to exception message if check fails + * @return the action or null + * @throws AssertionError if no action is found, but it should present, or if action is found, but it should not present. + */ + @Nullable + public IntentionAction findAndCheck(Collection actions, Supplier infoSupplier) { + IntentionAction result = actions.stream().filter(t -> t.getText().equals(myExpectedText)).findFirst().orElse(null); + if(result == null && myShouldPresent) { + fail("Action with text '" + myExpectedText + "' not found\nAvailable actions: " + + actions.stream().map(IntentionAction::getText).collect(Collectors.joining(", ", "[", "]\n")) + + infoSupplier.get()); + } else if(result != null && !myShouldPresent) { + fail("Action with text '" + myExpectedText + "' is present, but should not\n" + infoSupplier.get()); + } + return result; + } + + /** + * Parse given file with given contents extracting ActionHint of it. + *

+ * Currently the following syntax is supported: + * // "quick-fix name or intention text" "true|false" + * (replace // with line comment prefix in the corresponding language if necessary) + *

+ * + * @param file PsiFile associated with contents (used to determine the language) + * @param contents file contents + * @return ActionHint object + * @throws AssertionError if action hint is absent or has invalid format + */ + @NotNull + public static ActionHint parse(@NotNull PsiFile file, @NotNull String contents) { + PsiFile hostFile = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file); + + final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(hostFile.getLanguage()); + String comment = commenter.getLineCommentPrefix(); + if (comment == null) { + comment = commenter.getBlockCommentPrefix(); + } + + assert comment != null : commenter; + // "quick fix action text to perform" "should be available" + Pattern pattern = Pattern.compile("^" + Pattern.quote(comment) + " \"(.*)\" \"(true|false)\".*", Pattern.DOTALL); + Matcher matcher = pattern.matcher(contents); + TestCase.assertTrue("No comment found in " + file.getVirtualFile(), matcher.matches()); + final String text = matcher.group(1); + final Boolean actionShouldBeAvailable = Boolean.valueOf(matcher.group(2)); + return new ActionHint(text, actionShouldBeAvailable); + } +} diff --git a/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/LightQuickFixTestCase.java b/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/LightQuickFixTestCase.java index 60456ec77ef9..5920dc3016be 100644 --- a/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/LightQuickFixTestCase.java +++ b/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/LightQuickFixTestCase.java @@ -19,14 +19,10 @@ import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase; import com.intellij.codeInsight.daemon.impl.HighlightInfo; import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler; -import com.intellij.lang.Commenter; -import com.intellij.lang.LanguageCommenters; -import com.intellij.lang.injection.InjectedLanguageManager; import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vcs.readOnlyHandler.ReadonlyStatusHandlerImpl; @@ -41,18 +37,13 @@ import com.intellij.util.IncorrectOperationException; import com.intellij.util.ObjectUtils; import com.intellij.util.io.ReadOnlyAttributeUtil; import com.intellij.util.ui.UIUtil; -import org.intellij.lang.annotations.RegExp; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.IOException; import java.io.UncheckedIOException; -import java.util.ArrayList; -import java.util.Collection; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; public abstract class LightQuickFixTestCase extends LightDaemonAnalyzerTestCase { @NonNls protected static final String BEFORE_PREFIX = "before"; @@ -65,8 +56,8 @@ public abstract class LightQuickFixTestCase extends LightDaemonAnalyzerTestCase } @NotNull - protected Pair parseActionHintImpl(@NotNull PsiFile file, @NotNull String contents) { - return parseActionHint(file, contents); + protected ActionHint parseActionHintImpl(@NotNull PsiFile file, @NotNull String contents) { + return ActionHint.parse(file, contents); } private static void doTestFor(final String testName, final QuickFixTestCase quickFixTestCase) { @@ -78,15 +69,13 @@ public abstract class LightQuickFixTestCase extends LightDaemonAnalyzerTestCase String contents = StringUtil.convertLineSeparators(FileUtil.loadFile(testFile, CharsetToolkit.UTF8_CHARSET)); quickFixTestCase.configureFromFileText(testFile.getName(), contents); quickFixTestCase.bringRealEditorBack(); - final Pair pair = quickFixTestCase.parseActionHintImpl(quickFixTestCase.getFile(), contents); - final String text = pair.getFirst(); - final boolean actionShouldBeAvailable = pair.getSecond().booleanValue(); + final ActionHint actionHint = quickFixTestCase.parseActionHintImpl(quickFixTestCase.getFile(), contents); quickFixTestCase.beforeActionStarted(testName, contents); try { myWrapper = quickFixTestCase; - quickFixTestCase.doAction(text, actionShouldBeAvailable, testFullPath, testName); + quickFixTestCase.doAction(actionHint, testFullPath, testName); } finally { myWrapper = null; @@ -109,62 +98,20 @@ public abstract class LightQuickFixTestCase extends LightDaemonAnalyzerTestCase protected void beforeActionStarted(final String testName, final String contents) { } - @NotNull - public static Pair parseActionHint(@NotNull PsiFile file, @NotNull String contents) { - return parseActionHint(file, contents, " \"(.*)\" \"(true|false)\".*"); - } - - @NotNull - public static Pair parseActionHint(@NotNull PsiFile file, - @NotNull String contents, - @NotNull @NonNls @RegExp String actionPattern) { - PsiFile hostFile = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file); - - final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(hostFile.getLanguage()); - String comment = commenter.getLineCommentPrefix(); - if (comment == null) { - comment = commenter.getBlockCommentPrefix(); - } - - // "quick fix action text to perform" "should be available" - assert comment != null : commenter; - Pattern pattern = Pattern.compile("^" + comment.replace("*", "\\*") + actionPattern, Pattern.DOTALL); - Matcher matcher = pattern.matcher(contents); - assertTrue("No comment found in "+file.getVirtualFile(), matcher.matches()); - final String text = matcher.group(1); - final Boolean actionShouldBeAvailable = Boolean.valueOf(matcher.group(2)); - return Pair.create(text, actionShouldBeAvailable); - } - - public static void doAction(@NotNull String text, - boolean actionShouldBeAvailable, + public static void doAction(@NotNull ActionHint actionHint, String testFullPath, String testName, QuickFixTestCase quickFix) throws Exception { - IntentionAction action = quickFix.findActionWithText(text); - if (action == null) { - if (actionShouldBeAvailable) { - List actions = quickFix.getAvailableActions(); - List texts = new ArrayList<>(); - for (IntentionAction intentionAction : actions) { - texts.add(intentionAction.getText()); - } - Collection infos = quickFix.doHighlighting(); - fail("Action with text '" + text + "' is not available in test " + testFullPath + "\n" + - "Available actions (" + texts.size() + "): " + texts + "\n" + actions + "\nInfos:" + infos); - } - } - else { - if (!actionShouldBeAvailable) { - fail("Action '" + text + "' is available (but must not) in test " + testFullPath); - } + IntentionAction action = actionHint.findAndCheck(quickFix.getAvailableActions(), + () -> "Test: "+testFullPath+"\nInfos: "+quickFix.doHighlighting()); + if (action != null) { quickFix.invoke(action); UIUtil.dispatchAllInvocationEvents(); UIUtil.dispatchAllInvocationEvents(); if (!quickFix.shouldBeAvailableAfterExecution()) { - final IntentionAction afterAction = quickFix.findActionWithText(text); + final IntentionAction afterAction = quickFix.findActionWithText(action.getText()); if (afterAction != null) { - fail("Action '" + text + "' is still available after its invocation in test " + testFullPath); + fail("Action '" + action.getText() + "' is still available after its invocation in test " + testFullPath); } } String expectedFilePath = ObjectUtils.notNull(quickFix.getBasePath(), "") + "/" + AFTER_PREFIX + testName; @@ -172,9 +119,9 @@ public abstract class LightQuickFixTestCase extends LightDaemonAnalyzerTestCase } } - protected void doAction(@NotNull String text, final boolean actionShouldBeAvailable, final String testFullPath, final String testName) + protected void doAction(@NotNull ActionHint actionHint, final String testFullPath, final String testName) throws Exception { - doAction(text, actionShouldBeAvailable, testFullPath, testName, myWrapper); + doAction(actionHint, testFullPath, testName, myWrapper); } protected void doAction(@NotNull String actionName) { @@ -208,6 +155,10 @@ public abstract class LightQuickFixTestCase extends LightDaemonAnalyzerTestCase } } + protected IntentionAction findActionAndCheck(@NotNull ActionHint hint, String testFullPath) { + return hint.findAndCheck(getAvailableActions(), () -> "Test: "+testFullPath); + } + protected IntentionAction findActionWithText(@NotNull String text) { return findActionWithText(getAvailableActions(), text); } @@ -283,7 +234,7 @@ public abstract class LightQuickFixTestCase extends LightDaemonAnalyzerTestCase @NotNull @Override - public Pair parseActionHintImpl(@NotNull PsiFile file, @NotNull String contents) { + public ActionHint parseActionHintImpl(@NotNull PsiFile file, @NotNull String contents) { return LightQuickFixTestCase.this.parseActionHintImpl(file, contents); } @@ -298,8 +249,8 @@ public abstract class LightQuickFixTestCase extends LightDaemonAnalyzerTestCase } @Override - public void doAction(String text, boolean actionShouldBeAvailable, String testFullPath, String testName) throws Exception { - LightQuickFixTestCase.this.doAction(text, actionShouldBeAvailable, testFullPath, testName); + public void doAction(ActionHint actionHint, String testFullPath, String testName) throws Exception { + LightQuickFixTestCase.this.doAction(actionHint, testFullPath, testName); } @Override diff --git a/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/QuickFixTestCase.java b/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/QuickFixTestCase.java index b8f6816ea8e1..8c00ae4149dc 100644 --- a/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/QuickFixTestCase.java +++ b/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/QuickFixTestCase.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * 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. @@ -18,7 +18,6 @@ package com.intellij.codeInsight.daemon.quickFix; import com.intellij.codeInsight.daemon.impl.HighlightInfo; import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Pair; import com.intellij.psi.PsiFile; import org.jetbrains.annotations.NotNull; @@ -35,13 +34,13 @@ public interface QuickFixTestCase { String getTestDataPath(); @NotNull - Pair parseActionHintImpl(@NotNull PsiFile file, @NotNull String contents); + ActionHint parseActionHintImpl(@NotNull PsiFile file, @NotNull String contents); void beforeActionStarted(String testName, String contents); void afterActionCompleted(String testName, String contents); - void doAction(String text, boolean actionShouldBeAvailable, String testFullPath, String testName) throws Exception; + void doAction(ActionHint actionHint, String testFullPath, String testName) throws Exception; void checkResultByFile(String s, @NotNull String expectedFilePath, boolean b) throws Exception; diff --git a/xml/tests/src/com/intellij/codeInsight/daemon/quickFix/FetchExternalResourcesFixTest.java b/xml/tests/src/com/intellij/codeInsight/daemon/quickFix/FetchExternalResourcesFixTest.java index 33f476336697..a67b0f9b9934 100644 --- a/xml/tests/src/com/intellij/codeInsight/daemon/quickFix/FetchExternalResourcesFixTest.java +++ b/xml/tests/src/com/intellij/codeInsight/daemon/quickFix/FetchExternalResourcesFixTest.java @@ -1,3 +1,18 @@ +/* + * 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 com.intellij.codeInsight.daemon.quickFix; import com.intellij.codeInsight.daemon.impl.quickfix.FetchExtResourceAction; @@ -24,13 +39,9 @@ public class FetchExternalResourcesFixTest extends LightQuickFixParameterizedTes // just check for action availability @Override - protected void doAction(@NotNull String text, boolean actionShouldBeAvailable, String testFullPath, String testName) throws Exception { - IntentionAction action = findActionWithText(text); - if (action == null && actionShouldBeAvailable) { - fail("Action with text '" + text + "' is not available in test " + testFullPath); - } - - if (actionShouldBeAvailable && testName.equals("5.xml")) { + protected void doAction(@NotNull ActionHint actionHint, String testFullPath, String testName) throws Exception { + IntentionAction action = findActionAndCheck(actionHint, testFullPath); + if (action != null && testName.equals("5.xml")) { final String uri = FetchExtResourceAction.findUri(myFile, myEditor.getCaretModel().getOffset()); final String url = FetchExtResourceAction.findUrl(myFile, myEditor.getCaretModel().getOffset(),uri); assertEquals("http://www.springframework.org/schema/aop/spring-aop.xsd",url); diff --git a/xml/tests/src/com/intellij/codeInsight/daemon/quickFix/IgnoreExternalResourcesFixTest.java b/xml/tests/src/com/intellij/codeInsight/daemon/quickFix/IgnoreExternalResourcesFixTest.java index 98d5491fb0af..bf35b1cb505f 100644 --- a/xml/tests/src/com/intellij/codeInsight/daemon/quickFix/IgnoreExternalResourcesFixTest.java +++ b/xml/tests/src/com/intellij/codeInsight/daemon/quickFix/IgnoreExternalResourcesFixTest.java @@ -1,6 +1,20 @@ +/* + * 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 com.intellij.codeInsight.daemon.quickFix; -import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.testFramework.PlatformTestUtil; import org.jetbrains.annotations.NotNull; @@ -16,11 +30,8 @@ public class IgnoreExternalResourcesFixTest extends LightQuickFixParameterizedTe // just check for action availability @Override - protected void doAction(@NotNull String text, boolean actionShouldBeAvailable, String testFullPath, String testName) throws Exception { - IntentionAction action = findActionWithText(text); - if (action == null && actionShouldBeAvailable) { - fail("Action with text '" + text + "' is not available in test " + testFullPath); - } + protected void doAction(@NotNull ActionHint actionHint, String testFullPath, String testName) throws Exception { + findActionAndCheck(actionHint, testFullPath); } @NotNull diff --git a/xml/tests/src/com/intellij/codeInsight/daemon/quickFix/ManuallySetupExternalResourcesFixTest.java b/xml/tests/src/com/intellij/codeInsight/daemon/quickFix/ManuallySetupExternalResourcesFixTest.java index 2ab8dabf1be2..17280af94540 100644 --- a/xml/tests/src/com/intellij/codeInsight/daemon/quickFix/ManuallySetupExternalResourcesFixTest.java +++ b/xml/tests/src/com/intellij/codeInsight/daemon/quickFix/ManuallySetupExternalResourcesFixTest.java @@ -1,6 +1,20 @@ +/* + * 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 com.intellij.codeInsight.daemon.quickFix; -import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.testFramework.PlatformTestUtil; import org.jetbrains.annotations.NotNull; @@ -16,11 +30,8 @@ public class ManuallySetupExternalResourcesFixTest extends LightQuickFixParamete // just check for action availability @Override - protected void doAction(@NotNull String text, boolean actionShouldBeAvailable, String testFullPath, String testName) throws Exception { - IntentionAction action = findActionWithText(text); - if (action == null && actionShouldBeAvailable) { - fail("Action with text '" + text + "' is not available in test " + testFullPath); - } + protected void doAction(@NotNull ActionHint actionHint, String testFullPath, String testName) throws Exception { + findActionAndCheck(actionHint, testFullPath); } @NotNull