@@ -27,49 +27,51 @@ public void executionFinished(
2727
2828 Map <String , String > sourceFiles = TestSourceFileExtension .getSourceFiles ();
2929
30- // hard coded for now
31- // get xml report file
32- Path path =
33- Paths .get (
34- "/Users/sarah.chen/Source/github.com/DataDog/dd-trace-java/components/context/build/test-results/test/TEST-datadog.context.ContextKeyTest.xml" );
35- try {
36- String content = new String (Files .readAllBytes (path ));
30+ // for each test
31+ for (String sourceFile : sourceFiles .keySet ()) {
32+ String pathString =
33+ Paths .get ("" ).toAbsolutePath () + "/build/test-results/test/TEST-" + sourceFile + ".xml" ;
3734
38- // modify report with test source file info
39- // use regex pattern to get class name
40- Pattern pattern = Pattern .compile ("<testcase(.*?)classname=\" (.*?)\" (.*?)>" );
41- Matcher matcher = pattern .matcher (content );
42- StringBuffer result = new StringBuffer ();
43- while (matcher .find ()) {
44- String attributes = matcher .group (1 );
45- String className = matcher .group (2 );
46- String timeAttribute = matcher .group (3 );
35+ // get xml report file
36+ Path filePath = Paths .get (pathString );
37+ try {
38+ String fileContent = new String (Files .readAllBytes (filePath ));
4739
48- // add source file attribute
49- String fileAttribute = "" ;
50- if (sourceFiles .containsKey (className )) {
51- fileAttribute = " file=\" " + sourceFiles .get (className ) + "\" " ;
52- }
53- String newTestCase =
54- "<testcase"
55- + attributes
56- + "classname=\" "
57- + className
58- + "\" "
59- + fileAttribute
60- + timeAttribute
61- + ">" ;
62- matcher .appendReplacement (result , newTestCase );
63- }
64- // add the rest
65- matcher .appendTail (result );
40+ // modify report with test source file info
41+ // use regex pattern to get class name
42+ Pattern pattern = Pattern .compile ("<testcase(.*?)classname=\" (.*?)\" (.*?)>" );
43+ Matcher matcher = pattern .matcher (fileContent );
44+ StringBuffer result = new StringBuffer ();
45+ while (matcher .find ()) {
46+ String beg_attributes = matcher .group (1 );
47+ String className = matcher .group (2 );
48+ String end_attributes = matcher .group (3 );
6649
67- // set old path to new xml result
68- // System.out.println("result: " + result); shows that result is correct, but haven't figured
69- // out a way to overwrite the original path yet
50+ // add source file attribute
51+ String fileAttribute = "" ;
52+ if (sourceFiles .containsKey (className )) {
53+ fileAttribute = " file=\" " + sourceFiles .get (className ) + "\" " ;
54+ }
55+ String newTestCase =
56+ "<testcase"
57+ + beg_attributes
58+ + "classname=\" "
59+ + className
60+ + "\" "
61+ + fileAttribute
62+ + end_attributes
63+ + ">" ;
64+ matcher .appendReplacement (result , newTestCase );
65+ }
66+ // add the rest
67+ matcher .appendTail (result );
7068
71- } catch (Exception e ) {
72- System .out .println ("didnt work." );
69+ // set old filePath to new xml result
70+ // System.out.println("result: " + result);
71+ Files .write (filePath , result .toString ().getBytes ()); // does not work
72+ } catch (Exception e ) {
73+ System .out .println ("Modifying XML files did not work." );
74+ }
7375 }
7476 }
7577
0 commit comments