Search in sources :

Example 6 with TestFramework

use of com.intellij.testIntegration.TestFramework in project intellij-community by JetBrains.

the class CreateTestAction method invoke.

@Override
public void invoke(@NotNull final Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    final Module srcModule = ModuleUtilCore.findModuleForPsiElement(element);
    if (srcModule == null)
        return;
    final PsiClass srcClass = getContainingClass(element);
    if (srcClass == null)
        return;
    PsiDirectory srcDir = element.getContainingFile().getContainingDirectory();
    PsiPackage srcPackage = JavaDirectoryService.getInstance().getPackage(srcDir);
    final PropertiesComponent propertiesComponent = PropertiesComponent.getInstance(project);
    Module testModule = suggestModuleForTests(project, srcModule);
    final List<VirtualFile> testRootUrls = computeTestRoots(testModule);
    if (testRootUrls.isEmpty() && computeSuitableTestRootUrls(testModule).isEmpty()) {
        testModule = srcModule;
        if (!propertiesComponent.getBoolean(CREATE_TEST_IN_THE_SAME_ROOT)) {
            if (Messages.showOkCancelDialog(project, "Create test in the same source root?", "No Test Roots Found", Messages.getQuestionIcon()) != Messages.OK) {
                return;
            }
            propertiesComponent.setValue(CREATE_TEST_IN_THE_SAME_ROOT, true);
        }
    }
    final CreateTestDialog d = createTestDialog(project, testModule, srcClass, srcPackage);
    if (!d.showAndGet()) {
        return;
    }
    CommandProcessor.getInstance().executeCommand(project, () -> {
        TestFramework framework = d.getSelectedTestFrameworkDescriptor();
        final TestGenerator generator = TestGenerators.INSTANCE.forLanguage(framework.getLanguage());
        DumbService.getInstance(project).withAlternativeResolveEnabled(() -> generator.generateTest(project, d));
    }, CodeInsightBundle.message("intention.create.test"), this);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TestFramework(com.intellij.testIntegration.TestFramework) Module(com.intellij.openapi.module.Module) PropertiesComponent(com.intellij.ide.util.PropertiesComponent)

Example 7 with TestFramework

use of com.intellij.testIntegration.TestFramework in project intellij-community by JetBrains.

the class TestIconProvider method getIcon.

@Override
public Icon getIcon(@NotNull PsiElement element, int flags) {
    final TestFramework[] testFrameworks = Extensions.getExtensions(TestFramework.EXTENSION_NAME);
    for (TestFramework framework : testFrameworks) {
        try {
            if (framework.isIgnoredMethod(element)) {
                final Icon ignoredTestIcon = AllIcons.RunConfigurations.IgnoredTest;
                final LayeredIcon icon = new LayeredIcon(ignoredTestIcon, PlatformIcons.PUBLIC_ICON);
                icon.setIcon(PlatformIcons.PUBLIC_ICON, 1, ignoredTestIcon.getIconWidth(), 0);
                return icon;
            }
        } catch (AbstractMethodError ignored) {
        }
    }
    for (TestFramework framework : testFrameworks) {
        try {
            if (framework.isTestMethod(element)) {
                final LayeredIcon mark = new LayeredIcon(PlatformIcons.METHOD_ICON, AllIcons.RunConfigurations.TestMark, PlatformIcons.PUBLIC_ICON);
                mark.setIcon(PlatformIcons.PUBLIC_ICON, 2, PlatformIcons.METHOD_ICON.getIconWidth(), 0);
                return mark;
            }
        } catch (AbstractMethodError ignore) {
        }
    }
    return null;
}
Also used : LayeredIcon(com.intellij.ui.LayeredIcon) TestFramework(com.intellij.testIntegration.TestFramework) LayeredIcon(com.intellij.ui.LayeredIcon)

Example 8 with TestFramework

use of com.intellij.testIntegration.TestFramework in project intellij-community by JetBrains.

the class TestDiscoveryConfigurationProducer method getSourceMethod.

private static PsiMethod getSourceMethod(Location location) {
    final PsiElement psiElement = location.getPsiElement();
    final PsiMethod psiMethod = PsiTreeUtil.getParentOfType(psiElement, PsiMethod.class);
    if (psiMethod != null) {
        final PsiClass containingClass = psiMethod.getContainingClass();
        if (containingClass != null) {
            final TestFramework testFramework = TestFrameworks.detectFramework(containingClass);
            if (testFramework != null) {
                return null;
            }
            return psiMethod;
        }
    }
    return null;
}
Also used : TestFramework(com.intellij.testIntegration.TestFramework) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) PsiElement(com.intellij.psi.PsiElement)

Example 9 with TestFramework

use of com.intellij.testIntegration.TestFramework in project intellij-community by JetBrains.

the class CreateTestDialog method createCenterPanel.

protected JComponent createCenterPanel() {
    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints constr = new GridBagConstraints();
    constr.fill = GridBagConstraints.HORIZONTAL;
    constr.anchor = GridBagConstraints.WEST;
    int gridy = 1;
    constr.insets = insets(4);
    constr.gridy = gridy++;
    constr.gridx = 0;
    constr.weightx = 0;
    final JLabel libLabel = new JLabel(CodeInsightBundle.message("intention.create.test.dialog.testing.library"));
    libLabel.setLabelFor(myLibrariesCombo);
    panel.add(libLabel, constr);
    constr.gridx = 1;
    constr.weightx = 1;
    constr.gridwidth = GridBagConstraints.REMAINDER;
    panel.add(myLibrariesCombo, constr);
    myFixLibraryPanel = new JPanel(new BorderLayout());
    myFixLibraryLabel = new JLabel();
    myFixLibraryLabel.setIcon(AllIcons.Actions.IntentionBulb);
    myFixLibraryPanel.add(myFixLibraryLabel, BorderLayout.CENTER);
    myFixLibraryPanel.add(myFixLibraryButton, BorderLayout.EAST);
    constr.insets = insets(1);
    constr.gridy = gridy++;
    constr.gridx = 0;
    panel.add(myFixLibraryPanel, constr);
    constr.gridheight = 1;
    constr.insets = insets(6);
    constr.gridy = gridy++;
    constr.gridx = 0;
    constr.weightx = 0;
    constr.gridwidth = 1;
    panel.add(new JLabel(CodeInsightBundle.message("intention.create.test.dialog.class.name")), constr);
    myTargetClassNameField = new EditorTextField(suggestTestClassName(myTargetClass));
    myTargetClassNameField.getDocument().addDocumentListener(new DocumentAdapter() {

        @Override
        public void documentChanged(DocumentEvent e) {
            getOKAction().setEnabled(PsiNameHelper.getInstance(myProject).isIdentifier(getClassName()));
        }
    });
    constr.gridx = 1;
    constr.weightx = 1;
    panel.add(myTargetClassNameField, constr);
    constr.insets = insets(1);
    constr.gridy = gridy++;
    constr.gridx = 0;
    constr.weightx = 0;
    panel.add(new JLabel(CodeInsightBundle.message("intention.create.test.dialog.super.class")), constr);
    mySuperClassField = new ReferenceEditorComboWithBrowseButton(new MyChooseSuperClassAction(), null, myProject, true, JavaCodeFragment.VisibilityChecker.EVERYTHING_VISIBLE, RECENT_SUPERS_KEY);
    mySuperClassField.setMinimumSize(mySuperClassField.getPreferredSize());
    constr.gridx = 1;
    constr.weightx = 1;
    panel.add(mySuperClassField, constr);
    constr.insets = insets(1);
    constr.gridy = gridy++;
    constr.gridx = 0;
    constr.weightx = 0;
    panel.add(new JLabel(CodeInsightBundle.message("dialog.create.class.destination.package.label")), constr);
    constr.gridx = 1;
    constr.weightx = 1;
    String targetPackageName = myTargetPackage != null ? myTargetPackage.getQualifiedName() : "";
    myTargetPackageField = new PackageNameReferenceEditorCombo(targetPackageName, myProject, RECENTS_KEY, CodeInsightBundle.message("dialog.create.class.package.chooser.title"));
    new AnAction() {

        public void actionPerformed(AnActionEvent e) {
            myTargetPackageField.getButton().doClick();
        }
    }.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_DOWN_MASK)), myTargetPackageField.getChildComponent());
    JPanel targetPackagePanel = new JPanel(new BorderLayout());
    targetPackagePanel.add(myTargetPackageField, BorderLayout.CENTER);
    panel.add(targetPackagePanel, constr);
    constr.insets = insets(6);
    constr.gridy = gridy++;
    constr.gridx = 0;
    constr.weightx = 0;
    panel.add(new JLabel(CodeInsightBundle.message("intention.create.test.dialog.generate")), constr);
    constr.gridx = 1;
    constr.weightx = 1;
    panel.add(myGenerateBeforeBox, constr);
    constr.insets = insets(1);
    constr.gridy = gridy++;
    panel.add(myGenerateAfterBox, constr);
    constr.insets = insets(6);
    constr.gridy = gridy++;
    constr.gridx = 0;
    constr.weightx = 0;
    final JLabel membersLabel = new JLabel(CodeInsightBundle.message("intention.create.test.dialog.select.methods"));
    membersLabel.setLabelFor(myMethodsTable);
    panel.add(membersLabel, constr);
    constr.gridx = 1;
    constr.weightx = 1;
    panel.add(myShowInheritedMethodsBox, constr);
    constr.insets = insets(1, 8);
    constr.gridy = gridy++;
    constr.gridx = 0;
    constr.gridwidth = GridBagConstraints.REMAINDER;
    constr.fill = GridBagConstraints.BOTH;
    constr.weighty = 1;
    panel.add(ScrollPaneFactory.createScrollPane(myMethodsTable), constr);
    myLibrariesCombo.setRenderer(new ListCellRendererWrapper<TestFramework>() {

        @Override
        public void customize(JList list, TestFramework value, int index, boolean selected, boolean hasFocus) {
            if (value != null) {
                setText(value.getName());
                setIcon(value.getIcon());
            }
        }
    });
    final boolean hasTestRoots = !ModuleRootManager.getInstance(myTargetModule).getSourceRoots(JavaModuleSourceRootTypes.TESTS).isEmpty();
    final List<TestFramework> attachedLibraries = new ArrayList<>();
    final String defaultLibrary = getDefaultLibraryName();
    TestFramework defaultDescriptor = null;
    final DefaultComboBoxModel model = (DefaultComboBoxModel) myLibrariesCombo.getModel();
    final List<TestFramework> descriptors = new ArrayList<>();
    descriptors.addAll(Arrays.asList(Extensions.getExtensions(TestFramework.EXTENSION_NAME)));
    descriptors.sort((d1, d2) -> Comparing.compare(d1.getName(), d2.getName()));
    for (final TestFramework descriptor : descriptors) {
        model.addElement(descriptor);
        if (hasTestRoots && descriptor.isLibraryAttached(myTargetModule)) {
            attachedLibraries.add(descriptor);
            if (defaultLibrary == null) {
                defaultDescriptor = descriptor;
            }
        }
        if (Comparing.equal(defaultLibrary, descriptor.getName())) {
            defaultDescriptor = descriptor;
        }
    }
    myLibrariesCombo.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            final Object selectedItem = myLibrariesCombo.getSelectedItem();
            if (selectedItem != null) {
                final DumbService dumbService = DumbService.getInstance(myProject);
                dumbService.setAlternativeResolveEnabled(true);
                try {
                    onLibrarySelected((TestFramework) selectedItem);
                } finally {
                    dumbService.setAlternativeResolveEnabled(false);
                }
            }
        }
    });
    if (defaultDescriptor != null && (attachedLibraries.contains(defaultDescriptor) || attachedLibraries.isEmpty())) {
        myLibrariesCombo.setSelectedItem(defaultDescriptor);
    } else {
        myLibrariesCombo.setSelectedIndex(0);
    }
    myFixLibraryButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (mySelectedFramework instanceof JavaTestFramework) {
                ((JavaTestFramework) mySelectedFramework).setupLibrary(myTargetModule);
            } else {
                OrderEntryFix.addJarToRoots(mySelectedFramework.getLibraryPath(), myTargetModule, null);
            }
            myFixLibraryPanel.setVisible(false);
        }
    });
    myShowInheritedMethodsBox.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            updateMethodsTable();
        }
    });
    restoreShowInheritedMembersStatus();
    updateMethodsTable();
    return panel;
}
Also used : AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) ActionEvent(java.awt.event.ActionEvent) JavaTestFramework(com.intellij.testIntegration.JavaTestFramework) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) AnAction(com.intellij.openapi.actionSystem.AnAction) CustomShortcutSet(com.intellij.openapi.actionSystem.CustomShortcutSet) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) DumbService(com.intellij.openapi.project.DumbService) PackageNameReferenceEditorCombo(com.intellij.refactoring.ui.PackageNameReferenceEditorCombo) TestFramework(com.intellij.testIntegration.TestFramework) JavaTestFramework(com.intellij.testIntegration.JavaTestFramework) ActionListener(java.awt.event.ActionListener)

Example 10 with TestFramework

use of com.intellij.testIntegration.TestFramework in project intellij-community by JetBrains.

the class MoveInitializerToSetUpMethodAction method isAvailable.

@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
    final boolean isAvailable = super.isAvailable(project, editor, element) && TestIntegrationUtils.isTest(element);
    if (isAvailable) {
        final PsiField field = PsiTreeUtil.getParentOfType(element, PsiField.class);
        LOG.assertTrue(field != null);
        final PsiClass aClass = field.getContainingClass();
        LOG.assertTrue(aClass != null);
        final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
        for (TestFramework framework : Extensions.getExtensions(TestFramework.EXTENSION_NAME)) {
            if (framework instanceof JavaTestFramework && framework.isTestClass(aClass)) {
                try {
                    ((JavaTestFramework) framework).createSetUpPatternMethod(elementFactory);
                    return true;
                } catch (Exception e) {
                    return false;
                }
            }
        }
        return true;
    }
    return false;
}
Also used : JavaTestFramework(com.intellij.testIntegration.JavaTestFramework) TestFramework(com.intellij.testIntegration.TestFramework) JavaTestFramework(com.intellij.testIntegration.JavaTestFramework)

Aggregations

TestFramework (com.intellij.testIntegration.TestFramework)15 Editor (com.intellij.openapi.editor.Editor)3 PsiClass (com.intellij.psi.PsiClass)3 Nullable (org.jetbrains.annotations.Nullable)3 FileTemplateDescriptor (com.intellij.ide.fileTemplates.FileTemplateDescriptor)2 Project (com.intellij.openapi.project.Project)2 Computable (com.intellij.openapi.util.Computable)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 JavaTestFramework (com.intellij.testIntegration.JavaTestFramework)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 CodeInsightBundle (com.intellij.codeInsight.CodeInsightBundle)1 CodeInsightUtil (com.intellij.codeInsight.CodeInsightUtil)1 FileModificationService (com.intellij.codeInsight.FileModificationService)1 Template (com.intellij.codeInsight.template.Template)1 JUnit5Framework (com.intellij.execution.junit.JUnit5Framework)1 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 FileTemplateManager (com.intellij.ide.fileTemplates.FileTemplateManager)1 FileTemplateUtil (com.intellij.ide.fileTemplates.FileTemplateUtil)1 PropertiesComponent (com.intellij.ide.util.PropertiesComponent)1 AnAction (com.intellij.openapi.actionSystem.AnAction)1