db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r395500 - in /db/torque/runtime/trunk/src/test/org/apache/torque: BaseTestCase.java TorqueInstanceTest.java
Date Thu, 20 Apr 2006 06:01:03 GMT
Author: tfischer
Date: Wed Apr 19 23:01:02 2006
New Revision: 395500

URL: http://svn.apache.org/viewcvs?rev=395500&view=rev
Log:
Added more tests for correct initialisation procedure of Torque

Modified:
    db/torque/runtime/trunk/src/test/org/apache/torque/BaseTestCase.java
    db/torque/runtime/trunk/src/test/org/apache/torque/TorqueInstanceTest.java

Modified: db/torque/runtime/trunk/src/test/org/apache/torque/BaseTestCase.java
URL: http://svn.apache.org/viewcvs/db/torque/runtime/trunk/src/test/org/apache/torque/BaseTestCase.java?rev=395500&r1=395499&r2=395500&view=diff
==============================================================================
--- db/torque/runtime/trunk/src/test/org/apache/torque/BaseTestCase.java (original)
+++ db/torque/runtime/trunk/src/test/org/apache/torque/BaseTestCase.java Wed Apr 19 23:01:02
2006
@@ -29,7 +29,7 @@
 public abstract class BaseTestCase extends TestCase
 {
     /** The path to the configuration file. */
-    private static final String CONFIG_FILE
+    protected static final String CONFIG_FILE
             = "src/test/TurbineResources.properties";
 
     /** Whether torque has been initialized. */

Modified: db/torque/runtime/trunk/src/test/org/apache/torque/TorqueInstanceTest.java
URL: http://svn.apache.org/viewcvs/db/torque/runtime/trunk/src/test/org/apache/torque/TorqueInstanceTest.java?rev=395500&r1=395499&r2=395500&view=diff
==============================================================================
--- db/torque/runtime/trunk/src/test/org/apache/torque/TorqueInstanceTest.java (original)
+++ db/torque/runtime/trunk/src/test/org/apache/torque/TorqueInstanceTest.java Wed Apr 19
23:01:02 2006
@@ -1,5 +1,18 @@
 package org.apache.torque;
 
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.torque.dsfactory.DataSourceFactory;
+import org.apache.torque.map.DatabaseMap;
+import org.apache.torque.map.MapBuilder;
+import org.apache.torque.map.TableMap;
+import org.apache.torque.util.BasePeer;
+
 /*
  * Copyright 2001-2005 The Apache Software Foundation.
  *
@@ -16,18 +29,23 @@
  * limitations under the License.
  */
 
-
 /**
  * Tests the TorqueInstance Class.
  *
  * @author <a href="mailto:fischer@seitenbau.de">Thomas Fischer</a>
  * @version $Id$
  */
-public class TorqueInstanceTest extends BaseTestCase
+public class TorqueInstanceTest extends TestCase
 {  
     /** The name of the "default" dataSourceFactory" as used by Turbine */
     private static final String DEFAULT_NAME = "default";
     
+    /** 
+     * The name of the "turbine" dataSourceFactory" 
+     * as used by the Turbine configuration
+     */
+    private static final String TURBINE_NAME = "turbine";
+    
     /**
      * Creates a new instance.
      *
@@ -39,12 +57,199 @@
     }
     
     /**
+     * Tests whether registration of Map Builders works before and after
+     * initialisation of Torque.
+     * @throws Exception if an error occurs during the Test.
+     */
+    public void testClassLoading() throws Exception
+    {
+        Torque.registerMapBuilder(MapBuilderA.CLASS_NAME);
+        Torque.init(getConfiguration());
+        BasePeer.getMapBuilder(MapBuilderB.CLASS_NAME);
+        
+        DatabaseMap databaseMap = Torque.getDatabaseMap(Torque.getDefaultDB());
+        assertNotNull(databaseMap.getTable(MapBuilderA.TABLE_NAME));
+        assertNotNull(databaseMap.getTable(MapBuilderB.TABLE_NAME));
+    }
+    
+    /**
      * Checks whether a DataSourceFactory with the name 
-     * <code>DEFAULT_NAME</code> is defined (TRQS 322)
-     * @throws Exception if an error occurs during the Test
+     * <code>DEFAULT_NAME</code> is defined. (TRQS 322)
+     * @throws Exception if an error occurs during the Test.
      */
     public void testDefaultDataSourceFactory() throws Exception
     {
-        assertNotNull(Torque.getInstance().getDataSourceFactory(DEFAULT_NAME));
+        DataSourceFactory defaultDataSourceFactory 
+                = Torque.getInstance().getDataSourceFactory(DEFAULT_NAME);
+        assertNotNull(
+                "The DataSourceFactory for Key " 
+                + DEFAULT_NAME
+                + " should not be null",
+                defaultDataSourceFactory);
+        DataSourceFactory turbineDataSourceFactory 
+                = Torque.getInstance().getDataSourceFactory(DEFAULT_NAME);
+        assertSame("The default DataSourceFactory "
+                + "and the turbine DataSourceFactory "
+                + "are not the same object",
+                defaultDataSourceFactory,
+                turbineDataSourceFactory);
+    }
+
+    /**
+     * Tests whether the databaseInfo objects are filled correctly.
+     * @throws Exception if an error occurs during the Test.
+     */
+    public void testDatabases() throws Exception
+    {
+        //Torque.init(getConfiguration());
+        Map databases = Torque.getDatabases();
+        // check whether all expected databases are contained in the Map
+        assertEquals(
+                "Databases should contain 2 Databases, not " 
+                    + databases.size(),
+                databases.size(), 
+                2);
+        
+        // check that the default database and the turbine database
+        // refer to the same object
+        Database defaultDatabase = Torque.getDatabase(DEFAULT_NAME);
+        Database turbineDatabase = Torque.getDatabase(TURBINE_NAME);
+        
+        assertNotSame("The default database and the turbine database "
+                        + "are the same object",
+                    defaultDatabase,
+                    turbineDatabase);    
+    }
+    
+    public void testShutdown() throws Exception
+    {
+        // because we have not properly initialized the DataSourceFactory,
+        // closing the DatasourceFactory down would result in an error.
+        // So we have to remove the reference to the DatasourceFactory.
+        Torque.getDatabase(TURBINE_NAME).setDataSourceFactory(null);
+
+        Torque.shutdown();
+        assertFalse("Torque.isInit() should return false after shutdown",
+                Torque.isInit());
+        try
+        {
+            Torque.getDatabases();
+            fail("Torque.getDatabases() should throw an Exception "
+                    + "after shutdown");
+        }
+        catch (Exception e)
+        {
+        }
+    }
+    
+    /**
+     * Reads and returns the configuration out of the configuration file.
+     * @return
+     * @throws ConfigurationException
+     */
+    private Configuration getConfiguration() throws ConfigurationException
+    {
+        Configuration conf 
+                = new PropertiesConfiguration(BaseTestCase.CONFIG_FILE);
+        return conf;
+    }
+    
+    /**
+     * The base class for the Map Builders used in this testbed.
+     */
+    public abstract static class MapBuilderBase implements MapBuilder
+    {
+
+        /** The name of the associated table. */
+        private String tableName;
+
+        /** The database map. */
+        private DatabaseMap dbMap = null;
+
+        /**
+         * Constructs a MapBuilder.
+         * @param tableName the name of the table to register.
+         */
+        public MapBuilderBase(String tableName)
+        {
+            this.tableName = tableName;
+        }
+        
+        /**
+         * Tells us if this DatabaseMapBuilder is built so that we
+         * don't have to re-build it every time.
+         *
+         * @return true if this DatabaseMapBuilder is built
+         */
+        public boolean isBuilt()
+        {
+            return (dbMap != null);
+        }
+
+        /**
+         * Gets the databasemap this map builder built.
+         *
+         * @return the databasemap
+         */
+        public DatabaseMap getDatabaseMap()
+        {
+            return this.dbMap;
+        }
+
+        /**
+         * Builds the DatabaseMap.
+         *
+         * @throws TorqueException in an error occurs during building.
+         */
+        public void doBuild() throws TorqueException
+        {
+            dbMap = Torque.getDatabaseMap(TURBINE_NAME);
+
+            dbMap.addTable(tableName);
+            TableMap tMap = dbMap.getTable(tableName);
+
+            tMap.setPrimaryKeyMethod(TableMap.NATIVE);
+
+            tMap.setPrimaryKeyMethodInfo(tableName);
+
+            tMap.addPrimaryKey(tableName + "ID", new Integer(0));
+            tMap.addColumn(tableName + "NAME", "", 50 );
+        }
+    }
+    
+    /**
+     * Map builder implementation for testing.
+     */
+    public static class MapBuilderA extends MapBuilderBase implements MapBuilder
+    {
+        /** The name of this class. */
+        public static final String CLASS_NAME =
+            MapBuilderA.class.getName();
+        
+        /** The name of the associated table. */
+        public static final String TABLE_NAME = "a";
+
+        public MapBuilderA()
+        {
+            super(TABLE_NAME);
+        }
+    }
+    
+    /**
+     * Second map builder implementation for testing.
+     */
+    public static class MapBuilderB extends MapBuilderBase implements MapBuilder
+    {
+        /** The name of this class. */
+        public static final String CLASS_NAME =
+            MapBuilderB.class.getName();
+        
+        /** The name of the associated table. */
+        public static final String TABLE_NAME = "b";
+
+        public MapBuilderB()
+        {
+            super(TABLE_NAME);
+        }
     }
-}
+}
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message