Skip to content

Commit 1220319

Browse files
committed
Added linters
1 parent 86ac9ae commit 1220319

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package custom_framework.utils.framework_linters;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.regex.Matcher;
9+
import java.util.regex.Pattern;
10+
11+
public class TestGroupCounter {
12+
13+
private static int smokeTestCount = 0;
14+
private static int regressionTestCount = 0;
15+
private static int deprecatedCount = 0;
16+
private static int cleanupCount = 0;
17+
private static int ignoreCount = 0;
18+
private static int noGroupCount = 0;
19+
20+
public static void main(String[] args) {
21+
String projectPath = System.getProperty("user.dir");
22+
List<File> testFiles = findTestFiles(projectPath);
23+
24+
for (File file : testFiles) {
25+
List<String> lines;
26+
try {
27+
lines = Files.readAllLines(file.toPath());
28+
} catch (IOException e) {
29+
System.err.println("Failed to read file: " + file.getAbsolutePath());
30+
continue;
31+
}
32+
33+
String content = String.join("\n", lines);
34+
countTestGroups(content);
35+
}
36+
37+
// Output the counts for each test group
38+
System.out.println("SmokeTest count: " + smokeTestCount);
39+
System.out.println("RegressionTest count: " + regressionTestCount);
40+
System.out.println("Deprecated count: " + deprecatedCount);
41+
System.out.println("Cleanup count: " + cleanupCount);
42+
System.out.println("Ignore count: " + ignoreCount);
43+
System.out.println("No group count: " + noGroupCount);
44+
}
45+
46+
private static List<File> findTestFiles(String projectPath) {
47+
List<File> testFiles = new ArrayList<>();
48+
File projectDir = new File(projectPath);
49+
50+
if (projectDir.isDirectory()) {
51+
File[] files = projectDir.listFiles();
52+
if (files != null) {
53+
for (File file : files) {
54+
if (file.isDirectory()) {
55+
testFiles.addAll(findTestFiles(file.getAbsolutePath()));
56+
} else if (file.getName().endsWith("Test.java")) {
57+
testFiles.add(file);
58+
}
59+
}
60+
}
61+
}
62+
63+
return testFiles;
64+
}
65+
66+
private static void countTestGroups(String content) {
67+
// Updated regex to match @Test annotation with groups and capture the group names
68+
Pattern testPattern = Pattern.compile("@Test\\s*\\(.*groups\\s*=\\s*\\{(\"[^\"]+\")\\}.*\\)");
69+
Matcher matcher = testPattern.matcher(content);
70+
71+
while (matcher.find()) {
72+
String groupContent = matcher.group(1); // Get the captured group names
73+
74+
// Check for specific group names within the captured content
75+
if (groupContent.contains("SmokeTest")) {
76+
smokeTestCount++;
77+
} else if (groupContent.contains("RegressionTest")) {
78+
regressionTestCount++;
79+
} else if (groupContent.contains("Deprecated")) {
80+
deprecatedCount++;
81+
} else if (groupContent.contains("Cleanup")) {
82+
cleanupCount++;
83+
} else if (groupContent.contains("Ignore")) {
84+
ignoreCount++;
85+
} else {
86+
noGroupCount++; // If no known groups are found
87+
}
88+
}
89+
90+
// Count tests that have no group annotation at all
91+
if (!matcher.find()) {
92+
Pattern simpleTestPattern = Pattern.compile("@Test\\s*\\(\\)");
93+
Matcher simpleMatcher = simpleTestPattern.matcher(content);
94+
while (simpleMatcher.find()) {
95+
noGroupCount++;
96+
}
97+
}
98+
}
99+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package custom_framework.utils.framework_linters;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.regex.Matcher;
9+
import java.util.regex.Pattern;
10+
11+
public class TestGroupCounterByClass {
12+
13+
public static void main(String[] args) {
14+
String projectPath = System.getProperty("user.dir");
15+
List<File> testFiles = findTestFiles(projectPath);
16+
17+
for (File file : testFiles) {
18+
List<String> lines;
19+
try {
20+
lines = Files.readAllLines(file.toPath());
21+
} catch (IOException e) {
22+
System.err.println("Failed to read file: " + file.getAbsolutePath());
23+
continue;
24+
}
25+
26+
String content = String.join("\n", lines);
27+
countTestGroups(file.getName(), content);
28+
}
29+
}
30+
31+
private static List<File> findTestFiles(String projectPath) {
32+
List<File> testFiles = new ArrayList<>();
33+
File projectDir = new File(projectPath);
34+
35+
if (projectDir.isDirectory()) {
36+
File[] files = projectDir.listFiles();
37+
if (files != null) {
38+
for (File file : files) {
39+
if (file.isDirectory()) {
40+
testFiles.addAll(findTestFiles(file.getAbsolutePath()));
41+
} else if (file.getName().endsWith("Test.java")) {
42+
testFiles.add(file);
43+
}
44+
}
45+
}
46+
}
47+
48+
return testFiles;
49+
}
50+
51+
private static void countTestGroups(String fileName, String content) {
52+
int totalCount = 0;
53+
int smokeTestCount = 0;
54+
int regressionTestCount = 0;
55+
int deprecatedCount = 0;
56+
int cleanupCount = 0;
57+
int ignoreCount = 0;
58+
int noGroupCount = 0;
59+
60+
// Updated regex to match @Test annotation with groups
61+
Pattern testPattern = Pattern.compile("@Test\\s*\\(.*groups\\s*=\\s*\\{([^}]+)\\}.*\\)");
62+
Matcher matcher = testPattern.matcher(content);
63+
64+
while (matcher.find()) {
65+
totalCount++; // Count this @Test annotation
66+
String groupContent = matcher.group(1); // Get the captured group names
67+
68+
// Split group names by comma and trim whitespace
69+
String[] groups = groupContent.split(",");
70+
for (String group : groups) {
71+
String trimmedGroup = group.trim().replace("\"", ""); // Remove quotes
72+
switch (trimmedGroup) {
73+
case "SmokeTest" -> smokeTestCount++;
74+
case "RegressionTest" -> regressionTestCount++;
75+
case "Deprecated" -> deprecatedCount++;
76+
case "Cleanup" -> cleanupCount++;
77+
case "Ignore" -> ignoreCount++;
78+
default -> noGroupCount++; // If no known groups are found
79+
}
80+
}
81+
}
82+
83+
// Count tests that have no group annotation at all
84+
Pattern simpleTestPattern = Pattern.compile("@Test\\s*\\(\\)");
85+
Matcher simpleMatcher = simpleTestPattern.matcher(content);
86+
while (simpleMatcher.find()) {
87+
totalCount++; // Count this @Test annotation
88+
noGroupCount++;
89+
}
90+
91+
// Print results for the current file
92+
System.out.println("Results for " + fileName + ":");
93+
System.out.println(" - Total @Test count: " + totalCount);
94+
System.out.println(" - SmokeTest count: " + smokeTestCount);
95+
System.out.println(" - RegressionTest count: " + regressionTestCount);
96+
System.out.println(" - Deprecated count: " + deprecatedCount);
97+
System.out.println(" - Cleanup count: " + cleanupCount);
98+
System.out.println(" - Ignore count: " + ignoreCount);
99+
System.out.println(" - No group count: " + noGroupCount);
100+
System.out.println();
101+
}
102+
}

0 commit comments

Comments
 (0)