Skip to content

Commit 2e5a35c

Browse files
committed
Add test-name-matching-surefire-and-failsafe.ql
1 parent 370d1b9 commit 2e5a35c

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Finds test classes whose name matches the default patterns of both the Maven
3+
* Surefire and Failsafe plugin. Because the class name matches both patterns,
4+
* it might be redundantly executed twice, once as unit test and a second
5+
* time as integration test.
6+
*
7+
* @kind problem
8+
*/
9+
10+
import java
11+
12+
// TODO: If necessary, could maybe improve accuracy by making sure Failsafe is actually configured as plugin in `pom.xml`
13+
14+
from TopLevelClass c, string name, string prefix, string suffix
15+
where
16+
name = c.getName()
17+
and name.matches(prefix + "%" + suffix)
18+
// https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#includes
19+
// https://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#includes
20+
and (
21+
// Surefire prefix, Failsafe suffix
22+
prefix = ["Test"] and suffix = ["IT", "ITCase"]
23+
or
24+
// Failsafe prefix, Surefire suffix
25+
prefix = ["IT"] and suffix = ["Test", "Tests", "TestCase"]
26+
)
27+
// Ignore if prefix or suffix might be part of abbreviation (mostly relevant for "IT")
28+
// For example "ITFBlackBox1TestCase" in zxing repository
29+
and not(
30+
prefix.isUppercase()
31+
and exists (int tooManyUpperLettersCount |
32+
// Should at most expect + 1 to match Java naming conventions, e.g. "ITHighTraffic"
33+
tooManyUpperLettersCount = prefix.length() + 2
34+
|
35+
name.regexpMatch("\\p{Upper}{" + tooManyUpperLettersCount.toString() + ",}.*")
36+
)
37+
or
38+
suffix.isUppercase()
39+
and exists (int tooManyUpperLettersCount |
40+
// There should not be any leading uppercase letter to match Java naming conventions, e.g. "HighTrafficIT"
41+
tooManyUpperLettersCount = suffix.length() + 1
42+
|
43+
name.regexpMatch(".*\\p{Upper}{" + tooManyUpperLettersCount.toString() + ",}")
44+
)
45+
)
46+
select c, "Name of this class matches default Surefire and Failsafe pattern"

0 commit comments

Comments
 (0)