|
9 | 9 | import main.model.dto.project.TestSuiteDto;
|
10 | 10 | import org.xml.sax.Attributes;
|
11 | 11 |
|
12 |
| -import java.util.ArrayList; |
13 |
| -import java.util.Calendar; |
14 |
| -import java.util.Date; |
15 |
| -import java.util.List; |
| 12 | +import java.util.*; |
| 13 | +import java.util.function.Function; |
16 | 14 |
|
17 | 15 | import static main.model.db.imports.ResultStatus.*;
|
18 | 16 |
|
19 | 17 | public class JavaJUnitTestNGHandler extends Handler {
|
20 | 18 | private TestSuiteDto testSuite = new TestSuiteDto();
|
21 | 19 | private List<TestResultDto> results = new ArrayList<>();
|
22 |
| - private TestResultDto result = new TestResultDto(); |
| 20 | + private TestResultDto result; |
23 | 21 | private List<TestDto> tests = new ArrayList<>();
|
24 | 22 | private TestDto test = new TestDto();
|
25 | 23 | private String currentElement = "";
|
26 | 24 | private Calendar calendar = Calendar.getInstance();
|
27 | 25 | private Date currentTimeSlot;
|
28 | 26 | private TestNameNodeType testNameNodeType;
|
29 | 27 |
|
| 28 | + private static final String BLANK_RESULT = "$blank"; |
| 29 | + |
30 | 30 | public JavaJUnitTestNGHandler(TestNameNodeType testNameNodeType, Date finishTime) throws AqualityException {
|
31 | 31 | super();
|
32 | 32 | this.testNameNodeType = testNameNodeType;
|
33 |
| - result.setFail_reason("$blank"); |
34 | 33 | testRun.setFinish_time(finishTime);
|
| 34 | + initEmptyResult(); |
35 | 35 | }
|
36 | 36 |
|
37 | 37 | @Override
|
38 |
| - public void startDocument(){ |
| 38 | + public void startDocument() { |
| 39 | + // there are no needs to do something on this stages |
39 | 40 | }
|
40 | 41 |
|
41 | 42 | @Override
|
42 | 43 | public void endDocument() {
|
| 44 | + // there are no needs to do something on this stages |
43 | 45 | }
|
44 | 46 |
|
45 | 47 | @Override
|
46 |
| - public void startElement(String uri, String localName, String qName, Attributes attributes){ |
| 48 | + public void startElement(String uri, String localName, String qName, Attributes attributes) { |
47 | 49 | currentElement = qName;
|
48 |
| - if(qName.equals("testsuite")){ |
| 50 | + if (qName.equals("testsuite")) { |
49 | 51 | calendar.setTime(testRun.getFinish_time());
|
50 |
| - long longTime = Math.round(Double.parseDouble(attributes.getValue("time").replaceAll(",","").split("\\.")[0])); |
| 52 | + long longTime = Math.round(parseTime(attributes, Double::parseDouble)); |
51 | 53 | calendar.add(Calendar.SECOND, -(int) longTime);
|
52 | 54 | testRun.setStart_time(calendar.getTime());
|
53 | 55 | currentTimeSlot = testRun.getFinish_time();
|
54 |
| - } else if(qName.equals("property") && attributes.getValue("name").equals("suite")){ |
| 56 | + } else if (qName.equals("property") && attributes.getValue("name").equals("suite")) { |
55 | 57 | testSuite.setName(attributes.getValue("value"));
|
56 |
| - } else if(qName.equals("testcase")) { |
57 |
| - if(testNameNodeType.equals(TestNameNodeType.className)){ |
58 |
| - test.setName(attributes.getValue("classname")); |
59 |
| - }else if(testNameNodeType.equals(TestNameNodeType.testName)){ |
60 |
| - test.setName(attributes.getValue("name")); |
61 |
| - } |
62 |
| - |
| 58 | + } else if (qName.equals("testcase")) { |
| 59 | + String name = getTestName(testNameNodeType, attributes); |
| 60 | + test.setName(name); |
63 | 61 | result.setFinal_result_id(PASSED.getValue());
|
64 | 62 | result.setFinish_date(currentTimeSlot);
|
65 | 63 | calendar.setTime(currentTimeSlot);
|
66 |
| - calendar.add(Calendar.SECOND, -Integer.parseInt(attributes.getValue("time").replaceAll(",","").split("\\.")[0])); |
| 64 | + calendar.add(Calendar.SECOND, -parseTime(attributes, Integer::parseInt)); |
67 | 65 | currentTimeSlot = calendar.getTime();
|
68 | 66 | result.setStart_date(currentTimeSlot);
|
69 |
| - |
70 |
| - } else if(qName.equals("failure")){ |
71 |
| - |
| 67 | + } else if (isError(qName)) { |
72 | 68 | result.setFinal_result_id(FAILED.getValue());
|
73 |
| - } else if(qName.equals("skipped")){ |
74 |
| - |
75 |
| - result.setFinal_result_id(PASSED.getValue()); |
| 69 | + } else if (qName.equals("skipped")) { |
| 70 | + result.setFinal_result_id(NOT_EXECUTED.getValue()); |
76 | 71 | result.setFail_reason(attributes.getValue("message"));
|
77 | 72 | }
|
78 | 73 | }
|
79 | 74 |
|
80 | 75 | @Override
|
81 |
| - public void endElement(String uri, String localName, String qName){ |
82 |
| - currentElement = ""; |
| 76 | + public void endElement(String uri, String localName, String qName) { |
83 | 77 | if (qName.equals("testcase")) {
|
84 | 78 | test.setInternalId(test.getName());
|
85 | 79 | tests.add(test);
|
86 | 80 | test = new TestDto();
|
87 | 81 | result.setInternalTestId(test.getName());
|
88 |
| - if(result.getFail_reason() != null && result.getFail_reason().equals("$blank")) result.setFail_reason(""); |
| 82 | + |
| 83 | + String failReason = result.getFail_reason(); |
| 84 | + if (failReason != null && failReason.equals(BLANK_RESULT)) { |
| 85 | + result.setFail_reason(""); |
| 86 | + } |
89 | 87 | results.add(result);
|
90 |
| - result = new TestResultDto(); |
91 |
| - result.setFail_reason("$blank"); |
| 88 | + |
| 89 | + currentElement = ""; |
| 90 | + initEmptyResult(); |
92 | 91 | }
|
93 | 92 | }
|
94 | 93 |
|
95 | 94 | @Override
|
96 |
| - public void characters(char[] ch, int start, int length){ |
97 |
| - String value = new String(ch,start,length); |
98 |
| - if(currentElement.equals("failure")){ |
| 95 | + public void characters(char[] ch, int start, int length) { |
| 96 | + String value = new String(ch, start, length); |
| 97 | + if (isError(currentElement)) { |
99 | 98 | String res = result.getFail_reason();
|
100 |
| - if (res == null || res.equals("$blank")) res = ""; |
101 |
| - result.setFail_reason(""); |
| 99 | + res = (res == null || res.equals(BLANK_RESULT)) ? "" : res; |
102 | 100 | result.setFail_reason(res.concat(value));
|
103 |
| - } |
104 |
| - else if(currentElement.equals("system-out")){ |
| 101 | + } else if (currentElement.equals("system-out")) { |
105 | 102 | String log = result.getLog();
|
106 |
| - if (log == null) log = ""; |
| 103 | + if (log == null) log = ""; |
107 | 104 | result.setLog(log.concat(value));
|
108 | 105 | }
|
109 | 106 | }
|
110 | 107 |
|
111 |
| - public TestSuiteDto getTestSuite(){ |
| 108 | + private void initEmptyResult() { |
| 109 | + result = new TestResultDto(); |
| 110 | + result.setFail_reason(BLANK_RESULT); |
| 111 | + } |
| 112 | + |
| 113 | + private String getTestName(TestNameNodeType type, Attributes attributes) { |
| 114 | + switch (type) { |
| 115 | + case className: |
| 116 | + return attributes.getValue("classname"); |
| 117 | + case testName: |
| 118 | + return attributes.getValue("name"); |
| 119 | + default: |
| 120 | + throw new IllegalArgumentException("There is no implementation for getting test name by type " + type); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private <T extends Number> T parseTime(Attributes attributes, Function<String, T> transform) { |
| 125 | + String value = attributes.getValue("time") |
| 126 | + .replace(",", "") |
| 127 | + .split("\\.")[0]; |
| 128 | + return transform.apply(value); |
| 129 | + } |
| 130 | + |
| 131 | + private boolean isError(String qName) { |
| 132 | + List<String> errorTags = Arrays.asList("failure", "error"); |
| 133 | + return errorTags.contains(qName.toLowerCase()); |
| 134 | + } |
| 135 | + |
| 136 | + public TestSuiteDto getTestSuite() { |
112 | 137 | return testSuite;
|
113 | 138 | }
|
114 | 139 |
|
115 |
| - public TestRunDto getTestRun(){ |
| 140 | + public TestRunDto getTestRun() { |
116 | 141 | return testRun;
|
117 | 142 | }
|
118 | 143 |
|
119 |
| - public List<TestDto> getTests(){ |
| 144 | + public List<TestDto> getTests() { |
120 | 145 | return tests;
|
121 | 146 | }
|
122 | 147 |
|
123 |
| - public List<TestResultDto> getTestResults(){ |
| 148 | + public List<TestResultDto> getTestResults() { |
124 | 149 | return results;
|
125 | 150 | }
|
126 | 151 | }
|
0 commit comments