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);
}
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;
}
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;
}
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;
}
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;
}
Aggregations