Skip to content

Commit 9662b47

Browse files
committed
Move likely test method logic to library
1 parent 05502bc commit 9662b47

File tree

3 files changed

+46
-59
lines changed

3 files changed

+46
-59
lines changed

java/ql/lib/semmle/code/java/UnitTests.qll

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ class TestClass extends Class {
6666
}
6767
}
6868

69+
/**
70+
* A class that is likely a test class. That is either a definite test class, or
71+
* a class whose name, package, or location suggests that it might be a test class.
72+
*/
73+
class LikelyTestClass extends Class {
74+
LikelyTestClass() {
75+
this instanceof TestClass or
76+
this.getName().toLowerCase().matches("%test%") or
77+
this.getPackage().getName().toLowerCase().matches("%test%") or
78+
this.getLocation().getFile().getAbsolutePath().matches("%/src/test/java%")
79+
}
80+
}
81+
6982
/**
7083
* A test method declared within a JUnit 3.8 test class.
7184
*/
@@ -185,6 +198,37 @@ class TestMethod extends Method {
185198
}
186199
}
187200

201+
/**
202+
* Any method that is likely a test method.
203+
*/
204+
class LikelyTestMethod extends Method {
205+
LikelyTestMethod() {
206+
this.getDeclaringType() instanceof LikelyTestClass
207+
or
208+
this instanceof TestMethod
209+
or
210+
this instanceof LikelyJunitTest
211+
}
212+
}
213+
214+
/**
215+
* A `Method` that is public, has no parameters,
216+
* has a "void" return type, AND either has a name that starts with "test" OR
217+
* has an annotation that ends with "Test"
218+
*/
219+
class LikelyJunitTest extends Method {
220+
LikelyJunitTest() {
221+
this.isPublic() and
222+
this.getReturnType().hasName("void") and
223+
this.hasNoParameters() and
224+
(
225+
this.getName().matches("JUnit%") or
226+
this.getName().matches("test%") or
227+
this.getAnAnnotation().getType().getName().matches("%Test")
228+
)
229+
}
230+
}
231+
188232
/**
189233
* A TestNG annotation used to mark a method that runs "before".
190234
*/

java/ql/src/Language Abuse/EmptyMethod.ql

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,7 @@
1616
import java
1717

1818
/**
19-
* Represents a likely a test method, which is either a method that is already
20-
* recognized as a `TestMethod` or something that is likely a JUnit test or
21-
* something in the expected test path for Java tests.
22-
*/
23-
class LikelyTestMethod extends Method {
24-
LikelyTestMethod() {
25-
this.getDeclaringType() instanceof TestClass
26-
or
27-
this instanceof TestMethod
28-
or
29-
this instanceof LikelyJunitTest
30-
or
31-
//standard Maven/Gradle test file discovery filepath
32-
this.getFile().getAbsolutePath().matches("%src/test/java%")
33-
or
34-
this.getDeclaringType() instanceof SurefireTest
35-
}
36-
}
37-
38-
/**
39-
* Classes that are likely part of junit tests (more general than `TestMethod` from `UnitTest.qll`)
40-
* A `Method` that is public, has no parameters,
41-
* has a "void" return type, AND either has a name that starts with "test" OR
42-
* has an annotation that ends with "Test"
43-
*/
44-
class LikelyJunitTest extends Method {
45-
LikelyJunitTest() {
46-
this.isPublic() and
47-
this.getReturnType().hasName("void") and
48-
this.hasNoParameters() and
49-
(
50-
this.getName().matches("JUnit%") or
51-
this.getName().matches("test%") or
52-
this.getAnAnnotation().getType().getName().matches("%Test")
53-
)
54-
}
55-
}
56-
57-
/**
58-
* Maven surefire patterns to consider which files are testcases:
59-
* https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
60-
*/
61-
class SurefireTest extends Class {
62-
SurefireTest() {
63-
this.getFile().getAbsolutePath().matches("%src/test/java%") and
64-
this.getFile()
65-
.getBaseName()
66-
.matches(["Test%.java", "%Test.java", "%Tests.java", "%TestCase.java"])
67-
}
68-
}
69-
70-
/**
71-
* A `Method` from source that is not abstract
19+
* A `Method` from source that is not abstract, and likely not a test method
7220
*/
7321
class NonAbstractSource extends Method {
7422
NonAbstractSource() {

java/ql/src/experimental/Security/CWE/CWE-489/TestLib.qll

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,4 @@ import java
1010
* c) in a test class whose name has the word `test`
1111
* d) in a test class implementing a test framework such as JUnit or TestNG
1212
*/
13-
predicate isTestMethod(Method m) {
14-
m.getDeclaringType().getName().toLowerCase().matches("%test%") or // Simple check to exclude test classes to reduce FPs
15-
m.getDeclaringType().getPackage().getName().toLowerCase().matches("%test%") or // Simple check to exclude classes in test packages to reduce FPs
16-
exists(m.getLocation().getFile().getAbsolutePath().indexOf("/src/test/java")) or // Match test directory structure of build tools like maven
17-
m instanceof TestMethod // Test method of a test case implementing a test framework such as JUnit or TestNG
18-
}
13+
predicate isTestMethod(LikelyTestMethod m) { any() }

0 commit comments

Comments
 (0)