atg.dms.sql.InitializingSqlJmsManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of atgdust Show documentation
Show all versions of atgdust Show documentation
ATG DUST is a framework for building JUnit tests for
applications built on the ATG Dynamo platform. This framework allows
one to quickly write test code that depends up Nucleus or ATG
Repositories. By using this framework one can drastically cut down
on development time. It takes only a few seconds to start up a test
with a repository, but it may take multiple minutes to start up an
application server. To get started with DUST, take a look at
http://atgdust.sourceforge.net/first-test.html. This page will walk
you through the process of running a basic test which starts
Nucleus. After that, read the other getting started guides to
describe how to create standalone Junit tests which can startup
repositories and use the DynamoHttpServletResponse classes.
For only ATG10 and tested.
The newest version!
package atg.dms.sql;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Scanner;
import javax.sql.DataSource;
import org.apache.ddlutils.Platform;
import org.apache.ddlutils.PlatformFactory;
import org.apache.ddlutils.util.SqlTokenizer;
import atg.core.io.FileUtils;
import atg.nucleus.NucleusTestUtils;
import atg.nucleus.ServiceException;
/**
* This SQLJmsManager sub class will create its required database schema on
* startup and optionally drop the schema (tables) on shutdown. By default this
* class will use the datasource assigned to the
* /atg/dynamo/service/jdbc/SQLRepository
*
* @version $Id: //test/UnitTests/base/main/src/Java/atg/dms/sql/InitializingSqlJmsManager.java#1 $
* @author adamb
*/
public class InitializingSqlJmsManager extends SqlJmsManager {
public String mSQLRepositoryPath = "/atg/dynamo/service/jdbc/SQLRepository";
boolean mCreatingSchemaOnStartup = true;
boolean mDropSchemaOnShutdown = false;
boolean mDropSchemaOnStartup = true;
// ---------------------
/**
* Creates schema and continues with SQLJms startup.
*/
public void doStartService() throws ServiceException {
DataSource ds = getDataSource();
// get the db type
logInfo("Database = " + getDatabaseName());
if (isLoggingInfo())
logInfo("Initializing schema using datasource " + ds);
if (mCreatingSchemaOnStartup) {
createSchema();
}
super.doStartService();
}
/**
* Creates the required tables for this component.
*
* @throws FileNotFoundException
*/
public void createSchema() {
String ddlPath = getDDLPath(getDatabaseName());
Platform platform = PlatformFactory
.createNewPlatformInstance(getDataSource());
try {
String sqlString = FileUtils.readFileString(new File(ddlPath));
platform.evaluateBatch(sqlString, true);
} catch (FileNotFoundException e) {
if (isLoggingError())
logError("Can't create schema. ", e);
} catch (IOException e) {
if (isLoggingError())
logError("Can't create schema. ", e);
}
}
/**
* Returns the path to the DDL file for the given database.
*
* @param pDatabaseName
* = DBName returned from DatabaseMetaData
* @return
*/
String getDDLPath(String pDatabaseName) {
if (pDatabaseName == null)
return null;
// Get Dynamo Home or Root
String dynamoRootEnv = System.getenv("DYNAMO_ROOT");
String dynamoHomeEnv = System.getenv("DYNAMO_HOME");
String ddlRelativePath = "/DAS/sql/db_components/";
String root = (dynamoRootEnv != null ? dynamoRootEnv : dynamoHomeEnv
+ File.separatorChar + "..");
if (pDatabaseName.toLowerCase().startsWith("hsql")) {
pDatabaseName = "mysql";
// Not in the build so take it from source tree
File configDir = NucleusTestUtils.getConfigpath(
InitializingSqlJmsManager.class, InitializingSqlJmsManager.class
.getSimpleName(), false);
if (!configDir.exists()) {
if (isLoggingError()) {
logError("Can't locate sql scripts for HSQLDB. Directory "
+ configDir + " does not exist.");
}
return null;
} else {
return configDir.getAbsolutePath() + "/hsql/create_sql_jms_ddl.sql";
}
} else if (pDatabaseName.toLowerCase().startsWith("db2")) {
pDatabaseName = "db2";
} else if (pDatabaseName.toLowerCase().startsWith("microsoft")) {
pDatabaseName = "mssql";
}
return root + ddlRelativePath + pDatabaseName.toLowerCase()
+ "/create_sql_jms_ddl.sql";
}
/**
* Returns the name of the database to which this component is connected.
*
* @return
*/
public String getDatabaseName() {
if (getDataSource() == null)
return null;
Connection c = null;
String dbName = null;
try {
c = getDataSource().getConnection();
DatabaseMetaData meta = c.getMetaData();
dbName = meta.getDatabaseProductName();
} catch (SQLException e) {
if (isLoggingError())
logError("Could not get database type name. ", e);
} finally {
if (c != null)
try {
c.close();
} catch (SQLException e) {
;
}
}
return dbName;
}
}