junit 5: warn for static nested classes annotated with @Nested (IDEA-187572)

This commit is contained in:
Anna.Kozlova
2018-05-09 18:28:38 +02:00
parent ebcc6906bd
commit f99269157d
6 changed files with 84 additions and 1 deletions
@@ -2282,4 +2282,5 @@ inspection.list.remove.in.loop.fix.family.name=Replace with 'List.subList().clea
inspection.serializable.can.have.default.serial.uid='Serializable' can have default generated 'serialVersionUID'
inspection.serializable.can.have.default.serial.uid.message='serialVersionUID' differs from default generated
inspection.serializable.can.have.default.serial.uid.fix.name=Change 'serialVersionUID' to generated by signature
inspection.serializable.can.have.default.serial.uid.fix.name=Change 'serialVersionUID' to generated by signature
junit5.nested.test.display.name=JUnit 5 malformed @Nested class
+5
View File
@@ -92,6 +92,11 @@
groupBundle="messages.InspectionsBundle" groupKey="group.names.junit.issues" enabledByDefault="true" level="WARNING"
implementationClass="com.intellij.execution.junit.codeInsight.JUnit5MalformedRepeatedTestInspection"/>
<localInspection groupPath="Java" language="JVM" shortName="JUnit5MalformedNestedClass" bundle="com.siyeh.InspectionGadgetsBundle"
key="junit5.nested.test.display.name"
groupBundle="messages.InspectionsBundle" groupKey="group.names.junit.issues" enabledByDefault="true" level="WARNING"
implementationClass="com.intellij.execution.junit.codeInsight.JUnit5MalformedNestedClassInspection"/>
<runDashboardContributor implementation="com.intellij.execution.junit.JUnitRunDashboardContributor"/>
</extensions>
@@ -0,0 +1,33 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.execution.junit.codeInsight
import com.intellij.codeInspection.IntentionWrapper
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.lang.jvm.DefaultJvmElementVisitor
import com.intellij.lang.jvm.JvmClass
import com.intellij.lang.jvm.JvmElementVisitor
import com.intellij.lang.jvm.JvmModifier
import com.intellij.lang.jvm.actions.MemberRequest
import com.intellij.lang.jvm.actions.createModifierActions
import com.intellij.lang.jvm.inspection.JvmLocalInspection
import com.intellij.openapi.project.Project
import com.siyeh.ig.junit.JUnitCommonClassNames
class JUnit5MalformedNestedClassInspection : JvmLocalInspection() {
override fun buildVisitor(project: Project, sink: JvmLocalInspection.HighlightSink, isOnTheFly: Boolean): JvmElementVisitor<Boolean>? {
return object : DefaultJvmElementVisitor<Boolean> {
override fun visitClass(clazz: JvmClass): Boolean {
if (clazz.containingClass != null &&
clazz.hasModifier(JvmModifier.STATIC) &&
clazz.hasAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_NESTED)) {
val fixes = createModifierActions(clazz, MemberRequest.Modifier(JvmModifier.STATIC, false)).toTypedArray()
sink.highlight("Only non-static nested classes can serve as @Nested test classes.",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
*IntentionWrapper.wrapToQuickFixes(fixes, clazz.sourceElement!!.containingFile))
}
return true
}
}
}
}
@@ -0,0 +1,8 @@
<html>
<body>
Reports static inner classes annotated with @Nested.
<!-- tooltip end -->
<p>
<small>New in 2018.2</small>
</body>
</html>
@@ -0,0 +1,32 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.execution.junit.codeInsight
import com.intellij.codeInspection.InspectionProfileEntry
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import com.siyeh.ig.LightInspectionTestCase
class JUnit5MalformedNestedClassTest : LightInspectionTestCase() {
override fun getInspection(): InspectionProfileEntry? {
return JUnit5MalformedNestedClassInspection()
}
@Throws(Exception::class)
override fun setUp() {
super.setUp()
addEnvironmentClass("package org.junit.jupiter.api;" +
"public @interface Nested {}")
}
fun testMalformed() {
doTest()
}
override fun getBasePath(): String {
return "/plugins/junit/testData/codeInsight/malformedNested"
}
override fun getProjectDescriptor(): LightProjectDescriptor {
return LightCodeInsightFixtureTestCase.JAVA_8
}
}
@@ -0,0 +1,4 @@
class A {
@org.junit.jupiter.api.Nested
static class <warning descr="Only non-static nested classes can serve as @Nested test classes.">B</warning> {}
}