KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > TorqueInstanceTest


1 package org.apache.torque;
2
3 import java.util.Map JavaDoc;
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  * Licensed to the Apache Software Foundation (ASF) under one
19  * or more contributor license agreements. See the NOTICE file
20  * distributed with this work for additional information
21  * regarding copyright ownership. The ASF licenses this file
22  * to you under the Apache License, Version 2.0 (the
23  * "License"); you may not use this file except in compliance
24  * with the License. You may obtain a copy of the License at
25  *
26  * http://www.apache.org/licenses/LICENSE-2.0
27  *
28  * Unless required by applicable law or agreed to in writing,
29  * software distributed under the License is distributed on an
30  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
31  * KIND, either express or implied. See the License for the
32  * specific language governing permissions and limitations
33  * under the License.
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
117     {
118         //Torque.init(getConfiguration());
119
Map JavaDoc databases = Torque.getDatabases();
120         // check whether all expected databases are contained in the Map
121
assertEquals(
122                 "Databases should contain 2 Databases, not "
123                     + databases.size(),
124                 databases.size(),
125                 2);
126
127         // check that the default database and the turbine database
128
// refer to the same object
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 JavaDoc
139     {
140         // because we have not properly initialized the DataSourceFactory,
141
// closing the DatasourceFactory down would result in an error.
142
// So we have to remove the reference to the DatasourceFactory.
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc(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 JavaDoc CLASS_NAME =
241             MapBuilderA.class.getName();
242
243         /** The name of the associated table. */
244         public static final String JavaDoc 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 JavaDoc CLASS_NAME =
259             MapBuilderB.class.getName();
260
261         /** The name of the associated table. */
262         public static final String JavaDoc TABLE_NAME = "b";
263
264         public MapBuilderB()
265         {
266             super(TABLE_NAME);
267         }
268     }
269 }
270
Popular Tags