|
| 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