Skip to content

Commit bca09e4

Browse files
committed
chore: refactor ExecutedTestTagReporterMojo - tag surefire and check facade
1 parent 31af990 commit bca09e4

File tree

3 files changed

+120
-96
lines changed

3 files changed

+120
-96
lines changed

src/main/java/org/fugerit/java/junit5/tag/check/ExecutedTestTagReporterMojo.java

Lines changed: 4 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,16 @@
55
import org.apache.maven.plugin.MojoExecutionException;
66
import org.apache.maven.plugins.annotations.*;
77
import org.apache.maven.project.MavenProject;
8-
import org.dom4j.Document;
9-
import org.dom4j.Element;
10-
import org.dom4j.io.SAXReader;
8+
import org.fugerit.java.junit5.tag.check.facade.TagCheckFacade;
119
import org.fugerit.java.junit5.tag.check.facade.TagReportFacade;
1210
import org.fugerit.java.junit5.tag.check.facade.TagScanFacade;
11+
import org.fugerit.java.junit5.tag.check.facade.TagSurefireFacade;
1312
import org.fugerit.java.junit5.tag.check.model.ExecutedTest;
14-
import org.fugerit.java.junit5.tag.check.model.TestStats;
15-
import org.junit.jupiter.api.Tag;
1613

1714
import java.io.File;
18-
import java.io.FileWriter;
19-
import java.io.IOException;
20-
import java.lang.reflect.Method;
2115
import java.net.URL;
2216
import java.net.URLClassLoader;
2317
import java.util.*;
24-
import java.util.stream.Collectors;
2518

2619
/**
2720
* Reports tags from actually executed tests by parsing Surefire reports
@@ -71,7 +64,7 @@ public void execute() throws MojoExecutionException {
7164
URLClassLoader classLoader = createTestClassLoader();
7265

7366
// Parse Surefire reports to find executed tests
74-
List<ExecutedTest> executedTests = parseSurefireReports();
67+
List<ExecutedTest> executedTests = TagSurefireFacade.parseSurefireReports( this.surefireReportsDirectory, this.includeSkipped );
7568

7669
getLog().info("Found " + executedTests.size() + " executed tests");
7770

@@ -84,7 +77,7 @@ public void execute() throws MojoExecutionException {
8477

8578
// Check for required tags
8679
if (requiredTags != null && !requiredTags.isEmpty()) {
87-
checkRequiredTags(testTagMap);
80+
TagCheckFacade.checkRequiredTags( this.requiredTags, this.failOnMissingTag, testTagMap);
8881
}
8982

9083
getLog().info("Executed Test Tag Report generated: " +
@@ -112,89 +105,4 @@ private URLClassLoader createTestClassLoader() throws DependencyResolutionRequir
112105
return new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
113106
}
114107

115-
private List<ExecutedTest> parseSurefireReports() {
116-
List<ExecutedTest> executedTests = new ArrayList<>();
117-
SAXReader reader = new SAXReader();
118-
119-
// Find all XML report files
120-
File[] reportFiles = surefireReportsDirectory.listFiles(
121-
(dir, name) -> name.startsWith("TEST-") && name.endsWith(".xml"));
122-
123-
if (reportFiles == null || reportFiles.length == 0) {
124-
getLog().warn("No Surefire XML reports found in: " +
125-
surefireReportsDirectory.getAbsolutePath());
126-
return executedTests;
127-
}
128-
129-
for (File reportFile : reportFiles) {
130-
getLog().debug("Parsing report: " + reportFile.getName());
131-
132-
try {
133-
Document document = reader.read(reportFile);
134-
Element root = document.getRootElement();
135-
136-
String className = root.attributeValue("name");
137-
138-
@SuppressWarnings("unchecked")
139-
List<Element> testCases = root.elements("testcase");
140-
141-
for (Element testCase : testCases) {
142-
String methodName = testCase.attributeValue("name");
143-
String testClassName = testCase.attributeValue("classname", className);
144-
String time = testCase.attributeValue("time");
145-
146-
boolean skipped = testCase.element("skipped") != null;
147-
boolean failed = testCase.element("failure") != null;
148-
boolean error = testCase.element("error") != null;
149-
150-
ExecutedTest test = new ExecutedTest(
151-
testClassName,
152-
methodName,
153-
skipped,
154-
failed,
155-
error,
156-
time
157-
);
158-
159-
if (!skipped || includeSkipped) {
160-
executedTests.add(test);
161-
}
162-
}
163-
} catch (Exception e) {
164-
getLog().warn("Error parsing report file: " + reportFile.getName(), e);
165-
}
166-
}
167-
168-
return executedTests;
169-
}
170-
171-
172-
173-
private void checkRequiredTags(Map<ExecutedTest, Set<String>> testTagMap)
174-
throws MojoExecutionException {
175-
Set<String> foundTags = testTagMap.values().stream()
176-
.flatMap(Set::stream)
177-
.collect(Collectors.toSet());
178-
179-
List<String> missingTags = new ArrayList<>();
180-
for (String requiredTag : requiredTags) {
181-
if (!foundTags.contains(requiredTag)) {
182-
missingTags.add(requiredTag);
183-
}
184-
}
185-
186-
if (!missingTags.isEmpty()) {
187-
String message = "Missing required tags in executed tests: " +
188-
String.join(", ", missingTags);
189-
if (failOnMissingTag) {
190-
throw new MojoExecutionException(message);
191-
} else {
192-
getLog().warn(message);
193-
}
194-
} else {
195-
getLog().info("All required tags found in executed tests: " +
196-
String.join(", ", requiredTags));
197-
}
198-
}
199-
200108
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.fugerit.java.junit5.tag.check.facade;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.apache.maven.plugin.MojoExecutionException;
5+
import org.fugerit.java.junit5.tag.check.model.ExecutedTest;
6+
7+
import java.util.*;
8+
import java.util.stream.Collectors;
9+
10+
@Slf4j
11+
public class TagCheckFacade {
12+
13+
private TagCheckFacade() {}
14+
15+
public static void checkRequiredTags(Collection<String> requiredTags, boolean failOnMissingTag, Map<ExecutedTest, Set<String>> testTagMap)
16+
throws MojoExecutionException {
17+
Set<String> foundTags = testTagMap.values().stream()
18+
.flatMap(Set::stream)
19+
.collect(Collectors.toSet());
20+
21+
List<String> missingTags = new ArrayList<>();
22+
for (String requiredTag : requiredTags) {
23+
if (!foundTags.contains(requiredTag)) {
24+
missingTags.add(requiredTag);
25+
}
26+
}
27+
28+
if (!missingTags.isEmpty()) {
29+
String message = "Missing required tags in executed tests: " +
30+
String.join(", ", missingTags);
31+
if (failOnMissingTag) {
32+
throw new MojoExecutionException(message);
33+
} else {
34+
log.warn(message);
35+
}
36+
} else {
37+
log.info("All required tags found in executed tests: {}",
38+
String.join(", ", requiredTags));
39+
}
40+
}
41+
42+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.fugerit.java.junit5.tag.check.facade;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.dom4j.Document;
5+
import org.dom4j.Element;
6+
import org.dom4j.io.SAXReader;
7+
import org.fugerit.java.junit5.tag.check.model.ExecutedTest;
8+
9+
import java.io.File;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
@Slf4j
14+
public class TagSurefireFacade {
15+
16+
private TagSurefireFacade() {}
17+
18+
public static List<ExecutedTest> parseSurefireReports( File surefireReportsDirectory, boolean includeSkipped ) {
19+
List<ExecutedTest> executedTests = new ArrayList<>();
20+
SAXReader reader = new SAXReader();
21+
22+
// Find all XML report files
23+
File[] reportFiles = surefireReportsDirectory.listFiles(
24+
(dir, name) -> name.startsWith("TEST-") && name.endsWith(".xml"));
25+
26+
if (reportFiles == null || reportFiles.length == 0) {
27+
log.warn("No Surefire XML reports found in: {}",
28+
surefireReportsDirectory.getAbsolutePath());
29+
return executedTests;
30+
}
31+
32+
for (File reportFile : reportFiles) {
33+
log.debug("Parsing report: {}", reportFile.getName());
34+
35+
try {
36+
Document document = reader.read(reportFile);
37+
Element root = document.getRootElement();
38+
39+
String className = root.attributeValue("name");
40+
41+
@SuppressWarnings("unchecked")
42+
List<Element> testCases = root.elements("testcase");
43+
44+
for (Element testCase : testCases) {
45+
String methodName = testCase.attributeValue("name");
46+
String testClassName = testCase.attributeValue("classname", className);
47+
String time = testCase.attributeValue("time");
48+
49+
boolean skipped = testCase.element("skipped") != null;
50+
boolean failed = testCase.element("failure") != null;
51+
boolean error = testCase.element("error") != null;
52+
53+
ExecutedTest test = new ExecutedTest(
54+
testClassName,
55+
methodName,
56+
skipped,
57+
failed,
58+
error,
59+
time
60+
);
61+
62+
if (!skipped || includeSkipped) {
63+
executedTests.add(test);
64+
}
65+
}
66+
} catch (Exception e) {
67+
log.warn("Error parsing report file: {}", reportFile.getName(), e);
68+
}
69+
}
70+
71+
return executedTests;
72+
}
73+
74+
}

0 commit comments

Comments
 (0)