mirror of
https://github.com/github/codeql.git
synced 2026-04-28 10:15:14 +02:00
Merge pull request #2628 from aschackmull/java/no-adhoc-testclass
Java: Replace ad-hoc TestClass detection.
This commit is contained in:
@@ -36,14 +36,32 @@ class TearDownMethod extends Method {
|
||||
}
|
||||
}
|
||||
|
||||
private class TestRelatedAnnotation extends Annotation {
|
||||
TestRelatedAnnotation() {
|
||||
this.getType().getPackage().hasName("org.testng.annotations") or
|
||||
this.getType().getPackage().hasName("org.junit") or
|
||||
this.getType().getPackage().hasName("org.junit.runner") or
|
||||
this.getType().getPackage().hasName("org.junit.jupiter.api") or
|
||||
this.getType().getPackage().hasName("org.junit.jupiter.params")
|
||||
}
|
||||
}
|
||||
|
||||
private class TestRelatedMethod extends Method {
|
||||
TestRelatedMethod() { this.getAnAnnotation() instanceof TestRelatedAnnotation }
|
||||
}
|
||||
|
||||
/**
|
||||
* A class detected to be a test class, either because it is a JUnit test class
|
||||
* or because its name or the name of one of its super-types contains the substring "Test".
|
||||
* A class detected to be a test class, either because it or one of its super-types
|
||||
* and/or enclosing types contains a test method or method with a unit-test-related
|
||||
* annotation.
|
||||
*/
|
||||
class TestClass extends Class {
|
||||
TestClass() {
|
||||
this instanceof JUnit38TestClass or
|
||||
this.getASupertype*().getSourceDeclaration().getName().matches("%Test%")
|
||||
exists(TestMethod m | m.getDeclaringType() = this) or
|
||||
exists(TestRelatedMethod m | m.getDeclaringType() = this) or
|
||||
this.getASourceSupertype() instanceof TestClass or
|
||||
this.getEnclosingType() instanceof TestClass
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user