1212import org .jetbrains .annotations .Nullable ;
1313import org .slf4j .Logger ;
1414import org .slf4j .LoggerFactory ;
15+ import org .slf4j .helpers .NOPLogger ;
1516
1617import java .io .IOException ;
1718import java .nio .file .FileSystems ;
@@ -33,15 +34,14 @@ public class TestSplit {
3334 Set .of ("org.junit.jupiter.api.Disabled" , "org.junit.Ignore" );
3435 private static final @ NotNull Set <String > SKIP_TEST_ANNOTATIONS = Set .of ("Disabled" , "Ignore" );
3536
36- private static final @ NotNull Logger LOG = LoggerFactory .getLogger (TestSplit .class );
37-
3837 private final int splitTotal ;
3938 private final @ NotNull String glob ;
4039 private final @ Nullable String excludeGlob ;
4140 private final @ Nullable String junitGlob ;
4241 private final @ NotNull FormatOption formatOption ;
4342 private final @ NotNull NewTestTimeOption newTestTimeOption ;
4443 private final @ NotNull Path workingDirectory ;
44+ private final @ NotNull Logger log ;
4545 private final boolean debug ;
4646 private final @ NotNull Consumer <Integer > exitCodeConsumer ;
4747
@@ -53,6 +53,7 @@ public TestSplit(
5353 final @ NotNull FormatOption formatOption ,
5454 final @ NotNull NewTestTimeOption newTestTimeOption ,
5555 final @ NotNull Path workingDirectory ,
56+ final boolean log ,
5657 final boolean debug ,
5758 final @ NotNull Consumer <Integer > exitCodeConsumer ) {
5859 this .splitTotal = splitTotal ;
@@ -62,6 +63,7 @@ public TestSplit(
6263 this .formatOption = formatOption ;
6364 this .newTestTimeOption = newTestTimeOption ;
6465 this .workingDirectory = workingDirectory ;
66+ this .log = log ? LoggerFactory .getLogger (TestSplit .class ) : NOPLogger .NOP_LOGGER ;
6567 this .debug = debug ;
6668 this .exitCodeConsumer = exitCodeConsumer ;
6769 }
@@ -70,17 +72,17 @@ public TestSplit(
7072 final var testPaths = getPaths (workingDirectory , glob , excludeGlob );
7173 final var classNames = fileToClassName (testPaths , exitCodeConsumer );
7274 if (classNames .isEmpty ()) {
73- LOG .error ("Found no test classes" );
75+ log .error ("Found no test classes" );
7476 exitCodeConsumer .accept (1 );
7577 } else {
76- LOG .info ("Found {} test classes" , classNames .size ());
78+ log .info ("Found {} test classes" , classNames .size ());
7779 }
7880
7981 final var testCases = new HashSet <TestCase >();
8082 if (junitGlob != null ) {
8183 // analyze JUnit reports
8284 final var junitPaths = getPaths (workingDirectory , junitGlob , null );
83- LOG .info ("Found {} JUnit report files" , junitPaths .size ());
85+ log .info ("Found {} JUnit report files" , junitPaths .size ());
8486 if (!junitPaths .isEmpty ()) {
8587 var fastestTest = new TestCase ("" , Double .MAX_VALUE );
8688 var slowestTest = new TestCase ("" , Double .MIN_VALUE );
@@ -90,7 +92,7 @@ public TestSplit(
9092 final var testCase = new TestCase (testSuite .getName (), testSuite .getTime ());
9193 if (classNames .contains (testCase .name ())) {
9294 if (testCases .add (testCase )) {
93- LOG .debug ("Adding test {} [{}]" , testCase .name (), formatTime (testCase .time ()));
95+ log .debug ("Adding test {} [{}]" , testCase .name (), formatTime (testCase .time ()));
9496 if (testCase .time () < fastestTest .time ()) {
9597 fastestTest = testCase ;
9698 }
@@ -99,53 +101,53 @@ public TestSplit(
99101 }
100102 }
101103 } else {
102- LOG .info ("Skipping test {} from JUnit report" , testCase .name ());
104+ log .info ("Skipping test {} from JUnit report" , testCase .name ());
103105 }
104106 }
105- LOG .debug ("Found {} recorded test classes with time information" , testCases .size ());
106- LOG .debug ("Fastest test class: {} ({})" , fastestTest .name (), formatTime (fastestTest .time ()));
107- LOG .debug ("Slowest test class: {} ({})" , slowestTest .name (), formatTime (slowestTest .time ()));
107+ log .debug ("Found {} recorded test classes with time information" , testCases .size ());
108+ log .debug ("Fastest test class: {} ({})" , fastestTest .name (), formatTime (fastestTest .time ()));
109+ log .debug ("Slowest test class: {} ({})" , slowestTest .name (), formatTime (slowestTest .time ()));
108110 }
109111 }
110112 // add tests without timing records
111113 final var newTestTime = getNewTestTime (newTestTimeOption , testCases );
112114 classNames .forEach (className -> {
113115 final var testCase = new TestCase (className , newTestTime );
114116 if (testCases .add (testCase )) {
115- LOG .debug ("Adding test {} [estimated {}]" , testCase .name (), formatTime (testCase .time ()));
117+ log .debug ("Adding test {} [estimated {}]" , testCase .name (), formatTime (testCase .time ()));
116118 }
117119 });
118120
119121 // split tests
120- LOG .debug ("Splitting {} tests" , testCases .size ());
122+ log .debug ("Splitting {} tests" , testCases .size ());
121123 final var splits = new Splits (splitTotal , formatOption );
122124 testCases .stream ().sorted (Comparator .reverseOrder ()).forEach (testCase -> {
123125 final var split = splits .add (testCase );
124- LOG .debug ("Adding test {} to split #{}" , testCase .name (), split .index ());
126+ log .debug ("Adding test {} to split #{}" , testCase .name (), split .index ());
125127 });
126128
127129 if (debug ) {
128130 if (splitTotal > 1 ) {
129131 final var fastestSplit = splits .getFastest ();
130- LOG .debug ("Fastest test plan is #{} with {} tests ({})" ,
132+ log .debug ("Fastest test plan is #{} with {} tests ({})" ,
131133 fastestSplit .formatIndex (),
132134 fastestSplit .tests ().size (),
133135 formatTime (fastestSplit .totalRecordedTime ()));
134136 final var slowestSplit = splits .getSlowest ();
135- LOG .debug ("Slowest test plan is #{} with {} tests ({})" ,
137+ log .debug ("Slowest test plan is #{} with {} tests ({})" ,
136138 slowestSplit .formatIndex (),
137139 slowestSplit .tests ().size (),
138140 formatTime (slowestSplit .totalRecordedTime ()));
139- LOG .debug ("Difference between the fastest and slowest test plan: {}" ,
141+ log .debug ("Difference between the fastest and slowest test plan: {}" ,
140142 formatTime (slowestSplit .totalRecordedTime () - fastestSplit .totalRecordedTime ()));
141143 }
142- LOG .debug ("Test splits:" );
143- splits .forEach (split -> LOG .debug (split .toString ()));
144+ log .debug ("Test splits:" );
145+ splits .forEach (split -> log .debug (split .toString ()));
144146 }
145147 return splits ;
146148 }
147149
148- private static @ NotNull Set <Path > getPaths (
150+ private @ NotNull Set <Path > getPaths (
149151 final @ NotNull Path rootPath ,
150152 final @ NotNull String glob ,
151153 final @ Nullable String excludeGlob ) throws Exception {
@@ -161,7 +163,7 @@ public TestSplit(
161163 final var candidate = path .normalize ();
162164 if (includeMatcher .matches (candidate )) {
163165 if (excludeMatcher .matches (candidate )) {
164- LOG .debug ("Excluding test file {}" , candidate );
166+ log .debug ("Excluding test file {}" , candidate );
165167 } else {
166168 files .add (candidate );
167169 }
@@ -178,7 +180,7 @@ public TestSplit(
178180 return files ;
179181 }
180182
181- private static @ NotNull Set <String > fileToClassName (
183+ private @ NotNull Set <String > fileToClassName (
182184 final @ NotNull Set <Path > testPaths ,
183185 final @ NotNull Consumer <Integer > exitCodeConsumer ) {
184186 final var javaParser = new JavaParser ();
@@ -189,10 +191,10 @@ public TestSplit(
189191 final var declaration = compilationUnit .findFirst (ClassOrInterfaceDeclaration .class ).orElseThrow ();
190192 final var className = declaration .getFullyQualifiedName ().orElseThrow ();
191193 if (declaration .isInterface ()) {
192- LOG .info ("Skipping interface {}" , className );
194+ log .info ("Skipping interface {}" , className );
193195 continue ;
194196 } else if (declaration .isAbstract ()) {
195- LOG .info ("Skipping abstract class {}" , className );
197+ log .info ("Skipping abstract class {}" , className );
196198 continue ;
197199 }
198200 final var hasSkipTestImport = compilationUnit .getImports ()
@@ -204,19 +206,19 @@ public TestSplit(
204206 .map (AnnotationExpr ::getNameAsString )
205207 .anyMatch (SKIP_TEST_ANNOTATIONS ::contains );
206208 if (hasSkipTestImport && hasSkipTestAnnotation ) {
207- LOG .info ("Skipping disabled test class {}" , className );
209+ log .info ("Skipping disabled test class {}" , className );
208210 continue ;
209211 }
210212 classNames .add (className );
211213 } catch (final Exception e ) {
212- LOG .error ("Failed to parse test class {}" , testPath , e );
214+ log .error ("Failed to parse test class {}" , testPath , e );
213215 exitCodeConsumer .accept (1 );
214216 }
215217 }
216218 return classNames ;
217219 }
218220
219- private static double getNewTestTime (
221+ private double getNewTestTime (
220222 final @ NotNull NewTestTimeOption useAverageTimeForNewTests ,
221223 final @ NotNull Set <TestCase > testCases ) {
222224 if (testCases .isEmpty ()) {
@@ -227,17 +229,17 @@ private static double getNewTestTime(
227229 case AVERAGE -> {
228230 final var averageTime =
229231 testCases .stream ().mapToDouble (TestCase ::time ).sum () / (double ) testCases .size ();
230- LOG .info ("Average test time is {}" , formatTime (averageTime ));
232+ log .info ("Average test time is {}" , formatTime (averageTime ));
231233 yield averageTime ;
232234 }
233235 case MIN -> {
234236 final var minTime = testCases .stream ().mapToDouble (TestCase ::time ).min ().orElseThrow ();
235- LOG .info ("Minimum test time is {}" , formatTime (minTime ));
237+ log .info ("Minimum test time is {}" , formatTime (minTime ));
236238 yield minTime ;
237239 }
238240 case MAX -> {
239241 final var maxTime = testCases .stream ().mapToDouble (TestCase ::time ).max ().orElseThrow ();
240- LOG .info ("Maximum test time is {}" , formatTime (maxTime ));
242+ log .info ("Maximum test time is {}" , formatTime (maxTime ));
241243 yield maxTime ;
242244 }
243245 };
0 commit comments