Project

General

Profile

JunitGenerator.java

Chris Weaver, 08/18/2022 09:39 AM

Download (4.5 KB)

 
1
package com.goldencode.harness;
2

    
3
import com.goldencode.harness.test.Test;
4
import java.io.File;
5
import java.util.List;
6
import javax.xml.parsers.DocumentBuilder;
7
import javax.xml.parsers.DocumentBuilderFactory;
8
import javax.xml.transform.Transformer;
9
import javax.xml.transform.TransformerFactory;
10
import javax.xml.transform.dom.DOMSource;
11
import javax.xml.transform.stream.StreamResult;
12
import org.w3c.dom.Attr;
13
import org.w3c.dom.Document;
14
import org.w3c.dom.Element;
15

    
16
public class JunitGenerator {
17

    
18
   /** Test plan for which to generate a report. */
19
   private final TestPlan testPlan;
20

    
21
   /** Object in which to store the details of failures. */
22
   private final FailureNotification rc;
23

    
24
   /**
25
    * Create an instance.
26
    *
27
    * @param    testPlan
28
    *           The test plan for which to generate a report.
29
    * @param    rc
30
    *           Object in which to store the details of failures.
31
    */
32
   public JunitGenerator(TestPlan testPlan, FailureNotification rc)
33
   {
34
      this.testPlan = testPlan;
35
      this.rc       = rc;
36
   }
37

    
38
   public void generate() {
39
      String outputFileName = "results_" + testPlan.getSourceFileName();
40
      
41
      try {
42
         DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
43
         DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
44
         Document document = documentBuilder.newDocument();
45

    
46
         // root element
47
         Element testSuite = document.createElement("testsuite");
48
         document.appendChild(testSuite);
49

    
50
         int total = 0, passed = 0, failed = 0, skipped = 0;
51
         double totalDuration = 0;
52

    
53
         List<TestSet> sets = testPlan.getSets();
54
         for (TestSet set : sets) {
55
            List<Test> tests = set.getTests();
56
            for (Test test : tests) {
57
               total++;
58

    
59
               Element testCase = document.createElement("testcase");
60

    
61
               Attr cName = document.createAttribute("classname");
62
               cName.setValue(test.getName());
63
               testCase.setAttributeNode(cName);
64

    
65
               Attr nName = document.createAttribute("name");
66
               nName.setValue(test.getDescription());
67
               testCase.setAttributeNode(nName);
68

    
69
               Attr tName = document.createAttribute("time");
70
               totalDuration += test.getElapsed();
71
               tName.setValue(String.valueOf(((double) test.getElapsed()) / 1000));
72
               testCase.setAttributeNode(tName);
73

    
74
               switch (test.getStatus()) {
75
                  case FAILED:
76
                     if(!test.getFailureMode().equals(FailureMode.SILENT)) {
77
                        Element failure = document.createElement("failure");
78

    
79
                        Attr failType = document.createAttribute("type");
80
                        failType.setValue(test.getReason().substring(0, 10));
81
                        failure.setAttributeNode(failType);
82
                        failure.appendChild(document.createTextNode(test.getReason()));
83

    
84
                        testCase.appendChild(failure);
85

    
86
                        failed++;
87
                     }
88
                  case NOT_RUN:
89
                     skipped++;
90
                     break;
91
               }
92

    
93
               testSuite.appendChild(testCase);
94
            }
95
         }
96

    
97
         Attr testsTotal = document.createAttribute("tests");
98
         testsTotal.setValue(String.valueOf(total));
99
         testSuite.setAttributeNode(testsTotal);
100

    
101
         Attr testsFail = document.createAttribute("failures");
102
         testsFail.setValue(String.valueOf(failed));
103
         testSuite.setAttributeNode(testsFail);
104

    
105
         Attr testsSkip = document.createAttribute("skipped");
106
         testsSkip.setValue(String.valueOf(skipped));
107
         testSuite.setAttributeNode(testsSkip);
108

    
109
         Attr testsDuration = document.createAttribute("time");
110
         testsDuration.setValue(String.valueOf(totalDuration / 1000));
111
         testSuite.setAttributeNode(testsDuration);
112

    
113
         TransformerFactory transformerFactory = TransformerFactory.newInstance();
114
         Transformer transformer = transformerFactory.newTransformer();
115
         DOMSource domSource = new DOMSource(document);
116
         StreamResult streamResult = new StreamResult(new File(outputFileName));
117

    
118
         transformer.transform(domSource, streamResult);
119

    
120
      }catch (Exception ex) {
121
         rc.setResult(ExitCode.UNKNOWN_FAILURE);
122
         rc.setDetails(outputFileName);
123
         return;
124
      }
125
   }
126
}