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

org.etlunit.feature.database.DbOperationsExecutor Maven / Gradle / Ivy

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

import org.etlunit.*;
import org.etlunit.context.VariableContext;
import org.etlunit.feature.Feature;
import org.etlunit.feature.extend.Extender;
import org.etlunit.io.FileBuilder;
import org.etlunit.parser.ETLTestMethod;
import org.etlunit.parser.ETLTestOperation;
import org.etlunit.parser.ETLTestValueObject;
import org.etlunit.util.IOUtils;

import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class DbOperationsExecutor implements Extender, ExecuteOperationProcessor
{
	private Log applicationLog;
	private final DatabaseFeatureModule databaseFeature;
	private DatabaseRuntimeSupport databaseRuntimeSupport;
	private RuntimeSupport runtimeSupport;
	private JDBCClient jdbcClient;

	public DbOperationsExecutor(DatabaseFeatureModule databaseFeature)
	{
		this.databaseFeature = databaseFeature;
	}

	@Inject
	public void setJdbcClient(JDBCClient client)
	{
		jdbcClient = client;
	}

	@Inject
	public void setDatabaseRuntimeSupport(DatabaseRuntimeSupport databaseRuntimeSupport)
	{
		this.databaseRuntimeSupport = databaseRuntimeSupport;
	}

	@Inject
	public void setRuntimeSupport(RuntimeSupport runtimeSupport)
	{
		this.runtimeSupport = runtimeSupport;
	}

	@Inject
	public void setLogger(@Named("applicationLog") Log log)
	{
		applicationLog = log;
	}

	public boolean canHandleRequest(ETLTestOperation etlTestOperation, ETLTestValueObject etlTestValueObject, VariableContext context, ExecutionContext executionContext)
	{
		return false;
	}

	public ClassResponder.action_code process(ETLTestOperation etlTestOperation, ETLTestValueObject etlTestValueObject, VariableContext context, ExecutionContext executionContext)
	{
		return ClassResponder.action_code.handled;
	}

	public Feature getFeature()
	{
		return databaseFeature;
	}

	public ClassResponder.action_code processExecute(final ExecuteOperation operation, final ETLTestOperation op, ETLTestValueObject obj, VariableContext context, ExecutionContext econtext) throws TestAssertionFailure, TestExecutionError, TestWarning
	{
		String script = operation.getSqlScript();
		String sqlText = operation.getSql();

		if (script != null)
		{
			applicationLog.info("executing script " + script);
		}
		else
		{
			applicationLog.info("executing statement " + sqlText);
		}

		final String aPackage = op.getTestClass().getPackage();

		// this can be a script or a file reference
		if (sqlText == null)
		{
			if (script == null)
			{
				throw new TestExecutionError("Execute requires " + ExecuteOperation.SQL_JSON_NAME + " or " + ExecuteOperation.SQLSCRIPT_JSON_NAME, DatabaseConstants.ERR_BAD_EXECUTE_OPERATION);
			}

			File scriptFile = new FileBuilder(runtimeSupport.getSourceDirectory(aPackage)).subdir("sql").mkdirs().name(script).file();

			try
			{
				sqlText = IOUtils.readFileToString(scriptFile);
			}
			catch (IOException e)
			{
				throw new TestExecutionError("", TestConstants.ERR_IO_ERROR, e);
			}
		}

		final String sql = sqlText;

		DatabaseClassListener.ConnectionMode connMode = DatabaseClassListener.getConnection(databaseFeature, obj, context, op.getTestMethod()).get(0);

		final DatabaseConnection conn = connMode.conn;

		final String mode = connMode.getMode();

		DatabaseImplementation impl = DatabaseClassListener.getDatabaseImplementation(databaseFeature, conn);

		jdbcClient.useStatement(conn, mode, new JDBCClient.StatementClient()
		{
			public void connection(Connection conn, Statement st, DatabaseConnection connection, String mode, int id) throws Exception
			{
				SQLAggregator sqlagg = databaseFeature.resolveSQLRef(sql);

				SQLAggregator.Aggregator statementAggregator = sqlagg.getStatementAggregator();
				while (statementAggregator.hasNext())
				{
					st.execute(statementAggregator.next().getLine());
				}
			}
		});

		return ClassResponder.action_code.handled;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy