public static void main(String[] args) {
//NOTE: to run this test to validate the DataSourceMgr, do the following:
// --- need 3 datasources, Oracle, SqlServer and 1 other
ConfigPropertyLoader config = ConfigPropertyLoader.getInstance();
DataSourceFactory factory = new DataSourceFactory(config);
try {
if (factory.getDatasource("model1") == null) {
throw new TransactionRuntimeException("No datasource was not found");
}
} catch (QueryTestFailedException e) {
e.printStackTrace();
}
factory.cleanup();
ConfigPropertyLoader.reset();
// the following verifies that order of "use" datasources is applied to request for datasources.
config = ConfigPropertyLoader.getInstance();
config.setProperty(ConfigPropertyNames.USE_DATASOURCES_PROP, "oracle,sqlserver");
factory = new DataSourceFactory(config);
try {
DataSource dsfind = factory.getDatasource( "model2");
if (dsfind == null) {
throw new TransactionRuntimeException("No datasource was not found as the 2nd datasource");
}
if (dsfind.getConnectorType() == null) {
throw new TransactionRuntimeException("Connector types was not defined");
}
if (!dsfind.getName().equalsIgnoreCase("sqlserver")) {
throw new TransactionRuntimeException("Sqlserver was not found as the 2nd datasource");
}
dsfind = factory.getDatasource( "model1");
if (dsfind == null) {
throw new TransactionRuntimeException("No datasource was not found as the 2nd datasource");
}
if (!dsfind.getName().equalsIgnoreCase("oracle")) {
throw new TransactionRuntimeException("Oracle was not found as the 2nd datasource");
}
System.out.println("Datasource :" + dsfind.getName() + " was found");
// the following test verifies that a sqlserver datasource is not
// returned (excluded)
factory.cleanup();
ConfigPropertyLoader.reset();
config = ConfigPropertyLoader.getInstance();
config.setProperty(ConfigPropertyNames.EXCLUDE_DATASBASE_TYPES_PROP, "sqlserver");
factory = new DataSourceFactory(config);
int n = factory.getNumberAvailableDataSources();
TestLogger.log("Num avail datasources: " + n);
for (int i=0; i<n; i++) {
String k = String.valueOf(i);
DataSource ds1 = factory.getDatasource( "model" + k);
if (ds1 == null) {
throw new TransactionRuntimeException("No datasource was found for: model:" + k);
} if (ds1.getDBType().equalsIgnoreCase(DataSourceFactory.DataBaseTypes.SQLSERVER)) {
throw new TransactionRuntimeException("sqlserver dbtype should have been excluded");
}
}
DataSource reuse = factory.getDatasource( "model1");
if (reuse != null) {
} else {
throw new TransactionRuntimeException("The process was not able to reassign an already used datasource");
}
factory.cleanup();
ConfigPropertyLoader.reset();
// test required database types
// test 1 source
config = ConfigPropertyLoader.getInstance();
config.setModelAssignedToDatabaseType("pm1", DataSourceFactory.DataBaseTypes.ORACLE);
factory = new DataSourceFactory(config);
DataSource ds1 = factory.getDatasource("pm1");
if (!ds1.getDBType().equalsIgnoreCase(DataSourceFactory.DataBaseTypes.ORACLE)) {
throw new TransactionRuntimeException("Required DB Type of oracle for model pm1 is :" + ds1.getDBType());
}
TestLogger.log("Test1 Required DS1 " + ds1.getDBType());
factory.cleanup();
ConfigPropertyLoader.reset();
// test required database types
// test 2 sources, 1 required and other ANY
config = ConfigPropertyLoader.getInstance();
config.setModelAssignedToDatabaseType("pm2", DataSourceFactory.DataBaseTypes.SQLSERVER);
config.setModelAssignedToDatabaseType("pm1", DataSourceFactory.DataBaseTypes.ANY);
factory = new DataSourceFactory(config);
DataSource ds2 = factory.getDatasource("pm2");
if (!ds2.getDBType().equalsIgnoreCase(DataSourceFactory.DataBaseTypes.SQLSERVER)) {
throw new TransactionRuntimeException("Required DB Type of sqlserver for model pm2 is :" + ds2.getDBType());
}
TestLogger.log("Test2 Required DS2 " + ds2.getDBType());
factory.cleanup();
ConfigPropertyLoader.reset();
// test required database types
// test 2 sources, 2 required
config = ConfigPropertyLoader.getInstance();
config.setModelAssignedToDatabaseType("pm2", DataSourceFactory.DataBaseTypes.SQLSERVER);
config.setModelAssignedToDatabaseType("pm1", DataSourceFactory.DataBaseTypes.ORACLE);
factory = new DataSourceFactory(config);
DataSource ds3a = factory.getDatasource("pm2");
if (!ds3a.getDBType().equalsIgnoreCase(DataSourceFactory.DataBaseTypes.SQLSERVER)) {