blob: d5902aecde696fbb5bd3b4b6f51759b6d673a56f (
plain)
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
|
package cz.crcs.ectester.reader.output;
import cz.crcs.ectester.reader.Util;
import cz.crcs.ectester.reader.response.Response;
import cz.crcs.ectester.reader.test.Test;
import cz.crcs.ectester.reader.test.TestSuite;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.OutputStream;
/**
* @author Jan Jancar johny@neuromancer.sk
*/
public class XMLTestWriter implements TestWriter {
private OutputStream output;
private DocumentBuilder db;
private Document doc;
private Node root;
public XMLTestWriter(OutputStream output) throws ParserConfigurationException {
this.output = output;
this.db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}
@Override
public void begin(TestSuite suite) {
doc = db.newDocument();
Element rootElem = doc.createElement("testSuite");
rootElem.setAttribute("name", suite.getName());
rootElem.setAttribute("desc", suite.getDescription());
root = rootElem;
doc.appendChild(root);
}
private Element responseElement(Response r) {
Element responseElem = doc.createElement("response");
responseElem.setAttribute("successful", r.successful() ? "true" : "false");
Element apdu = doc.createElement("apdu");
apdu.setTextContent(Util.bytesToHex(r.getAPDU().getBytes()));
responseElem.appendChild(apdu);
Element naturalSW = doc.createElement("natural-sw");
naturalSW.setTextContent(String.valueOf(r.getNaturalSW()));
responseElem.appendChild(naturalSW);
Element sws = doc.createElement("sws");
for (int i = 0; i < r.getNumSW(); ++i) {
Element sw = doc.createElement("sw");
sw.setTextContent(String.valueOf(r.getSW(i)));
sws.appendChild(sw);
}
responseElem.appendChild(sws);
Element duration = doc.createElement("duration");
duration.setTextContent(String.valueOf(r.getDuration()));
responseElem.appendChild(duration);
Element description = doc.createElement("desc");
description.setTextContent(r.getDescription());
responseElem.appendChild(description);
return responseElem;
}
private Element testElement(Test t) {
Element testElem = doc.createElement("test");
if (t instanceof Test.Simple) {
Test.Simple test = (Test.Simple) t;
testElem.setAttribute("type", "simple");
testElem.appendChild(responseElement(test.getResponse()));
} else if (t instanceof Test.Compound) {
Test.Compound test = (Test.Compound) t;
testElem.setAttribute("type", "compound");
for (Test innerTest : test.getTests()) {
testElem.appendChild(testElement(innerTest));
}
}
Element description = doc.createElement("desc");
description.setTextContent(t.getDescription());
testElem.appendChild(description);
Element result = doc.createElement("result");
Element value = doc.createElement("value");
value.setTextContent(t.getResultValue().name());
Element cause = doc.createElement("cause");
cause.setTextContent(t.getResultCause());
result.appendChild(value);
result.appendChild(cause);
testElem.appendChild(result);
return testElem;
}
@Override
public void outputTest(Test t) {
if (!t.hasRun())
return;
root.appendChild(testElement(t));
}
@Override
public void end() {
try {
DOMSource domSource = new DOMSource(doc);
StreamResult result = new StreamResult(output);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(domSource, result);
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
|