/* Generated by Together */ package org.ensembl.mart.lib.test; import java.net.URL; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import junit.framework.TestCase; import org.ensembl.mart.lib.DetailedDataSource; import org.ensembl.mart.lib.Engine; import org.ensembl.mart.lib.Query; /** * Base class for tests that sets up the logging system if necessary. * * <ol>Loads these configuration files from classpath: * <li>data/test_logging.conf - loads if it exists,otherwise * the logging level defaults to WARNING for all classes. Can be overridden * by setting the JVM parameter "java.util.logging.config.file". * <li>data/test_connection.properties - mart db connection settings. * Defaults to latest ensmart db on kaka.sanger.ac.uk if file unavailable. * <li>data/test_connection_ensj.properties - database connection settings * for ensembl database. Needed to compare output from martj with ensj. * </ol> */ public abstract class Base extends TestCase { private Logger logger = Logger.getLogger(Base.class.getName()); public final static String UNITTESTDIR = "data/unitTests"; protected final static String MARTJ_DB_CONFIG_URL = Base.UNITTESTDIR + "/connection.properties"; private URL connectionconf; private Properties p = new Properties(); protected String databaseType; protected String jdbcDriver; protected String host; protected String port; protected String databaseName; protected String schema; protected String user; protected String password; protected String connectionName; protected Engine engine; protected Query genequery = new Query(); protected Query snpquery = new Query(); protected DetailedDataSource martJDataSource; public void init() throws Exception { connectionconf = ClassLoader.getSystemResource(MARTJ_DB_CONFIG_URL); assertTrue("Failed to find connection configuration file: " + MARTJ_DB_CONFIG_URL.toString() + "\n", connectionconf != null); p.load(connectionconf.openStream()); databaseType = p.getProperty("databaseType"); if (databaseType == null) databaseType = DetailedDataSource.DEFAULTDATABASETYPE; host = p.getProperty("host"); port = p.getProperty("port"); if (port == null) port = DetailedDataSource.DEFAULTPORT; databaseName = p.getProperty("databaseName"); user = p.getProperty("user"); jdbcDriver = p.getProperty("jdbc_driver"); if (jdbcDriver == null) jdbcDriver = DetailedDataSource.DEFAULTDRIVER; password = p.getProperty("password"); String tmp = p.getProperty("connection"); connectionName = (tmp != null && tmp.length() > 1) ? tmp : DetailedDataSource.defaultName(host, port, databaseName, schema,user); } public void setUp() throws Exception { super.setUp(); init(); martJDataSource = new DetailedDataSource( databaseType, host, port, databaseName, schema, user, password, DetailedDataSource.DEFAULTPOOLSIZE, jdbcDriver, connectionName); assertTrue("Cannot connect to null DataSource\n", martJDataSource != null); assertTrue("Could not connect to mart Database with " + MARTJ_DB_CONFIG_URL + "\n", connected(martJDataSource)); engine = new Engine(); genequery.setMainTables( new String[] { "hsapiens_gene_ensembl__gene__main", "hsapiens_gene_ensembl__transcript__main" }); genequery.setPrimaryKeys(new String[] { "gene_id_key", "transcript_id_key" }); genequery.setDataSource(martJDataSource); genequery.setDataset("hsapiens_gene_ensembl"); snpquery.setMainTables(new String[] { "hsapiens_snp__snp__main" }); snpquery.setPrimaryKeys(new String[] { "snp_id_key" }); snpquery.setDataSource(martJDataSource); snpquery.setDataset("hsapiens_snp"); } public boolean connected(DetailedDataSource dsource) { boolean connected = false; Connection conf = null; try { conf = dsource.getConnection(); connected = true; } catch (SQLException e) { connected = false; } finally { DetailedDataSource.close(conf); } return connected; } public Base(String name) { super(name); String envVar = System.getProperty("java.util.logging.config.file"); if (envVar == null) Logger.getLogger("").setLevel(Level.WARNING); } }