Test code for DELETE TABLES
//
// Lapetus Ltd Java Class. Copyright (c) Lapetus Systems Ltd, 2009, 2010.
// -----------------------------------------------------------------------
// This is the intellectual property of Lapetus Systems Ltd, Artemis, Greece.
// --------------------------------------------------------------------------
// www.lapetus-ltd.com, www.lapetus.com.gr, www.lapetus.eu
// -------------------------------------------------------
// $LastChangedRevision: 1190 $
// $LastChangedDate:: 2010-11-17 13:21:35#$
// ----------------------------------------
//
import com.lapetus_ltd._2009.xml.types.XLptsDBTypeOfStatementType;
import com.lapetus_ltd._2009.xml.types.XLptsDriverType;
import com.lapetus_ltd.api.TLptsMainDatabase;
import com.lapetus_ltd.api.common.TLptsCryptoUtil;
import com.lapetus_ltd.api.common.TLptsFileUtil;
import com.lapetus_ltd.api.common.TLptsSysInfoUtil;
import com.lapetus_ltd.api.common.logger.ILptsLogListener;
import com.lapetus_ltd.api.common.logger.TLptsLog;
import com.lapetus_ltd.api.common.logger.TLptsLogger;
import com.lapetus_ltd.api.db.control.*;
import com.lapetus_ltd.api.db.utils.TLptsDriverLoader;
import com.lapetus_ltd.api.db.xml.types.TLptsDBConnectionType;
import com.lapetus_ltd.api.db.xml.types.TLptsDriverType;
import junit.framework.TestCase;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
// ######################################################################################################
// #### Test code for the dbJAPI build process. This code will stop the build process on ERROR ####
// ######################################################################################################
//
// Class Description : Deletes the tables that are used for testing the capabilities of the system.
//
// This modules drops the tables required for the CREATE, INSERT, UPDATE and DELETE tests (run in that order).
// This needs to be run before the TestCreateTables.
//
// All in all it is a very simple test, which just accommodates the test cycle.
//
public class TestDeleteTables extends TestCase
{
private String connectionMSSqlId = "";
private String connectionPostgresId = "";
private String connectionMsAccessId = "";
static public void main(String[] args)
{
TestDeleteTables tct = new TestDeleteTables();
tct.testConnections();
}
public void testConnections()
{
// required so that the API can function correctly.
TLptsMainDatabase.init();
// register a listener for the logging system.
TLptsLogger.addListener(new ILptsLogListener()
{
public void newLogGenerated(TLptsLog log)
{
if (log.getType().equals(TLptsLogger.LOG_TYPE.ERROR))
{
fail("LOG ERROR :" + log.getMessage() + " : " + log.getSupportingText() + " : Exception : " + log.getExceptionMessage());
System.exit(0);
}
if (log.getType().equals(TLptsLogger.LOG_TYPE.WARNING))
System.out.println("LOG WARNING :" + log.getMessage() + " : " + log.getSupportingText());
if (log.getType().equals(TLptsLogger.LOG_TYPE.MESSAGE))
System.out.println("LOG MESSAGE :" + log.getMessage() + " : " + log.getSupportingText());
}
});
// register a listener for the connections
TLptsFactoryConnection.addListener(new ConnectionStatementListener());
// register a listener for the statements
TLptsFactoryStatement.addListener(new StatementListener());
TLptsDBConnectionType connectionType;
//Connect To PostgreSQL
connectionType = connectToDB("POSTGRESQL_DB");
if (connectionType != null)
{
connectionPostgresId = connectionType.getId();
TLptsFactoryConnection.initiateConnection(connectionType); // This generates another thread!!
}
//Connect To MS SQL
connectionType = connectToDB("MSSQL_DB");
if (connectionType != null)
{
connectionMSSqlId = connectionType.getId();
TLptsFactoryConnection.initiateConnection(connectionType); // This generates another thread!!
}
// the odbc drivers are Windows only
if (TLptsSysInfoUtil.isHostWindows())
{
System.out.println("This is a Windows HOST. Able to test ODBC");
connectionType = connectToDB("ACCESS_DB");
if (connectionType != null)
{
connectionMsAccessId = connectionType.getId();
TLptsFactoryConnection.initiateConnection(connectionType); // This generates another thread!!
}
}
// ##############################################################################################################
// See ExampleConnectionUsage.connectToDB for an example of how to connect to other Data Sources
// ##############################################################################################################
}
// ####################################################################################
// C O N N E C T I O N
// ####################################################################################
private TLptsDBConnectionType connectToDB(String title)
{
String driverName = "", dbName = "", userName = "", password = "";
ResourceBundle bundle = ResourceBundle.getBundle("resources"); // we have stored all the database info in this bundle
TLptsDBConnectionType connectionType = new TLptsDBConnectionType();
connectionType.setTitle(title);
if (title.equals("MSSQL_DB"))
{
dbName = bundle.getString("Testing.database.mssql.testdb1");
userName = bundle.getString("Testing.database.mssql.user");
password = bundle.getString("Testing.database.mssql.password");
driverName = "com.microsoft.sqlserver.jdbc.SQLServerDataSource";
}
if (title.equals("POSTGRESQL_DB"))
{
dbName = bundle.getString("Testing.database.postgresql.testdb1");
userName = bundle.getString("Testing.database.postgresql.user");
password = bundle.getString("Testing.database.postgresql.password");
driverName = "org.postgresql.ds.PGSimpleDataSource";
}
if (title.equals("ACCESS_DB"))
{
String fileName = TLptsFileUtil.getCurrentDirectory() + bundle.getString("Testing.database.access.testdb1");
connectionType.setDataFileUrl(fileName); // Access file from sample-data directory
userName = bundle.getString("Testing.database.access.user");
password = bundle.getString("Testing.database.access.password");
driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
}
XLptsDriverType driver = TLptsDriverLoader.getDriverTypeByClassName(driverName);
if (driver == null)
{
System.out.println("Could not find driver for class " + driverName);
return null;
}
TLptsDriverType driverType = new TLptsDriverType(driver);
// changing values that are default in the driver.loader.xml file.
// the DB is now set correctly and will filter through to the Datasource interface execution
if (!title.equals("ACCESS_DB"))
driverType.setValueForInterfaceFunctionParameter("setDatabaseName", "Database Name", dbName);
connectionType.setDriverType(driverType);
if (userName == null || password == null)
connectionType.setCredentials(TLptsCryptoUtil.defaultEncryptCredentialsRSA(driverType.getGuestUser(),
driverType.getGuestPassword()));
else
connectionType.setCredentials(TLptsCryptoUtil.defaultEncryptCredentialsRSA(userName, password));
return connectionType;
}
// ###############################################################################################################
// #### C O N N E C T I O N L I S T E N E R
// ###############################################################################################################
private class ConnectionStatementListener implements ILptsFactoryConnectionListener
{
public synchronized void newConnectionCreated(TLptsConnection connection)
{
System.out.println("New Connection created successfully. Statements can be processed.");
if ((TLptsFactoryConnection.getConnectionListSize() == 3 && TLptsSysInfoUtil.isHostWindows()) ||
(TLptsFactoryConnection.getConnectionListSize() == 2 && !TLptsSysInfoUtil.isHostWindows()))
clearTables();
}
public void newConnectionProcessStarted()
{
System.out.println("Connecting...");
}
public void newConnectionFailed(TLptsLog log)
{
System.out.println("New Connection Failed. The logger got this.");
}
public void removedAndClosedConnection(TLptsConnection connection)
{
System.out.println("Connection removed from Connection Manager and closed successfully ... " + connection.getTitle());
}
}
// ###############################################################################################################
// #### S T A T E M E N T L I S T E N E R
// ###############################################################################################################
// not doing much here - just catching the failed situation.
private class StatementListener implements ILptsFactoryStatementListener
{
public void newStatementProcessStarted(TLptsConnection connection)
{
}
public void newStatementCreated(TLptsConnection connection, TLptsStatement statement)
{
}
public void newStatementFailed(TLptsConnection connection)
{
TLptsLogger.logError("Failed to Create Statement for connection " + connection.getTitle(), null);
}
public void removedAndClosedStatement(TLptsConnection connection, TLptsStatement statement)
{
}
}
// ####################################################################################
// C L E A N U P
// ####################################################################################
private void clearTables()
{
TLptsConnection connection;
TLptsStatement lptsStatement;
Statement statement;
try
{
// 1st way to execute a DROP statement. We could also use TLptsFactoryStatement.executeOtherStatement
connection = TLptsFactoryConnection.getConnection(connectionPostgresId);
statement = connection.createStatement();
statement.executeUpdate("DROP TABLE \"testTable\" ");
connection = TLptsFactoryConnection.getConnection(connectionMSSqlId);
statement = connection.createStatement();
statement.executeUpdate("DROP TABLE [insurance_policy].[dbo].[testTable]");
if (TLptsSysInfoUtil.isHostWindows())
{
// 2nd way to execute a DROP statement
connection = TLptsFactoryConnection.getConnection(connectionMsAccessId);
lptsStatement = new TLptsStatement(connection, XLptsDBTypeOfStatementType.OTHER);
lptsStatement.setSqlStatementUser("DROP TABLE \"testTable\" ");
lptsStatement.setSqlStatementModified(true);
TLptsFactoryStatement.executeOtherStatement(lptsStatement);
}
} catch (SQLException e)
{
TLptsLogger.logWarning("SQL Exception", e.getLocalizedMessage(), e);
}
System.out.println("DB Cleaned up by deleting temp tables.");
System.exit(0);
}
}