11package com .reajason .javaweb ;
22
3+ import lombok .Builder ;
4+ import lombok .Data ;
35import lombok .SneakyThrows ;
46import org .junit .platform .engine .TestExecutionResult ;
57import org .junit .platform .engine .UniqueId ;
1416import java .time .Duration ;
1517import java .time .Instant ;
1618import java .util .ArrayList ;
17- import java .util .HashMap ;
1819import java .util .List ;
1920import java .util .Map ;
21+ import java .util .concurrent .ConcurrentHashMap ;
22+ import java .util .concurrent .atomic .AtomicLong ;
23+ import java .util .stream .Collectors ;
2024
2125/**
2226 * @author ReaJason
2327 * @since 2024/12/1
2428 */
2529public class MarkdownTestExecutionListener implements TestExecutionListener {
30+ @ Data
31+ @ Builder
32+ static class TestCase {
33+ private String imageName ;
34+ private String shellType ;
35+ private String packer ;
36+ private Duration duration ;
37+ private TestExecutionResult .Status status ;
38+ }
2639
27- private final Map <UniqueId , Instant > timeStamps = new HashMap <>();
40+ private final Map <UniqueId , Instant > timeStamps = new ConcurrentHashMap <>();
41+ private final Map <String , List <TestCase >> testCases = new ConcurrentHashMap <>();
2842 private Instant startTime ;
29- private final Path markdownPath = Paths .get ("build" , "test-results" , "result .md" );
30- private final List < String > passedResults = new ArrayList <>( );
31- private final List < String > failedResults = new ArrayList <>( );
43+ private final Path markdownPath = Paths .get ("build" , "test-results" , "report .md" );
44+ private final AtomicLong totalCount = new AtomicLong ( 0 );
45+ private final AtomicLong successCount = new AtomicLong ( 0 );
3246
3347 @ SneakyThrows
3448 @ Override
@@ -37,7 +51,7 @@ public void testPlanExecutionStarted(TestPlan testPlan) {
3751 ArrayList <String > lines = new ArrayList <>();
3852 lines .add ("## Integration Test" );
3953 startTime = Instant .now ();
40- lines .add ("- Started At : " + startTime );
54+ lines .add ("- Started: " + startTime );
4155 Files .write (markdownPath , lines , StandardOpenOption .CREATE_NEW );
4256 }
4357
@@ -46,36 +60,75 @@ public void testPlanExecutionStarted(TestPlan testPlan) {
4660 public void testPlanExecutionFinished (TestPlan testPlan ) {
4761 List <String > lines = new ArrayList <>();
4862 Instant endTime = Instant .now ();
49- lines .add ("- Finished At: " + endTime );
50- lines .add ("- Total Duration: " + Duration .between (startTime , endTime ).getSeconds () + " seconds" );
63+ lines .add ("- Finished: " + endTime );
64+ Duration duration = Duration .between (startTime , endTime );
65+ lines .add ("- Total Duration: " + getDuration (duration ));
66+ lines .add (String .format ("- Total Cases: %d/%d, %d failed" , successCount .get (), totalCount .get (), totalCount .get () - successCount .get ()));
5167 lines .add ("" );
52- lines .add ("| **Image Name** | **Shell Type** | **Packer** | **Status**| **Duration(ms)** |" );
53- lines .add ("|----------------|----------------|------------|-----------|------------------|" );
54- lines .addAll (failedResults );
55- lines .addAll (passedResults );
68+
69+ for (Map .Entry <String , List <TestCase >> entry : testCases .entrySet ()) {
70+ lines .add (String .format ("### %s" , entry .getKey ()));
71+ lines .add ("|**Shell Type** | **Packer** | **Status**| **Duration(ms)** |" );
72+ lines .add ("|---------------|------------|-----------|------------------|" );
73+ List <TestCase > value = entry .getValue ().stream ().sorted ((tc1 , tc2 ) -> {
74+ if (tc1 .getStatus () == tc2 .getStatus ()) {
75+ return 0 ;
76+ }
77+ return tc1 .getStatus () == TestExecutionResult .Status .FAILED ? -1 : 1 ;
78+ }).collect (Collectors .toList ());
79+ for (TestCase testCase : value ) {
80+ String status = testCase .getStatus ().equals (TestExecutionResult .Status .SUCCESSFUL ) ? "✔" : "✘" ;
81+ lines .add ("|" + testCase .getShellType () + "|" + testCase .getPacker () + "|" + status + "|" + testCase .getDuration ().toMillis () + "|" );
82+ }
83+ }
84+
5685 Files .write (markdownPath , lines , StandardOpenOption .APPEND );
5786 }
5887
88+ private String getDuration (Duration duration ) {
89+ long totalSeconds = duration .getSeconds ();
90+ long minutes = totalSeconds / 60 ;
91+ long seconds = totalSeconds % 60 ;
92+ if (totalSeconds < 60 ) {
93+ return String .format ("%d seconds" , totalSeconds );
94+ } else {
95+ return String .format ("%d minutes and %d seconds" , minutes , seconds );
96+ }
97+ }
98+
5999 @ Override
60100 public void executionFinished (TestIdentifier testIdentifier , TestExecutionResult testExecutionResult ) {
61- if (testIdentifier .isTest ()) {
62- Instant startTime = timeStamps .get (testIdentifier .getUniqueIdObject ());
63- if (startTime != null ) {
64- String [] split = testIdentifier .getDisplayName ().split ("\\ |" );
65- if (split .length == 3 ) {
66- if (testExecutionResult .getStatus ().equals (TestExecutionResult .Status .SUCCESSFUL )) {
67- passedResults .add ("|" + split [0 ].trim () + "|" + split [1 ].trim () + "|" + split [2 ].trim () + "|✔|" + Duration .between (startTime , Instant .now ()).toMillis () + "|" );
68- } else {
69- failedResults .add ("|" + split [0 ].trim () + "|" + split [1 ].trim () + "|" + split [2 ].trim () + "|✘|" + Duration .between (startTime , Instant .now ()).toMillis () + "|" );
70- }
71- }
101+ if (!testIdentifier .isTest ()) {
102+ return ;
103+ }
104+ Instant startTime = timeStamps .get (testIdentifier .getUniqueIdObject ());
105+ if (startTime == null ) {
106+ return ;
107+ }
108+ String [] split = testIdentifier .getDisplayName ().split ("\\ |" );
109+ if (split .length == 3 ) {
110+ String imageName = split [0 ].trim ();
111+ String shellType = split [1 ].trim ();
112+ String packer = split [2 ].trim ();
113+ List <TestCase > cases = testCases .getOrDefault (imageName , new ArrayList <>());
114+ cases .add (TestCase .builder ()
115+ .imageName (imageName )
116+ .shellType (shellType )
117+ .packer (packer )
118+ .duration (Duration .between (startTime , Instant .now ()))
119+ .status (testExecutionResult .getStatus ())
120+ .build ());
121+ testCases .put (imageName , cases );
122+ if (testExecutionResult .getStatus ().equals (TestExecutionResult .Status .SUCCESSFUL )) {
123+ successCount .incrementAndGet ();
72124 }
73125 }
74126 }
75127
76128 @ Override
77129 public void executionStarted (TestIdentifier testIdentifier ) {
78130 if (testIdentifier .isTest ()) {
131+ totalCount .incrementAndGet ();
79132 timeStamps .put (testIdentifier .getUniqueIdObject (), Instant .now ());
80133 }
81134 }
0 commit comments