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

org.etlunit.feature.logging.LogFileManagerImpl Maven / Gradle / Ivy

There is a newer version: 1.6.9
Show newest version
package org.etlunit.feature.logging;

import org.etlunit.RuntimeSupport;
import org.etlunit.parser.ETLTestClass;
import org.etlunit.parser.ETLTestMethod;
import org.etlunit.parser.ETLTestOperation;
import org.etlunit.util.IOUtils;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LogFileManagerImpl implements LogFileManager
{
	private RuntimeSupport runtimeSupport;
	private File logFileBase;
	private final List files = new ArrayList();

	private final Map> classLogFileMap = new HashMap>();
	private final Map> methodLogFileMap = new HashMap>();
	private final
	Map>
			operationLogFileMap =
			new HashMap>();

	@Inject
	public void receiveRuntimeSupport(RuntimeSupport rs)
	{
		runtimeSupport = rs;
		logFileBase = rs.getGeneratedSourceDirectory("log");

		IOUtils.purge(logFileBase, true);
	}

	@Override
	public List getLogFilesByETLTestOperation(ETLTestOperation operation)
	{
		return operationLogFileMap.get(operation);
	}

	@Override
	public List getLogFilesByETLTestMethod(ETLTestMethod operation)
	{
		return methodLogFileMap.get(operation);
	}

	@Override
	public List getLogFilesByETLTestClass(ETLTestClass operation)
	{
		return classLogFileMap.get(operation);
	}

	@Override
	public void addLogFile(ETLTestOperation operation, File log) throws IOException
	{
		LogFileImpl
				logFile =
				new LogFileImpl(null,
						cacheLog(log, operation.getQualifiedName()),
						log,
						operation.getTestClass(),
						operation.getTestMethod(),
						operation);
		files.add(logFile);
		catalog(logFile);
	}

	@Override
	public void addLogFile(ETLTestMethod operation, File log) throws IOException
	{
		LogFileImpl
				logFile =
				new LogFileImpl(null,
						cacheLog(log, operation.getQualifiedName()),
						log,
						operation.getTestClass(),
						operation,
						null);
		files.add(logFile);
		catalog(logFile);
	}

	@Override
	public void addLogFile(ETLTestClass operation, File log) throws IOException
	{
		LogFileImpl
				logFile =
				new LogFileImpl(null, cacheLog(log, operation.getQualifiedName()), log, operation, null, null);
		files.add(logFile);
		catalog(logFile);
	}

	@Override
	public void addLogFile(File log) throws IOException
	{
		LogFileImpl logFile = new LogFileImpl(null, cacheLog(log, "NULL"), log, null, null, null);
		files.add(logFile);
		catalog(logFile);
	}

	@Override
	public void addLogFile(ETLTestOperation operation, File log, String message) throws IOException
	{
		LogFileImpl
				logFile =
				new LogFileImpl(message,
						cacheLog(log, operation.getQualifiedName()),
						log,
						operation.getTestClass(),
						operation.getTestMethod(),
						operation);
		files.add(logFile);
		catalog(logFile);
	}

	@Override
	public void addLogFile(ETLTestMethod operation, File log, String message) throws IOException
	{
		LogFileImpl
				logFile =
				new LogFileImpl(message,
						cacheLog(log, operation.getQualifiedName()),
						log,
						operation.getTestClass(),
						operation,
						null);
		files.add(logFile);
		catalog(logFile);
	}

	@Override
	public void addLogFile(ETLTestClass operation, File log, String message) throws IOException
	{
		LogFileImpl
				logFile =
				new LogFileImpl(message, cacheLog(log, operation.getQualifiedName()), log, operation, null, null);
		files.add(logFile);
		catalog(logFile);
	}

	@Override
	public void addLogFile(File log, String message) throws IOException
	{
		LogFileImpl logFile = new LogFileImpl(message, cacheLog(log, "NULL"), log, null, null, null);
		files.add(logFile);
		catalog(logFile);
	}

	@Override
	public File getLogRoot()
	{
		return logFileBase;
	}

	private void catalog(LogFileImpl logFile)
	{
		if (logFile.getTestClass() != null)
		{
			getList(classLogFileMap, logFile.getTestClass()).add(logFile);
		}
		if (logFile.getTestMethod() != null)
		{
			getList(methodLogFileMap, logFile.getTestMethod()).add(logFile);
		}
		if (logFile.getTestOperation() != null)
		{
			getList(operationLogFileMap, logFile.getTestOperation()).add(logFile);
		}
	}

	private  List getList(Map> operationLogFileMap, K testClass)
	{
		if (!operationLogFileMap.containsKey(testClass))
		{
			operationLogFileMap.put(testClass, new ArrayList());
		}

		return operationLogFileMap.get(testClass);
	}

	private File cacheLog(File log, String path) throws IOException
	{
		File ffCache = new File(logFileBase, path + "." + log.getName());

		IOUtils.copyFiles(log, ffCache);

		return ffCache;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy