diff --git a/java/java-impl/src/com/intellij/testIntegration/JavaTestCreator.java b/java/java-impl/src/com/intellij/testIntegration/JavaTestCreator.java index 3edb3d5bdc5c..d85ac9212f16 100644 --- a/java/java-impl/src/com/intellij/testIntegration/JavaTestCreator.java +++ b/java/java-impl/src/com/intellij/testIntegration/JavaTestCreator.java @@ -15,14 +15,26 @@ */ package com.intellij.testIntegration; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiFile; import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; import com.intellij.testIntegration.createTest.CreateTestAction; import com.intellij.util.IncorrectOperationException; public class JavaTestCreator implements TestCreator { + private static final Logger LOG = Logger.getInstance("com.intellij.testIntegration.JavaTestCreator"); + + + @Override + public boolean isAvailable(Project project, Editor editor, PsiFile file) { + CreateTestAction action = new CreateTestAction(); + PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); + + return action.isAvailableForElement(element); + } + public void createTest(Project project, Editor editor, PsiFile file) { try { CreateTestAction action = new CreateTestAction(); @@ -32,7 +44,7 @@ public class JavaTestCreator implements TestCreator { } } catch (IncorrectOperationException e) { - throw new RuntimeException(e); + LOG.warn(e); } } } diff --git a/platform/lang-api/src/com/intellij/testIntegration/TestCreator.java b/platform/lang-api/src/com/intellij/testIntegration/TestCreator.java index bb426123e500..8cb85c1ed3aa 100644 --- a/platform/lang-api/src/com/intellij/testIntegration/TestCreator.java +++ b/platform/lang-api/src/com/intellij/testIntegration/TestCreator.java @@ -21,5 +21,6 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.PsiFile; public interface TestCreator { + boolean isAvailable(Project project, Editor editor, PsiFile file); void createTest(Project project, Editor editor, PsiFile file); } diff --git a/platform/lang-impl/src/com/intellij/testIntegration/GotoTestOrCodeHandler.java b/platform/lang-impl/src/com/intellij/testIntegration/GotoTestOrCodeHandler.java index 285a1b33ebe2..48900b1271c5 100644 --- a/platform/lang-impl/src/com/intellij/testIntegration/GotoTestOrCodeHandler.java +++ b/platform/lang-impl/src/com/intellij/testIntegration/GotoTestOrCodeHandler.java @@ -53,23 +53,25 @@ public class GotoTestOrCodeHandler extends GotoTargetHandler { } else { candidates = TestFinderHelper.findTestsForClass(selectedElement); - actions.add(new AdditionalAction() { - @Override - public String getText() { - return "Create New Test..."; - } + final TestCreator creator = LanguageTestCreators.INSTANCE.forLanguage(file.getLanguage()); + if (creator != null && creator.isAvailable(file.getProject(), editor, file)) { + actions.add(new AdditionalAction() { + @Override + public String getText() { + return "Create New Test..."; + } - @Override - public Icon getIcon() { - return IconLoader.getIcon("/actions/intentionBulb.png"); - } + @Override + public Icon getIcon() { + return IconLoader.getIcon("/actions/intentionBulb.png"); + } - @Override - public void execute() { - final TestCreator creator = LanguageTestCreators.INSTANCE.forLanguage(file.getLanguage()); - if (creator != null) creator.createTest(file.getProject(), editor, file); - } - }); + @Override + public void execute() { + creator.createTest(file.getProject(), editor, file); + } + }); + } } return new GotoData(sourceElement, candidates.toArray(new PsiElement[candidates.size()]), actions);