1 21 22 package org.dbunit; 23 24 import org.dbunit.database.DatabaseConnection; 25 import org.dbunit.database.IDatabaseConnection; 26 import org.dbunit.dataset.IDataSet; 27 import org.dbunit.dataset.xml.XmlDataSet; 28 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.FileReader ; 32 import java.sql.Connection ; 33 import java.sql.DriverManager ; 34 import java.util.Properties ; 35 36 41 public class DatabaseEnvironment 42 { 43 private static DatabaseEnvironment INSTANCE = null; 44 45 private DatabaseProfile _profile = null; 46 private IDatabaseConnection _connection = null; 47 private IDataSet _dataSet = null; 48 49 public static DatabaseEnvironment getInstance() throws Exception 50 { 51 if (INSTANCE == null) 52 { 53 Properties properties = new Properties (); 54 properties.load(new FileInputStream (new File ("profile.properties"))); 55 DatabaseProfile profile = new DatabaseProfile(properties); 56 57 String profileName = profile.getActiveProfile(); 58 if (profileName == null || profileName.equals("hypersonic")) 59 { 60 INSTANCE = new HypersonicEnvironment(profile); 61 } 62 else if (profileName.equals("oracle")) 63 { 64 INSTANCE = new OracleEnvironment(profile); 65 } 66 else 67 { 68 INSTANCE = new DatabaseEnvironment(profile); 69 } 70 } 71 72 return INSTANCE; 73 } 74 75 public DatabaseEnvironment(DatabaseProfile profile) throws Exception 76 { 77 _profile = profile; 78 File file = new File ("src/xml/dataSetTest.xml"); 79 _dataSet = new XmlDataSet(new FileReader (file)); 80 } 81 82 public IDatabaseConnection getConnection() throws Exception 83 { 84 if (_connection == null) 85 { 86 String name = _profile.getDriverClass(); 87 Class.forName(name); 88 Connection connection = DriverManager.getConnection( 89 _profile.getConnectionUrl(), _profile.getUser(), 90 _profile.getPassword()); 91 _connection = new DatabaseConnection(connection, 92 _profile.getSchema()); 93 } 94 return _connection; 95 } 96 97 public void closeConnection() throws Exception 98 { 99 if (_connection != null) 100 { 101 _connection.close(); 102 _connection = null; 103 } 104 } 105 106 public IDataSet getInitDataSet() throws Exception 107 { 108 return _dataSet; 109 } 110 111 public DatabaseProfile getProfile() throws Exception 112 { 113 return _profile; 114 } 115 116 public boolean support(TestFeature feature) 117 { 118 String [] unsupportedFeatures = _profile.getUnsupportedFeatures(); 119 for (int i = 0; i < unsupportedFeatures.length; i++) 120 { 121 String unsupportedFeature = unsupportedFeatures[i]; 122 if (feature.toString().equals(unsupportedFeature)) 123 { 124 return false; 125 } 126 } 127 128 return true; 129 } 130 } 131 132 133 134 135 136 137 | Popular Tags |