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