Package: TestCase

TestCase

nameinstructionbranchcomplexitylinemethod
TestCase()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
recordTestResults(String, String, boolean, ExitException, long)
M: 281 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 48 C: 0
0%
M: 1 C: 0
0%
reflectionRunTest(Value, Value)
M: 156 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 32 C: 0
0%
M: 1 C: 0
0%

Coverage

1: import org.overture.interpreter.runtime.*;
2: import org.overture.interpreter.values.*;
3: import org.w3c.dom.Document;
4: import org.w3c.dom.Element;
5: import org.w3c.dom.Node;
6: import org.w3c.dom.NodeList;
7: import org.xml.sax.SAXException;
8:
9: import javax.xml.parsers.DocumentBuilder;
10: import javax.xml.parsers.DocumentBuilderFactory;
11: import javax.xml.parsers.ParserConfigurationException;
12: import javax.xml.transform.*;
13: import javax.xml.transform.dom.DOMSource;
14: import javax.xml.transform.stream.StreamResult;
15: import javax.xml.xpath.*;
16: import java.io.*;
17:
18:
19: public class TestCase {
20: private final static String xmlReportTemplate = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\t";
21: private final static String xmlReportSuiteTemplate = "<testsuite xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd\" name=\"%s\" time=\"0.0\" tests=\"0\" errors=\"0\" skipped=\"0\" failures=\"0\"/>";
22: private final static String vdmUnitReportEnable = "vdm.unit.report";
23:
24: public static Value reflectionRunTest(Value obj, Value name)
25: throws Exception {
26: String methodName = name.toString().replaceAll("\"", "").trim();
27:
28: ObjectValue instance = (ObjectValue) obj;
29:• for (NameValuePair p : instance.members.asList()) {
30:• if (p.name.getName().equals(methodName)) {
31:• if (p.value instanceof OperationValue) {
32: OperationValue opVal = (OperationValue) p.value;
33: Context mainContext = new StateContext(Interpreter.getInstance().getAssistantFactory(), p.name.getLocation(), "reflection scope");
34:
35: mainContext.putAll(ClassInterpreter.getInstance().initialContext);
36: // mainContext.putAll(ClassInterpreter.getInstance().);
37: mainContext.setThreadState(ClassInterpreter.getInstance().initialContext.threadState.dbgp, ClassInterpreter.getInstance().initialContext.threadState.CPU);
38:
39: long timerStart = System.nanoTime();
40: boolean success = false;
41: ExitException error = null;
42: try {
43: opVal.eval(p.name.getLocation(), new ValueList(), mainContext);
44: success = true;
45: } catch (Exception e) {
46:• if (e instanceof ExitException) {
47:• if(((ExitException)e).value.objectValue(null).type.getName().equals("AssertionFailedError"))
48: {
49: success = false;
50: }
51: throw e;
52: }
53:
54: try {
55: return ClassInterpreter.getInstance().evaluate("Error`throw(\""
56: + e.getMessage().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"").replaceAll("\'", "\\\'")
57: + "\")", mainContext);
58: }catch(ExitException e2)
59: {
60: error = e2;
61: throw e2;
62: }
63: } finally {
64: long totalExecTime = System.nanoTime() - timerStart;
65:
66:• if (System.getProperty(vdmUnitReportEnable) != null) {
67:
68: String containerName = "";
69:
70:• if (obj instanceof ObjectValue) {
71: containerName = ((ObjectValue) obj).type.getName().getName();
72: }
73:
74: recordTestResults(containerName, methodName, success, error, totalExecTime);
75: }
76: }
77: }
78: }
79: }
80: return new VoidValue();
81:
82: }
83:
84: private static void recordTestResults(String containerName, String methodName, boolean success, ExitException error, long totalExecTime) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException, TransformerException {
85:
86: File report = new File("TEST-" + containerName + ".xml");
87:
88: DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
89: DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
90: Document doc = null;
91:
92:• if (report.exists()) {
93: doc = docBuilder.parse(report);
94:
95: } else {
96: doc = docBuilder.parse(new ByteArrayInputStream(String.format(xmlReportTemplate + xmlReportSuiteTemplate, containerName).getBytes()));
97: }
98:
99: XPathFactory xPathfactory = XPathFactory.newInstance();
100: XPath xpath = xPathfactory.newXPath();
101: XPathExpression expr = xpath.compile("/testsuite[@name='" + containerName + "']");
102:
103: NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
104:
105:• if (nl.getLength() > 0) {
106: Element testSuiteNode = (Element) nl.item(0);
107:
108: expr = xpath.compile("testcase[@name='" + methodName + "']");
109: NodeList nlT = (NodeList) expr.evaluate(testSuiteNode, XPathConstants.NODESET);
110: Element n = null;
111:
112:• if (nlT.getLength() > 0) {
113: n = (Element) nlT.item(0);
114: } else {
115: n = doc.createElement("testcase");
116: }
117:
118:• while(n.getFirstChild()!=null)
119: {
120: n.removeChild(n.getFirstChild());
121: }
122:
123: n.setAttribute("name", methodName);
124: n.setAttribute("classname", containerName);
125: n.setAttribute("time", totalExecTime * 1E-9 + "");
126: testSuiteNode.appendChild(n);
127:
128: testSuiteNode.setAttribute("tests", String.valueOf(Integer.parseInt(testSuiteNode.getAttribute("tests")) + 1));
129:
130:• if (error!=null) {
131: testSuiteNode.setAttribute("error", String.valueOf(Integer.parseInt(testSuiteNode.getAttribute("errors")) + 1));
132: Element errorElement = doc.createElement("error");
133: errorElement.setAttribute("message",error.number+"");
134: errorElement.setAttribute("type","ERROR");
135: StringWriter strOut = new StringWriter();
136: error.ctxt.printStackTrace(new PrintWriter(strOut),true);
137: errorElement.setTextContent(strOut.toString());
138: n.appendChild(errorElement);
139:• } else if (!success) {
140: testSuiteNode.setAttribute("failures", String.valueOf(Integer.parseInt(testSuiteNode.getAttribute("failures")) + 1));
141: Element failureElement = doc.createElement("failure");
142: failureElement.setAttribute("message",methodName);
143: failureElement.setAttribute("type","WARNING");
144: failureElement.setAttribute("time", totalExecTime * 1E-9 + "");
145: n.appendChild(failureElement);
146: }
147:
148: testSuiteNode.setAttribute("time", String.valueOf(Double.parseDouble(testSuiteNode.getAttribute("time")) + (totalExecTime * 1E-9)));
149:
150: }
151:
152: Transformer transformer = TransformerFactory.newInstance().newTransformer();
153: Result output = new StreamResult(report);
154: Source input = new DOMSource(doc);
155:
156: transformer.transform(input, output);
157:
158:
159: }
160: }