1 25 package org.objectweb.easybeans.tests.common.core; 26 27 import java.io.FileInputStream ; 28 import java.io.IOException ; 29 import java.util.Hashtable ; 30 import java.util.Properties ; 31 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingException ; 35 36 import org.objectweb.easybeans.server.EmbeddedException; 37 import org.objectweb.easybeans.tests.common.db.DBManager; 38 39 45 public class EmbeddedTest { 46 47 50 private static final String FILE_SEPARATOR = System.getProperty("file.separator"); 51 52 55 private static final String PATH_PROPERTIES_FILE = System.getProperty("user.dir") + FILE_SEPARATOR + "tests" 56 + FILE_SEPARATOR + "conf" + FILE_SEPARATOR; 57 58 61 private static final String PROPERTIES_FILE = "dbTest.properties"; 62 63 66 private static final boolean BIND_POSTGRESQL = false; 67 68 71 private static final boolean BIND_ORACLE = false; 72 73 76 private static final boolean BIND_MYSQL = false; 77 78 81 private Properties properties = new Properties (); 82 83 87 public EmbeddedTest() throws EmbeddedException { 88 try{ 89 properties.load(new FileInputStream (PATH_PROPERTIES_FILE + PROPERTIES_FILE)); 90 }catch(IOException e){ 91 e.printStackTrace(); 92 throw new EmbeddedException("Cannot open the properties file."); 93 } 94 } 95 96 100 private Hashtable <Integer , String > createDefaultPostgreSQL() { 101 Hashtable <Integer , String > htParameters = new Hashtable <Integer , String >(); 102 htParameters.put(DBManager.JDBC_DRIVER, properties.getProperty("PostgresqlDriver")); 103 htParameters.put(DBManager.URL, properties.getProperty("PostgresqlURL")); 104 htParameters.put(DBManager.LOGIN, properties.getProperty("PostgresqlLogin")); 105 htParameters.put(DBManager.PASSWD, properties.getProperty("PostgresqlPassword")); 106 return htParameters; 107 108 } 109 110 114 private Hashtable <Integer , String > createDefaultMySQL() { 115 Hashtable <Integer , String > htParameters = new Hashtable <Integer , String >(); 116 htParameters.put(DBManager.JDBC_DRIVER, properties.getProperty("MysqlDriver")); 117 htParameters.put(DBManager.URL, properties.getProperty("MysqlURL")); 118 htParameters.put(DBManager.LOGIN, properties.getProperty("MysqlLogin")); 119 htParameters.put(DBManager.PASSWD, properties.getProperty("MysqlPassword")); 120 return htParameters; 121 122 } 123 124 128 private Hashtable <Integer , String > createDefaultOracle() { 129 Hashtable <Integer , String > htParameters = new Hashtable <Integer , String >(); 130 htParameters.put(DBManager.JDBC_DRIVER, properties.getProperty("OracleDriver")); 131 htParameters.put(DBManager.URL, properties.getProperty("OracleURL")); 132 htParameters.put(DBManager.LOGIN, properties.getProperty("OracleLogin")); 133 htParameters.put(DBManager.PASSWD, properties.getProperty("OraclePassword")); 134 return htParameters; 135 136 } 137 138 143 public void bindDatabases() throws EmbeddedException { 144 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, 146 "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory"); 147 if (BIND_POSTGRESQL) { 150 try { 151 new InitialContext ().rebind("postgresql", new DBManager(createDefaultPostgreSQL())); 152 } catch (NamingException e) { 153 throw new EmbeddedException("Cannot start the PostgresSQL database.", e); 154 } catch (ClassNotFoundException e) { 155 throw new EmbeddedException("Cannot start the Postgres database.", e); 156 } 157 } 158 if (BIND_MYSQL) { 160 try { 161 new InitialContext ().rebind("mysql", new DBManager(createDefaultMySQL())); 162 } catch (NamingException e) { 163 throw new EmbeddedException("Cannot start the MySQL database.", e); 164 } catch (ClassNotFoundException e) { 165 throw new EmbeddedException("Cannot start the MySQL database.", e); 166 } 167 } 168 169 if (BIND_ORACLE) { 171 try { 172 new InitialContext ().rebind("oracle", new DBManager(createDefaultOracle())); 173 } catch (NamingException e) { 174 throw new EmbeddedException("Cannot start the Oracle database.", e); 175 } catch (ClassNotFoundException e) { 176 throw new EmbeddedException("Cannot start the Oracle database.", e); 177 } 178 } 179 180 } 181 182 186 public void unbindDataBases() throws EmbeddedException { 187 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, 189 "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory"); 190 191 if (BIND_POSTGRESQL) { 192 unbindDataBase("postgresql"); 193 } 194 195 if (BIND_MYSQL) { 196 unbindDataBase("mysql"); 197 } 198 if (BIND_ORACLE) { 199 unbindDataBase("oracle"); 200 } 201 202 } 203 204 209 private void unbindDataBase(final String dbName) throws EmbeddedException { 210 try { 211 new InitialContext ().unbind(dbName); 212 } catch (NamingException e) { 213 throw new EmbeddedException("Cannot stop the " + dbName + "dataBase", e); 214 } 215 } 216 } 217 | Popular Tags |