db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r636151 - in /db/ddlutils/trunk/src: java/org/apache/ddlutils/alteration/ java/org/apache/ddlutils/model/ java/org/apache/ddlutils/platform/ java/org/apache/ddlutils/task/ test/org/apache/ddlutils/ test/org/apache/ddlutils/task/
Date Tue, 11 Mar 2008 23:37:57 GMT
Author: tomdz
Date: Tue Mar 11 16:37:52 2008
New Revision: 636151

URL: http://svn.apache.org/viewvc?rev=636151&view=rev
Log:
Implementation and tests for DDLUTILS-87: Add ability to specify which tables to include/exclude when reading a model from the database

Added:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ModelHelper.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/TaskHelper.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestWriteSchemaToFileCommand.java
Removed:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/RemoveTablesChange.java
Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropTablesCommand.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/RunAllTests.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestDropTablesCommand.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java?rev=636151&r1=636150&r2=636151&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java Tue Mar 11 16:37:52 2008
@@ -22,9 +22,12 @@
 import java.io.Serializable;
 import java.sql.Types;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
 
 import org.apache.commons.beanutils.DynaBean;
 import org.apache.commons.lang.builder.EqualsBuilder;
@@ -243,7 +246,7 @@
     }
 
     /**
-     * Removes the given table.
+     * Removes the given table. This method does not check whether there are foreign keys to the table.
      * 
      * @param table The table to remove
      */
@@ -256,7 +259,7 @@
     }
 
     /**
-     * Removes the indicated table.
+     * Removes the indicated table. This method does not check whether there are foreign keys to the table.
      * 
      * @param idx The index of the table to remove
      */
@@ -265,6 +268,30 @@
         _tables.remove(idx);
     }
 
+    /**
+     * Removes the given tables. This method does not check whether there are foreign keys to the tables.
+     * 
+     * @param tables The tables to remove
+     */
+    public void removeTables(Table[] tables)
+    {
+        _tables.removeAll(Arrays.asList(tables));
+    }
+
+    /**
+     * Removes all but the given tables. This method does not check whether there are foreign keys to the
+     * removed tables.
+     * 
+     * @param tables The tables to keep
+     */
+    public void removeAllTablesExcept(Table[] tables)
+    {
+        ArrayList allTables = new ArrayList(_tables);
+
+        allTables.removeAll(Arrays.asList(tables));
+        _tables.removeAll(allTables);
+    }
+
     // Helper methods
 
     /**
@@ -479,6 +506,67 @@
             }
         }
         return null;
+    }
+
+    /**
+     * Returns the indicated tables.
+     * 
+     * @param tableNames    The names of the tables
+     * @param caseSensitive Whether the case of the table names matters
+     * @return The tables
+     */
+    public Table[] findTables(String[] tableNames, boolean caseSensitive)
+    {
+        ArrayList tables = new ArrayList();
+
+        if (tableNames != null)
+        {
+            for (int idx = 0; idx < tableNames.length; idx++)
+            {
+                Table table = findTable(tableNames[idx], caseSensitive);
+
+                if (table != null)
+                {
+                    tables.add(table);
+                }
+            }
+        }
+        return (Table[])tables.toArray(new Table[tables.size()]);
+    }
+
+    /**
+     * Finds the tables whose names match the given regular expression.
+     * 
+     * @param tableNameRegExp The table name regular expression
+     * @param caseSensitive   Whether the case of the table names matters; if not, then the regular expression should
+     *                        assume that the table names are all-uppercase
+     * @return The tables
+     * @throws PatternSyntaxException If the regular expression is invalid
+     */
+    public Table[] findTables(String tableNameRegExp, boolean caseSensitive) throws PatternSyntaxException
+    {
+        ArrayList tables = new ArrayList();
+
+        if (tableNameRegExp != null)
+        {
+            Pattern pattern = Pattern.compile(tableNameRegExp);
+
+            for (Iterator tableIt = _tables.iterator(); tableIt.hasNext();)
+            {
+                Table  table     = (Table)tableIt.next();
+                String tableName = table.getName();
+
+                if (!caseSensitive)
+                {
+                    tableName = tableName.toUpperCase();
+                }
+                if (pattern.matcher(tableName).matches())
+                {
+                    tables.add(table);
+                }
+            }
+        }
+        return (Table[])tables.toArray(new Table[tables.size()]);
     }
 
     /**

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ModelHelper.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ModelHelper.java?rev=636151&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ModelHelper.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ModelHelper.java Tue Mar 11 16:37:52 2008
@@ -0,0 +1,95 @@
+package org.apache.ddlutils.model;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Contains some utility functions for working with the model classes.
+ * 
+ * @version $Revision: $
+ */
+public class ModelHelper
+{
+    /**
+     * Determines whether one of the tables in the list has a foreign key to a table outside of the list,
+     * or a table outside of the list has a foreign key to one of the tables in the list.
+     * 
+     * @param model  The database model
+     * @param tables The tables
+     * @throws ModelException If such a foreign key exists
+     */
+    public void checkForForeignKeysToAndFromTables(Database model, Table[] tables) throws ModelException
+    {
+        List tableList = Arrays.asList(tables);
+
+        for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++)
+        {
+            Table   curTable         = model.getTable(tableIdx);
+            boolean curTableIsInList = tableList.contains(curTable);
+
+            for (int fkIdx = 0; fkIdx < curTable.getForeignKeyCount(); fkIdx++)
+            {
+                ForeignKey curFk = curTable.getForeignKey(fkIdx);
+
+                if (curTableIsInList != tableList.contains(curFk.getForeignTable()))
+                {
+                    throw new ModelException("The table " + curTable.getName() + " has a foreign key to table " + curFk.getForeignTable().getName());
+                }
+            }
+        }
+    }
+
+    /**
+     * Removes all foreign keys from the tables in the list to tables outside of the list,
+     * or from tables outside of the list to tables in the list.
+     * 
+     * @param model  The database model
+     * @param tables The tables
+     */
+    public void removeForeignKeysToAndFromTables(Database model, Table[] tables)
+    {
+        List tableList = Arrays.asList(tables);
+
+        for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++)
+        {
+            Table     curTable         = model.getTable(tableIdx);
+            boolean   curTableIsInList = tableList.contains(curTable);
+            ArrayList fksToRemove      = new ArrayList();
+
+            for (int fkIdx = 0; fkIdx < curTable.getForeignKeyCount(); fkIdx++)
+            {
+                ForeignKey curFk = curTable.getForeignKey(fkIdx);
+
+                if (curTableIsInList != tableList.contains(curFk.getForeignTable()))
+                {
+                    fksToRemove.add(curFk);
+                }
+                for (Iterator fkIt = fksToRemove.iterator(); fkIt.hasNext();)
+                {
+                    curTable.removeForeignKey((ForeignKey)fkIt.next());
+                }
+            }
+        }
+    }
+}

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java?rev=636151&r1=636150&r2=636151&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java Tue Mar 11 16:37:52 2008
@@ -1201,6 +1201,11 @@
      */
     public String shortenName(String name, int desiredLength)
     {
+        if (name == null)
+        {
+            return null;
+        }
+
         // TODO: Find an algorithm that generates unique names
         int originalLength = name.length();
 

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java?rev=636151&r1=636150&r2=636151&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java Tue Mar 11 16:37:52 2008
@@ -23,6 +23,8 @@
 import java.util.StringTokenizer;
 
 import org.apache.ddlutils.model.Database;
+import org.apache.ddlutils.model.ModelHelper;
+import org.apache.ddlutils.model.Table;
 import org.apache.tools.ant.BuildException;
 
 /**
@@ -60,6 +62,14 @@
     private String _tableTypes;
     /** The name of the model read from the database. */
     private String _modelName = "unnamed";
+    /** The names of the tables to read. */
+    private String[] _includeTableNames; 
+    /** The regular expression matching the names of the tables to read. */
+    private String _includeTableNameRegExp;
+    /** The names of the tables to ignore. */
+    private String[] _excludeTableNames; 
+    /** The regular expression matching the names of the tables to ignore. */
+    private String _excludeTableNameRegExp;
 
     /**
      * Specifies the table types to be processed. More precisely, all tables that are of a
@@ -90,6 +100,70 @@
     }
 
     /**
+     * Sets the names of the tables that shall be read, as a comma-separated list. Escape a
+     * comma via '\,' if it is part of the table name. Please note that table names are
+     * not trimmed which means that whitespace characters should only be present in
+     * this string if they are actually part of the table name (i.e. in delimited
+     * identifer mode).
+     * 
+     * @param tableNameList The comma-separated list of table names
+     * @ant.not-required If no table filter is specified, then all tables will be read unless
+     *                   <code>excludeTables</code> or <code>excludeTableFilter</code> is
+     *                   specifed
+     */
+    public void setIncludeTables(String tableNameList)
+    {
+        _includeTableNames = new TaskHelper().parseCommaSeparatedStringList(tableNameList);
+    }
+
+    /**
+     * Sets the regular expression matching the names of the tables that shall be read.
+     * For case insensitive matching, an uppercase name can be assumed.
+     * 
+     * @param tableNameRegExp The regular expression; see {@link java.util.regex.Pattern}
+     *                        for details
+     * @ant.not-required If no table filter is specified, then all tables will be read unless
+     *                   <code>excludeTables</code> or <code>excludeTableFilter</code> is
+     *                   specifed
+     */
+    public void setIncludeTableFilter(String tableNameRegExp)
+    {
+        _includeTableNameRegExp = tableNameRegExp;
+    }
+
+    /**
+     * Sets the names of the tables that shall be ignored, as a comma-separated list. Escape a
+     * comma via '\,' if it is part of the table name. Please note that table names are
+     * not trimmed which means that whitespace characters should only be present in
+     * this string if they are actually part of the table name (i.e. in delimited
+     * identifer mode).
+     * 
+     * @param tableNameList The comma-separated list of table names
+     * @ant.not-required If no table filter is specified, then all tables will be read unless
+     *                   <code>includeTables</code> or <code>includeTableFilter</code> is
+     *                   specifed
+     */
+    public void setExcludeTables(String tableNameList)
+    {
+        _excludeTableNames = new TaskHelper().parseCommaSeparatedStringList(tableNameList);
+    }
+
+    /**
+     * Sets the regular expression matching the names of the tables that shall be ignored.
+     * For case insensitive matching, an uppercase name can be assumed.
+     * 
+     * @param tableNameRegExp The regular expression; see {@link java.util.regex.Pattern}
+     *                        for details
+     * @ant.not-required If no table filter is specified, then all tables will be read unless
+     *                   <code>includeTables</code> or <code>includeTableFilter</code> is
+     *                   specifed
+     */
+    public void setExcludeTableFilter(String tableNameRegExp)
+    {
+        _excludeTableNameRegExp = tableNameRegExp;
+    }
+
+    /**
      * Adds the "create dtd"-command.
      * 
      * @param command The command
@@ -188,10 +262,46 @@
 
         try
         {
-            return getPlatform().readModelFromDatabase(_modelName,
-                                                       getPlatformConfiguration().getCatalogPattern(),
-                                                       getPlatformConfiguration().getSchemaPattern(),
-                                                       getTableTypes());
+            Database model = getPlatform().readModelFromDatabase(_modelName,
+                                                                 getPlatformConfiguration().getCatalogPattern(),
+                                                                 getPlatformConfiguration().getSchemaPattern(),
+                                                                 getTableTypes());
+
+            if ((_includeTableNames != null) || (_includeTableNameRegExp != null) ||
+                (_excludeTableNames != null) || (_excludeTableNameRegExp != null))
+            {
+                ModelHelper helper = new ModelHelper();
+
+                if (_includeTableNames != null)
+                {
+                    Table[] tables = model.findTables(_includeTableNames, getPlatformConfiguration().isUseDelimitedSqlIdentifiers());
+
+                    helper.checkForForeignKeysToAndFromTables(model, tables);
+                    model.removeAllTablesExcept(tables);
+                }
+                else if (_includeTableNameRegExp != null)
+                {
+                    Table[] tables = model.findTables(_includeTableNameRegExp, getPlatformConfiguration().isUseDelimitedSqlIdentifiers());
+
+                    helper.checkForForeignKeysToAndFromTables(model, tables);
+                    model.removeAllTablesExcept(tables);
+                }
+                if (_excludeTableNames != null)
+                {
+                    Table[] tables = model.findTables(_excludeTableNames, getPlatformConfiguration().isUseDelimitedSqlIdentifiers());
+
+                    helper.checkForForeignKeysToAndFromTables(model, tables);
+                    model.removeTables(tables);
+                }
+                else if (_excludeTableNameRegExp != null)
+                {
+                    Table[] tables = model.findTables(_excludeTableNameRegExp, getPlatformConfiguration().isUseDelimitedSqlIdentifiers());
+
+                    helper.checkForForeignKeysToAndFromTables(model, tables);
+                    model.removeTables(tables);
+                }
+            }
+            return model;
         }
         catch (Exception ex)
         {

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropTablesCommand.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropTablesCommand.java?rev=636151&r1=636150&r2=636151&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropTablesCommand.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropTablesCommand.java Tue Mar 11 16:37:52 2008
@@ -19,13 +19,12 @@
  * under the License.
  */
 
-import java.util.ArrayList;
-
 import org.apache.commons.dbcp.BasicDataSource;
 import org.apache.ddlutils.Platform;
-import org.apache.ddlutils.alteration.RemoveTablesChange;
 import org.apache.ddlutils.model.CloneHelper;
 import org.apache.ddlutils.model.Database;
+import org.apache.ddlutils.model.ModelHelper;
+import org.apache.ddlutils.model.Table;
 import org.apache.tools.ant.BuildException;
 
 /**
@@ -51,42 +50,9 @@
      * @param tableNameList The comma-separated list of table names
      * @ant.not-required If no table filter is specified, then all tables will be dropped.
      */
-    public void setTableNames(String tableNameList)
+    public void setTables(String tableNameList)
     {
-        String[]  tmpTableNames = tableNameList.split(",");
-        ArrayList tableNames    = new ArrayList();
-        String    last          = null;
-
-        for (int idx = 0; idx < tmpTableNames.length; idx++)
-        {
-            String  str         = tmpTableNames[idx];
-            int     strLen      = str.length();
-            boolean endsInSlash = (strLen > 0) && (str.charAt(strLen - 1) == '\\') &&
-                                  ((strLen == 1) || (str.charAt(strLen - 2) != '\\'));
-
-            if (last != null)
-            {
-                last += "," + str;
-                if (!endsInSlash)
-                {
-                    tableNames.add(last);
-                    last = null;
-                }
-            }
-            else if (endsInSlash)
-            {
-                last = str.substring(0, strLen - 1);
-            }
-            else
-            {
-                tableNames.add(str);
-            }
-        }
-        if (last != null)
-        {
-            tableNames.add(last + ",");
-        }
-        _tableNames = (String[])tableNames.toArray(new String[tableNames.size()]);
+        _tableNames = new TaskHelper().parseCommaSeparatedStringList(tableNameList);
     }
 
     /**
@@ -98,7 +64,7 @@
      *                        for details
      * @ant.not-required If no table filter is specified, then all tables will be dropped.
      */
-    public void setTableNameRegExp(String tableNameRegExp)
+    public void setTableFilter(String tableNameRegExp)
     {
         _tableNameRegExp = tableNameRegExp;
     }
@@ -128,12 +94,14 @@
 
         if ((_tableNames != null) || (_tableNameRegExp != null))
         {
-            RemoveTablesChange change = _tableNames != null ? new RemoveTablesChange(_tableNames)
-                                                            : new RemoveTablesChange(_tableNameRegExp);
-
             targetModel = new CloneHelper().clone(model);
             targetModel.initialize();
-            change.apply(targetModel, task.isUseDelimitedSqlIdentifiers());
+
+            Table[] tables = _tableNames != null ? targetModel.findTables(_tableNames, task.isUseDelimitedSqlIdentifiers())
+                                                 : targetModel.findTables(_tableNameRegExp, task.isUseDelimitedSqlIdentifiers());
+
+            new ModelHelper().removeForeignKeysToAndFromTables(targetModel, tables);
+            targetModel.removeTables(tables);
         }
         try
         {

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/TaskHelper.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/TaskHelper.java?rev=636151&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/TaskHelper.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/TaskHelper.java Tue Mar 11 16:37:52 2008
@@ -0,0 +1,75 @@
+package org.apache.ddlutils.task;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ArrayList;
+
+/**
+ * Contains some utility functions for the Ant tasks.
+ * 
+ * @version $Revision: $
+ */
+public class TaskHelper
+{
+    /**
+     * Parses the given comma-separated string list. A comma within a string needs to be
+     * escaped as '\,'. Also, the individual strings are not trimmed but returned as-is.
+     * 
+     * @param stringList The comma-separated list of strings
+     * @return The strings as an array
+     */
+    public String[] parseCommaSeparatedStringList(String stringList)
+    {
+        String[]  tokens = stringList.split(",");
+        ArrayList values = new ArrayList();
+        String    last   = null;
+
+        for (int idx = 0; idx < tokens.length; idx++)
+        {
+            String  str         = tokens[idx];
+            int     strLen      = str.length();
+            boolean endsInSlash = (strLen > 0) && (str.charAt(strLen - 1) == '\\') &&
+                                  ((strLen == 1) || (str.charAt(strLen - 2) != '\\'));
+
+            if (last != null)
+            {
+                last += "," + str;
+                if (!endsInSlash)
+                {
+                    values.add(last);
+                    last = null;
+                }
+            }
+            else if (endsInSlash)
+            {
+                last = str.substring(0, strLen - 1);
+            }
+            else
+            {
+                values.add(str);
+            }
+        }
+        if (last != null)
+        {
+            values.add(last + ",");
+        }
+        return (String[])values.toArray(new String[values.size()]);
+    }
+}

Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/RunAllTests.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/RunAllTests.java?rev=636151&r1=636150&r2=636151&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/RunAllTests.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/RunAllTests.java Tue Mar 11 16:37:52 2008
@@ -54,6 +54,7 @@
 import org.apache.ddlutils.platform.TestSqlBuilder;
 import org.apache.ddlutils.platform.TestSybasePlatform;
 import org.apache.ddlutils.task.TestDropTablesCommand;
+import org.apache.ddlutils.task.TestWriteSchemaToFileCommand;
 
 import junit.framework.Test;
 import junit.framework.TestCase;
@@ -135,6 +136,7 @@
             suite.addTestSuite(TestDropColumn.class);
             suite.addTestSuite(TestMisc.class);
             suite.addTestSuite(TestDropTablesCommand.class);
+            suite.addTestSuite(TestWriteSchemaToFileCommand.class);
         }
 
         return suite;

Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestDropTablesCommand.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestDropTablesCommand.java?rev=636151&r1=636150&r2=636151&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestDropTablesCommand.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestDropTablesCommand.java Tue Mar 11 16:37:52 2008
@@ -298,7 +298,7 @@
         DatabaseToDdlTask task    = getDatabaseToDdlTaskInstance();
         DropTablesCommand subTask = new DropTablesCommand();
 
-        subTask.setTableNames("roundtrip1");
+        subTask.setTables("roundtrip1");
         task.addDropTables(subTask);
         task.execute();
 
@@ -336,7 +336,7 @@
         DatabaseToDdlTask task    = getDatabaseToDdlTaskInstance();
         DropTablesCommand subTask = new DropTablesCommand();
 
-        subTask.setTableNames("roundtrip1,roundtrip2,roundtrip3");
+        subTask.setTables("roundtrip1,roundtrip2,roundtrip3");
         task.addDropTables(subTask);
         task.execute();
 
@@ -388,7 +388,7 @@
         DatabaseToDdlTask task    = getDatabaseToDdlTaskInstance();
         DropTablesCommand subTask = new DropTablesCommand();
 
-        subTask.setTableNames("Roundtrip 1");
+        subTask.setTables("Roundtrip 1");
         task.addDropTables(subTask);
         task.setUseDelimitedSqlIdentifiers(true);
         task.execute();
@@ -433,7 +433,7 @@
         DatabaseToDdlTask task    = getDatabaseToDdlTaskInstance();
         DropTablesCommand subTask = new DropTablesCommand();
 
-        subTask.setTableNames("Roundtrip 2,Roundtrip 1");
+        subTask.setTables("Roundtrip 2,Roundtrip 1");
         task.addDropTables(subTask);
         task.setUseDelimitedSqlIdentifiers(true);
         task.execute();
@@ -486,7 +486,7 @@
         DatabaseToDdlTask task    = getDatabaseToDdlTaskInstance();
         DropTablesCommand subTask = new DropTablesCommand();
 
-        subTask.setTableNames("Roundtrip\\, 2");
+        subTask.setTables("Roundtrip\\, 2");
         task.addDropTables(subTask);
         task.setUseDelimitedSqlIdentifiers(true);
         task.execute();
@@ -539,7 +539,7 @@
         DatabaseToDdlTask task    = getDatabaseToDdlTaskInstance();
         DropTablesCommand subTask = new DropTablesCommand();
 
-        subTask.setTableNames("Roundtrip 2\\,");
+        subTask.setTables("Roundtrip 2\\,");
         task.addDropTables(subTask);
         task.setUseDelimitedSqlIdentifiers(true);
         task.execute();
@@ -584,7 +584,7 @@
         DatabaseToDdlTask task    = getDatabaseToDdlTaskInstance();
         DropTablesCommand subTask = new DropTablesCommand();
 
-        subTask.setTableNames("Roundtrip\\, 1,Roundtrip 2\\,");
+        subTask.setTables("Roundtrip\\, 1,Roundtrip 2\\,");
         task.addDropTables(subTask);
         task.setUseDelimitedSqlIdentifiers(true);
         task.execute();
@@ -623,7 +623,7 @@
         DatabaseToDdlTask task    = getDatabaseToDdlTaskInstance();
         DropTablesCommand subTask = new DropTablesCommand();
 
-        subTask.setTableNames("");
+        subTask.setTables("");
         task.addDropTables(subTask);
         task.execute();
 
@@ -669,7 +669,7 @@
         DatabaseToDdlTask task    = getDatabaseToDdlTaskInstance();
         DropTablesCommand subTask = new DropTablesCommand();
 
-        subTask.setTableNameRegExp(".*2");
+        subTask.setTableFilter(".*2");
         task.addDropTables(subTask);
         task.execute();
 
@@ -721,7 +721,7 @@
         DatabaseToDdlTask task    = getDatabaseToDdlTaskInstance();
         DropTablesCommand subTask = new DropTablesCommand();
 
-        subTask.setTableNameRegExp(".*\\s[2|3]");
+        subTask.setTableFilter(".*\\s[2|3]");
         task.addDropTables(subTask);
         task.setUseDelimitedSqlIdentifiers(true);
         task.execute();
@@ -776,7 +776,7 @@
         DatabaseToDdlTask task    = getDatabaseToDdlTaskInstance();
         DropTablesCommand subTask = new DropTablesCommand();
 
-        subTask.setTableNameRegExp(".*trip.*");
+        subTask.setTableFilter(".*trip.*");
         task.addDropTables(subTask);
         task.setUseDelimitedSqlIdentifiers(true);
         task.execute();
@@ -836,7 +836,7 @@
         DatabaseToDdlTask task    = getDatabaseToDdlTaskInstance();
         DropTablesCommand subTask = new DropTablesCommand();
 
-        subTask.setTableNameRegExp(".*\\d");
+        subTask.setTableFilter(".*\\d");
         task.addDropTables(subTask);
         task.setUseDelimitedSqlIdentifiers(true);
         task.execute();
@@ -875,7 +875,7 @@
         DatabaseToDdlTask task    = getDatabaseToDdlTaskInstance();
         DropTablesCommand subTask = new DropTablesCommand();
 
-        subTask.setTableNameRegExp(".*\\s\\D");
+        subTask.setTableFilter(".*\\s\\D");
         task.addDropTables(subTask);
         task.execute();
 
@@ -913,7 +913,7 @@
         DatabaseToDdlTask task    = getDatabaseToDdlTaskInstance();
         DropTablesCommand subTask = new DropTablesCommand();
 
-        subTask.setTableNameRegExp("");
+        subTask.setTableFilter("");
         task.addDropTables(subTask);
         task.execute();
 

Added: db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestWriteSchemaToFileCommand.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestWriteSchemaToFileCommand.java?rev=636151&view=auto
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestWriteSchemaToFileCommand.java (added)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestWriteSchemaToFileCommand.java Tue Mar 11 16:37:52 2008
@@ -0,0 +1,944 @@
+package org.apache.ddlutils.task;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.ddlutils.io.DatabaseIO;
+import org.apache.ddlutils.model.Database;
+import org.apache.tools.ant.BuildException;
+
+/**
+ * Tests the writeSchemaToFile sub task.
+ * 
+ * @version $Revision: $
+ */
+public class TestWriteSchemaToFileCommand extends TestTaskBase
+{
+    /**
+     * Adds the writeSchemaToFile sub task to the given task, executes it, and checks its output.
+     *
+     * @param task                    The task
+     * @param useDelimitedIdentifiers Whether to run with delimited identifiers
+     * @param expectedModel           The expected model
+     */
+    private void runTask(DatabaseToDdlTask task, boolean useDelimitedIdentifiers, Database expectedModel) throws IOException
+    {
+        WriteSchemaToFileCommand subTask = new WriteSchemaToFileCommand();
+        File                     tmpFile = File.createTempFile("schema", ".xml");
+
+        try
+        {
+            subTask.setOutputFile(tmpFile);
+            task.setUseDelimitedSqlIdentifiers(useDelimitedIdentifiers);
+            task.addWriteSchemaToFile(subTask);
+            task.setModelName("roundtriptest");
+            task.execute();
+
+            assertEquals(expectedModel,
+                         new DatabaseIO().read(tmpFile),
+                         useDelimitedIdentifiers);
+        }
+        finally
+        {
+            if (!tmpFile.delete())
+            {
+                getLog().warn("Could not delete temporary file " + tmpFile.getAbsolutePath());
+            }
+        }
+    }
+
+    /**
+     * Tests the task against an empty database. 
+     */
+    public void testEmptyDatabase() throws IOException
+    {
+        runTask(getDatabaseToDdlTaskInstance(), false, new Database("roundtriptest"));
+    }
+
+    /**
+     * Tests against a model with two tables and a FK. 
+     */
+    public void testSimpleModel() throws IOException
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='roundtrip1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(modelXml);
+        runTask(getDatabaseToDdlTaskInstance(), false, readModelFromDatabase("roundtriptest"));
+    }
+
+    /**
+     * Tests against a model with two tables and a FK. 
+     */
+    public void testSimpleModelWithDelimitedIdentifiers() throws IOException
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 1'>\n"+
+            "    <column name='A PK' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='A Value' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 2'>\n"+
+            "      <reference local='A Value' foreign='A PK'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 2'>\n"+
+            "    <column name='A PK' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='A Value' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 1'>\n"+
+            "      <reference local='A Value' foreign='A PK'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        getPlatform().setDelimitedIdentifierModeOn(true);
+        createDatabase(modelXml);
+        runTask(getDatabaseToDdlTaskInstance(), true, readModelFromDatabase("roundtriptest"));
+    }
+
+    /**
+     * Tests of the includeTables filter. 
+     */
+    public void testIncludeSingleTable() throws IOException
+    {
+        final String model1Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+        final String model2Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(model1Xml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setIncludeTables("roundtrip1");
+        runTask(task, false, parseDatabaseFromString(model2Xml));
+    }
+
+    /**
+     * Tests of the includeTables filter in the presence of a foreign key to the indicated table.
+     */
+    public void testIncludeSingleTableWithFk() throws IOException
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='roundtrip1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setIncludeTables("roundtrip1");
+        try
+        {
+            runTask(task, false, readModelFromDatabase("roundtriptest"));
+            fail();
+        }
+        catch (BuildException ex)
+        {
+            // expected
+        }
+    }
+
+    /**
+     * Tests of the includeTableFilter filter. 
+     */
+    public void testIncludeSingleTableViaRegExp() throws IOException
+    {
+        final String model1Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+        final String model2Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        getPlatform().setDelimitedIdentifierModeOn(true);
+        createDatabase(model1Xml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setIncludeTableFilter("Round.*\\s1");
+        runTask(task, true, parseDatabaseFromString(model2Xml));
+    }
+
+    /**
+     * Tests of the includeTableFilter filter in the presence of a foreign key to the indicated table.
+     */
+    public void testIncludeSingleTableWithFkViaRegExp() throws IOException
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        getPlatform().setDelimitedIdentifierModeOn(true);
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setIncludeTableFilter("Round.*\\s1");
+        try
+        {
+            runTask(task, true, readModelFromDatabase("roundtriptest"));
+            fail();
+        }
+        catch (BuildException ex)
+        {
+            // expected
+        }
+    }
+
+    /**
+     * Tests of the includeTables filter for multiple tables. 
+     */
+    public void testIncludeMultipleTables() throws IOException
+    {
+        final String model1Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip3'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip3'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='roundtrip1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+        final String model2Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip3'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip3'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='roundtrip1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(model1Xml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setIncludeTables("roundtrip1,roundtrip3");
+        runTask(task, false, parseDatabaseFromString(model2Xml));
+    }
+
+    /**
+     * Tests of the includeTables filter for multiple tables. 
+     */
+    public void testIncludeMultipleTablesWithFKPointingToThem() throws IOException
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip3'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='roundtrip1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setIncludeTables("roundtrip1,roundtrip3");
+        try
+        {
+            runTask(task, false, readModelFromDatabase("roundtriptest"));
+            fail();
+        }
+        catch (BuildException ex)
+        {
+            // expected
+        }
+    }
+
+    /**
+     * Tests of the includeTableFilter filter for multiple tables. 
+     */
+    public void testIncludeMultipleTablesViaRegExp() throws IOException
+    {
+        final String model1Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 3'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 3'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+        final String model2Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 3'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 3'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        getPlatform().setDelimitedIdentifierModeOn(true);
+        createDatabase(model1Xml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setIncludeTableFilter(".*trip [1|3]");
+        runTask(task, true, parseDatabaseFromString(model2Xml));
+    }
+
+    /**
+     * Tests of the includeTables filter for multiple tables via reg exp. 
+     */
+    public void testIncludeMultipleTablesWithFKPointingToThemViaRegExp() throws IOException
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 3'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 3'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 3'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        getPlatform().setDelimitedIdentifierModeOn(true);
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setIncludeTableFilter(".*trip\\s*[1|3]");
+        try
+        {
+            runTask(task, true, readModelFromDatabase("roundtriptest"));
+            fail();
+        }
+        catch (BuildException ex)
+        {
+            // expected
+        }
+    }
+
+    /**
+     * Tests of the excludeTables filter. 
+     */
+    public void testExcludeSingleTable() throws IOException
+    {
+        final String model1Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+        final String model2Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(model1Xml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setExcludeTables("roundtrip2");
+        runTask(task, false, parseDatabaseFromString(model2Xml));
+    }
+
+    /**
+     * Tests of the excludeTables filter in the presence of a foreign key to the indicated table.
+     */
+    public void testExcludeSingleTableWithFk() throws IOException
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='roundtrip1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setExcludeTables("roundtrip1");
+        try
+        {
+            runTask(task, false, readModelFromDatabase("roundtriptest"));
+            fail();
+        }
+        catch (BuildException ex)
+        {
+            // expected
+        }
+    }
+
+    /**
+     * Tests of the excludeTableFilter filter. 
+     */
+    public void testExcludeSingleTableViaRegExp() throws IOException
+    {
+        final String model1Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+        final String model2Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        getPlatform().setDelimitedIdentifierModeOn(true);
+        createDatabase(model1Xml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setExcludeTableFilter("Round.*\\s2");
+        runTask(task, true, parseDatabaseFromString(model2Xml));
+    }
+
+    /**
+     * Tests of the excludeTableFilter filter in the presence of a foreign key to the indicated table.
+     */
+    public void testExcludeSingleTableWithFkViaRegExp() throws IOException
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        getPlatform().setDelimitedIdentifierModeOn(true);
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setExcludeTableFilter("Round.*\\s2");
+        try
+        {
+            runTask(task, true, readModelFromDatabase("roundtriptest"));
+            fail();
+        }
+        catch (BuildException ex)
+        {
+            // expected
+        }
+    }
+
+    /**
+     * Tests of the excludeTables filter for multiple tables. 
+     */
+    public void testExcludeMultipleTables() throws IOException
+    {
+        final String model1Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip3'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip3'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='roundtrip1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+        final String model2Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(model1Xml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setExcludeTables("roundtrip1,roundtrip3");
+        runTask(task, false, parseDatabaseFromString(model2Xml));
+    }
+
+    /**
+     * Tests of the excludeTables filter for multiple tables. 
+     */
+    public void testExcludeMultipleTablesWithFKPointingToThem() throws IOException
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip3'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='roundtrip1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setExcludeTables("roundtrip1,roundtrip3");
+        try
+        {
+            runTask(task, false, readModelFromDatabase("roundtriptest"));
+            fail();
+        }
+        catch (BuildException ex)
+        {
+            // expected
+        }
+    }
+
+    /**
+     * Tests of the excludeTableFilter filter for multiple tables. 
+     */
+    public void testExcludeMultipleTablesViaRegExp() throws IOException
+    {
+        final String model1Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 3'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 3'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+        final String model2Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        getPlatform().setDelimitedIdentifierModeOn(true);
+        createDatabase(model1Xml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setExcludeTableFilter(".*trip [1|3]");
+        runTask(task, true, parseDatabaseFromString(model2Xml));
+    }
+
+    /**
+     * Tests of the excludeTables filter for multiple tables via reg exp. 
+     */
+    public void testExcludeMultipleTablesWithFKPointingToThemViaRegExp() throws IOException
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 3'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 3'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 3'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        getPlatform().setDelimitedIdentifierModeOn(true);
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setExcludeTableFilter(".*trip\\s*[1|3]");
+        try
+        {
+            runTask(task, true, readModelFromDatabase("roundtriptest"));
+            fail();
+        }
+        catch (BuildException ex)
+        {
+            // expected
+        }
+    }
+
+    /**
+     * Tests of the includeTables and excludeTables filters for multiple tables. 
+     */
+    public void testIncludeAndExcludeMultipleTables() throws IOException
+    {
+        final String model1Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 3'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 3'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='Roundtrip 1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='Roundtrip 4'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "</database>";
+        final String model2Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='Roundtrip 4'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        getPlatform().setDelimitedIdentifierModeOn(true);
+        createDatabase(model1Xml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setIncludeTables("Roundtrip 1,Roundtrip 3,Roundtrip 4");
+        task.setExcludeTables("Roundtrip 1,Roundtrip 3");
+        runTask(task, true, parseDatabaseFromString(model2Xml));
+    }
+
+    /**
+     * Tests of the includeTableFilter and excludeTableFilter filters for multiple tables. 
+     */
+    public void testIncludeAndExcludeMultipleTablesViaRegExp() throws IOException
+    {
+        final String model1Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip3'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip3'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='roundtrip1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip4'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "  </table>\n"+
+            "</database>";
+        final String model2Xml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(model1Xml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.setIncludeTableFilter(".*[1|2|3]");
+        task.setExcludeTableFilter(".*[1|3]");
+        runTask(task, false, parseDatabaseFromString(model2Xml));
+    }
+}



Mime
View raw message