55import com .github .javaparser .ast .body .ClassOrInterfaceDeclaration ;
66import com .github .javaparser .ast .expr .AnnotationExpr ;
77import com .github .javaparser .ast .nodeTypes .NodeWithName ;
8- import de .donnerbart .split .model .Split ;
8+ import de .donnerbart .split .model .Splits ;
99import de .donnerbart .split .model .TestCase ;
1010import de .donnerbart .split .model .TestSuite ;
1111import org .jetbrains .annotations .NotNull ;
2020import java .nio .file .Path ;
2121import java .nio .file .SimpleFileVisitor ;
2222import java .nio .file .attribute .BasicFileAttributes ;
23- import java .util .ArrayList ;
2423import java .util .Comparator ;
2524import java .util .HashSet ;
26- import java .util .List ;
2725import java .util .Set ;
2826import java .util .function .Consumer ;
29- import java .util .stream .Collectors ;
3027
3128import static de .donnerbart .split .util .FormatUtil .formatTime ;
3229
@@ -38,7 +35,6 @@ public class TestSplit {
3835
3936 private static final @ NotNull Logger LOG = LoggerFactory .getLogger (TestSplit .class );
4037
41- private final int splitIndex ;
4238 private final int splitTotal ;
4339 private final @ NotNull String glob ;
4440 private final @ Nullable String excludeGlob ;
@@ -50,7 +46,6 @@ public class TestSplit {
5046 private final @ NotNull Consumer <Integer > exitCodeConsumer ;
5147
5248 public TestSplit (
53- final int splitIndex ,
5449 final int splitTotal ,
5550 final @ NotNull String glob ,
5651 final @ Nullable String excludeGlob ,
@@ -60,7 +55,6 @@ public TestSplit(
6055 final @ NotNull Path workingDirectory ,
6156 final boolean debug ,
6257 final @ NotNull Consumer <Integer > exitCodeConsumer ) {
63- this .splitIndex = splitIndex ;
6458 this .splitTotal = splitTotal ;
6559 this .glob = glob ;
6660 this .excludeGlob = excludeGlob ;
@@ -72,17 +66,7 @@ public TestSplit(
7266 this .exitCodeConsumer = exitCodeConsumer ;
7367 }
7468
75- public @ NotNull List <String > run () throws Exception {
76- LOG .info ("Split index {} (total: {})" , splitIndex , splitTotal );
77- LOG .info ("Working directory: {}" , workingDirectory );
78- LOG .info ("Glob: {}" , glob );
79- if (excludeGlob != null ) {
80- LOG .info ("Exclude glob: {}" , excludeGlob );
81- }
82- if (junitGlob != null ) {
83- LOG .info ("JUnit glob: {}" , junitGlob );
84- }
85- LOG .info ("Output format: {}" , formatOption );
69+ public @ NotNull Splits run () throws Exception {
8670 final var testPaths = getPaths (workingDirectory , glob , excludeGlob );
8771 final var classNames = fileToClassName (testPaths , exitCodeConsumer );
8872 if (classNames .isEmpty ()) {
@@ -134,25 +118,20 @@ public TestSplit(
134118
135119 // split tests
136120 LOG .debug ("Splitting {} tests" , testCases .size ());
137- final var splits = new ArrayList <Split >(splitTotal );
138- for (int i = 0 ; i < splitTotal ; i ++) {
139- splits .add (new Split (i ));
140- }
141- testCases .stream ()
142- .sorted (Comparator .reverseOrder ())
143- .forEach (testCase -> splits .stream ().sorted ().findFirst ().ifPresent (split -> {
144- LOG .debug ("Adding test {} to split #{}" , testCase .name (), split .index ());
145- split .add (testCase );
146- }));
121+ final var splits = new Splits (splitTotal , formatOption );
122+ testCases .stream ().sorted (Comparator .reverseOrder ()).forEach (testCase -> {
123+ final var split = splits .add (testCase );
124+ LOG .debug ("Adding test {} to split #{}" , testCase .name (), split .index ());
125+ });
147126
148127 if (debug ) {
149128 if (splitTotal > 1 ) {
150- final var fastestSplit = splits .stream (). min ( Comparator . naturalOrder ()). orElseThrow ();
129+ final var fastestSplit = splits .getFastest ();
151130 LOG .debug ("Fastest test plan is #{} with {} tests ({})" ,
152131 fastestSplit .formatIndex (),
153132 fastestSplit .tests ().size (),
154133 formatTime (fastestSplit .totalRecordedTime ()));
155- final var slowestSplit = splits .stream (). max ( Comparator . naturalOrder ()). orElseThrow ();
134+ final var slowestSplit = splits .getSlowest ();
156135 LOG .debug ("Slowest test plan is #{} with {} tests ({})" ,
157136 slowestSplit .formatIndex (),
158137 slowestSplit .tests ().size (),
@@ -161,19 +140,9 @@ public TestSplit(
161140 formatTime (slowestSplit .totalRecordedTime () - fastestSplit .totalRecordedTime ()));
162141 }
163142 LOG .debug ("Test splits:" );
164- splits .stream (). sorted ( Comparator . reverseOrder ()). forEach (n -> LOG .debug (n .toString ()));
143+ splits .forEach (split -> LOG .debug (split .toString ()));
165144 }
166- final var split = splits .get (splitIndex );
167- LOG .info ("This test split has {} tests ({})" , split .tests ().size (), formatTime (split .totalRecordedTime ()));
168- return split .tests ()
169- .stream ()
170- .sorted (Comparator .reverseOrder ())
171- .map (TestCase ::name )
172- .map (test -> switch (formatOption ) {
173- case LIST -> test ;
174- case GRADLE -> "--tests " + test ;
175- })
176- .collect (Collectors .toList ());
145+ return splits ;
177146 }
178147
179148 private static @ NotNull Set <Path > getPaths (
0 commit comments