| 1 | package net.minecraft.gametest.framework;
|
| 2 |
|
| 3 | import com.google.common.base.Stopwatch;
|
| 4 | import java.io.File;
|
| 5 | import java.time.Instant;
|
| 6 | import java.time.format.DateTimeFormatter;
|
| 7 | import java.util.concurrent.TimeUnit;
|
| 8 | import javax.xml.parsers.DocumentBuilderFactory;
|
| 9 | import javax.xml.parsers.ParserConfigurationException;
|
| 10 | import javax.xml.transform.Transformer;
|
| 11 | import javax.xml.transform.TransformerException;
|
| 12 | import javax.xml.transform.TransformerFactory;
|
| 13 | import javax.xml.transform.dom.DOMSource;
|
| 14 | import javax.xml.transform.stream.StreamResult;
|
| 15 | import org.w3c.dom.Document;
|
| 16 | import org.w3c.dom.Element;
|
| 17 |
|
| 18 | public class JUnitLikeTestReporter implements TestReporter {
|
| 19 | private final Document document;
|
| 20 | private final Element testSuite;
|
| 21 | private final Stopwatch stopwatch;
|
| 22 | private final File destination;
|
| 23 |
|
| 24 | public JUnitLikeTestReporter(final File destination) throws ParserConfigurationException {
|
| 25 | this.destination = destination;
|
| 26 | this.document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
| 27 | this.testSuite = this.document.createElement("testsuite");
|
| 28 | Element testSuites = this.document.createElement("testsuite");
|
| 29 | testSuites.appendChild(this.testSuite);
|
| 30 | this.document.appendChild(testSuites);
|
| 31 | this.testSuite.setAttribute("timestamp", DateTimeFormatter.ISO_INSTANT.format(Instant.now()));
|
| 32 | this.stopwatch = Stopwatch.createStarted();
|
| 33 | }
|
| 34 |
|
| 35 | private Element createTestCase(final GameTestInfo testInfo, final String name) {
|
| 36 | Element testCase = this.document.createElement("testcase");
|
| 37 | testCase.setAttribute("name", name);
|
| 38 | testCase.setAttribute("classname", testInfo.getStructure().toString());
|
| 39 | testCase.setAttribute("time", String.valueOf(testInfo.getRunTime() / 1000.0));
|
| 40 | this.testSuite.appendChild(testCase);
|
| 41 | return testCase;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public void onTestFailed(final GameTestInfo testInfo) {
|
| 46 | String name = testInfo.id().toString();
|
| 47 | String message = testInfo.getError().getMessage();
|
| 48 | Element result = this.document.createElement(testInfo.isRequired() ? "failure" : "skipped");
|
| 49 | result.setAttribute("message", "(" + testInfo.getTestBlockPos().toShortString() + ") " + message);
|
| 50 | Element testCase = this.createTestCase(testInfo, name);
|
| 51 | testCase.appendChild(result);
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public void onTestSuccess(final GameTestInfo testInfo) {
|
| 56 | String name = testInfo.id().toString();
|
| 57 | this.createTestCase(testInfo, name);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public void finish() {
|
| 62 | this.stopwatch.stop();
|
| 63 | this.testSuite.setAttribute("time", String.valueOf(this.stopwatch.elapsed(TimeUnit.MILLISECONDS) / 1000.0));
|
| 64 |
|
| 65 | try {
|
| 66 | this.save(this.destination);
|
| 67 | } catch (TransformerException exception) {
|
| 68 | throw new Error("Couldn't save test report", exception);
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | public void save(final File file) throws TransformerException {
|
| 73 | TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
| 74 | Transformer transformer = transformerFactory.newTransformer();
|
| 75 | DOMSource source = new DOMSource(this.document);
|
| 76 | StreamResult result = new StreamResult(file);
|
| 77 | transformer.transform(source, result);
|
| 78 | }
|
| 79 | }
|
| 80 |
|