001: package org.apache.torque;
002:
003: import java.util.Map;
004:
005: import junit.framework.TestCase;
006:
007: import org.apache.commons.configuration.Configuration;
008: import org.apache.commons.configuration.ConfigurationException;
009: import org.apache.commons.configuration.PropertiesConfiguration;
010: import org.apache.torque.adapter.DB;
011: import org.apache.torque.dsfactory.DataSourceFactory;
012: import org.apache.torque.map.DatabaseMap;
013: import org.apache.torque.map.MapBuilder;
014: import org.apache.torque.map.TableMap;
015: import org.apache.torque.util.BasePeer;
016:
017: /*
018: * Licensed to the Apache Software Foundation (ASF) under one
019: * or more contributor license agreements. See the NOTICE file
020: * distributed with this work for additional information
021: * regarding copyright ownership. The ASF licenses this file
022: * to you under the Apache License, Version 2.0 (the
023: * "License"); you may not use this file except in compliance
024: * with the License. You may obtain a copy of the License at
025: *
026: * http://www.apache.org/licenses/LICENSE-2.0
027: *
028: * Unless required by applicable law or agreed to in writing,
029: * software distributed under the License is distributed on an
030: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
031: * KIND, either express or implied. See the License for the
032: * specific language governing permissions and limitations
033: * under the License.
034: */
035:
036: /**
037: * Tests the TorqueInstance Class.
038: *
039: * @author <a href="mailto:fischer@seitenbau.de">Thomas Fischer</a>
040: * @version $Id: TorqueInstanceTest.java 473821 2006-11-11 22:37:25Z tv $
041: */
042: public class TorqueInstanceTest extends TestCase {
043: /** The name of the "default" dataSourceFactory" as used by Turbine */
044: private static final String DEFAULT_NAME = "default";
045:
046: /**
047: * The name of the "turbine" dataSourceFactory"
048: * as used by the Turbine configuration
049: */
050: private static final String TURBINE_NAME = "turbine";
051:
052: /**
053: * Creates a new instance.
054: *
055: * @param name the name of the test case to run
056: */
057: public TorqueInstanceTest(String name) {
058: super (name);
059: }
060:
061: /**
062: * Tests whether registration of Map Builders works before and after
063: * initialisation of Torque.
064: * @throws Exception if an error occurs during the Test.
065: */
066: public void testClassLoading() throws Exception {
067: Torque.registerMapBuilder(MapBuilderA.CLASS_NAME);
068: Torque.init(getConfiguration());
069: BasePeer.getMapBuilder(MapBuilderB.CLASS_NAME);
070:
071: DatabaseMap databaseMap = Torque.getDatabaseMap(Torque
072: .getDefaultDB());
073: assertNotNull(databaseMap.getTable(MapBuilderA.TABLE_NAME));
074: assertNotNull(databaseMap.getTable(MapBuilderB.TABLE_NAME));
075: }
076:
077: /**
078: * Tests whether an external adapter is loaded correctly.
079: * @throws Exception if an error occurs during the Test.
080: */
081: public void testExternalAdapter() throws Exception {
082: DB adapter = Torque.getDatabase(TURBINE_NAME).getAdapter();
083: assertNotNull(adapter);
084: }
085:
086: /**
087: * Checks whether a DataSourceFactory with the name
088: * <code>DEFAULT_NAME</code> is defined. (TRQS 322)
089: * @throws Exception if an error occurs during the Test.
090: */
091: public void testDefaultDataSourceFactory() throws Exception {
092: DataSourceFactory defaultDataSourceFactory = Torque
093: .getInstance().getDataSourceFactory(DEFAULT_NAME);
094: assertNotNull("The DataSourceFactory for Key " + DEFAULT_NAME
095: + " should not be null", defaultDataSourceFactory);
096: DataSourceFactory turbineDataSourceFactory = Torque
097: .getInstance().getDataSourceFactory(DEFAULT_NAME);
098: assertSame("The default DataSourceFactory "
099: + "and the turbine DataSourceFactory "
100: + "are not the same object", defaultDataSourceFactory,
101: turbineDataSourceFactory);
102: }
103:
104: /**
105: * Tests whether the databaseInfo objects are filled correctly.
106: * @throws Exception if an error occurs during the Test.
107: */
108: public void testDatabases() throws Exception {
109: //Torque.init(getConfiguration());
110: Map databases = Torque.getDatabases();
111: // check whether all expected databases are contained in the Map
112: assertEquals("Databases should contain 2 Databases, not "
113: + databases.size(), databases.size(), 2);
114:
115: // check that the default database and the turbine database
116: // refer to the same object
117: Database defaultDatabase = Torque.getDatabase(DEFAULT_NAME);
118: Database turbineDatabase = Torque.getDatabase(TURBINE_NAME);
119:
120: assertNotSame("The default database and the turbine database "
121: + "are the same object", defaultDatabase,
122: turbineDatabase);
123: }
124:
125: public void testShutdown() throws Exception {
126: // because we have not properly initialized the DataSourceFactory,
127: // closing the DatasourceFactory down would result in an error.
128: // So we have to remove the reference to the DatasourceFactory.
129: Torque.getDatabase(TURBINE_NAME).setDataSourceFactory(null);
130:
131: Torque.shutdown();
132: assertFalse(
133: "Torque.isInit() should return false after shutdown",
134: Torque.isInit());
135: try {
136: Torque.getDatabases();
137: fail("Torque.getDatabases() should throw an Exception "
138: + "after shutdown");
139: } catch (Exception e) {
140: }
141: }
142:
143: /**
144: * Reads and returns the configuration out of the configuration file.
145: * @return
146: * @throws ConfigurationException
147: */
148: private Configuration getConfiguration()
149: throws ConfigurationException {
150: Configuration conf = new PropertiesConfiguration(
151: BaseTestCase.CONFIG_FILE);
152: return conf;
153: }
154:
155: /**
156: * The base class for the Map Builders used in this testbed.
157: */
158: public abstract static class MapBuilderBase implements MapBuilder {
159:
160: /** The name of the associated table. */
161: private String tableName;
162:
163: /** The database map. */
164: private DatabaseMap dbMap = null;
165:
166: /**
167: * Constructs a MapBuilder.
168: * @param tableName the name of the table to register.
169: */
170: public MapBuilderBase(String tableName) {
171: this .tableName = tableName;
172: }
173:
174: /**
175: * Tells us if this DatabaseMapBuilder is built so that we
176: * don't have to re-build it every time.
177: *
178: * @return true if this DatabaseMapBuilder is built
179: */
180: public boolean isBuilt() {
181: return (dbMap != null);
182: }
183:
184: /**
185: * Gets the databasemap this map builder built.
186: *
187: * @return the databasemap
188: */
189: public DatabaseMap getDatabaseMap() {
190: return this .dbMap;
191: }
192:
193: /**
194: * Builds the DatabaseMap.
195: *
196: * @throws TorqueException in an error occurs during building.
197: */
198: public void doBuild() throws TorqueException {
199: dbMap = Torque.getDatabaseMap(TURBINE_NAME);
200:
201: dbMap.addTable(tableName);
202: TableMap tMap = dbMap.getTable(tableName);
203:
204: tMap.setPrimaryKeyMethod(TableMap.NATIVE);
205:
206: tMap.setPrimaryKeyMethodInfo(tableName);
207:
208: tMap.addPrimaryKey(tableName + "ID", new Integer(0));
209: tMap.addColumn(tableName + "NAME", "", 50);
210: }
211: }
212:
213: /**
214: * Map builder implementation for testing.
215: */
216: public static class MapBuilderA extends MapBuilderBase implements
217: MapBuilder {
218: /** The name of this class. */
219: public static final String CLASS_NAME = MapBuilderA.class
220: .getName();
221:
222: /** The name of the associated table. */
223: public static final String TABLE_NAME = "a";
224:
225: public MapBuilderA() {
226: super (TABLE_NAME);
227: }
228: }
229:
230: /**
231: * Second map builder implementation for testing.
232: */
233: public static class MapBuilderB extends MapBuilderBase implements
234: MapBuilder {
235: /** The name of this class. */
236: public static final String CLASS_NAME = MapBuilderB.class
237: .getName();
238:
239: /** The name of the associated table. */
240: public static final String TABLE_NAME = "b";
241:
242: public MapBuilderB() {
243: super(TABLE_NAME);
244: }
245: }
246: }
|