View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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   * @author Manuel Laflamme
38   * @version $Revision$
39   * @since Feb 18, 2002
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         // First check if the current connection is still valid and open
106         // The connection may have been closed by a consumer
107         if(_connection != null && _connection.getConnection().isClosed()){
108             // Reset the member so that a new connection will be created
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 //         override in subclasses as necessary
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      * Returns the string converted as an identifier according to the metadata rules of the database environment.
171      * Most databases convert all metadata identifiers to uppercase.
172      * PostgreSQL converts identifiers to lowercase.
173      * MySQL preserves case.
174      * @param str The identifier.
175      * @return The identifier converted according to database rules.
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