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

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

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

import org.etlunit.parser.ETLTestValueObject;
import org.etlunit.util.StringUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DatabaseConnectionImpl implements DatabaseConnection {
	private final String implementationId;
	private final String id;
	private final String projectVersion;
	private final String projectName;
	private final String projectUid;
	private final String userName;
	private final String serverName;

	private final String adminUserName;
	private final String adminPassword;

	private final List modes = new ArrayList();
	private final List ddlScripts = new ArrayList();

	private final Map modeMap = new HashMap();
	private final ETLTestValueObject databaseConfiguration;

	public DatabaseConnectionImpl(String id, ETLTestValueObject databaseConfiguration, String defaultImplementationId, String projectName, String projectVersion, String userName, String projectUid)
	{
		this.projectName = projectName;
		this.projectVersion = projectVersion;
		this.userName = userName;
		this.projectUid = projectUid;
		this.id = id;

		this.databaseConfiguration = databaseConfiguration;

		ETLTestValueObject eImplementationId = databaseConfiguration.query("implementation-id");

		if (eImplementationId == null)
		{
			implementationId = defaultImplementationId;

			if (implementationId == null)
			{
				throw new IllegalArgumentException("implementation-id is not specified and no default is provided");
			}
		}
		else
		{
			implementationId = eImplementationId.getValueAsString();
		}

		ETLTestValueObject serverQuery = databaseConfiguration.query("server-name");

		if (serverQuery != null)
		{
			serverName = serverQuery.getValueAsString();
		}
		else
		{
			try {
				serverName = InetAddress.getLocalHost().getHostName();
			} catch (UnknownHostException e) {
				throw new IllegalArgumentException(e);
			}
		}

		ETLTestValueObject mmodes = databaseConfiguration.query("modes");

		if (mmodes != null)
		{
			modes.addAll(mmodes.getValueAsListOfStrings());

			for (String mode : modes)
			{
				modeMap.put(mode, mode);
			}

			// mode named default is reserved
			if (isModeValid("default"))
			{
				throw new IllegalArgumentException("'default' is not a valid mode name");
			}

			if (!declaresModes())
			{
				throw new IllegalArgumentException("List of declared modes must not be empty");
			}
		}

		ETLTestValueObject ddl = databaseConfiguration.query("schema-scripts");

		if (ddl != null)
		{
			for (String script : ddl.getValueAsListOfStrings())
			{
				ddlScripts.add(script);
			}
		}

		ETLTestValueObject user = databaseConfiguration.query("user-name");

		if (user != null)
		{
			adminUserName = user.getValueAsString();
		}
		else
		{
			adminUserName = null;
		}

		ETLTestValueObject pass = databaseConfiguration.query("password");

		if (pass != null)
		{
			adminPassword = pass.getValueAsString();
		}
		else
		{
			adminPassword = null;
		}
	}

	public String getAdminUserName()
	{
		return adminUserName;
	}

	public String getAdminPassword()
	{
		return adminPassword;
	}

	public String getId()
	{
		return id;
	}

	public boolean declaresModes()
	{
		return modes.size() > 0;
	}

	public boolean isModeValid(String mode)
	{
		return modes.size() == 0 || modeMap.get(mode) != null;
	}

	public List getModes() {
		return modes;
	}

	public String getImplementationId() {
		return implementationId;
	}

	public String getDatabaseName(String mode)
	{
		return getUserName() + "_" + StringUtils.sanitize(getId(), '_') + (mode == null ? "" : ("_" + StringUtils.sanitize(mode, '_'))) + "_" + projectName + "_" + projectVersion + "_" + projectUid;
	}

	private String getUserName() {
		return userName;
	}

	public String getLoginName(String mode)
	{
		return getDatabaseName(mode);
	}

	public String getPassword(String mode)
	{
		return getDatabaseName(mode);
	}

	public String getServerName() {
		return serverName;
	}

	public List getSchemaScripts() {
		return ddlScripts;
	}

	public ETLTestValueObject getDatabaseConfiguration() {
		return databaseConfiguration;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy