11
11
import java .nio .file .attribute .PosixFilePermission ;
12
12
import java .util .ArrayList ;
13
13
import java .util .Set ;
14
+ import java .util .concurrent .locks .Lock ;
15
+ import java .util .concurrent .locks .ReentrantLock ;
14
16
15
17
import javax .xml .parsers .DocumentBuilder ;
16
18
import javax .xml .parsers .DocumentBuilderFactory ;
@@ -44,6 +46,12 @@ public class JunitXmlReportWriter
44
46
45
47
private Long startSeconds = null ;
46
48
49
+ /**
50
+ * The DocResource class does a file lock preventing concurrent access from other processes, but we still need
51
+ * to prevent concurrent access of the document in *THIS* class.
52
+ */
53
+ private static Lock lock = new ReentrantLock ();
54
+
47
55
public void init (String junitReport ) {
48
56
49
57
if (junitReport == null ) {
@@ -70,6 +78,7 @@ public void updateJunitReport(String totals,
70
78
return ;
71
79
}
72
80
81
+ lock .lock ();
73
82
try (DocResource docResource = new DocResource (pathToReport )) {
74
83
Document doc = docResource .getDocument ();
75
84
@@ -94,20 +103,27 @@ public void updateJunitReport(String totals,
94
103
catch (IOException e ) {
95
104
logger .error ("Exception writing log: " + e );
96
105
}
106
+ finally {
107
+ lock .unlock ();
108
+ }
97
109
}
98
110
99
111
public void writeXmlReport (String pathToReport ) throws Exception {
100
112
if (skip ) {
101
113
return ;
102
114
}
103
115
116
+ lock .lock ();
104
117
try (DocResource docResource = new DocResource (pathToReport )) {
105
118
Document doc = docResource .getDocument ();
106
119
String testCount = getTotalOfElementsByName (doc , "testcase" ).toString ();
107
120
modifyElementAttribute (doc , "testsuites" , 0 , "tests" , testCount );
108
121
String failureCount = getTotalOfElementsByName (doc , "failure" ).toString ();
109
122
modifyElementAttribute (doc , "testsuites" , 0 , "failures" , failureCount );
110
123
}
124
+ finally {
125
+ lock .unlock ();
126
+ }
111
127
}
112
128
113
129
private Element addChildElement (Document doc , Element parent , String name , String data ) {
0 commit comments