You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ddlutils-dev@db.apache.org by to...@apache.org on 2005/12/28 15:04:33 UTC

svn commit: r359533 - in /db/ddlutils/trunk/src/test/org/apache/ddlutils/io: RoundtripTestBase.java TestDerbyConstraints.java TestDerbyDatatypes.java

Author: tomdz
Date: Wed Dec 28 06:04:28 2005
New Revision: 359533

URL: http://svn.apache.org/viewcvs?rev=359533&view=rev
Log:
Added more constraints tests

Modified:
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDerbyConstraints.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDerbyDatatypes.java

Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java?rev=359533&r1=359532&r2=359533&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java Wed Dec 28 06:04:28 2005
@@ -26,10 +26,9 @@
 import org.apache.ddlutils.dynabean.SqlDynaProperty;
 import org.apache.ddlutils.model.Column;
 import org.apache.ddlutils.model.Database;
-import org.apache.ddlutils.model.IndexColumn;
+import org.apache.ddlutils.model.ForeignKey;
 import org.apache.ddlutils.model.Table;
 import org.apache.ddlutils.model.TypeMap;
-import org.apache.ddlutils.model.UniqueIndex;
 import org.apache.ddlutils.platform.DefaultValueHelper;
 
 /**
@@ -512,11 +511,74 @@
         "    </index>\n"+
         "  </table>\n"+
         "</database>";
+    /** Test model with two tables and a simple foreign key relationship between them. */
+    protected static final String TEST_SIMPLE_FOREIGN_KEY_MODEL = 
+        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+        "<database name='roundtriptest'>\n"+
+        "  <table name='ROUNDTRIP_1'>\n"+
+        "    <column name='PK' type='INTEGER' primaryKey='true' required='true'/>\n"+
+        "  </table>\n"+
+        "  <table name='ROUNDTRIP_2'>\n"+
+        "    <column name='PK' type='VARCHAR' primaryKey='true' required='true'/>\n"+
+        "    <column name='VALUE' type='INTEGER' required='true'/>\n"+
+        "    <foreign-key foreignTable='ROUNDTRIP_1'>\n"+
+        "      <reference local='VALUE' foreign='PK'/>\n"+
+        "    </foreign-key>\n"+
+        "  </table>\n"+
+        "</database>";
+    /** Test model with two tables and overlapping foreign keys between them. */
+    protected static final String TEST_OVERLAPPING_FOREIGN_KEYS_MODEL = 
+        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+        "<database name='roundtriptest'>\n"+
+        "  <table name='ROUNDTRIP_1'>\n"+
+        "    <column name='PK_1' type='INTEGER' primaryKey='true' required='true'/>\n"+
+        "    <column name='PK_2' type='VARCHAR' primaryKey='true' required='true'/>\n"+
+        "  </table>\n"+
+        "  <table name='ROUNDTRIP_2'>\n"+
+        "    <column name='PK' type='VARCHAR' primaryKey='true' required='true'/>\n"+
+        "    <column name='VALUE_1' type='INTEGER' required='true'/>\n"+
+        "    <column name='VALUE_2' type='INTEGER'/>\n"+
+        "    <column name='VALUE_3' type='VARCHAR'/>\n"+
+        "    <foreign-key name='FK_1' foreignTable='ROUNDTRIP_1'>\n"+
+        "      <reference local='VALUE_1' foreign='PK_1'/>\n"+
+        "      <reference local='VALUE_3' foreign='PK_2'/>\n"+
+        "    </foreign-key>\n"+
+        "    <foreign-key foreignTable='ROUNDTRIP_1'>\n"+
+        "      <reference local='VALUE_2' foreign='PK_1'/>\n"+
+        "      <reference local='VALUE_3' foreign='PK_2'/>\n"+
+        "    </foreign-key>\n"+
+        "  </table>\n"+
+        "</database>";
+    /** Test model with two tables and circular foreign key relationships between them. */
+    protected static final String TEST_CIRCULAR_FOREIGN_KEYS_MODEL = 
+        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+        "<database name='roundtriptest'>\n"+
+        "  <table name='ROUNDTRIP_1'>\n"+
+        "    <column name='PK_1' type='INTEGER' primaryKey='true' required='true'/>\n"+
+        "    <column name='PK_2' type='VARCHAR' primaryKey='true' required='true'/>\n"+
+        "    <column name='VALUE_1' type='INTEGER'/>\n"+
+        "    <column name='VALUE_2' type='VARCHAR'/>\n"+
+        "    <foreign-key foreignTable='ROUNDTRIP_2'>\n"+
+        "      <reference local='VALUE_1' foreign='PK_1'/>\n"+
+        "      <reference local='VALUE_2' foreign='PK_2'/>\n"+
+        "    </foreign-key>\n"+
+        "  </table>\n"+
+        "  <table name='ROUNDTRIP_2'>\n"+
+        "    <column name='PK_1' type='INTEGER' primaryKey='true' required='true'/>\n"+
+        "    <column name='PK_2' type='VARCHAR' primaryKey='true' required='true'/>\n"+
+        "    <column name='VALUE_1' type='VARCHAR' required='true'/>\n"+
+        "    <column name='VALUE_2' type='INTEGER' required='true'/>\n"+
+        "    <foreign-key foreignTable='ROUNDTRIP_1'>\n"+
+        "      <reference local='VALUE_2' foreign='PK_1'/>\n"+
+        "      <reference local='VALUE_1' foreign='PK_2'/>\n"+
+        "    </foreign-key>\n"+
+        "  </table>\n"+
+        "</database>";
 
 
     // TODO: special columns (java_object, array, distinct, ...)
 
-    // fks (incl. multiple columns, circular references)
+    // fks (incl. multiple columns, multiple overlapping fks without names, circular references)
 
     /**
      * Inserts a row into the designated table.
@@ -554,32 +616,6 @@
     }
 
     /**
-     * Adds unique indices for the pks to the model (for comparison).
-     */
-    protected void addPrimaryKeyUniqueIndicesToModel()
-    {
-        for (int tableIdx = 0; tableIdx < getModel().getTableCount(); tableIdx++)
-        {
-            Table       table = getModel().getTable(tableIdx);
-            UniqueIndex index = new UniqueIndex();
-    
-            for (int pkIdx = 0; pkIdx < table.getPrimaryKeyColumns().length; pkIdx++)
-            {
-                index.addColumn(new IndexColumn(table.getPrimaryKeyColumns()[pkIdx].getName()));
-            }
-            table.addIndex(index);
-        }
-    }
-
-    /**
-     * Specifies whether the platform has unique indices for pks in the model
-     * read back from the database.
-     * 
-     * @return <code>true</code> if there will be unique indices for pks in read-back models
-     */
-    protected abstract boolean hasPkUniqueIndices();
-    
-    /**
      * Returns the original model adjusted for type changes because of the native type mappings
      * which when read back from the database will map to different types.
      * 
@@ -626,10 +662,16 @@
                         }
                     }
                 }
-            }
-            if (hasPkUniqueIndices())
-            {
-                addPrimaryKeyUniqueIndicesToModel();
+                // we also add the default names to foreign keys that are initially unnamed
+                for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++)
+                {
+                    ForeignKey fk = table.getForeignKey(fkIdx);
+
+                    if (fk.getName() == null)
+                    {
+                        fk.setName(getPlatform().getSqlBuilder().getForeignKeyName(table, fk));
+                    }
+                }
             }
             return model;
         }

Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDerbyConstraints.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDerbyConstraints.java?rev=359533&r1=359532&r2=359533&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDerbyConstraints.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDerbyConstraints.java Wed Dec 28 06:04:28 2005
@@ -51,17 +51,6 @@
     }
 
     /**
-     * Tests an auto-increment DOUBLE column. 
-     */
-    public void testAutoIncrementDoubleColumn()
-    {
-        createDatabase(TEST_AUTO_INCREMENT_DOUBLE_MODEL);
-
-        assertEquals(getAdjustedModel(),
-                     getPlatform().readModelFromDatabase("roundtriptest"));
-    }
-
-    /**
      * Tests an auto-increment primary key column. 
      */
     public void testPrimaryKeyAutoIncrementColumn()
@@ -100,6 +89,39 @@
     public void testPrimaryKeyIndex()
     {
         createDatabase(TEST_PRIMARY_KEY_INDEX_MODEL);
+
+        assertEquals(getAdjustedModel(),
+                     getPlatform().readModelFromDatabase("roundtriptest"));
+    }
+
+    /**
+     * Tests two tables with a simple foreign key relationship between them. 
+     */
+    public void testSimpleForeignKey()
+    {
+        createDatabase(TEST_SIMPLE_FOREIGN_KEY_MODEL);
+
+        assertEquals(getAdjustedModel(),
+                     getPlatform().readModelFromDatabase("roundtriptest"));
+    }
+
+    /**
+     * Tests two tables with overlapping foreign key relationships between them. 
+     */
+    public void testOverlappingForeignKeys()
+    {
+        createDatabase(TEST_OVERLAPPING_FOREIGN_KEYS_MODEL);
+
+        assertEquals(getAdjustedModel(),
+                     getPlatform().readModelFromDatabase("roundtriptest"));
+    }
+
+    /**
+     * Tests two tables with circular foreign key relationships between them. 
+     */
+    public void testCircularForeignKeys()
+    {
+        createDatabase(TEST_CIRCULAR_FOREIGN_KEYS_MODEL);
 
         assertEquals(getAdjustedModel(),
                      getPlatform().readModelFromDatabase("roundtriptest"));

Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDerbyDatatypes.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDerbyDatatypes.java?rev=359533&r1=359532&r2=359533&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDerbyDatatypes.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestDerbyDatatypes.java Wed Dec 28 06:04:28 2005
@@ -18,14 +18,6 @@
 public class TestDerbyDatatypes extends RoundtripTestBase
 {
     /**
-     * {@inheritDoc}
-     */
-    protected boolean hasPkUniqueIndices()
-    {
-        return true;
-    }
-
-    /**
      * Performs a data type test.
      * 
      * @param modelXml The model as XML