1 package org.apache.torque;
2
3 import java.util.Map;
4
5 import junit.framework.TestCase;
6
7 import org.apache.commons.configuration.Configuration;
8 import org.apache.commons.configuration.ConfigurationException;
9 import org.apache.commons.configuration.PropertiesConfiguration;
10 import org.apache.torque.adapter.DB;
11 import org.apache.torque.dsfactory.DataSourceFactory;
12 import org.apache.torque.map.DatabaseMap;
13 import org.apache.torque.map.MapBuilder;
14 import org.apache.torque.map.TableMap;
15 import org.apache.torque.util.BasePeer;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 /***
37 * Tests the TorqueInstance Class.
38 *
39 * @author <a href="mailto:fischer@seitenbau.de">Thomas Fischer</a>
40 * @version $Id: TorqueInstanceTest.java 473821 2006-11-11 22:37:25Z tv $
41 */
42 public class TorqueInstanceTest extends TestCase
43 {
44 /*** The name of the "default" dataSourceFactory" as used by Turbine */
45 private static final String DEFAULT_NAME = "default";
46
47 /***
48 * The name of the "turbine" dataSourceFactory"
49 * as used by the Turbine configuration
50 */
51 private static final String TURBINE_NAME = "turbine";
52
53 /***
54 * Creates a new instance.
55 *
56 * @param name the name of the test case to run
57 */
58 public TorqueInstanceTest(String name)
59 {
60 super(name);
61 }
62
63 /***
64 * Tests whether registration of Map Builders works before and after
65 * initialisation of Torque.
66 * @throws Exception if an error occurs during the Test.
67 */
68 public void testClassLoading() throws Exception
69 {
70 Torque.registerMapBuilder(MapBuilderA.CLASS_NAME);
71 Torque.init(getConfiguration());
72 BasePeer.getMapBuilder(MapBuilderB.CLASS_NAME);
73
74 DatabaseMap databaseMap = Torque.getDatabaseMap(Torque.getDefaultDB());
75 assertNotNull(databaseMap.getTable(MapBuilderA.TABLE_NAME));
76 assertNotNull(databaseMap.getTable(MapBuilderB.TABLE_NAME));
77 }
78
79 /***
80 * Tests whether an external adapter is loaded correctly.
81 * @throws Exception if an error occurs during the Test.
82 */
83 public void testExternalAdapter() throws Exception
84 {
85 DB adapter = Torque.getDatabase(TURBINE_NAME).getAdapter();
86 assertNotNull(adapter);
87 }
88
89 /***
90 * Checks whether a DataSourceFactory with the name
91 * <code>DEFAULT_NAME</code> is defined. (TRQS 322)
92 * @throws Exception if an error occurs during the Test.
93 */
94 public void testDefaultDataSourceFactory() throws Exception
95 {
96 DataSourceFactory defaultDataSourceFactory
97 = Torque.getInstance().getDataSourceFactory(DEFAULT_NAME);
98 assertNotNull(
99 "The DataSourceFactory for Key "
100 + DEFAULT_NAME
101 + " should not be null",
102 defaultDataSourceFactory);
103 DataSourceFactory turbineDataSourceFactory
104 = Torque.getInstance().getDataSourceFactory(DEFAULT_NAME);
105 assertSame("The default DataSourceFactory "
106 + "and the turbine DataSourceFactory "
107 + "are not the same object",
108 defaultDataSourceFactory,
109 turbineDataSourceFactory);
110 }
111
112 /***
113 * Tests whether the databaseInfo objects are filled correctly.
114 * @throws Exception if an error occurs during the Test.
115 */
116 public void testDatabases() throws Exception
117 {
118
119 Map databases = Torque.getDatabases();
120
121 assertEquals(
122 "Databases should contain 2 Databases, not "
123 + databases.size(),
124 databases.size(),
125 2);
126
127
128
129 Database defaultDatabase = Torque.getDatabase(DEFAULT_NAME);
130 Database turbineDatabase = Torque.getDatabase(TURBINE_NAME);
131
132 assertNotSame("The default database and the turbine database "
133 + "are the same object",
134 defaultDatabase,
135 turbineDatabase);
136 }
137
138 public void testShutdown() throws Exception
139 {
140
141
142
143 Torque.getDatabase(TURBINE_NAME).setDataSourceFactory(null);
144
145 Torque.shutdown();
146 assertFalse("Torque.isInit() should return false after shutdown",
147 Torque.isInit());
148 try
149 {
150 Torque.getDatabases();
151 fail("Torque.getDatabases() should throw an Exception "
152 + "after shutdown");
153 }
154 catch (Exception e)
155 {
156 }
157 }
158
159 /***
160 * Reads and returns the configuration out of the configuration file.
161 * @return
162 * @throws ConfigurationException
163 */
164 private Configuration getConfiguration() throws ConfigurationException
165 {
166 Configuration conf
167 = new PropertiesConfiguration(BaseTestCase.CONFIG_FILE);
168 return conf;
169 }
170
171 /***
172 * The base class for the Map Builders used in this testbed.
173 */
174 public abstract static class MapBuilderBase implements MapBuilder
175 {
176
177 /*** The name of the associated table. */
178 private String tableName;
179
180 /*** The database map. */
181 private DatabaseMap dbMap = null;
182
183 /***
184 * Constructs a MapBuilder.
185 * @param tableName the name of the table to register.
186 */
187 public MapBuilderBase(String tableName)
188 {
189 this.tableName = tableName;
190 }
191
192 /***
193 * Tells us if this DatabaseMapBuilder is built so that we
194 * don't have to re-build it every time.
195 *
196 * @return true if this DatabaseMapBuilder is built
197 */
198 public boolean isBuilt()
199 {
200 return (dbMap != null);
201 }
202
203 /***
204 * Gets the databasemap this map builder built.
205 *
206 * @return the databasemap
207 */
208 public DatabaseMap getDatabaseMap()
209 {
210 return this.dbMap;
211 }
212
213 /***
214 * Builds the DatabaseMap.
215 *
216 * @throws TorqueException in an error occurs during building.
217 */
218 public void doBuild() throws TorqueException
219 {
220 dbMap = Torque.getDatabaseMap(TURBINE_NAME);
221
222 dbMap.addTable(tableName);
223 TableMap tMap = dbMap.getTable(tableName);
224
225 tMap.setPrimaryKeyMethod(TableMap.NATIVE);
226
227 tMap.setPrimaryKeyMethodInfo(tableName);
228
229 tMap.addPrimaryKey(tableName + "ID", new Integer(0));
230 tMap.addColumn(tableName + "NAME", "", 50 );
231 }
232 }
233
234 /***
235 * Map builder implementation for testing.
236 */
237 public static class MapBuilderA extends MapBuilderBase implements MapBuilder
238 {
239 /*** The name of this class. */
240 public static final String CLASS_NAME =
241 MapBuilderA.class.getName();
242
243 /*** The name of the associated table. */
244 public static final String TABLE_NAME = "a";
245
246 public MapBuilderA()
247 {
248 super(TABLE_NAME);
249 }
250 }
251
252 /***
253 * Second map builder implementation for testing.
254 */
255 public static class MapBuilderB extends MapBuilderBase implements MapBuilder
256 {
257 /*** The name of this class. */
258 public static final String CLASS_NAME =
259 MapBuilderB.class.getName();
260
261 /*** The name of the associated table. */
262 public static final String TABLE_NAME = "b";
263
264 public MapBuilderB()
265 {
266 super(TABLE_NAME);
267 }
268 }
269 }