15
15
import gherkin .formatter .model .Step ;
16
16
import org .w3c .dom .Document ;
17
17
import org .w3c .dom .Element ;
18
+ import org .w3c .dom .Node ;
18
19
19
20
import javax .xml .parsers .DocumentBuilderFactory ;
20
21
import javax .xml .parsers .ParserConfigurationException ;
@@ -41,6 +42,7 @@ class JUnitFormatter implements Formatter, Reporter {
41
42
private final Element rootElement ;
42
43
43
44
private TestCase testCase ;
45
+ private Element root ;
44
46
45
47
public JUnitFormatter (URL out ) throws IOException {
46
48
this .out = new UTF8OutputStreamWriter (new URLOutputStream (out ));
@@ -61,15 +63,19 @@ public void feature(Feature feature) {
61
63
@ Override
62
64
public void background (Background background ) {
63
65
testCase = new TestCase ();
66
+ root = testCase .createElement (doc );
64
67
}
65
68
66
69
@ Override
67
70
public void scenario (Scenario scenario ) {
68
- if (testCase != null ) {
71
+ if (testCase != null && testCase . scenario == null ) {
69
72
testCase .scenario = scenario ;
70
73
} else {
71
74
testCase = new TestCase (scenario );
75
+ root = testCase .createElement (doc );
72
76
}
77
+ testCase .writeElement (doc , root );
78
+ rootElement .appendChild (root );
73
79
74
80
increaseAttributeValue (rootElement , "tests" );
75
81
}
@@ -99,10 +105,7 @@ public void done() {
99
105
public void result (Result result ) {
100
106
testCase .results .add (result );
101
107
102
- if (testCase .scenario != null && testCase .results .size () == testCase .steps .size ()) {
103
- rootElement .appendChild (testCase .writeTo (doc ));
104
- testCase = null ;
105
- }
108
+ testCase .updateElement (doc , root );
106
109
}
107
110
108
111
@ Override
@@ -187,10 +190,16 @@ private TestCase() {
187
190
final List <Step > steps = new ArrayList <Step >();
188
191
final List <Result > results = new ArrayList <Result >();
189
192
190
- private Element writeTo (Document doc ) {
191
- Element tc = doc .createElement ("testcase" );
193
+ private Element createElement (Document doc ) {
194
+ return doc .createElement ("testcase" );
195
+ }
196
+
197
+ private void writeElement (Document doc , Element tc ) {
192
198
tc .setAttribute ("classname" , feature .getName ());
193
199
tc .setAttribute ("name" , examples > 0 ? scenario .getName () + "_" + examples -- : scenario .getName ());
200
+ }
201
+
202
+ public void updateElement (Document doc , Element tc ) {
194
203
long totalDurationNanos = 0 ;
195
204
for (Result r : results ) {
196
205
totalDurationNanos += r .getDuration () == null ? 0 : r .getDuration ();
@@ -229,9 +238,15 @@ private Element writeTo(Document doc) {
229
238
child = doc .createElement ("system-out" );
230
239
child .appendChild (doc .createCDATASection (sb .toString ()));
231
240
}
232
- tc .appendChild (child );
233
- return tc ;
241
+
242
+ Node existingChild = tc .getFirstChild ();
243
+ if (existingChild == null ) {
244
+ tc .appendChild (child );
245
+ } else {
246
+ tc .replaceChild (child , existingChild );
247
+ }
234
248
}
249
+
235
250
}
236
251
237
252
}
0 commit comments