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