db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r684637 - in /db/ddlutils/trunk/src/test/java/org/apache/ddlutils: RunAllTests.java task/TestWriteDataToDatabaseCommand.java
Date Mon, 11 Aug 2008 05:40:21 GMT
Author: tomdz
Date: Sun Aug 10 22:40:21 2008
New Revision: 684637

URL: http://svn.apache.org/viewvc?rev=684637&view=rev
Log:
Added test for the writeDataToDatabase Ant task

Added:
    db/ddlutils/trunk/src/test/java/org/apache/ddlutils/task/TestWriteDataToDatabaseCommand.java
Modified:
    db/ddlutils/trunk/src/test/java/org/apache/ddlutils/RunAllTests.java

Modified: db/ddlutils/trunk/src/test/java/org/apache/ddlutils/RunAllTests.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/java/org/apache/ddlutils/RunAllTests.java?rev=684637&r1=684636&r2=684637&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/java/org/apache/ddlutils/RunAllTests.java (original)
+++ db/ddlutils/trunk/src/test/java/org/apache/ddlutils/RunAllTests.java Sun Aug 10 22:40:21
2008
@@ -59,6 +59,7 @@
 import org.apache.ddlutils.platform.TestSqlBuilder;
 import org.apache.ddlutils.platform.TestSybasePlatform;
 import org.apache.ddlutils.task.TestDropTablesCommand;
+import org.apache.ddlutils.task.TestWriteDataToDatabaseCommand;
 import org.apache.ddlutils.task.TestWriteSchemaToFileCommand;
 
 import junit.framework.Test;
@@ -144,6 +145,7 @@
         suite.addTest(TestMisc.suite());
         suite.addTest(TestDropTablesCommand.suite());
         suite.addTest(TestWriteSchemaToFileCommand.suite());
+        suite.addTest(TestWriteDataToDatabaseCommand.suite());
 
         return suite;
     }

Added: db/ddlutils/trunk/src/test/java/org/apache/ddlutils/task/TestWriteDataToDatabaseCommand.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/java/org/apache/ddlutils/task/TestWriteDataToDatabaseCommand.java?rev=684637&view=auto
==============================================================================
--- db/ddlutils/trunk/src/test/java/org/apache/ddlutils/task/TestWriteDataToDatabaseCommand.java
(added)
+++ db/ddlutils/trunk/src/test/java/org/apache/ddlutils/task/TestWriteDataToDatabaseCommand.java
Sun Aug 10 22:40:21 2008
@@ -0,0 +1,151 @@
+package org.apache.ddlutils.task;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.List;
+
+import junit.framework.Test;
+
+import org.apache.ddlutils.io.DatabaseIO;
+
+/**
+ * Tests the writeDataToDatabase sub task.
+ * 
+ * @version $Revision: $
+ */
+public class TestWriteDataToDatabaseCommand extends TestTaskBase
+{
+    /**
+     * Parameterized test case pattern.
+     * 
+     * @return The tests
+     */
+    public static Test suite() throws Exception
+    {
+        return getTests(TestWriteDataToDatabaseCommand.class);
+    }
+
+    /**
+     * Adds the writeDataToDatabase sub task to the given task, executes it, and checks its
output.
+     *
+     * @param task          The task
+     * @param dataXml       The data xml to write
+     * @param useBatchMode  Whether to use batch mode for inserting the data
+     * @param ensureFkOrder Whether to ensure foreign key order
+     */
+    private void runTask(DatabaseToDdlTask task, String dataXml, boolean useBatchMode, boolean
ensureFkOrder) throws IOException
+    {
+        WriteDataToDatabaseCommand subTask = new WriteDataToDatabaseCommand();
+        File                       tmpFile = File.createTempFile("schema", ".xml");
+        FileWriter                 writer  = null;
+
+        try
+        {
+            writer = new FileWriter(tmpFile);
+
+            writer.write(dataXml);
+            writer.close();
+
+            subTask.setDataFile(tmpFile);
+            subTask.setBatchSize(100);
+            subTask.setFailOnError(true);
+            subTask.setUseBatchMode(useBatchMode);
+            subTask.setEnsureForeignKeyOrder(ensureFkOrder);
+            task.setUseDelimitedSqlIdentifiers(getPlatform().isDelimitedIdentifierModeOn());
+            task.addWriteDataToDatabase(subTask);
+            task.setModelName("roundtriptest");
+            task.execute();
+        }
+        finally
+        {
+            if (writer != null)
+            {
+                try
+                {
+                    writer.close();
+                }
+                catch (IOException ex)
+                {
+                    getLog().error("Could not close the writer for the temporary file " +
tmpFile.getAbsolutePath(), ex);
+                }
+            }
+            if (!tmpFile.delete())
+            {
+                getLog().warn("Could not delete temporary file " + tmpFile.getAbsolutePath());
+            }
+        }
+    }
+
+    public void testSimple() throws Exception
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
+            "  <table name='roundtrip'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "</database>";
+        final String dataXml =
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<data>\n" +
+            "  <roundtrip pk='val1' avalue='1'/>\n" +
+            "  <roundtrip pk='val2' avalue='2'/>\n" +
+            "  <roundtrip pk='val3' avalue='3'/>\n" +
+            "</data>";
+
+        createDatabase(modelXml);
+
+        runTask(getDatabaseToDdlTaskInstance(), dataXml, false, false);
+
+        List beans = getRows("roundtrip");
+
+        assertEquals(3, beans.size());
+        assertEquals((Object)"val1", beans.get(0), "pk");
+        assertEquals(new Integer(1), beans.get(0), "avalue");
+        assertEquals((Object)"val2", beans.get(1), "pk");
+        assertEquals(new Integer(2), beans.get(1), "avalue");
+        assertEquals((Object)"val3", beans.get(2), "pk");
+        assertEquals(new Integer(3), beans.get(2), "avalue");
+    }
+
+    public void testBatchMode() throws Exception
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
+            "  <table name='roundtrip'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        StringBuffer dataXml = new StringBuffer();
+        final int    numObjs = 2000;
+
+        dataXml.append("<?xml version='1.0' encoding='ISO-8859-1'?>\n<data>");
+        for (int idx = 0; idx < numObjs; idx++)
+        {
+            dataXml.append("  <roundtrip pk='val");
+            dataXml.append(idx);
+            dataXml.append("' avalue='");
+            dataXml.append(idx);
+            dataXml.append("'/>\n");
+        }
+        dataXml.append("</data>");
+
+        createDatabase(modelXml);
+
+        runTask(getDatabaseToDdlTaskInstance(), dataXml.toString(), true, false);
+
+        List beans = getRows("roundtrip");
+
+        assertEquals(numObjs, beans.size());
+        for (int idx = 0; idx < numObjs; idx++)
+        {
+            assertEquals((Object)("val" + idx), beans.get(idx), "pk");
+            assertEquals(new Integer(idx),      beans.get(idx), "avalue");
+        }
+    }
+}



Mime
View raw message