mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
testng: dependsOn checks extended to forbid test -> configuration dependencies (IDEA-110814)
This commit is contained in:
+11
-3
@@ -15,9 +15,11 @@
|
||||
*/
|
||||
package com.theoryinpractice.testng.inspection;
|
||||
|
||||
import com.intellij.codeInsight.AnnotationUtil;
|
||||
import com.intellij.codeInspection.*;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.theoryinpractice.testng.util.TestNGUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -122,8 +124,8 @@ public class DependsOnMethodInspection extends BaseJavaLocalInspectionTool
|
||||
problemDescriptors.add(descriptor);
|
||||
|
||||
} else {
|
||||
final String configAnnotation = TestNGUtil.getConfigAnnotation(PsiTreeUtil.getParentOfType(dep, PsiMethod.class));
|
||||
PsiMethod[] foundMethods = psiClass.findMethodsByName(methodName, true);
|
||||
|
||||
if (foundMethods.length == 0) {
|
||||
LOGGER.debug("dependsOnMethods method doesn't exist:" + methodName);
|
||||
ProblemDescriptor descriptor = manager.createProblemDescriptor(dep,
|
||||
@@ -135,11 +137,17 @@ public class DependsOnMethodInspection extends BaseJavaLocalInspectionTool
|
||||
} else {
|
||||
boolean hasTestsOrConfigs = false;
|
||||
for (PsiMethod foundMethod : foundMethods) {
|
||||
hasTestsOrConfigs |= TestNGUtil.hasTest(foundMethod) || TestNGUtil.hasConfig(foundMethod);
|
||||
if (configAnnotation != null) {
|
||||
hasTestsOrConfigs |= AnnotationUtil.isAnnotated(foundMethod, configAnnotation, true);
|
||||
} else {
|
||||
hasTestsOrConfigs |= TestNGUtil.hasTest(foundMethod);
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasTestsOrConfigs) {
|
||||
ProblemDescriptor descriptor = manager.createProblemDescriptor(dep,
|
||||
"Method '" + methodName + "' is not a test or configuration method.",
|
||||
configAnnotation == null ? "Method '" + methodName + "' is not a test or configuration method." :
|
||||
"Method '" + methodName + "' is not annotated with @" + configAnnotation,
|
||||
(LocalQuickFix) null,
|
||||
ProblemHighlightType.LIKE_UNKNOWN_SYMBOL, onTheFly);
|
||||
problemDescriptors.add(descriptor);
|
||||
|
||||
@@ -153,6 +153,15 @@ public class TestNGUtil
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String getConfigAnnotation(PsiMethod method) {
|
||||
if (method != null) {
|
||||
for (String fqn : CONFIG_ANNOTATIONS_FQN) {
|
||||
if (AnnotationUtil.isAnnotated(method, fqn, false)) return fqn;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isTestNGAnnotation(PsiAnnotation annotation) {
|
||||
String qName = annotation.getQualifiedName();
|
||||
if (qName.equals(TEST_ANNOTATION_FQN)) return true;
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
import org.testng.annotations.*;
|
||||
public class MyTest {
|
||||
@Test(<warning descr="Method 'beforeMethod' is not a test or configuration method.">dependsOnMethods = "beforeMethod"</warning>)
|
||||
public void testFoo() throws Exception {
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testFoo")
|
||||
public void testBar() {}
|
||||
|
||||
@AfterSuite
|
||||
protected final void afterSuiteMethod() throws Throwable {
|
||||
}
|
||||
|
||||
@BeforeMethod(<warning descr="Method 'afterSuiteMethod' is not annotated with @org.testng.annotations.BeforeMethod">dependsOnMethods = "afterSuiteMethod"</warning>)
|
||||
public final void beforeMethod() throws Throwable {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 2000-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.theoryinpractice.testng.inspection;
|
||||
|
||||
import com.intellij.openapi.application.PluginPathManager;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class TestNGDependsOnInspectionTest extends LightCodeInsightFixtureTestCase {
|
||||
@Test
|
||||
public void testDependencies() throws Exception {
|
||||
Runnable runnable = new Runnable() {
|
||||
public void run() {
|
||||
myFixture.addClass("package org.testng.annotations;\n" +
|
||||
"public @interface AfterSuite { java.lang.String[] dependsOnMethods() default {};}");
|
||||
myFixture.addClass("package org.testng.annotations;\n" +
|
||||
"public @interface BeforeMethod { java.lang.String[] dependsOnMethods() default {};}");
|
||||
myFixture.addClass("package org.testng.annotations;\n" +
|
||||
"public @interface Test { java.lang.String[] dependsOnMethods() default {};}");
|
||||
myFixture.testHighlighting(true, false, false, "Dependencies.java");
|
||||
}
|
||||
};
|
||||
UIUtil.invokeAndWaitIfNeeded(runnable);
|
||||
}
|
||||
|
||||
public void testNothing(){}
|
||||
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return PluginPathManager.getPluginHomePath("testng") + "/testData/inspection/dependsOn/";
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
TestNGDependsOnInspectionTest.super.setUp();
|
||||
myFixture.enableInspections(new DependsOnMethodInspection());
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
TestNGDependsOnInspectionTest.super.tearDown();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
<class name="com.theoryinpractice.testng.inspection.UndeclaredTestsInspectionTest"/>
|
||||
<class name="com.theoryinpractice.testng.intention.AddTestNGJarFixTest"/>
|
||||
<class name="com.theoryinpractice.testng.referenceContributor.TestNGDataProviderTest"/>
|
||||
<class name="com.theoryinpractice.testng.inspection.TestNGDependsOnInspectionTest"/>
|
||||
</classes>
|
||||
</test>
|
||||
</suite>
|
||||
|
||||
Reference in New Issue
Block a user