/
JUnitXMLTestResult.cfc
143 lines (111 loc) · 5.16 KB
/
JUnitXMLTestResult.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!---
Extension of TestResult
JUnit style XML representation of MXUnit TestResult.
TODO Get pacakge for testresult name ex: mxunit.tests.framework for any in that package
--->
<cfcomponent extends="TestResult" displayname="JUnitXMLTestResult" output="false">
<cfparam name="this.testRuns" type="numeric" default="0" />
<cfparam name="this.failures" type="numeric" default="0" />
<cfparam name="this.errors" type="numeric" default="0" />
<cfparam name="this.successes" type="numeric" default="0" />
<cfparam name="this.totalExecutionTime" type="numeric" default="0" />
<cfparam name="this.resultsXML" type="string" default='' />
<cfparam name="tempTestCase" type="any" default="" />
<cfparam name="tempTestComponent" type="any" default="" />
<cfparam name="this.closeCalls" default="0" type="numeric" />
<cfparam name="this.name" default="mxunit.tests" type="string" />
<!---
Constructor
--->
<cffunction name="JUnitXMLTestResult" access="public" returntype="component">
<cfargument name="testResults" type="component" required="false" />
<cfset this.testRuns = arguments.testResults.testRuns />
<cfset this.failures = arguments.testResults.testFailures />
<cfset this.errors = arguments.testResults.testErrors />
<cfset this.successes = arguments.testResults.testSuccesses />
<cfset this.totalExecutionTime = arguments.testResults.totalExecutionTime />
<cfset this.name = arguments.testResults.getPackage() />
<cfset buildXmlresults(arguments.testResults.getResults()) />
<cfreturn this />
</cffunction>
<cffunction name="generateStacktrace" access="private" returntype="string" output="false">
<cfargument name="catchResult" type="any" required="true" />
<cfset var i = '' />
<cfset var context = '' />
<cfset var stacktrace = '' />
<!--- Prime the stacktrace --->
<cfset stacktrace = arguments.catchResult.message />
<cfloop from="1" to="#arrayLen(arguments.catchResult.tagContext)#" index="i">
<cfset context = arguments.catchResult.tagContext[i] />
<cfset stacktrace = stacktrace & ' at ' & context.template & ':' & context.line />
</cfloop>
<cfreturn stacktrace />
</cffunction>
<!---
Returns an XML Dom representation of a TestResult
--->
<cffunction name="getXMLDomResults" access="public" returntype="xml" output="false">
<cfset var dom= "" />
<cfset dom = xmlParse(this.resultsXML) />
<cfreturn dom />
</cffunction>
<!---
Returns an XML string representation of a TestResult
--->
<cffunction name="getXMLResults" access="public" returntype="string" output="false">
<cfset var dom= "" />
<cfset dom = this.resultsXML />
<cfreturn dom />
</cffunction>
<!---
Converts the TestResult into the xml representation
--->
<cffunction name="buildXmlResults" access="public" output="false">
<cfargument name="results" type="array" required="true" />
<cfscript>
var i = "";
this.resultsXML = this.resultsXML & '<testsuite name="#this.name#" hostname="#cgi.remote_host#" tests="#this.testRuns#" failures="#this.failures#" errors="#this.errors#" timestamp="#dateFormat(now(),"mm/dd/yy")# #timeFormat(now(),"medium")#" time="#this.totalExecutionTime/1000#">';
this.resultsXML = this.resultsXML & '<properties>';
this.resultsXML = this.resultsXML & genProps(server.coldfusion);
this.resultsXML = this.resultsXML & genProps(server.os);
this.resultsXML = this.resultsXML & genProps(cgi);
this.resultsXML = this.resultsXML & genProps(cookie);
this.resultsXML = this.resultsXML & genProps(request);
if(isDefined("application")) {
this.resultsXML = this.resultsXML & genProps(application);
}
//JUnitReport XSL transformation doesn't like some of the CGI stuff, like query string &
//CGI props should not be as important at sever level properties
this.resultsXML = this.resultsXML & '</properties>';
for(i = 1; i lte arrayLen(arguments.results); i = i + 1){
testResults = arguments.results[i];
this.resultsXML = this.resultsXML & '<testcase classname="#testResults.component#" name="#testResults.testname#" time="#testResults.time/1000#">';
if( listFindNoCase("Failed",testResults.testStatus)){
this.resultsXML = this.resultsXML & '<failure message="#xmlformat(testResults.error.message)#"><![CDATA[#generateStacktrace(testResults.error)#]]></failure>';
} else if( listFindNoCase("Error",testResults.testStatus)) {
this.resultsXML = this.resultsXML & '<error message="#testResults.error.type#"><![CDATA[#generateStacktrace(testResults.error)#]]></error>';
}
this.resultsXML = this.resultsXML & '</testcase>';
}
this.resultsXML = this.resultsXML & '</testsuite>';
</cfscript>
</cffunction>
<!---
Util
Generates JUnitReport style XML properties
--->
<cffunction name="genProps" access="private" output="false">
<cfargument name="collection" type="struct" />
<cfscript>
var properties = '';
for(prop in collection){
if( isSimpleValue(collection[prop]) ){
properties = properties & '<property name="#ucase(prop)#" value="#xmlFormat(collection[prop])#" />';
}else{
properties = properties & '<property name="#ucase(prop)#" value="Complex Data Type...Not Displaying" />';
}
}
return properties;
</cfscript>
</cffunction>
</cfcomponent>