Search in sources :

Example 36 with Project

use of com.intellij.openapi.project.Project in project intellij-community by JetBrains.

the class ConfigurationsTest method testRenameMethod.

@Test
public void testRenameMethod() {
    final Project project = myProjectFixture.getProject();
    final PsiClass psiClass = findTestClass(project);
    final String className = psiClass.getName();
    final TestNGConfiguration configuration = createConfiguration(project);
    final PsiMethod method = findTestMethod(psiClass);
    configuration.beMethodConfiguration(new PsiLocation<>(project, method));
    final String newMethodName = "renamedTest";
    final RenameRefactoring renameMethod = RefactoringFactory.getInstance(project).createRename(method, newMethodName);
    renameMethod.setSearchInComments(false);
    renameMethod.setSearchInNonJavaFiles(false);
    new WriteCommandAction(project) {

        protected void run(@NotNull final Result result) throws Throwable {
            renameMethod.run();
        }
    }.execute();
    Assert.assertEquals(className, configuration.getPersistantData().getMainClassName());
    Assert.assertEquals(newMethodName, configuration.getPersistantData().getMethodName());
    final PsiMethod notATestMethod = findNotATestMethod(psiClass);
    final RenameRefactoring renameNotATestMethod1 = RefactoringFactory.getInstance(project).createRename(notATestMethod, "bbbbb");
    renameNotATestMethod1.setSearchInComments(false);
    renameNotATestMethod1.setSearchInNonJavaFiles(false);
    new WriteCommandAction(project) {

        protected void run(@NotNull final Result result) throws Throwable {
            renameNotATestMethod1.run();
        }
    }.execute();
    Assert.assertEquals(className, configuration.getPersistantData().getMainClassName());
    Assert.assertEquals(newMethodName, configuration.getPersistantData().getMethodName());
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) PsiMethod(com.intellij.psi.PsiMethod) RenameRefactoring(com.intellij.refactoring.RenameRefactoring) PsiClass(com.intellij.psi.PsiClass) Result(com.intellij.openapi.application.Result) Test(org.testng.annotations.Test)

Example 37 with Project

use of com.intellij.openapi.project.Project in project intellij-community by JetBrains.

the class ConfigurationsTest method testCreateFromContext.

@Test
public void testCreateFromContext() {
    final Project project = myProjectFixture.getProject();
    final PsiClass psiClass = findTestClass(project);
    final TestNGInClassConfigurationProducer producer = new TestNGInClassConfigurationProducer();
    final MapDataContext dataContext = new MapDataContext();
    dataContext.put(CommonDataKeys.PROJECT, project);
    dataContext.put(LangDataKeys.MODULE, ModuleUtil.findModuleForPsiElement(psiClass));
    dataContext.put(Location.DATA_KEY, PsiLocation.fromPsiElement(psiClass));
    final ConfigurationFromContext fromContext = producer.createConfigurationFromContext(ConfigurationContext.getFromContext(dataContext));
    assert fromContext != null;
    final RunnerAndConfigurationSettings config = fromContext.getConfigurationSettings();
    final RunConfiguration runConfiguration = config.getConfiguration();
    Assert.assertTrue(runConfiguration instanceof TestNGConfiguration);
    TestNGConfigurationType t = (TestNGConfigurationType) runConfiguration.getType();
    Assert.assertTrue(t.isConfigurationByLocation(runConfiguration, new PsiLocation(project, psiClass)));
}
Also used : Project(com.intellij.openapi.project.Project) MapDataContext(com.intellij.testFramework.MapDataContext) RunConfiguration(com.intellij.execution.configurations.RunConfiguration) ConfigurationFromContext(com.intellij.execution.actions.ConfigurationFromContext) PsiClass(com.intellij.psi.PsiClass) RunnerAndConfigurationSettings(com.intellij.execution.RunnerAndConfigurationSettings) PsiLocation(com.intellij.execution.PsiLocation) Test(org.testng.annotations.Test)

Example 38 with Project

use of com.intellij.openapi.project.Project in project intellij-community by JetBrains.

the class ConfigurationsTest method testClassRename.

@Test
public void testClassRename() {
    final Project project = myProjectFixture.getProject();
    final PsiClass psiClass = findTestClass(project);
    final TestNGConfiguration configuration = createConfiguration(project);
    configuration.beClassConfiguration(psiClass);
    final String newName = "Testt1";
    final RenameRefactoring renameClass = RefactoringFactory.getInstance(project).createRename(psiClass, newName);
    renameClass.setSearchInComments(false);
    renameClass.setSearchInNonJavaFiles(false);
    new WriteCommandAction(project) {

        protected void run(@NotNull final Result result) throws Throwable {
            renameClass.run();
        }
    }.execute();
    Assert.assertEquals(newName, configuration.getPersistantData().getMainClassName());
    final PsiMethod notATestMethod = findNotATestMethod(psiClass);
    final RenameRefactoring renameNotATestMethod = RefactoringFactory.getInstance(project).createRename(notATestMethod, "aaaa");
    renameNotATestMethod.setSearchInComments(false);
    renameNotATestMethod.setSearchInNonJavaFiles(false);
    new WriteCommandAction(project) {

        protected void run(@NotNull final Result result) throws Throwable {
            renameNotATestMethod.run();
        }
    }.execute();
    Assert.assertEquals(configuration.getPersistantData().getMainClassName(), newName);
    Assert.assertEquals(configuration.getPersistantData().getMethodName(), "");
    Assert.assertEquals(configuration.getPersistantData().TEST_OBJECT, TestType.CLASS.getType());
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) PsiMethod(com.intellij.psi.PsiMethod) RenameRefactoring(com.intellij.refactoring.RenameRefactoring) PsiClass(com.intellij.psi.PsiClass) Result(com.intellij.openapi.application.Result) Test(org.testng.annotations.Test)

Example 39 with Project

use of com.intellij.openapi.project.Project in project intellij-community by JetBrains.

the class UndeclaredTestInspection method checkClass.

@Nullable
public ProblemDescriptor[] checkClass(@NotNull final PsiClass aClass, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
    if (TestNGUtil.hasTest(aClass) && PsiClassUtil.isRunnableClass(aClass, true)) {
        final Project project = aClass.getProject();
        final String qName = aClass.getQualifiedName();
        if (qName == null)
            return null;
        final String packageQName = StringUtil.getPackageName(qName);
        final List<String> names = new ArrayList<>();
        for (int i = 0; i < qName.length(); i++) {
            if (qName.charAt(i) == '.') {
                names.add(qName.substring(0, i));
            }
        }
        names.add(qName);
        Collections.reverse(names);
        for (final String name : names) {
            final boolean isFullName = qName.equals(name);
            final boolean[] found = new boolean[] { false };
            PsiSearchHelper.SERVICE.getInstance(project).processUsagesInNonJavaFiles(name, (file, startOffset, endOffset) -> {
                if (file.findReferenceAt(startOffset) != null) {
                    if (!isFullName) {
                        //special package tag required
                        final XmlTag tag = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), XmlTag.class);
                        if (tag == null || !tag.getName().equals("package")) {
                            return true;
                        }
                        final XmlAttribute attribute = tag.getAttribute("name");
                        if (attribute == null)
                            return true;
                        final String value = attribute.getValue();
                        if (value == null)
                            return true;
                        if (!value.endsWith(".*") && !value.equals(packageQName))
                            return true;
                    }
                    found[0] = true;
                    return false;
                }
                return true;
            }, new TestNGSearchScope(project));
            if (found[0])
                return null;
        }
        final PsiIdentifier nameIdentifier = aClass.getNameIdentifier();
        LOG.assertTrue(nameIdentifier != null);
        return new ProblemDescriptor[] { manager.createProblemDescriptor(nameIdentifier, "Undeclared test \'" + aClass.getName() + "\'", isOnTheFly, new LocalQuickFix[] { new RegisterClassFix(aClass), new CreateTestngFix() }, ProblemHighlightType.GENERIC_ERROR_OR_WARNING) };
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with Project

use of com.intellij.openapi.project.Project in project intellij-community by JetBrains.

the class TestNGTestObject method collectDependsOnMethods.

private static void collectDependsOnMethods(final Map<PsiClass, Map<PsiMethod, List<String>>> results, final Set<PsiMember> alreadyMarkedToBeChecked, final Set<PsiMember> membersToCheckNow, final PsiMethod[] methods, final PsiClass... classes) {
    final PsiClass[] psiClasses;
    if (methods != null && methods.length > 0) {
        final Set<PsiClass> containingClasses = new LinkedHashSet<>();
        for (final PsiMethod method : methods) {
            containingClasses.add(ApplicationManager.getApplication().runReadAction(new Computable<PsiClass>() {

                @Override
                public PsiClass compute() {
                    return method.getContainingClass();
                }
            }));
        }
        psiClasses = containingClasses.toArray(new PsiClass[containingClasses.size()]);
    } else {
        psiClasses = classes;
    }
    for (final PsiClass containingClass : psiClasses) {
        final Set<String> testMethodDependencies = new LinkedHashSet<>();
        final HashMap<String, Collection<String>> valuesMap = new HashMap<>();
        valuesMap.put("dependsOnMethods", testMethodDependencies);
        TestNGUtil.collectAnnotationValues(valuesMap, methods, containingClass);
        if (!testMethodDependencies.isEmpty()) {
            ApplicationManager.getApplication().runReadAction(() -> {
                final Project project = containingClass.getProject();
                final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
                for (String dependency : testMethodDependencies) {
                    final String className = StringUtil.getPackageName(dependency);
                    final String methodName = StringUtil.getShortName(dependency);
                    if (StringUtil.isEmpty(className)) {
                        checkClassMethods(methodName, containingClass, alreadyMarkedToBeChecked, membersToCheckNow, results);
                    } else {
                        final PsiClass aClass = psiFacade.findClass(className, containingClass.getResolveScope());
                        if (aClass != null) {
                            checkClassMethods(methodName, aClass, alreadyMarkedToBeChecked, membersToCheckNow, results);
                        }
                    }
                }
            });
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) Computable(com.intellij.openapi.util.Computable)

Aggregations

Project (com.intellij.openapi.project.Project)3623 VirtualFile (com.intellij.openapi.vfs.VirtualFile)874 NotNull (org.jetbrains.annotations.NotNull)580 Nullable (org.jetbrains.annotations.Nullable)478 Module (com.intellij.openapi.module.Module)368 PsiFile (com.intellij.psi.PsiFile)334 Editor (com.intellij.openapi.editor.Editor)301 PsiElement (com.intellij.psi.PsiElement)292 ArrayList (java.util.ArrayList)214 File (java.io.File)212 Document (com.intellij.openapi.editor.Document)180 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)172 List (java.util.List)158 IOException (java.io.IOException)107 TextRange (com.intellij.openapi.util.TextRange)99 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)96 IncorrectOperationException (com.intellij.util.IncorrectOperationException)95 Presentation (com.intellij.openapi.actionSystem.Presentation)94 DataContext (com.intellij.openapi.actionSystem.DataContext)92 PsiDirectory (com.intellij.psi.PsiDirectory)90