33import java .io .BufferedWriter ;
44import java .io .FileWriter ;
55import java .nio .file .Files ;
6- import java .nio .file .Path ;
76import java .nio .file .Paths ;
87import java .util .Map ;
98import java .util .regex .Matcher ;
1413import org .junit .platform .launcher .TestPlan ;
1514
1615public class InsertSourceFileExtension implements TestExecutionListener {
16+ @ Override
1717 public void executionStarted (TestIdentifier testIdentifier ) {
18- System .out .println ("EXECUTIONSTARTED ." );
18+ System .out .println (testIdentifier . getDisplayName () + " test started ." );
1919 }
2020
21+ @ Override
2122 public void testPlanExecutionStarted (TestPlan testPlan ) {
22- System .out .println ("TESTPLANEXECUTIONSTARTED." );
23+ // does not print when tested locally
24+ System .out .println ("---testPlanExecutionStarted---" );
2325 }
2426
27+ @ Override
2528 public void executionFinished (
2629 TestIdentifier testIdentifier , TestExecutionResult testExecutionResult ) {
27- // should this happen in testPlanExecutionFinished after all tests are run?
28-
30+ // get mapping of test classname to source file
2931 Map <String , String > sourceFiles = GatherSourceFileInfoExtension .getSourceFiles ();
3032
31- // for each test
33+ // for each test...
3234 for (String sourceFile : sourceFiles .keySet ()) {
33- String pathString =
34- Paths .get ("" ).toAbsolutePath () + "/build/test-results/test/TEST-" + sourceFile + ".xml" ;
35-
3635 // get xml report file
37- Path filePath = Paths .get (pathString );
36+ String filePath =
37+ Paths .get ("" ).toAbsolutePath () + "/build/test-results/test/TEST-" + sourceFile + ".xml" ;
3838 try {
39- String fileContent = new String (Files .readAllBytes (filePath ));
39+ String fileContent = new String (Files .readAllBytes (Paths . get ( filePath ) ));
4040
41- // modify report with test source file info
42- // use regex pattern to get class name
41+ // add test source file info to report
4342 Pattern pattern = Pattern .compile ("<testcase(.*?)classname=\" (.*?)\" (.*?)>" );
4443 Matcher matcher = pattern .matcher (fileContent );
4544 StringBuffer result = new StringBuffer ();
@@ -48,11 +47,7 @@ public void executionFinished(
4847 String className = matcher .group (2 );
4948 String endAttributes = matcher .group (3 );
5049
51- // add source file attribute
52- String fileAttribute = "" ;
53- if (sourceFiles .containsKey (className )) {
54- fileAttribute = " file=\" " + sourceFiles .get (className ) + "\" " ;
55- }
50+ String fileAttribute = " file=\" " + sourceFiles .getOrDefault (className , "" ) + "\" " ;
5651 String newTestCase =
5752 "<testcase"
5853 + begAttributes
@@ -64,24 +59,23 @@ public void executionFinished(
6459 + ">" ;
6560 matcher .appendReplacement (result , newTestCase );
6661 }
67- // add the rest
6862 matcher .appendTail (result );
6963
7064 // set old filePath to new xml result
71- System .out .println ("result: " + result .substring (0 , 1000 ));
72-
73- // i think this logic gets re-overwritten by xml reports
74- BufferedWriter writer = new BufferedWriter (new FileWriter (pathString ));
65+ // this logic must be wrong or go elsewhere bc its getting overwritten. `result` output
66+ // seems correct.
67+ BufferedWriter writer = new BufferedWriter (new FileWriter (filePath ));
7568 writer .write (result .toString ());
7669 writer .close ();
77-
7870 } catch (Exception e ) {
7971 System .out .println ("Modifying XML files did not work." );
8072 }
8173 }
8274 }
8375
76+ @ Override
8477 public void testPlanExecutionFinished (TestPlan testPlan ) {
85- System .out .println ("TESTPLANEXECUTIONFINISHED." );
78+ // does not print when tested locally
79+ System .out .println ("---testPlanExecutionFinished---." );
8680 }
8781}
0 commit comments