From 9744fa362e7e7560a1cc4b8d32f664fbbd4d613f Mon Sep 17 00:00:00 2001 From: anna Date: Tue, 23 Jul 2013 18:02:38 +0200 Subject: [PATCH] testng: dependsOn checks extended to forbid test -> configuration dependencies (IDEA-110814) --- .../inspection/DependsOnMethodInspection.java | 14 ++- .../testng/util/TestNGUtil.java | 9 ++ .../inspection/dependsOn/Dependencies.java | 20 +++++ .../TestNGDependsOnInspectionTest.java | 86 +++++++++++++++++++ plugins/testng/testSources/testng.xml | 1 + 5 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 plugins/testng/testData/inspection/dependsOn/Dependencies.java create mode 100644 plugins/testng/testSources/com/theoryinpractice/testng/inspection/TestNGDependsOnInspectionTest.java diff --git a/plugins/testng/src/com/theoryinpractice/testng/inspection/DependsOnMethodInspection.java b/plugins/testng/src/com/theoryinpractice/testng/inspection/DependsOnMethodInspection.java index 818e6865a9cd..2171984f89ae 100644 --- a/plugins/testng/src/com/theoryinpractice/testng/inspection/DependsOnMethodInspection.java +++ b/plugins/testng/src/com/theoryinpractice/testng/inspection/DependsOnMethodInspection.java @@ -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); diff --git a/plugins/testng/src/com/theoryinpractice/testng/util/TestNGUtil.java b/plugins/testng/src/com/theoryinpractice/testng/util/TestNGUtil.java index 0ef2e28a8cdc..5138ca80c96d 100644 --- a/plugins/testng/src/com/theoryinpractice/testng/util/TestNGUtil.java +++ b/plugins/testng/src/com/theoryinpractice/testng/util/TestNGUtil.java @@ -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; diff --git a/plugins/testng/testData/inspection/dependsOn/Dependencies.java b/plugins/testng/testData/inspection/dependsOn/Dependencies.java new file mode 100644 index 000000000000..b708f2c7f705 --- /dev/null +++ b/plugins/testng/testData/inspection/dependsOn/Dependencies.java @@ -0,0 +1,20 @@ + +import org.testng.annotations.*; +public class MyTest { + @Test(dependsOnMethods = "beforeMethod") + public void testFoo() throws Exception { + } + + @Test(dependsOnMethods = "testFoo") + public void testBar() {} + + @AfterSuite + protected final void afterSuiteMethod() throws Throwable { + } + + @BeforeMethod(dependsOnMethods = "afterSuiteMethod") + public final void beforeMethod() throws Throwable { + } +} + + diff --git a/plugins/testng/testSources/com/theoryinpractice/testng/inspection/TestNGDependsOnInspectionTest.java b/plugins/testng/testSources/com/theoryinpractice/testng/inspection/TestNGDependsOnInspectionTest.java new file mode 100644 index 000000000000..3e317c0d6ca4 --- /dev/null +++ b/plugins/testng/testSources/com/theoryinpractice/testng/inspection/TestNGDependsOnInspectionTest.java @@ -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); + } + } + }); + } + +} diff --git a/plugins/testng/testSources/testng.xml b/plugins/testng/testSources/testng.xml index 513f38ba8ce6..52b07d6de88c 100644 --- a/plugins/testng/testSources/testng.xml +++ b/plugins/testng/testSources/testng.xml @@ -9,6 +9,7 @@ +