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 35 36 42 public class TorqueInstanceTest extends TestCase 43 { 44 45 private static final String DEFAULT_NAME = "default"; 46 47 51 private static final String TURBINE_NAME = "turbine"; 52 53 58 public TorqueInstanceTest(String name) 59 { 60 super(name); 61 } 62 63 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 83 public void testExternalAdapter() throws Exception 84 { 85 DB adapter = Torque.getDatabase(TURBINE_NAME).getAdapter(); 86 assertNotNull(adapter); 87 } 88 89 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 116 public void testDatabases() throws Exception 117 { 118 Map databases = Torque.getDatabases(); 120 assertEquals( 122 "Databases should contain 2 Databases, not " 123 + databases.size(), 124 databases.size(), 125 2); 126 127 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 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 164 private Configuration getConfiguration() throws ConfigurationException 165 { 166 Configuration conf 167 = new PropertiesConfiguration(BaseTestCase.CONFIG_FILE); 168 return conf; 169 } 170 171 174 public abstract static class MapBuilderBase implements MapBuilder 175 { 176 177 178 private String tableName; 179 180 181 private DatabaseMap dbMap = null; 182 183 187 public MapBuilderBase(String tableName) 188 { 189 this.tableName = tableName; 190 } 191 192 198 public boolean isBuilt() 199 { 200 return (dbMap != null); 201 } 202 203 208 public DatabaseMap getDatabaseMap() 209 { 210 return this.dbMap; 211 } 212 213 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 237 public static class MapBuilderA extends MapBuilderBase implements MapBuilder 238 { 239 240 public static final String CLASS_NAME = 241 MapBuilderA.class.getName(); 242 243 244 public static final String TABLE_NAME = "a"; 245 246 public MapBuilderA() 247 { 248 super(TABLE_NAME); 249 } 250 } 251 252 255 public static class MapBuilderB extends MapBuilderBase implements MapBuilder 256 { 257 258 public static final String CLASS_NAME = 259 MapBuilderB.class.getName(); 260 261 262 public static final String TABLE_NAME = "b"; 263 264 public MapBuilderB() 265 { 266 super(TABLE_NAME); 267 } 268 } 269 } 270 | Popular Tags |