Author: vhalbert(a)redhat.com
Date: 2009-11-16 15:57:31 -0500 (Mon, 16 Nov 2009)
New Revision: 1554
Modified:
trunk/test-integration/db/src/main/java/org/teiid/test/framework/datasource/DataSource.java
trunk/test-integration/db/src/main/java/org/teiid/test/framework/datasource/DataSourceFactory.java
trunk/test-integration/db/src/main/java/org/teiid/test/framework/datasource/DataSourceMgr.java
Log:
Teiid 773 - cleanup and refactoring so that the assumption is there will be 2 datasources
required in order to run the test.
Modified:
trunk/test-integration/db/src/main/java/org/teiid/test/framework/datasource/DataSource.java
===================================================================
---
trunk/test-integration/db/src/main/java/org/teiid/test/framework/datasource/DataSource.java 2009-11-16
20:57:15 UTC (rev 1553)
+++
trunk/test-integration/db/src/main/java/org/teiid/test/framework/datasource/DataSource.java 2009-11-16
20:57:31 UTC (rev 1554)
@@ -1,7 +1,13 @@
package org.teiid.test.framework.datasource;
+import java.sql.Connection;
import java.util.Properties;
+import javax.sql.XAConnection;
+
+import org.teiid.test.framework.connection.ConnectionStrategy;
+import org.teiid.test.framework.exception.QueryTestFailedException;
+
/**
* DataSource represents a single database that was configured by a connection.properties
file.
* @author vanhalbert
@@ -17,6 +23,15 @@
private String group;
private String dbtype;
+ // The connections are stored in the datasource and are reused
+ // for the duration of all tests so thats there's not
+ // disconnect/connect being performed over and over
+ private ConnectionStrategy conn;
+ private ConnectionStrategy xaconn;
+
+ public DataSource() {
+ this.name = "notassigned";
+ }
public DataSource(String name, String group, Properties properties) {
this.name = name;
this.group = group;
@@ -49,6 +64,26 @@
return this.dbtype;
}
+
+ public Connection getConnection() throws QueryTestFailedException {
+ if (this.conn == null) return null;
+
+ return this.conn.getConnection();
+ }
+
+ public void setConnection(ConnectionStrategy c) {
+ this.conn = c;
+ }
+
+ public XAConnection getXAConnection() throws QueryTestFailedException {
+ if (this.xaconn == null) return null;
+
+ return this.xaconn.getXAConnection();
+ }
+
+ public void setXAConnection(ConnectionStrategy xaconn) {
+ this.xaconn = xaconn;
+ }
Modified:
trunk/test-integration/db/src/main/java/org/teiid/test/framework/datasource/DataSourceFactory.java
===================================================================
---
trunk/test-integration/db/src/main/java/org/teiid/test/framework/datasource/DataSourceFactory.java 2009-11-16
20:57:15 UTC (rev 1553)
+++
trunk/test-integration/db/src/main/java/org/teiid/test/framework/datasource/DataSourceFactory.java 2009-11-16
20:57:31 UTC (rev 1554)
@@ -6,12 +6,14 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Properties;
import java.util.Set;
import org.teiid.test.framework.ConfigPropertyLoader;
import org.teiid.test.framework.ConfigPropertyNames;
import org.teiid.test.framework.exception.QueryTestFailedException;
+import com.metamatrix.common.util.PropertiesUtils;
import com.metamatrix.core.util.StringUtil;
@@ -87,16 +89,19 @@
// the DO_NO_USE_DEFAULT will be passed in when the test are run from maven and no
property is passed in for UseDataSources
private static final String DO_NOT_USE_DEFAULT="${usedatasources}";
- private ConfigPropertyLoader configprops;
-
+
+ private DataSourceMgr dsmgr = DataSourceMgr.getInstance();
+
+// private ConfigPropertyLoader configprops;
+
+ private Properties configprops;
+
// contains the names of the datasources when the -Dusedatasources option is used
private Map<String, String> useDS = null;
// contains all the datasources available to be used
private Map<String, DataSource> availDS = null;
- // map of the datasources assigned to with model
- private Map<String, DataSource> modelToDatasourceMap = new HashMap<String,
DataSource>(); // key = modelname
// contains any dbtype preconditions on a model, which requires a model to be assigned a
certain database type
private Map<String, String> requiredDataBaseTypes = null; // key=modelname
value=dbtype
@@ -115,10 +120,14 @@
public DataSourceFactory(ConfigPropertyLoader config) {
- this.configprops = config;
+ this.configprops = PropertiesUtils.clone(config.getProperties());
this.requiredDataBaseTypes = config.getModelAssignedDatabaseTypes();
config();
}
+
+ public Properties getConfigProperties() {
+ return this.configprops;
+ }
/**
* config is called at the start / setup of the {@link
@@ -135,10 +144,24 @@
private void config() {
System.out.println("Configure Datasource Factory ");
- Map<String, DataSource> availDatasources =
DataSourceMgr.getInstance().getDataSources();
+ Map<String, DataSource> availDatasources = dsmgr.getDataSources();
availDS = new HashMap<String, DataSource>(availDatasources.size());
+ String usedstypeprop = configprops
+ .getProperty(ConfigPropertyNames.USE_DATASOURCE_TYPES_PROP);
+
+ Set<String> useDBTypes = null;
+
+ if (usedstypeprop != null && usedstypeprop.length() > 0) {
+ List<String> eprops = StringUtil.split(usedstypeprop, ",");
+ useDBTypes = new HashSet<String>(eprops.size());
+ useDBTypes.addAll(eprops);
+ System.out.println("EXCLUDE datasources: " + usedstypeprop);
+ } else {
+ useDBTypes = Collections.EMPTY_SET;
+ }
+
String excludeprop = configprops
.getProperty(ConfigPropertyNames.EXCLUDE_DATASBASE_TYPES_PROP);
@@ -180,43 +203,23 @@
} else {
for (Iterator<DataSource> it = availDatasources.values().iterator();
it.hasNext(); ) {
DataSource ds = it.next();
+ // if the datasource type is not excluded, then consider for usages
if (!excludedDBTypes.contains(ds.getDBType())) {
+
+ // if use a specific db type is specified, then it must match,
+ // otherwise add it to the available list
+ if (useDBTypes.size() > 0 && usedstypeprop.contains(ds.getDBType()))
{
+ availDS.put(ds.getName(), ds);
+ } else {
availDS.put(ds.getName(), ds);
-
- // availDS.putAll(availDatasources);
+ }
+
}
}
+
+
}
-// String excludeprop = configprops
-// .getProperty(ConfigPropertyNames.EXCLUDE_DATASBASE_TYPES_PROP);
-//
-// Set<String> excludedDBTypes = null;
-//
-// if (excludeprop != null && excludeprop.length() > 0) {
-// List<String> eprops = StringUtil.split(excludeprop, ",");
-// excludedDBTypes = new HashSet<String>(eprops.size());
-// excludedDBTypes.addAll(eprops);
-// System.out.println("EXCLUDE datasources: " + excludeprop);
-//
-// Iterator<DataSource> it = availDS.values().iterator();
-//
-// // go thru all the datasources and remove those that are excluded
-// while (it.hasNext()) {
-// DataSource checkit = it.next();
-//
-// if (excludedDBTypes.contains(checkit.getDBType())) {
-// it.remove();
-//
-// if (useDS != null) {
-// useDS
-// }
-// }
-//
-//
-// }
-//
-// }
if (requiredDataBaseTypes != null) {
@@ -239,7 +242,8 @@
if (ds.getDBType().equalsIgnoreCase(rdbtype)) {
assignedDataSources.add(ds.getName());
- modelToDatasourceMap.put(modelName, ds);
+ dsmgr.setDataSource(modelName, ds);
+ // modelToDatasourceMap.put(modelName, ds);
metDBRequiredTypes = true;
}
@@ -280,8 +284,10 @@
// so that all future request using that group name for a specified
// model
// will use the same datasource
- if (modelToDatasourceMap.containsKey(key)) {
- return modelToDatasourceMap.get(key);
+
+ ds = dsmgr.getDataSource(key);
+ if (ds != null) {
+ return ds;
}
if (this.hasRequiredDBTypes) {
@@ -373,17 +379,18 @@
assignedDataSources.add(ds.getName());
- modelToDatasourceMap.put(key, ds);
+ dsmgr.setDataSource(key, ds);
return ds;
}
public void cleanup() {
- modelToDatasourceMap.clear();
assignedDataSources.clear();
+ requiredDataBaseTypes.clear();
- useDS = null;
+ if (useDS != null) useDS.clear();
+ if (availDS != null) availDS.clear();
}
Modified:
trunk/test-integration/db/src/main/java/org/teiid/test/framework/datasource/DataSourceMgr.java
===================================================================
---
trunk/test-integration/db/src/main/java/org/teiid/test/framework/datasource/DataSourceMgr.java 2009-11-16
20:57:15 UTC (rev 1553)
+++
trunk/test-integration/db/src/main/java/org/teiid/test/framework/datasource/DataSourceMgr.java 2009-11-16
20:57:31 UTC (rev 1554)
@@ -15,149 +15,170 @@
import org.teiid.test.framework.exception.QueryTestFailedException;
import org.teiid.test.framework.exception.TransactionRuntimeException;
-
-
/**
- * The DataSourceMgr is responsible for loading and managing the datasources defined by
the datasource
- * connection properties file. There's only a need to load the set of available
datasources once for
- * the duration of the entire test suite. And it will maintain one {@link DataSource}
for each connection.properties
- * file that it finds.
+ * The DataSourceMgr is responsible for loading and managing the datasources
+ * defined by the datasource connection properties file. There's only a need to
+ * load the set of available datasources once for the duration of the entire
+ * test suite. And it will maintain one {@link DataSource} for each
+ * connection.properties file that it finds.
*
* @author vanhalbert
*
*/
public class DataSourceMgr {
+ private static DataSourceMgr _instance = null;
- private static DataSourceMgr _instance = null;
-
- private Map<String, DataSource> allDatasourcesMap = new HashMap<String,
DataSource>(); // key=datasource name
+ private Map<String, DataSource> allDatasourcesMap = new HashMap<String,
DataSource>(); // key
+ // =
+ // datasource
+ // name
- private DataSourceMgr() {
- }
+ // map of the datasources assigned to with model
+ // because only one VDB (Transactions) is used, then this mapping can live
+ // for the
+ // duration of all tests
+ private Map<String, DataSource> modelToDatasourceMap = new HashMap<String,
DataSource>(); // key
+ // =
+ // modelname
- public static synchronized DataSourceMgr getInstance() {
- if (_instance == null) {
- _instance = new DataSourceMgr();
- try {
- _instance.loadDataSourceMappings();
- } catch (QueryTestFailedException e) {
- throw new TransactionRuntimeException(e);
- } catch (TransactionRuntimeException e) {
- throw e;
- }
+ private DataSourceMgr() {
+ }
- }
- return _instance;
+ public static synchronized DataSourceMgr getInstance() {
+ if (_instance == null) {
+ _instance = new DataSourceMgr();
+ try {
+ _instance.loadDataSourceMappings();
+ } catch (QueryTestFailedException e) {
+ throw new TransactionRuntimeException(e);
+ } catch (TransactionRuntimeException e) {
+ throw e;
+ }
+
}
-
+ return _instance;
+ }
+
public Map<String, DataSource> getDataSources() {
- Map<String, DataSource> ds = new HashMap<String,
DataSource>(allDatasourcesMap.size());
- ds.putAll(allDatasourcesMap);
- return ds;
+ Map<String, DataSource> ds = new HashMap<String, DataSource>(
+ allDatasourcesMap.size());
+ ds.putAll(allDatasourcesMap);
+ return ds;
}
-
- public int numberOfAvailDataSources() {
- return allDatasourcesMap.size();
+
+ public int numberOfAvailDataSources() {
+ return allDatasourcesMap.size();
+ }
+
+ public DataSource getDataSource(String modelname) {
+ if (modelToDatasourceMap.containsKey(modelname)) {
+ return modelToDatasourceMap.get(modelname);
}
-
+ return null;
+ }
- private void loadDataSourceMappings()
- throws QueryTestFailedException {
-
-
- File[] dirs =
findAllChildDirectories("./target/classes/datasources/");
- if (dirs == null || dirs.length == 0) {
- throw new TransactionRuntimeException("No datasource directories found at
location " + "./target/classes/datasources/");
- }
- for (int i = 0; i < dirs.length; i++) {
- File d = dirs[i];
-
- String dname = d.getName();
-
- addDataSource(dname, d.getName(),allDatasourcesMap);
-
- }
-
- if (allDatasourcesMap == null || allDatasourcesMap.isEmpty()) {
- throw new TransactionRuntimeException(
- "Error: No Datasources were loaded.");
- }
-
- System.out.println("Number of total datasource mappings loaded " +
allDatasourcesMap.size());
+ public void setDataSource(String modelName, DataSource ds) {
+ modelToDatasourceMap.put(modelName, ds);
+ }
+ private void loadDataSourceMappings() throws QueryTestFailedException {
+
+ File[] dirs = findAllChildDirectories("./target/classes/datasources/");
+ if (dirs == null || dirs.length == 0) {
+ throw new TransactionRuntimeException(
+ "No datasource directories found at location "
+ + "./target/classes/datasources/");
}
-
- /**
- * Returns a <code>File</code> array that will contain all the
directories that exist in the directory
+ for (int i = 0; i < dirs.length; i++) {
+ File d = dirs[i];
+
+ String dname = d.getName();
+
+ addDataSource(dname, d.getName(), allDatasourcesMap);
+
+ }
+
+ if (allDatasourcesMap == null || allDatasourcesMap.isEmpty()) {
+ throw new TransactionRuntimeException(
+ "Error: No Datasources were loaded.");
+ }
+
+ System.out.println("Number of total datasource mappings loaded "
+ + allDatasourcesMap.size());
+
+ }
+
+ /**
+ * Returns a <code>File</code> array that will contain all the
directories
+ * that exist in the directory
*
* @return File[] of directories in the directory
*/
private static File[] findAllChildDirectories(String dir) {
- // Find all files in the specified directory
- File mfile = new File(dir);
-
- File modelsDirFile = null;
- try {
- modelsDirFile = new File(mfile.getCanonicalPath());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ // Find all files in the specified directory
+ File mfile = new File(dir);
+
+ File modelsDirFile = null;
+ try {
+ modelsDirFile = new File(mfile.getCanonicalPath());
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ if (!modelsDirFile.exists()) {
+ return null;
+ }
+ FileFilter fileFilter = new FileFilter() {
+
+ public boolean accept(File file) {
+ if (file.isDirectory()) {
+ return true;
}
- if (!modelsDirFile.exists()) {
- return null;
- }
- FileFilter fileFilter = new FileFilter() {
- public boolean accept(File file) {
- if (file.isDirectory()) {
- return true;
- }
+ return false;
+ }
+ };
- return false;
- }
- };
+ File[] modelFiles = modelsDirFile.listFiles(fileFilter);
- File[] modelFiles = modelsDirFile.listFiles(fileFilter);
+ return modelFiles;
- return modelFiles;
-
}
-
- private void addDataSource(String dirname, String dirloc, Map<String, DataSource>
datasources) {
-
-
- String dsfile = "/datasources/" + dirloc +
"/connection.properties";
- Properties dsprops = loadProperties(dsfile);
-
- if (dsprops != null) {
- DataSource ds = new DataSource(dirname,
- "dsgroup",
- dsprops);
- datasources.put(ds.getName(), ds);
- System.out.println("Loaded datasource " + ds.getName());
- }
+ private void addDataSource(String dirname, String dirloc,
+ Map<String, DataSource> datasources) {
+ String dsfile = "/datasources/" + dirloc +
"/connection.properties";
+ Properties dsprops = loadProperties(dsfile);
+
+ if (dsprops != null) {
+
+ DataSource ds = new DataSource(dirname, "dsgroup", dsprops);
+ datasources.put(ds.getName(), ds);
+ System.out.println("Loaded datasource " + ds.getName());
+
}
+ }
- private static Properties loadProperties(String filename) {
- Properties props = null;
-
- try {
- InputStream in = DataSourceMgr.class.getResourceAsStream(filename);
- if (in != null) {
- props = new Properties();
- props.load(in);
- return props;
- }
- return null;
- } catch (IOException e) {
- throw new TransactionRuntimeException("Error loading properties from file
'"
- + filename + "'" + e.getMessage());
- }
- }
+ private static Properties loadProperties(String filename) {
+ Properties props = null;
+ try {
+ InputStream in = DataSourceMgr.class.getResourceAsStream(filename);
+ if (in != null) {
+ props = new Properties();
+ props.load(in);
+ return props;
+ }
+ return null;
+ } catch (IOException e) {
+ throw new TransactionRuntimeException(
+ "Error loading properties from file '" + filename +
"'"
+ + e.getMessage());
+ }
+ }
+
}