Return-Path: Delivered-To: apmail-db-ddlutils-dev-archive@www.apache.org Received: (qmail 56485 invoked from network); 19 Dec 2005 23:58:10 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 19 Dec 2005 23:58:10 -0000 Received: (qmail 77698 invoked by uid 500); 19 Dec 2005 23:58:09 -0000 Delivered-To: apmail-db-ddlutils-dev-archive@db.apache.org Received: (qmail 77653 invoked by uid 500); 19 Dec 2005 23:58:09 -0000 Mailing-List: contact ddlutils-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: ddlutils-dev@db.apache.org Delivered-To: mailing list ddlutils-dev@db.apache.org Received: (qmail 77642 invoked by uid 500); 19 Dec 2005 23:58:09 -0000 Delivered-To: apmail-db-ddlutils-commits@db.apache.org Received: (qmail 77639 invoked by uid 99); 19 Dec 2005 23:58:09 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 19 Dec 2005 15:58:09 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Mon, 19 Dec 2005 15:58:06 -0800 Received: (qmail 56402 invoked by uid 65534); 19 Dec 2005 23:57:46 -0000 Message-ID: <20051219235746.56401.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r357846 - in /db/ddlutils/trunk/src/test/org/apache/ddlutils/io: RoundtripTestBase.java TestRoundtripDerby.java Date: Mon, 19 Dec 2005 23:57:45 -0000 To: ddlutils-commits@db.apache.org From: tomdz@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: tomdz Date: Mon Dec 19 15:57:41 2005 New Revision: 357846 URL: http://svn.apache.org/viewcvs?rev=357846&view=rev Log: Implemented first roundtrip tests against Derby Added: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestRoundtripDerby.java Added: 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=357846&view=auto ============================================================================== --- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java (added) +++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java Mon Dec 19 15:57:41 2005 @@ -0,0 +1,147 @@ +package org.apache.ddlutils.io; + +/* + * Copyright 1999-2005 The Apache Software Foundation. + * + * Licensed 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.List; + +import org.apache.commons.beanutils.DynaBean; +import org.apache.ddlutils.TestDatabaseWriterBase; +import org.apache.ddlutils.model.IndexColumn; +import org.apache.ddlutils.model.Table; +import org.apache.ddlutils.model.UniqueIndex; + +/** + * Base class for database roundtrip (creation & reconstruction from the database). + * + * @author Thomas Dudziak + * @version $Revision: 289996 $ + */ +public abstract class RoundtripTestBase extends TestDatabaseWriterBase +{ + /** Test model with a simple BIT column. */ + protected static final String TEST_BIT_MODEL = + "\n"+ + "\n"+ + " \n"+ + " \n"+ + " \n"+ + "
\n"+ + "
"; + /** Test model with a BIT column with a default value. */ + protected static final String TEST_BIT_MODEL_WITH_DEFAULT = + "\n"+ + "\n"+ + " \n"+ + " \n"+ + " \n"+ + "
\n"+ + "
"; + /** Test model with a simple BOOLEAN column. */ + protected static final String TEST_BOOLEAN_MODEL = + "\n"+ + "\n"+ + " \n"+ + " \n"+ + " \n"+ + "
\n"+ + "
"; + + /** + * Inserts a row into the designated table. + * + * @param tableName The name of the table (case insensitive) + * @param columnValues The values for the columns in order of definition + */ + protected void insertRow(String tableName, Object[] columnValues) + { + Table table = getModel().findTable(tableName); + DynaBean bean = getModel().createDynaBeanFor(table); + + for (int idx = 0; (idx < table.getColumnCount()) && (idx < columnValues.length); idx++) + { + bean.set(table.getColumn(idx).getName(), columnValues[idx]); + } + getPlatform().insert(getModel(), bean); + } + + /** + * Retrieves all rows from the given table. + * + * @param tableName The table + * @return The rows + */ + protected List getRows(String tableName) + { + Table table = getModel().findTable(tableName); + + return getPlatform().fetch(getModel(), + "SELECT * FROM " + tableName, + new Table[] { table }); + } + + /** + * 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); + } + } + + /** + * Compares the attribute value of the given bean to the expected object. + * + * @param expected The expected object + * @param bean The bean + * @param attrName The attribute name + */ + protected void assertEquals(Object expected, Object bean, String attrName) + { + DynaBean dynaBean = (DynaBean)bean; + + assertEquals(expected, + dynaBean.get(attrName)); + } + + // boolean/bit columns + // boolean with default value + // simple bit + // bit with default value + + // numerical columns + // numeric/decimal incl. precision/scale + // char/varchar columns incl. different sizes + // time columns + // binary/varbinary & java_object etc. columns + // blob/clob columns + + // auto-increment + // default values + // null/not null + // pk (incl. pk with auto-increment) + // index/unique (incl. for pks) + // fks (incl. multiple columns, circular references) +} Added: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestRoundtripDerby.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestRoundtripDerby.java?rev=357846&view=auto ============================================================================== --- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestRoundtripDerby.java (added) +++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestRoundtripDerby.java Mon Dec 19 15:57:41 2005 @@ -0,0 +1,95 @@ +package org.apache.ddlutils.io; + +import java.util.List; + +import org.apache.ddlutils.model.Database; + +/** + * Performs the roundtrip test against a derby database. + */ +public class TestRoundtripDerby extends RoundtripTestBase +{ + /** + * Tests a simple BIT column. + */ + public void testBit() + { + createDatabase(TEST_BIT_MODEL); + insertRow("ROUNDTRIP", new Object[] { new Integer(1), Boolean.TRUE }); + insertRow("ROUNDTRIP", new Object[] { new Integer(2), Boolean.FALSE }); + + List beans = getRows("ROUNDTRIP"); + + assertEquals(Boolean.TRUE, beans.get(0), "VALUE"); + assertEquals(Boolean.FALSE, beans.get(1), "VALUE"); + + Database db = getPlatform().readModelFromDatabase(); + + db.setName("roundtriptest"); + + // Derby does not have a boolean type, so it gets mapped to SMALLINT + // we therefore adjust the original model according to our expectations + getModel().getTable(0).getColumn(1).setType("SMALLINT"); + + // Also we get a unique index for the PK + addPrimaryKeyUniqueIndicesToModel(); + + assertEquals(getModel(), db); + } + + /** + * Tests a BIT column with a default value. + */ + public void testBitWithDefault() + { + createDatabase(TEST_BIT_MODEL_WITH_DEFAULT); + insertRow("ROUNDTRIP", new Object[] { new Integer(1) }); + insertRow("ROUNDTRIP", new Object[] { new Integer(2), Boolean.TRUE }); + + List beans = getRows("ROUNDTRIP"); + + assertEquals(Boolean.FALSE, beans.get(0), "VALUE"); + assertEquals(Boolean.TRUE, beans.get(1), "VALUE"); + + Database db = getPlatform().readModelFromDatabase(); + + db.setName("roundtriptest"); + + // Derby does not have a boolean type, so it gets mapped to SMALLINT + // we therefore adjust the original model according to our expectations + getModel().getTable(0).getColumn(1).setType("SMALLINT"); + + // Also we get a unique index for the PK + addPrimaryKeyUniqueIndicesToModel(); + + assertEquals(getModel(), db); + } + + /** + * Tests a simple BOOLEAN column. + */ + public void testBoolean() + { + createDatabase(TEST_BOOLEAN_MODEL); + insertRow("ROUNDTRIP", new Object[] { new Integer(1), Boolean.FALSE }); + insertRow("ROUNDTRIP", new Object[] { new Integer(2), Boolean.TRUE }); + + List beans = getRows("ROUNDTRIP"); + + assertEquals(Boolean.FALSE, beans.get(0), "VALUE"); + assertEquals(Boolean.TRUE, beans.get(1), "VALUE"); + + Database db = getPlatform().readModelFromDatabase(); + + db.setName("roundtriptest"); + + // Derby does not have a boolean type, so it gets mapped to SMALLINT + // we therefore adjust the original model according to our expectations + getModel().getTable(0).getColumn(1).setType("SMALLINT"); + + // Also we get a unique index for the PK + addPrimaryKeyUniqueIndicesToModel(); + + assertEquals(getModel(), db); + } +}