All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bitbucket.bradleysmithllc.etlunit.maven.SurefireStatusReporter Maven / Gradle / Ivy

There is a newer version: 4.6.0-eu
Show newest version
package org.bitbucket.bradleysmithllc.etlunit.maven;

import org.bitbucket.bradleysmithllc.etlunit.NullStatusReporterImpl;
import org.bitbucket.bradleysmithllc.etlunit.StatusReporter;
import org.bitbucket.bradleysmithllc.etlunit.TestExecutionError;
import org.bitbucket.bradleysmithllc.etlunit.parser.ETLTestMethod;
import org.bitbucket.bradleysmithllc.etlunit.util.IOUtils;
import org.bitbucket.bradleysmithllc.etlunit.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

public class SurefireStatusReporter extends NullStatusReporterImpl {
		private String currentTestClass = null;
		private List currentTests = new ArrayList();
		private long testStartTime;
		private long testClassElapsed;
		private int numFailures;
		private int numErrors;
		private final File targetDirectory;

		SurefireStatusReporter(File target)
		{
			targetDirectory = target;
		}

		@Override
		public void testBeginning(ETLTestMethod method) {
			testStartTime = System.currentTimeMillis();

			String qualifiedName = method.getTestClass().getQualifiedName();

			if (currentTestClass != null)
			{
				if (!currentTestClass.equals(qualifiedName))
				{
					publishReport();
					currentTestClass = qualifiedName;
				}
			}
			else
			{
				currentTestClass = qualifiedName;
			}
		}

	private void publishReport() {
		StringBuffer buffer = new StringBuffer("\n");
		buffer.append("\n");
		buffer.append("\n");

		for (TestResult result : currentTests)
		{
			buffer.append("\n");

			if (result.testResult == TestResult.result.error)
			{
				buffer.append("\t\n");
			}
			else if (result.testResult == TestResult.result.fail)
			{
				buffer.append("\t\n");
			}

			buffer.append("\n");
		}

		buffer.append("");

		try {
			IOUtils.writeBufferToFile(new File(targetDirectory, "TEST-" + currentTestClass + ".xml"), buffer);
		} catch (IOException e) {
			throw new RuntimeException("");
		}

		currentTestClass = null;
		numFailures = 0;
		numErrors = 0;
		testClassElapsed = 0L;
		currentTests.clear();
	}

	@Override
	public void testCompleted(ETLTestMethod method, StatusReporter.CompletionStatus status) {
		TestResult result = new TestResult();

		result.testName = method.getName();

		result.elapsedTime = System.currentTimeMillis() - testStartTime;

		testClassElapsed += result.elapsedTime;

		result.testResult = TestResult.result.pass;

		switch(status.getTestResult())
		{
			case success:
				break;
			case error:
				result.testResult = TestResult.result.error;

				StringWriter stringWriter = new StringWriter();
				PrintWriter writer = new PrintWriter(stringWriter);

				for (TestExecutionError err : status.getErrors())
				{
					err.printStackTrace(writer);
				}

				result.failureMessage = status.getErrors().toString() + "\n" + stringWriter.toString();

				numErrors++;
				break;
			case failure:
				result.testResult = TestResult.result.fail;
				result.failureMessage = status.getAssertionFailures().toString();
				numFailures++;
				break;
		}

		currentTests.add(result);
	}

	@Override
	public void testsCompleted() {
		if (currentTestClass != null)
		{
			publishReport();
		}
	}
}

final class TestResult {
	enum result {
		error,
		fail,
		pass
	}

	result testResult;
	String testName;
	long elapsedTime;
	String failureMessage;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy