1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.dbunit;
23
24 import java.io.File;
25 import java.io.FileReader;
26 import java.sql.Connection;
27 import java.sql.DriverManager;
28
29 import org.dbunit.database.DatabaseConfig;
30 import org.dbunit.database.DatabaseConnection;
31 import org.dbunit.database.IDatabaseConnection;
32 import org.dbunit.dataset.IDataSet;
33 import org.dbunit.dataset.xml.XmlDataSet;
34 import org.dbunit.testutil.TestUtils;
35
36
37
38
39
40
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 private IDatabaseTester _databaseTester = null;
49
50 public static DatabaseEnvironment getInstance() throws Exception
51 {
52 if (INSTANCE == null)
53 {
54 DatabaseProfile profile = new DatabaseProfile(System.getProperties());
55
56 String profileName = profile.getActiveProfile();
57 if (profileName == null || profileName.equals("hsqldb"))
58 {
59 INSTANCE = new HypersonicEnvironment(profile);
60 }
61 else if (profileName.equals("oracle"))
62 {
63 INSTANCE = new OracleEnvironment(profile);
64 }
65 else if (profileName.equals("oracle10"))
66 {
67 INSTANCE = new Oracle10Environment(profile);
68 }
69 else if (profileName.equals("postgresql"))
70 {
71 INSTANCE = new PostgresqlEnvironment(profile);
72 }
73 else if (profileName.equals("mysql"))
74 {
75 INSTANCE = new MySqlEnvironment(profile);
76 }
77 else if (profileName.equals("derby"))
78 {
79 INSTANCE = new DerbyEnvironment(profile);
80 }
81 else if (profileName.equals("h2"))
82 {
83 INSTANCE = new H2Environment(profile);
84 }
85 else
86 {
87 INSTANCE = new DatabaseEnvironment(profile);
88 }
89 }
90
91 return INSTANCE;
92 }
93
94 public DatabaseEnvironment(DatabaseProfile profile) throws Exception
95 {
96 _profile = profile;
97 File file = TestUtils.getFile("xml/dataSetTest.xml");
98 _dataSet = new XmlDataSet(new FileReader(file));
99 _databaseTester = new JdbcDatabaseTester( _profile.getDriverClass(),
100 _profile.getConnectionUrl(), _profile.getUser(), _profile.getPassword(), _profile.getSchema() );
101 }
102
103 public IDatabaseConnection getConnection() throws Exception
104 {
105
106
107 if(_connection != null && _connection.getConnection().isClosed()){
108
109 _connection = null;
110 }
111
112 if (_connection == null)
113 {
114 String name = _profile.getDriverClass();
115 Class.forName(name);
116 Connection connection = DriverManager.getConnection(
117 _profile.getConnectionUrl(), _profile.getUser(),
118 _profile.getPassword());
119 _connection = new DatabaseConnection(connection,
120 _profile.getSchema());
121 }
122 return _connection;
123 }
124
125 protected void setupDatabaseConfig(DatabaseConfig config)
126 {
127
128 }
129
130 public IDatabaseTester getDatabaseTester()
131 {
132 return _databaseTester;
133 }
134
135 public void closeConnection() throws Exception
136 {
137 if (_connection != null)
138 {
139 _connection.close();
140 _connection = null;
141 }
142 }
143
144 public IDataSet getInitDataSet() throws Exception
145 {
146 return _dataSet;
147 }
148
149 public DatabaseProfile getProfile() throws Exception
150 {
151 return _profile;
152 }
153
154 public boolean support(TestFeature feature)
155 {
156 String[] unsupportedFeatures = _profile.getUnsupportedFeatures();
157 for (int i = 0; i < unsupportedFeatures.length; i++)
158 {
159 String unsupportedFeature = unsupportedFeatures[i];
160 if (feature.toString().equals(unsupportedFeature))
161 {
162 return false;
163 }
164 }
165
166 return true;
167 }
168
169
170
171
172
173
174
175
176
177 public String convertString(String str)
178 {
179 return str == null ? null : str.toUpperCase();
180 }
181
182 public String toString()
183 {
184 StringBuffer sb = new StringBuffer();
185 sb.append(getClass().getName()).append("[");
186 sb.append("_profile=").append(_profile);
187 sb.append(", _connection=").append(_connection);
188 sb.append(", _dataSet=").append(_dataSet);
189 sb.append(", _databaseTester=").append(_databaseTester);
190 sb.append("]");
191 return sb.toString();
192 }
193
194 }
195
196
197
198
199
200