KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > DatabaseEnvironment


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 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 JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.FileReader JavaDoc;
32 import java.sql.Connection JavaDoc;
33 import java.sql.DriverManager JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 /**
37  * @author Manuel Laflamme
38  * @version $Revision: 1.15 $
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
49     public static DatabaseEnvironment getInstance() throws Exception JavaDoc
50     {
51         if (INSTANCE == null)
52         {
53             Properties JavaDoc properties = new Properties JavaDoc();
54             properties.load(new FileInputStream JavaDoc(new File JavaDoc("profile.properties")));
55             DatabaseProfile profile = new DatabaseProfile(properties);
56
57             String JavaDoc 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 JavaDoc
76     {
77         _profile = profile;
78         File JavaDoc file = new File JavaDoc("src/xml/dataSetTest.xml");
79         _dataSet = new XmlDataSet(new FileReader JavaDoc(file));
80     }
81
82     public IDatabaseConnection getConnection() throws Exception JavaDoc
83     {
84         if (_connection == null)
85         {
86             String JavaDoc 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 JavaDoc
98     {
99         if (_connection != null)
100         {
101             _connection.close();
102             _connection = null;
103         }
104     }
105
106     public IDataSet getInitDataSet() throws Exception JavaDoc
107     {
108         return _dataSet;
109     }
110
111     public DatabaseProfile getProfile() throws Exception JavaDoc
112     {
113         return _profile;
114     }
115
116     public boolean support(TestFeature feature)
117     {
118         String JavaDoc[] unsupportedFeatures = _profile.getUnsupportedFeatures();
119         for (int i = 0; i < unsupportedFeatures.length; i++)
120         {
121             String JavaDoc 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