From ae503e4fc04fb2a61fccb2b565f855e9e84b9bf7 Mon Sep 17 00:00:00 2001 From: chenson42 Date: Fri, 25 Jun 2010 18:03:18 +0000 Subject: [PATCH] Removed references to the Dynabeans --- .../org/jumpmind/symmetric/ddl/Platform.java | 298 --- .../ddl/dynabean/DynaClassCache.java | 149 -- .../symmetric/ddl/dynabean/SqlDynaBean.java | 70 - .../symmetric/ddl/dynabean/SqlDynaClass.java | 173 -- .../ddl/dynabean/SqlDynaException.java | 73 - .../ddl/dynabean/SqlDynaProperty.java | 87 - .../symmetric/ddl/dynabean/package.html | 35 - .../jumpmind/symmetric/ddl/io/DataReader.java | 161 -- .../jumpmind/symmetric/ddl/io/DataSink.java | 47 - .../symmetric/ddl/io/DataSinkException.java | 72 - .../symmetric/ddl/io/DataToDatabaseSink.java | 665 ----- .../jumpmind/symmetric/ddl/io/DataWriter.java | 444 ---- .../symmetric/ddl/io/DataWriterException.java | 72 - .../symmetric/ddl/io/DatabaseDataIO.java | 775 ------ .../symmetric/ddl/io/DynaSqlCreateRule.java | 83 - .../symmetric/ddl/model/Database.java | 79 - .../platform/ModelBasedResultSetIterator.java | 388 --- .../ddl/platform/PlatformImplBase.java | 1058 -------- .../postgresql/PostgreSqlPlatform.java | 32 - .../ddl/platform/sybase/SybasePlatform.java | 41 - .../ddl/task/ConvertingDatabaseCommand.java | 55 - .../symmetric/ddl/task/DatabaseToDdlTask.java | 20 - .../symmetric/ddl/task/DdlToDatabaseTask.java | 20 - .../ddl/task/WriteDataToDatabaseCommand.java | 226 -- .../ddl/task/WriteDataToFileCommand.java | 98 - .../jumpmind/symmetric/ddl/RunAllTests.java | 134 - .../symmetric/ddl/TestDatabaseWriterBase.java | 410 --- .../symmetric/ddl/TestSummaryCreatorTask.java | 447 ---- .../ddl/dynabean/TestDynaSqlQueries.java | 315 --- .../symmetric/ddl/dynabean/package.html | 29 - .../symmetric/ddl/io/RoundtripTestBase.java | 642 ----- .../symmetric/ddl/io/TestAlteration.java | 2199 ----------------- .../symmetric/ddl/io/TestConstraints.java | 442 ---- .../ddl/io/TestDataReaderAndWriter.java | 324 --- .../symmetric/ddl/io/TestDatatypes.java | 978 -------- .../jumpmind/symmetric/ddl/io/TestMisc.java | 1043 -------- .../ddl/platform/TestPlatformImplBase.java | 27 - .../ddl/util/DatabaseTestHelper.java | 212 -- 38 files changed, 12423 deletions(-) delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/DynaClassCache.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaBean.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaClass.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaException.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaProperty.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/package.html delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataReader.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataSink.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataSinkException.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataToDatabaseSink.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataWriter.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataWriterException.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DatabaseDataIO.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DynaSqlCreateRule.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/ModelBasedResultSetIterator.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/ConvertingDatabaseCommand.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/WriteDataToDatabaseCommand.java delete mode 100644 symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/WriteDataToFileCommand.java delete mode 100644 symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/RunAllTests.java delete mode 100644 symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/TestDatabaseWriterBase.java delete mode 100644 symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/TestSummaryCreatorTask.java delete mode 100644 symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/dynabean/TestDynaSqlQueries.java delete mode 100644 symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/dynabean/package.html delete mode 100644 symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/RoundtripTestBase.java delete mode 100644 symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestAlteration.java delete mode 100644 symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestConstraints.java delete mode 100644 symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestDataReaderAndWriter.java delete mode 100644 symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestDatatypes.java delete mode 100644 symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestMisc.java delete mode 100644 symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/util/DatabaseTestHelper.java diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/Platform.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/Platform.java index 230022953c..2d035f390f 100644 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/Platform.java +++ b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/Platform.java @@ -215,32 +215,6 @@ public interface Platform */ public void returnConnection(Connection connection); - /** - * Executes a series of sql statements which must be seperated by the delimiter - * configured as {@link PlatformInfo#getSqlCommandDelimiter()} of the info object - * of this platform. - * - * @param sql The sql statements to execute - * @param continueOnError Whether to continue executing the sql commands when an error occurred - * @return The number of errors - */ - public int evaluateBatch(String sql, boolean continueOnError) throws DatabaseOperationException; - - /** - * Executes a series of sql statements which must be seperated by the delimiter - * configured as {@link PlatformInfo#getSqlCommandDelimiter()} of the info object - * of this platform. - * - * TODO: consider outputting a collection of String or some kind of statement - * object from the SqlBuilder instead of having to parse strings here - * - * @param connection The connection to the database - * @param sql The sql statements to execute - * @param continueOnError Whether to continue executing the sql commands when an error occurred - * @return The number of errors - */ - public int evaluateBatch(Connection connection, String sql, boolean continueOnError) throws DatabaseOperationException; - /** * Performs a shutdown at the database. This is necessary for some embedded databases which otherwise * would be locked and thus would refuse other connections. Note that this does not change the database @@ -594,278 +568,6 @@ public interface Platform */ public void dropTables(Connection connection, Database model, boolean continueOnError) throws DatabaseOperationException; - /** - * Performs the given SQL query returning an iterator over the results. - * - * @param model The database model to use - * @param sql The sql query to perform - * @return An iterator for the dyna beans resulting from the query - */ - public Iterator query(Database model, String sql) throws DatabaseOperationException; - - /** - * Performs the given parameterized SQL query returning an iterator over the results. - * - * @param model The database model to use - * @param sql The sql query to perform - * @param parameters The query parameter values - * @return An iterator for the dyna beans resulting from the query - */ - public Iterator query(Database model, String sql, Collection parameters) throws DatabaseOperationException; - - /** - * Performs the given SQL query returning an iterator over the results. - * - * @param model The database model to use - * @param sql The sql query to perform - * @param queryHints The tables that are queried (optional) - * @return An iterator for the dyna beans resulting from the query - */ - public Iterator query(Database model, String sql, Table[] queryHints) throws DatabaseOperationException; - - /** - * Performs the given parameterized SQL query returning an iterator over the results. - * - * @param model The database model to use - * @param sql The sql query to perform - * @param parameters The query parameter values - * @param queryHints The tables that are queried (optional) - * @return An iterator for the dyna beans resulting from the query - */ - public Iterator query(Database model, String sql, Collection parameters, Table[] queryHints) throws DatabaseOperationException; - - /** - * Queries for a list of dyna beans representing rows of the given query. - * In contrast to the {@link #query(Database, String)} method all beans will be - * materialized and the connection will be closed before returning the beans. - * - * @param model The database model to use - * @param sql The sql query - * @return The dyna beans resulting from the query - */ - public List fetch(Database model, String sql) throws DatabaseOperationException; - - /** - * Queries for a list of dyna beans representing rows of the given query. - * In contrast to the {@link #query(Database, String, Collection)} method - * all beans will be materialized and the connection will be closed before - * returning the beans. - * - * @param model The database model to use - * @param sql The parameterized query - * @param parameters The parameter values - * @return The dyna beans resulting from the query - */ - public List fetch(Database model, String sql, Collection parameters) throws DatabaseOperationException; - - /** - * Queries for a list of dyna beans representing rows of the given query. - * In contrast to the {@link #query(Database, String)} method all beans will be - * materialized and the connection will be closed before returning the beans. - * - * @param model The database model to use - * @param sql The sql query - * @param queryHints The tables that are queried (optional) - * @return The dyna beans resulting from the query - */ - public List fetch(Database model, String sql, Table[] queryHints) throws DatabaseOperationException; - - /** - * Queries for a list of dyna beans representing rows of the given query. - * In contrast to the {@link #query(Database, String, Collection)} method - * all beans will be materialized and the connection will be closed before - * returning the beans. - * - * @param model The database model to use - * @param sql The parameterized query - * @param parameters The parameter values - * @param queryHints The tables that are queried (optional) - * @return The dyna beans resulting from the query - */ - public List fetch(Database model, String sql, Collection parameters, Table[] queryHints) throws DatabaseOperationException; - - /** - * Queries for a list of dyna beans representing rows of the given query. - * In contrast to the {@link #query(Database, String)} method all beans will be - * materialized and the connection will be closed before returning the beans. - * Also, the two int parameters specify which rows of the result set to use. - * If there are more rows than desired, they will be ignored (and not read - * from the database). - * - * @param model The database model to use - * @param sql The sql query - * @param start Row number to start from (0 for first row) - * @param end Row number to stop at (inclusively; -1 for last row) - * @return The dyna beans resulting from the query - */ - public List fetch(Database model, String sql, int start, int end) throws DatabaseOperationException; - - /** - * Queries for a list of dyna beans representing rows of the given query. - * In contrast to the {@link #query(Database, String, Collection)} method all - * beans will be materialized and the connection will be closed before returning - * the beans. Also, the two int parameters specify which rows of the result set - * to use. If there are more rows than desired, they will be ignored (and not - * read from the database). - * - * @param model The database model to use - * @param sql The parameterized sql query - * @param parameters The parameter values - * @param start Row number to start from (0 for first row) - * @param end Row number to stop at (inclusively; -1 for last row) - * @return The dyna beans resulting from the query - */ - public List fetch(Database model, String sql, Collection parameters, int start, int end) throws DatabaseOperationException; - - /** - * Queries for a list of dyna beans representing rows of the given query. - * In contrast to the {@link #query(Database, String, Table[])} method all - * beans will be materialized and the connection will be closed before - * returning the beans. Also, the two int parameters specify which rows of - * the result set to use. If there are more rows than desired, they will be - * ignored (and not read from the database). - * - * @param model The database model to use - * @param sql The sql query - * @param queryHints The tables that are queried (optional) - * @param start Row number to start from (0 for first row) - * @param end Row number to stop at (inclusively; -1 for last row) - * @return The dyna beans resulting from the query - */ - public List fetch(Database model, String sql, Table[] queryHints, int start, int end) throws DatabaseOperationException; - - /** - * Queries for a list of dyna beans representing rows of the given query. - * In contrast to the {@link #query(Database, String, Collection, Table[])} - * method all beans will be materialized and the connection will be closed - * before returning the beans. Also, the two int parameters specify which - * rows of the result set to use. If there are more rows than desired, they - * will be ignored (and not read from the database). - * - * @param model The database model to use - * @param sql The parameterized sql query - * @param parameters The parameter values - * @param queryHints The tables that are queried (optional) - * @param start Row number to start from (0 for first row) - * @param end Row number to stop at (inclusively; -1 for last row) - * @return The dyna beans resulting from the query - */ - public List fetch(Database model, String sql, Collection parameters, Table[] queryHints, int start, int end) throws DatabaseOperationException; - - /** - * Stores the given bean in the database, inserting it if there is no primary key - * otherwise the bean is updated in the database. - * - * @param model The database model to use - * @param dynaBean The bean to store - */ - public void store(Database model, DynaBean dynaBean) throws DatabaseOperationException; - - /** - * Returns the sql for inserting the given bean. - * - * @param model The database model to use - * @param dynaBean The bean - * @return The insert sql - */ - public String getInsertSql(Database model, DynaBean dynaBean); - - /** - * Inserts the given DynaBean in the database, assuming the primary key values are specified. - * - * @param model The database model to use - * @param dynaBean The bean to insert - */ - public void insert(Database model, DynaBean dynaBean) throws DatabaseOperationException; - - /** - * Inserts the bean. If one of the columns is an auto-incremented column, then the - * bean will also be updated with the column value generated by the database. - * Note that the connection will not be closed by this method. - * - * @param connection The database connection - * @param model The database model to use - * @param dynaBean The bean - */ - public void insert(Connection connection, Database model, DynaBean dynaBean) throws DatabaseOperationException; - - /** - * Inserts the given beans in the database, assuming the primary key values are specified. - * Note that a batch insert is used for subsequent beans of the same type. - * Also the properties for the primary keys are not updated in the beans. Hence you should - * not use this method when the primary key values are defined by the database (via a sequence - * or identity constraint). - * - * @param model The database model to use - * @param dynaBeans The beans to insert - */ - public void insert(Database model, Collection dynaBeans) throws DatabaseOperationException; - - /** - * Inserts the given beans. Note that a batch insert is used for subsequent beans of the same type. - * Also the properties for the primary keys are not updated in the beans. Hence you should - * not use this method when the primary key values are defined by the database (via a sequence - * or identity constraint). - * This method does not close the connection. - * - * @param connection The database connection - * @param model The database model to use - * @param dynaBeans The beans - */ - public void insert(Connection connection, Database model, Collection dynaBeans) throws DatabaseOperationException; - - /** - * Returns the sql for updating the given bean in the database. - * - * @param model The database model to use - * @param dynaBean The bean - * @return The update sql - */ - public String getUpdateSql(Database model, DynaBean dynaBean); - - /** - * Updates the given bean in the database, assuming the primary key values are specified. - * - * @param model The database model to use - * @param dynaBean The bean - */ - public void update(Database model, DynaBean dynaBean) throws DatabaseOperationException; - - /** - * Updates the row which maps to the given bean. - * - * @param connection The database connection - * @param model The database model to use - * @param dynaBean The bean - */ - public void update(Connection connection, Database model, DynaBean dynaBean) throws DatabaseOperationException; - - /** - * Returns the sql for deleting the given bean from the database. - * - * @param model The database model to use - * @param dynaBean The bean - * @return The sql - */ - public String getDeleteSql(Database model, DynaBean dynaBean); - - /** - * Deletes the given bean from the database, assuming the primary key values are specified. - * - * @param model The database model to use - * @param dynaBean The bean to delete - */ - public void delete(Database model, DynaBean dynaBean) throws DatabaseOperationException; - - /** - * Deletes the row which maps to the given bean from the database. - * - * @param model The database model to use - * @param dynaBean The bean - * @param connection The database connection - */ - public void delete(Connection connection, Database model, DynaBean dynaBean) throws DatabaseOperationException; - /** * Reads the database model from the live database as specified by the data source set for * this platform. diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/DynaClassCache.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/DynaClassCache.java deleted file mode 100644 index f7b1789247..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/DynaClassCache.java +++ /dev/null @@ -1,149 +0,0 @@ -package org.jumpmind.symmetric.ddl.dynabean; - -/* - * 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.lang.reflect.InvocationTargetException; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.beanutils.BeanUtils; -import org.apache.commons.beanutils.DynaBean; -import org.apache.commons.beanutils.DynaClass; -import org.jumpmind.symmetric.ddl.model.Table; - -/** - * Provides a cache of dyna class instances for a specific model, as well as - * helper methods for dealing with these classes. - * - * @version $Revision: 231110 $ - */ -public class DynaClassCache -{ - /** A cache of the SqlDynaClasses per table name. */ - private Map _dynaClassCache = new HashMap(); - - /** - * Creates a new dyna bean instance for the given table. - * - * @param table The table - * @return The new empty dyna bean - */ - public DynaBean createNewInstance(Table table) throws SqlDynaException - { - try - { - return getDynaClass(table).newInstance(); - } - catch (InstantiationException ex) - { - throw new SqlDynaException("Could not create a new dyna bean for table "+table.getName(), ex); - } - catch (IllegalAccessException ex) - { - throw new SqlDynaException("Could not create a new dyna bean for table "+table.getName(), ex); - } - } - - /** - * Creates a new dyna bean instance for the given table and copies the values from the - * given source object. The source object can be a bean, a map or a dyna bean. - * This method is useful when iterating through an arbitrary dyna bean - * result set after performing a query, then creating a copy as a DynaBean - * which is bound to a specific table. - * This new DynaBean can be kept around, changed and stored back into the database. - * - * @param table The table to create the dyna bean for - * @param source Either a bean, a {@link java.util.Map} or a dyna bean that will be used - * to populate the resultint dyna bean - * @return A new dyna bean bound to the given table and containing all the properties from - * the source object - */ - public DynaBean copy(Table table, Object source) throws SqlDynaException - { - DynaBean answer = createNewInstance(table); - - try - { - // copy all the properties from the source - BeanUtils.copyProperties(answer, source); - } - catch (InvocationTargetException ex) - { - throw new SqlDynaException("Could not populate the bean", ex); - } - catch (IllegalAccessException ex) - { - throw new SqlDynaException("Could not populate the bean", ex); - } - - return answer; - } - - /** - * Returns the {@link SqlDynaClass} for the given table. If the it does not - * exist yet, a new one will be created based on the Table definition. - * - * @param table The table - * @return The SqlDynaClass for the indicated table - */ - public SqlDynaClass getDynaClass(Table table) - { - SqlDynaClass answer = (SqlDynaClass)_dynaClassCache.get(table.getName()); - - if (answer == null) - { - answer = createDynaClass(table); - _dynaClassCache.put(table.getName(), answer); - } - return answer; - } - - /** - * Returns the {@link SqlDynaClass} for the given bean. - * - * @param dynaBean The bean - * @return The dyna bean class - */ - public SqlDynaClass getDynaClass(DynaBean dynaBean) throws SqlDynaException - { - DynaClass dynaClass = dynaBean.getDynaClass(); - - if (dynaClass instanceof SqlDynaClass) - { - return (SqlDynaClass)dynaClass; - } - else - { - // TODO: we could autogenerate an SqlDynaClass here ? - throw new SqlDynaException("The dyna bean is not an instance of a SqlDynaClass"); - } - } - - /** - * Creates a new {@link SqlDynaClass} instance for the given table based on the table definition. - * - * @param table The table - * @return The new dyna class - */ - private SqlDynaClass createDynaClass(Table table) - { - return SqlDynaClass.newInstance(table); - } -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaBean.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaBean.java deleted file mode 100644 index 0137dfe253..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaBean.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.jumpmind.symmetric.ddl.dynabean; - -/* - * 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 org.apache.commons.beanutils.BasicDynaBean; -import org.apache.commons.beanutils.DynaClass; -import org.apache.commons.beanutils.DynaProperty; - -/** - * SqlDynaBean is a DynaBean which can be persisted as a single row in - * a Database Table. - * - * @version $Revision: 463757 $ - */ -public class SqlDynaBean extends BasicDynaBean -{ - /** Unique ID for serializaion purposes. */ - private static final long serialVersionUID = -6946514447446174227L; - - /** - * Creates a new dyna bean of the given class. - * - * @param dynaClass The dyna class - */ - public SqlDynaBean(DynaClass dynaClass) - { - super(dynaClass); - } - - /** - * {@inheritDoc} - */ - public String toString() - { - StringBuffer result = new StringBuffer(); - DynaClass type = getDynaClass(); - DynaProperty[] props = type.getDynaProperties(); - - result.append(type.getName()); - result.append(": "); - for (int idx = 0; idx < props.length; idx++) - { - if (idx > 0) - { - result.append(", "); - } - result.append(props[idx].getName()); - result.append(" = "); - result.append(get(props[idx].getName())); - } - return result.toString(); - } -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaClass.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaClass.java deleted file mode 100644 index c854a9364c..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaClass.java +++ /dev/null @@ -1,173 +0,0 @@ -package org.jumpmind.symmetric.ddl.dynabean; - -/* - * 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.List; - -import org.apache.commons.beanutils.BasicDynaClass; -import org.apache.commons.beanutils.DynaProperty; -import org.jumpmind.symmetric.ddl.model.Table; - -/** - * SqlDynaClass is a DynaClass which is associated with a persistent - * Table in a Database. - * - * @version $Revision: 463757 $ - */ -public class SqlDynaClass extends BasicDynaClass -{ - /** Unique ID for serializaion purposes. */ - private static final long serialVersionUID = -5768155698352911245L; - - /** The table for which this dyna class is defined. */ - private Table _table; - /** The primary key dyna properties. */ - private SqlDynaProperty[] _primaryKeyProperties; - /** The non-primary key dyna properties. */ - private SqlDynaProperty[] _nonPrimaryKeyProperties; - - /** - * Factory method for creating and initializing a new dyna class instance - * for the given table. - * - * @param table The table - * @return The dyna class for the table - */ - public static SqlDynaClass newInstance(Table table) - { - List properties = new ArrayList(); - - for (int idx = 0; idx < table.getColumnCount(); idx++) - { - properties.add(new SqlDynaProperty(table.getColumn(idx))); - } - - SqlDynaProperty[] array = new SqlDynaProperty[properties.size()]; - - properties.toArray(array); - return new SqlDynaClass(table, array); - } - - /** - * Creates a new dyna class instance for the given table that has the given properties. - * - * @param table The table - * @param properties The dyna properties - */ - public SqlDynaClass(Table table, SqlDynaProperty[] properties) - { - super(table.getName(), SqlDynaBean.class, properties); - _table = table; - } - - /** - * Returns the table for which this dyna class is defined. - * - * @return The table - */ - public Table getTable() - { - return _table; - } - - // Helper methods - //------------------------------------------------------------------------- - - /** - * Returns the table name for which this dyna class is defined. - * - * @return The table name - */ - public String getTableName() - { - return getTable().getName(); - } - - /** - * Returns the properties of this dyna class. - * - * @return The properties - */ - public SqlDynaProperty[] getSqlDynaProperties() - { - return (SqlDynaProperty[])getDynaProperties(); - } - - /** - * Returns the properties for the primary keys of the corresponding table. - * - * @return The properties - */ - public SqlDynaProperty[] getPrimaryKeyProperties() - { - if (_primaryKeyProperties == null) - { - initPrimaryKeys(); - } - return _primaryKeyProperties; - } - - /** - * Returns the properties for the non-primary keys of the corresponding table. - * - * @return The properties - */ - public SqlDynaProperty[] getNonPrimaryKeyProperties() - { - if (_nonPrimaryKeyProperties == null) - { - initPrimaryKeys(); - } - return _nonPrimaryKeyProperties; - } - - // Implementation methods - //------------------------------------------------------------------------- - - /** - * Initializes the primary key and non primary key property arrays. - */ - protected void initPrimaryKeys() - { - List pkProps = new ArrayList(); - List nonPkProps = new ArrayList(); - DynaProperty[] properties = getDynaProperties(); - - for (int idx = 0; idx < properties.length; idx++) - { - if (properties[idx] instanceof SqlDynaProperty) - { - SqlDynaProperty sqlProperty = (SqlDynaProperty)properties[idx]; - - if (sqlProperty.isPrimaryKey()) - { - pkProps.add(sqlProperty); - } - else - { - nonPkProps.add(sqlProperty); - } - } - } - _primaryKeyProperties = (SqlDynaProperty[])pkProps.toArray(new SqlDynaProperty[pkProps.size()]); - _nonPrimaryKeyProperties = (SqlDynaProperty[])nonPkProps.toArray(new SqlDynaProperty[nonPkProps.size()]); - } -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaException.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaException.java deleted file mode 100644 index 9ef6b2ddd4..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaException.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.jumpmind.symmetric.ddl.dynabean; - -/* - * 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 org.jumpmind.symmetric.ddl.DdlUtilsException; - -/** - * This exception is thrown when something dealing with sql dyna beans or classes failed. - * - * @version $Revision: 289996 $ - */ -public class SqlDynaException extends DdlUtilsException -{ - /** Constant for serializing instances of this class. */ - private static final long serialVersionUID = 7904337501884384392L; - - /** - * Creates a new empty exception object. - */ - public SqlDynaException() - { - super(); - } - - /** - * Creates a new exception object. - * - * @param msg The exception message - */ - public SqlDynaException(String msg) - { - super(msg); - } - - /** - * Creates a new exception object. - * - * @param baseEx The base exception - */ - public SqlDynaException(Throwable baseEx) - { - super(baseEx); - } - - /** - * Creates a new exception object. - * - * @param msg The exception message - * @param baseEx The base exception - */ - public SqlDynaException(String msg, Throwable baseEx) - { - super(msg, baseEx); - } - -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaProperty.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaProperty.java deleted file mode 100644 index e40921253e..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/SqlDynaProperty.java +++ /dev/null @@ -1,87 +0,0 @@ -package org.jumpmind.symmetric.ddl.dynabean; - -/* - * 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 org.apache.commons.beanutils.DynaProperty; -import org.jumpmind.symmetric.ddl.model.Column; - -/** - * A DynaProperty which maps to a persistent Column in a database. - * The Column describes additional relational metadata - * for the property such as whether the property is a primary key column, - * an autoIncrement column and the SQL type etc. - * - * @version $Revision: 463757 $ - */ -public class SqlDynaProperty extends DynaProperty -{ - /** Unique ID for serializaion purposes. */ - private static final long serialVersionUID = -4491018827449106588L; - - /** The column for which this dyna property is defined. */ - private Column _column; - - /** - * Creates a property instance for the given column that accepts any data type. - * - * @param column The column - */ - public SqlDynaProperty(Column column) - { - super(column.getName()); - _column = column; - } - - /** - * Creates a property instance for the given column that only accepts the given type. - * - * @param column The column - * @param type The type of the property - */ - public SqlDynaProperty(Column column, Class type) - { - super(column.getName(), type); - _column = column; - } - - /** - * Returns the column for which this property is defined. - * - * @return The column - */ - public Column getColumn() - { - return _column; - } - - // Helper methods - //------------------------------------------------------------------------- - - /** - * Determines whether this property is for a primary key column. - * - * @return true if the property is for a primary key column - */ - public boolean isPrimaryKey() - { - return getColumn().isPrimaryKey(); - } - -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/package.html b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/package.html deleted file mode 100644 index c547eb208e..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/dynabean/package.html +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - -

- Provides the special DdlUtils implementations of {@link org.apache.commons.beanutils.DynaClass} - and {@link org.apache.commons.beanutils.DynaBean} that directly map to tables in the database - model. These are used by DdlUtils when reading data from or writing data to the database. - If you want to create these beans manually, use the - {@link org.apache.ddlutils.model.Database#createDynaBeanFor(org.apache.ddlutils.model.Table)} and - {@link org.apache.ddlutils.model.Database#createDynaBeanFor(java.lang.String, boolean)} methods - rather than the classes directly. -

- - diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataReader.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataReader.java deleted file mode 100644 index 6f970d4f73..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataReader.java +++ /dev/null @@ -1,161 +0,0 @@ -package org.jumpmind.symmetric.ddl.io; - -/* - * 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 org.apache.commons.digester.Digester; -import org.jumpmind.symmetric.ddl.io.converters.SqlTypeConverter; -import org.jumpmind.symmetric.ddl.model.Column; -import org.jumpmind.symmetric.ddl.model.Database; -import org.jumpmind.symmetric.ddl.model.Table; - -/** - * Reads data XML into dyna beans matching a specified database model. Note that - * the data sink won't be started or ended by the data reader, this has to be done - * in the code that uses the data reader. - * - * @version $Revision: 289996 $ - */ -public class DataReader extends Digester -{ - /** The database model. */ - private Database _model; - /** The object to receive the read beans. */ - private DataSink _sink; - /** Specifies whether the (lazy) configuration of the digester still needs to be performed. */ - private boolean _needsConfiguration = true; - /** The converters. */ - private ConverterConfiguration _converterConf = new ConverterConfiguration(); - /** Whether to be case sensitive or not. */ - private boolean _caseSensitive = false; - - /** - * Returns the converter configuration of this data reader. - * - * @return The converter configuration - */ - public ConverterConfiguration getConverterConfiguration() - { - return _converterConf; - } - - /** - * Returns the database model. - * - * @return The model - */ - public Database getModel() - { - return _model; - } - - /** - * Sets the database model. - * - * @param model The model - */ - public void setModel(Database model) - { - _model = model; - _needsConfiguration = true; - } - - /** - * Returns the data sink. - * - * @return The sink - */ - public DataSink getSink() - { - return _sink; - } - - /** - * Sets the data sink. - * - * @param sink The sink - */ - public void setSink(DataSink sink) - { - _sink = sink; - _needsConfiguration = true; - } - - /** - * Determines whether this rules object matches case sensitively. - * - * @return true if the case of the pattern matters - */ - public boolean isCaseSensitive() - { - return _caseSensitive; - } - - - /** - * Specifies whether this rules object shall match case sensitively. - * - * @param beCaseSensitive true if the case of the pattern shall matter - */ - public void setCaseSensitive(boolean beCaseSensitive) - { - _caseSensitive = beCaseSensitive; - } - - /** - * {@inheritDoc} - */ - protected void configure() - { - if (_needsConfiguration) - { - if (_model == null) - { - throw new NullPointerException("No database model specified"); - } - if (_sink == null) - { - throw new NullPointerException("No data sink model specified"); - } - - DigesterRules rules = new DigesterRules(); - - rules.setCaseSensitive(isCaseSensitive()); - setRules(rules); - for (int tableIdx = 0; tableIdx < _model.getTableCount(); tableIdx++) - { - // TODO: For now we hardcode the root as 'data' but ultimately we should wildcard it ('?') - Table table = _model.getTable(tableIdx); - String path = "data/"+table.getName(); - - addRule(path, new DynaSqlCreateRule(_model, table, _sink)); - for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++) - { - Column column = (Column)table.getColumn(columnIdx); - SqlTypeConverter converter = _converterConf.getRegisteredConverter(table, column); - - addRule(path, new SetColumnPropertyRule(column, converter, isCaseSensitive())); - addRule(path + "/" + column.getName(), new SetColumnPropertyFromSubElementRule(column, converter)); - } - } - _needsConfiguration = false; - } - super.configure(); - } -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataSink.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataSink.java deleted file mode 100644 index d5b253786b..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataSink.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.jumpmind.symmetric.ddl.io; - -/* - * 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 org.apache.commons.beanutils.DynaBean; - -/** - * Marks classes that can receive dyna beans read by the {@link org.jumpmind.symmetric.ddl.io.DataReader}. - * - * @version $Revision: 289996 $ - */ -public interface DataSink -{ - /** - * Notifies the sink that beans will be added. - */ - public void start() throws DataSinkException; - - /** - * Adds a dyna bean. - * - * @param bean The dyna bean to add - */ - public void addBean(DynaBean bean) throws DataSinkException; - - /** - * Notifies the sink that all beans have been added. - */ - public void end() throws DataSinkException; -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataSinkException.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataSinkException.java deleted file mode 100644 index 4034381f67..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataSinkException.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.jumpmind.symmetric.ddl.io; - -/* - * 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 org.apache.commons.lang.exception.NestableRuntimeException; - -/** - * Exception generated by {@link org.jumpmind.symmetric.ddl.io.DataSink} implementations. - * - * @version $Revision: 289996 $ - */ -public class DataSinkException extends NestableRuntimeException -{ - /** Unique id for serialization purposes. */ - private static final long serialVersionUID = 6790909409839782437L; - - /** - * Creates a new exception object. - */ - public DataSinkException() - { - super(); - } - - /** - * Creates a new exception object. - * - * @param message The exception message - */ - public DataSinkException(String message) - { - super(message); - } - - /** - * Creates a new exception object. - * - * @param baseEx The base exception - */ - public DataSinkException(Throwable baseEx) - { - super(baseEx); - } - - /** - * Creates a new exception object. - * - * @param message The exception message - * @param baseEx The base exception - */ - public DataSinkException(String message, Throwable baseEx) - { - super(message, baseEx); - } -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataToDatabaseSink.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataToDatabaseSink.java deleted file mode 100644 index e8bb9b6f66..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataToDatabaseSink.java +++ /dev/null @@ -1,665 +0,0 @@ -package org.jumpmind.symmetric.ddl.io; - -/* - * 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.sql.Connection; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; - -import org.apache.commons.beanutils.DynaBean; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.jumpmind.symmetric.ddl.DatabaseOperationException; -import org.jumpmind.symmetric.ddl.Platform; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaClass; -import org.jumpmind.symmetric.ddl.model.Column; -import org.jumpmind.symmetric.ddl.model.Database; -import org.jumpmind.symmetric.ddl.model.ForeignKey; -import org.jumpmind.symmetric.ddl.model.Reference; -import org.jumpmind.symmetric.ddl.model.Table; - -/** - * Data sink that directly inserts the beans into the database. If configured, it will make - * sure that the beans are inserted in the correct order according to the foreignkeys. Note - * that this will only work if there are no circles. - * - * @version $Revision: 289996 $ - */ -public class DataToDatabaseSink implements DataSink -{ - /** Our log. */ - private final Log _log = LogFactory.getLog(DataToDatabaseSink.class); - - /** Generates the sql and writes it to the database. */ - private Platform _platform; - /** The database model. */ - private Database _model; - /** The connection to the database. */ - private Connection _connection; - /** Whether to stop when an error has occurred while inserting a bean into the database. */ - private boolean _haltOnErrors = true; - /** Whether to delay the insertion of beans so that the beans referenced by it via foreignkeys, are already inserted into the database. */ - private boolean _ensureFkOrder = true; - /** Whether to use batch mode inserts. */ - private boolean _useBatchMode = false; - /** The queued objects for batch insertion. */ - private ArrayList _batchQueue = new ArrayList(); - /** The number of beans to insert in one batch. */ - private int _batchSize = 1024; - /** Stores the tables that are target of a foreign key. */ - private HashSet _fkTables = new HashSet(); - /** Contains the tables that have a self-referencing foreign key to a (partially) identity primary key. */ - private HashSet _tablesWithSelfIdentityReference = new HashSet(); - /** Contains the tables that have a self-referencing foreign key that is required. */ - private HashSet _tablesWithRequiredSelfReference = new HashSet(); - /** Maps original to processed identities. */ - private HashMap _identityMap = new HashMap(); - /** Stores the objects that are waiting for other objects to be inserted. */ - private ArrayList _waitingObjects = new ArrayList(); - - /** - * Creates a new sink instance. - * - * @param platform The database platform - * @param model The database model - */ - public DataToDatabaseSink(Platform platform, Database model) - { - _platform = platform; - _model = model; - for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++) - { - Table table = model.getTable(tableIdx); - ForeignKey selfRefFk = table.getSelfReferencingForeignKey(); - - if (selfRefFk != null) - { - Column[] pkColumns = table.getPrimaryKeyColumns(); - - for (int idx = 0; idx < pkColumns.length; idx++) - { - if (pkColumns[idx].isAutoIncrement()) - { - _tablesWithSelfIdentityReference.add(table); - break; - } - } - for (int idx = 0; idx < selfRefFk.getReferenceCount(); idx++) - { - if (selfRefFk.getReference(idx).getLocalColumn().isRequired()) - { - _tablesWithRequiredSelfReference.add(table); - break; - } - } - } - } - } - - /** - * Determines whether this sink halts when an error happens during the insertion of a bean - * into the database. Default is true. - * - * @return true if the sink stops when an error occurred - */ - public boolean isHaltOnErrors() - { - return _haltOnErrors; - } - - /** - * Specifies whether this sink halts when an error happens during the insertion of a bean - * into the database. - * - * @param haltOnErrors true if the sink shall stop when an error occurred - */ - public void setHaltOnErrors(boolean haltOnErrors) - { - _haltOnErrors = haltOnErrors; - } - - /** - * Determines whether the sink delays the insertion of beans so that the beans referenced by it - * via foreignkeys are already inserted into the database. - * - * @return true if beans are inserted after its foreignkey-references - */ - public boolean isEnsureFkOrder() - { - return _ensureFkOrder; - } - - /** - * Specifies whether the sink shall delay the insertion of beans so that the beans referenced by it - * via foreignkeys are already inserted into the database.
- * Note that you should careful with setting haltOnErrors to false as this might - * result in beans not inserted at all. The sink will then throw an appropriate exception at the end - * of the insertion process (method {@link #end()}). - * - * @param ensureFkOrder true if beans shall be inserted after its foreignkey-references - */ - public void setEnsureForeignKeyOrder(boolean ensureFkOrder) - { - _ensureFkOrder = ensureFkOrder; - } - - /** - * Determines whether batch mode is used for inserting the beans. - * - * @return true if batch mode is used (false per default) - */ - public boolean isUseBatchMode() - { - return _useBatchMode; - } - - /** - * Specifies whether batch mode is used for inserting the beans. Note that this requires - * that the primary key values are not defined by the database. - * - * @param useBatchMode true if batch mode shall be used - */ - public void setUseBatchMode(boolean useBatchMode) - { - _useBatchMode = useBatchMode; - } - - /** - * Returns the (maximum) number of beans to insert in one batch. - * - * @return The number of beans - */ - public int getBatchSize() - { - return _batchSize; - } - - /** - * Sets the (maximum) number of beans to insert in one batch. - * - * @param batchSize The number of beans - */ - public void setBatchSize(int batchSize) - { - _batchSize = batchSize; - } - - /** - * {@inheritDoc} - */ - public void end() throws DataSinkException - { - purgeBatchQueue(); - try - { - _connection.close(); - } - catch (SQLException ex) - { - throw new DataSinkException(ex); - } - if (!_waitingObjects.isEmpty()) - { - if (_log.isDebugEnabled()) - { - for (Iterator it = _waitingObjects.iterator(); it.hasNext();) - { - WaitingObject obj = (WaitingObject)it.next(); - Table table = _model.getDynaClassFor(obj.getObject()).getTable(); - Identity objId = buildIdentityFromPKs(table, obj.getObject()); - - _log.debug("Row " + objId + " is still not written because it depends on these yet unwritten rows"); - for (Iterator fkIt = obj.getPendingFKs(); fkIt.hasNext();) - { - Identity pendingFkId = (Identity)fkIt.next(); - - _log.debug(" " + pendingFkId); - } - - } - } - if (_waitingObjects.size() == 1) - { - throw new DataSinkException("There is one row still not written because of missing referenced rows"); - } - else - { - throw new DataSinkException("There are " + _waitingObjects.size() + " rows still not written because of missing referenced rows"); - } - } - } - - /** - * {@inheritDoc} - */ - public void start() throws DataSinkException - { - _fkTables.clear(); - _waitingObjects.clear(); - if (_ensureFkOrder) - { - for (int tableIdx = 0; tableIdx < _model.getTableCount(); tableIdx++) - { - Table table = _model.getTable(tableIdx); - - for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) - { - ForeignKey curFk = table.getForeignKey(fkIdx); - - _fkTables.add(curFk.getForeignTable()); - } - } - } - try - { - _connection = _platform.borrowConnection(); - } - catch (DatabaseOperationException ex) - { - throw new DataSinkException(ex); - } - } - - /** - * {@inheritDoc} - */ - public void addBean(DynaBean bean) throws DataSinkException - { - Table table = _model.getDynaClassFor(bean).getTable(); - Identity origIdentity = buildIdentityFromPKs(table, bean); - - if (_ensureFkOrder && (table.getForeignKeyCount() > 0)) - { - WaitingObject waitingObj = new WaitingObject(bean, origIdentity); - - for (int idx = 0; idx < table.getForeignKeyCount(); idx++) - { - ForeignKey fk = table.getForeignKey(idx); - Identity fkIdentity = buildIdentityFromFK(table, fk, bean); - - if ((fkIdentity != null) && !fkIdentity.equals(origIdentity)) - { - Identity processedIdentity = (Identity)_identityMap.get(fkIdentity); - - if (processedIdentity != null) - { - updateFKColumns(bean, fkIdentity.getForeignKeyName(), processedIdentity); - } - else - { - waitingObj.addPendingFK(fkIdentity); - } - } - } - if (waitingObj.hasPendingFKs()) - { - if (_log.isDebugEnabled()) - { - StringBuffer msg = new StringBuffer(); - - msg.append("Defering insertion of row "); - msg.append(buildIdentityFromPKs(table, bean).toString()); - msg.append(" because it is waiting for:"); - for (Iterator it = waitingObj.getPendingFKs(); it.hasNext();) - { - msg.append("\n "); - msg.append(it.next().toString()); - } - _log.debug(msg.toString()); - } - _waitingObjects.add(waitingObj); - return; - } - } - - insertBeanIntoDatabase(table, bean); - - if (_log.isDebugEnabled()) - { - _log.debug("Inserted bean " + origIdentity); - } - - if (_ensureFkOrder && _fkTables.contains(table)) - { - Identity newIdentity = buildIdentityFromPKs(table, bean); - ArrayList finishedObjs = new ArrayList(); - - _identityMap.put(origIdentity, newIdentity); - - // we're doing multiple passes so that we can insert as much objects in - // one go as possible - ArrayList identitiesToCheck = new ArrayList(); - - identitiesToCheck.add(origIdentity); - while (!identitiesToCheck.isEmpty() && !_waitingObjects.isEmpty()) - { - Identity curIdentity = (Identity)identitiesToCheck.get(0); - Identity curNewIdentity = (Identity)_identityMap.get(curIdentity); - - identitiesToCheck.remove(0); - finishedObjs.clear(); - for (Iterator waitingObjIt = _waitingObjects.iterator(); waitingObjIt.hasNext();) - { - WaitingObject waitingObj = (WaitingObject)waitingObjIt.next(); - Identity fkIdentity = waitingObj.removePendingFK(curIdentity); - - if (fkIdentity != null) - { - updateFKColumns(waitingObj.getObject(), fkIdentity.getForeignKeyName(), curNewIdentity); - } - if (!waitingObj.hasPendingFKs()) - { - waitingObjIt.remove(); - // we defer handling of the finished objects to avoid concurrent modification exceptions - finishedObjs.add(waitingObj.getObject()); - } - } - for (Iterator finishedObjIt = finishedObjs.iterator(); finishedObjIt.hasNext();) - { - DynaBean finishedObj = (DynaBean)finishedObjIt.next(); - Table tableForObj = _model.getDynaClassFor(finishedObj).getTable(); - Identity objIdentity = buildIdentityFromPKs(tableForObj, finishedObj); - - insertBeanIntoDatabase(tableForObj, finishedObj); - - Identity newObjIdentity = buildIdentityFromPKs(tableForObj, finishedObj); - - _identityMap.put(objIdentity, newObjIdentity); - identitiesToCheck.add(objIdentity); - if (_log.isDebugEnabled()) - { - _log.debug("Inserted deferred row " + objIdentity); - } - } - } - } - } - - /** - * Inserts the bean into the database or batch queue. - * - * @param table The table - * @param bean The bean - */ - private void insertBeanIntoDatabase(Table table, DynaBean bean) throws DataSinkException - { - if (_useBatchMode) - { - _batchQueue.add(bean); - if (_batchQueue.size() >= _batchSize) - { - purgeBatchQueue(); - } - } - else - { - insertSingleBeanIntoDatabase(table, bean); - } - } - - /** - * Purges the batch queue by inserting the objects into the database. - */ - private void purgeBatchQueue() throws DataSinkException - { - if (!_batchQueue.isEmpty()) - { - try - { - _platform.insert(_connection, _model, _batchQueue); - if (!_connection.getAutoCommit()) - { - _connection.commit(); - } - if (_log.isDebugEnabled()) - { - _log.debug("Inserted " + _batchQueue.size() + " rows in batch mode "); - } - } - catch (Exception ex) - { - if (_haltOnErrors) - { - _platform.returnConnection(_connection); - throw new DataSinkException(ex); - } - else - { - _log.warn("Exception while inserting " + _batchQueue.size() + " rows via batch mode into the database", ex); - } - } - _batchQueue.clear(); - } - } - - /** - * Directly inserts the given bean into the database. - * - * @param table The table of the bean - * @param bean The bean - */ - private void insertSingleBeanIntoDatabase(Table table, DynaBean bean) throws DataSinkException - { - try - { - boolean needTwoStepInsert = false; - ForeignKey selfRefFk = null; - - if (!_platform.isIdentityOverrideOn() && - _tablesWithSelfIdentityReference.contains(table)) - { - selfRefFk = table.getSelfReferencingForeignKey(); - - // in case of a self-reference (fk points to the very row that we're inserting) - // and (at least) one of the pk columns is an identity column, we first need - // to insert the row with the fk columns set to null - Identity pkIdentity = buildIdentityFromPKs(table, bean); - Identity fkIdentity = buildIdentityFromFK(table, selfRefFk, bean); - - if (pkIdentity.equals(fkIdentity)) - { - if (_tablesWithRequiredSelfReference.contains(table)) - { - throw new DataSinkException("Can only insert rows with fk pointing to themselves when all fk columns can be NULL (row pk is " + pkIdentity + ")"); - } - else - { - needTwoStepInsert = true; - } - } - } - - if (needTwoStepInsert) - { - // we first insert the bean without the fk, then in the second step we update the bean - // with the row with the identity pk values - ArrayList fkValues = new ArrayList(); - - for (int idx = 0; idx < selfRefFk.getReferenceCount(); idx++) - { - String columnName = selfRefFk.getReference(idx).getLocalColumnName(); - - fkValues.add(bean.get(columnName)); - bean.set(columnName, null); - } - _platform.insert(_connection, _model, bean); - for (int idx = 0; idx < selfRefFk.getReferenceCount(); idx++) - { - bean.set(selfRefFk.getReference(idx).getLocalColumnName(), fkValues.get(idx)); - } - _platform.update(_connection, _model, bean); - } - else - { - _platform.insert(_connection, _model, bean); - } - if (!_connection.getAutoCommit()) - { - _connection.commit(); - } - } - catch (Exception ex) - { - if (_haltOnErrors) - { - _platform.returnConnection(_connection); - throw new DataSinkException(ex); - } - else - { - _log.warn("Exception while inserting a row into the database", ex); - } - } - } - - /** - * Returns the name of the given foreign key. If it has no name, then a temporary one - * is generated from the names of the relevant tables and columns. - * - * @param owningTable The table owning the fk - * @param fk The foreign key - * @return The name - */ - private String getFKName(Table owningTable, ForeignKey fk) - { - if ((fk.getName() != null) && (fk.getName().length() > 0)) - { - return fk.getName(); - } - else - { - StringBuffer result = new StringBuffer(); - - result.append(owningTable.getName()); - result.append("["); - for (int idx = 0; idx < fk.getReferenceCount(); idx++) - { - if (idx > 0) - { - result.append(","); - } - result.append(fk.getReference(idx).getLocalColumnName()); - } - result.append("]->"); - result.append(fk.getForeignTableName()); - result.append("["); - for (int idx = 0; idx < fk.getReferenceCount(); idx++) - { - if (idx > 0) - { - result.append(","); - } - result.append(fk.getReference(idx).getForeignColumnName()); - } - result.append("]"); - return result.toString(); - } - } - - /** - * Builds an identity object from the primary keys of the specified table using the - * column values of the supplied bean. - * - * @param table The table - * @param bean The bean - * @return The identity - */ - private Identity buildIdentityFromPKs(Table table, DynaBean bean) - { - Identity identity = new Identity(table); - Column[] pkColumns = table.getPrimaryKeyColumns(); - - for (int idx = 0; idx < pkColumns.length; idx++) - { - identity.setColumnValue(pkColumns[idx].getName(), bean.get(pkColumns[idx].getName())); - } - return identity; - } - - /** - * Builds an identity object for the specified foreign key using the foreignkey column values - * of the supplied bean. - * - * @param owningTable The table owning the foreign key - * @param fk The foreign key - * @param bean The bean - * @return The identity - */ - private Identity buildIdentityFromFK(Table owningTable, ForeignKey fk, DynaBean bean) - { - Identity identity = new Identity(fk.getForeignTable(), getFKName(owningTable, fk)); - - for (int idx = 0; idx < fk.getReferenceCount(); idx++) - { - Reference reference = (Reference)fk.getReference(idx); - Object value = bean.get(reference.getLocalColumnName()); - - if (value == null) - { - return null; - } - identity.setColumnValue(reference.getForeignColumnName(), value); - } - return identity; - } - - /** - * Updates the values of the columns constituting the indicated foreign key with the values - * of the given identity. - * - * @param bean The bean whose columns shall be updated - * @param fkName The name of the foreign key - * @param identity The target identity - */ - private void updateFKColumns(DynaBean bean, String fkName, Identity identity) - { - Table sourceTable = ((SqlDynaClass)bean.getDynaClass()).getTable(); - Table targetTable = identity.getTable(); - ForeignKey fk = null; - - for (int idx = 0; idx < sourceTable.getForeignKeyCount(); idx++) - { - ForeignKey curFk = sourceTable.getForeignKey(idx); - - if (curFk.getForeignTableName().equalsIgnoreCase(targetTable.getName())) - { - if (fkName.equals(getFKName(sourceTable, curFk))) - { - fk = curFk; - break; - } - } - } - if (fk != null) - { - for (int idx = 0; idx < fk.getReferenceCount(); idx++) - { - Reference curRef = fk.getReference(idx); - Column sourceColumn = curRef.getLocalColumn(); - Column targetColumn = curRef.getForeignColumn(); - - bean.set(sourceColumn.getName(), identity.getColumnValue(targetColumn.getName())); - } - } - } -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataWriter.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataWriter.java deleted file mode 100644 index 3d30bac12f..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataWriter.java +++ /dev/null @@ -1,444 +0,0 @@ -package org.jumpmind.symmetric.ddl.io; - -/* - * 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.OutputStream; -import java.io.PrintWriter; -import java.io.Writer; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamWriter; - -import org.apache.commons.beanutils.DynaBean; -import org.apache.commons.codec.binary.Base64; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaBean; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaClass; -import org.jumpmind.symmetric.ddl.io.converters.ConversionException; -import org.jumpmind.symmetric.ddl.io.converters.SqlTypeConverter; -import org.jumpmind.symmetric.ddl.model.Column; -import org.jumpmind.symmetric.ddl.model.Table; - -/** - * Writes dyna beans matching a specified database model into an XML file. - * - * TODO: Make names (tables, columns) XML-compliant - * - * @version $Revision: 289996 $ - */ -public class DataWriter -{ - /** String values with a size not bigger than this value will be written to attributes; - if their size is longer, then a sub element is generated instead. */ - private static final int MAX_ATTRIBUTE_LENGTH = 255; - /** The indentation string. */ - private static final String INDENT_STRING = " "; - - /** Our log. */ - private final Log _log = LogFactory.getLog(DataWriter.class); - - /** The converters. */ - private ConverterConfiguration _converterConf = new ConverterConfiguration(); - /** The output stream. */ - private PrintWriter _output; - /** The xml writer. */ - private XMLStreamWriter _writer; - /** The output encoding. */ - private String _encoding; - /** Whether we're pretty-printing. */ - private boolean _prettyPrinting = true; - - /** - * Creates a data writer instance using UTF-8 encoding. - * - * @param output The target to write the data XML to - */ - public DataWriter(OutputStream output) throws DataWriterException - { - this(output, null); - } - - /** - * Creates a data writer instance. - * - * @param output The target to write the data XML to - * @param encoding The encoding of the XML file - */ - public DataWriter(OutputStream output, String encoding) throws DataWriterException - { - _output = new PrintWriter(output); - if ((encoding == null) || (encoding.length() == 0)) - { - _encoding = "UTF-8"; - } - else - { - _encoding = encoding; - } - - try - { - XMLOutputFactory factory = XMLOutputFactory.newInstance(); - - _writer = factory.createXMLStreamWriter(output, _encoding); - } - catch (XMLStreamException ex) - { - throw new DataWriterException(ex); - } - } - - /** - * Creates a data writer instance using the specified writer. Note that the writer - * needs to be configured using the specified encoding. - * - * @param output The target to write the data XML to - * @param encoding The encoding of the writer - */ - public DataWriter(Writer output, String encoding) throws DataWriterException - { - _output = new PrintWriter(output); - _encoding = encoding; - try - { - XMLOutputFactory factory = XMLOutputFactory.newInstance(); - - _writer = factory.createXMLStreamWriter(_output); - } - catch (XMLStreamException ex) - { - throw new DataWriterException(ex); - } - } - - /** - * Determines whether the output shall be pretty-printed. - * - * @return true if the output is pretty-printed - */ - public boolean isPrettyPrinting() - { - return _prettyPrinting; - } - - /** - * Specifies whether the output shall be pretty-printed. - * - * @param prettyPrinting true if the output is pretty-printed - */ - public void setPrettyPrinting(boolean prettyPrinting) - { - _prettyPrinting = prettyPrinting; - } - - /** - * Returns the converter configuration of this data reader. - * - * @return The converter configuration - */ - public ConverterConfiguration getConverterConfiguration() - { - return _converterConf; - } - - /** - * Prints a newline if we're pretty-printing. - */ - private void printlnIfPrettyPrinting() throws DataWriterException - { - if (_prettyPrinting) - { - try - { - _writer.writeCharacters("\n"); - } - catch (XMLStreamException ex) - { - throw new DataWriterException(ex); - } - } - } - - /** - * Prints the indentation if we're pretty-printing. - * - * @param level The indentation level - */ - private void indentIfPrettyPrinting(int level) throws DataWriterException - { - if (_prettyPrinting) - { - try - { - for (int idx = 0; idx < level; idx++) - { - _writer.writeCharacters(INDENT_STRING); - } - } - catch (XMLStreamException ex) - { - throw new DataWriterException(ex); - } - } - } - - /** - * Writes the start of the XML document, i.e. the "" section and the start of the - * root node. - */ - public void writeDocumentStart() throws DataWriterException - { - try - { - _writer.writeStartDocument(_encoding, "1.0"); - printlnIfPrettyPrinting(); - _writer.writeStartElement("data"); - printlnIfPrettyPrinting(); - } - catch (XMLStreamException ex) - { - throw new DataWriterException(ex); - } - } - - /** - * Writes the end of the XML document, i.e. end of the root node. - */ - public void writeDocumentEnd() throws DataWriterException - { - try - { - _writer.writeEndElement(); - printlnIfPrettyPrinting(); - _writer.writeEndDocument(); - _writer.flush(); - _writer.close(); - _output.close(); - } - catch (XMLStreamException ex) - { - throw new DataWriterException(ex); - } - } - - /** - * Writes the given bean. - * - * @param bean The bean to write - */ - public void write(SqlDynaBean bean) throws DataWriterException - { - SqlDynaClass dynaClass = (SqlDynaClass)bean.getDynaClass(); - Table table = dynaClass.getTable(); - HashMap subElements = new HashMap(); - - try - { - indentIfPrettyPrinting(1); - _writer.writeStartElement(table.getName()); - for (int idx = 0; idx < table.getColumnCount(); idx++) - { - Column column = table.getColumn(idx); - Object value = bean.get(column.getName()); - SqlTypeConverter converter = _converterConf.getRegisteredConverter(table, column); - String valueAsText = null; - - if (converter == null) - { - if (value != null) - { - valueAsText = value.toString(); - } - } - else - { - valueAsText = converter.convertToString(value, column.getTypeCode()); - } - if (valueAsText != null) - { - // we create an attribute only if the text is not too long - // and if it does not contain special characters - if ((valueAsText.length() > MAX_ATTRIBUTE_LENGTH) || analyzeText(valueAsText, null)) - { - // we defer writing the sub elements - subElements.put(column.getName(), valueAsText); - } - else - { - _writer.writeAttribute(column.getName(), valueAsText); - } - } - } - if (!subElements.isEmpty()) - { - List cutPoints = new ArrayList(); - - for (Iterator it = subElements.entrySet().iterator(); it.hasNext();) - { - Map.Entry entry = (Map.Entry)it.next(); - String content = entry.getValue().toString(); - - printlnIfPrettyPrinting(); - indentIfPrettyPrinting(2); - _writer.writeStartElement(entry.getKey().toString()); - - // if the content contains special characters, we have to apply base64 encoding to it - // if the content is too short, then it has to contain special characters (otherwise - // it would have been written as an attribute already), otherwise we check - cutPoints.clear(); - - boolean writeBase64Encoded = analyzeText(content, cutPoints); - - if (writeBase64Encoded) - { - _writer.writeAttribute(DatabaseIO.BASE64_ATTR_NAME, "true"); - _writer.writeCData(new String(Base64.encodeBase64(content.getBytes()))); - } - else - { - if (cutPoints.isEmpty()) - { - _writer.writeCData(content); - } - else - { - int lastPos = 0; - - for (Iterator cutPointIt = cutPoints.iterator(); cutPointIt.hasNext();) - { - int curPos = ((Integer)cutPointIt.next()).intValue(); - - _writer.writeCData(content.substring(lastPos, curPos)); - lastPos = curPos; - } - if (lastPos < content.length()) - { - _writer.writeCData(content.substring(lastPos)); - } - } - } - - _writer.writeEndElement(); - } - printlnIfPrettyPrinting(); - indentIfPrettyPrinting(1); - } - _writer.writeEndElement(); - printlnIfPrettyPrinting(); - } - catch (XMLStreamException ex) - { - throw new DataWriterException(ex); - } - catch (ConversionException ex) - { - throw new DataWriterException(ex); - } - } - - /** - * Determines whether the given string contains special characters that cannot - * be used in XML, and if not, finds the cut points where to split the text - * when writing it in a CDATA section. - * - * @param text The text - * @param cutPoints Will be filled with cut points to split the text when writing it - * in a CDATA section (only if the method returns false) - * @return true if the text contains special characters - */ - private boolean analyzeText(String text, List cutPoints) - { - List tmpCutPoints = cutPoints == null ? null : new ArrayList(); - int numChars = text.length(); - int numFoundCDataEndChars = 0; - - for (int charPos = 0; charPos < numChars; charPos++) - { - char c = text.charAt(charPos); - - if ((c < 0x0020) && (c != '\n') && (c != '\r') && (c != '\t')) - { - return true; - } - else if (cutPoints != null) - { - if ((c == ']') && ((numFoundCDataEndChars == 0) || (numFoundCDataEndChars == 1))) - { - numFoundCDataEndChars++; - } - else if ((c == '>') && (numFoundCDataEndChars == 2)) - { - // we have to split the CDATA right here before the '>' (see DDLUTILS-174) - tmpCutPoints.add(new Integer(charPos)); - numFoundCDataEndChars = 0; - } - else - { - numFoundCDataEndChars = 0; - } - } - } - if (cutPoints != null) - { - cutPoints.addAll(tmpCutPoints); - } - return false; - } - - /** - * Writes the beans contained in the given iterator. - * - * @param beans The beans iterator - */ - public void write(Iterator beans) throws DataWriterException - { - while (beans.hasNext()) - { - DynaBean bean = (DynaBean)beans.next(); - - if (bean instanceof SqlDynaBean) - { - write((SqlDynaBean)bean); - } - else - { - _log.warn("Cannot write normal dyna beans (type: "+bean.getDynaClass().getName()+")"); - } - } - } - - /** - * Writes the beans contained in the given collection. - * - * @param beans The beans - */ - public void write(Collection beans) throws DataWriterException - { - write(beans.iterator()); - } -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataWriterException.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataWriterException.java deleted file mode 100644 index e50e9d0ba9..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DataWriterException.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.jumpmind.symmetric.ddl.io; - -/* - * 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 org.apache.commons.lang.exception.NestableRuntimeException; - -/** - * Exception generated by the {@link org.jumpmind.symmetric.ddl.io.DataWriter}. - * - * @version $Revision: 289996 $ - */ -public class DataWriterException extends NestableRuntimeException -{ - /** Unique id for serialization purposes. */ - private static final long serialVersionUID = 6254759931565130848L; - - /** - * Creates a new exception object. - */ - public DataWriterException() - { - super(); - } - - /** - * Creates a new exception object. - * - * @param message The exception message - */ - public DataWriterException(String message) - { - super(message); - } - - /** - * Creates a new exception object. - * - * @param baseEx The base exception - */ - public DataWriterException(Throwable baseEx) - { - super(baseEx); - } - - /** - * Creates a new exception object. - * - * @param message The exception message - * @param baseEx The base exception - */ - public DataWriterException(String message, Throwable baseEx) - { - super(message, baseEx); - } -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DatabaseDataIO.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DatabaseDataIO.java deleted file mode 100644 index e53cac4ff2..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DatabaseDataIO.java +++ /dev/null @@ -1,775 +0,0 @@ -package org.jumpmind.symmetric.ddl.io; - -/* - * 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.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Reader; -import java.io.Writer; -import java.sql.Connection; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import org.apache.commons.collections.map.ListOrderedMap; -import org.jumpmind.symmetric.ddl.DdlUtilsException; -import org.jumpmind.symmetric.ddl.Platform; -import org.jumpmind.symmetric.ddl.model.Column; -import org.jumpmind.symmetric.ddl.model.Database; -import org.jumpmind.symmetric.ddl.model.Table; - -/** - * Provides basic live database data <-> XML functionality. - * - * @version $Revision: $ - */ -public class DatabaseDataIO -{ - /** The converters to use for converting between data and its XML representation. */ - private ArrayList _converters = new ArrayList(); - /** Whether we should continue when an error was detected. */ - private boolean _failOnError = true; - /** Whether foreign key order shall be followed when inserting data into the database. */ - private boolean _ensureFKOrder = true; - /** Whether we should use batch mode. */ - private boolean _useBatchMode; - /** The maximum number of objects to insert in one batch. */ - private Integer _batchSize; - - /** Whether DdlUtils should search for the schema of the tables. @deprecated */ - private boolean _determineSchema; - /** The schema pattern for finding tables when reading data from a live database. @deprecated */ - private String _schemaPattern; - - /** - * Registers a converter. - * - * @param converterRegistration The registration info - */ - public void registerConverter(DataConverterRegistration converterRegistration) - { - _converters.add(converterRegistration); - } - - /** - * Determines whether data io is stopped when an error happens. - * - * @return Whether io is stopped when an error was detected (true by default) - */ - public boolean isFailOnError() - { - return _failOnError; - } - - /** - * Specifies whether data io shall be stopped when an error happens. - * - * @param failOnError Whether io should stop when an error was detected - */ - public void setFailOnError(boolean failOnError) - { - _failOnError = failOnError; - } - - /** - * Determines whether batch mode is used for inserting data into the database. - * - * @return true if batch mode is used - */ - public boolean getUseBatchMode() - { - return _useBatchMode; - } - - /** - * Specifies whether batch mode should be used for inserting data into the database. - * - * @param useBatchMode true if batch mode shall be used - */ - public void setUseBatchMode(boolean useBatchMode) - { - _useBatchMode = useBatchMode; - } - - /** - * Returns the batch size override. - * - * @return The batch size if different from the default, null otherwise - */ - public Integer getBatchSize() - { - return _batchSize; - } - - /** - * Sets the batch size to be used by this object. - * - * @param batchSize The batch size if different from the default, or null if - * the default shall be used - */ - public void setBatchSize(Integer batchSize) - { - _batchSize = batchSize; - } - - /** - * Determines whether the sink delays the insertion of beans so that the beans referenced by it - * via foreignkeys are already inserted into the database. - * - * @return true if beans are inserted after its foreignkey-references - */ - public boolean isEnsureFKOrder() - { - return _ensureFKOrder; - } - - /** - * Specifies whether the sink shall delay the insertion of beans so that the beans referenced by it - * via foreignkeys are already inserted into the database.
- * Note that you should careful with setting haltOnErrors to false as this might - * result in beans not inserted at all. The sink will then throw an appropriate exception at the end - * of the insertion process (method {@link DataSink#end()}). - * - * @param ensureFKOrder true if beans shall be inserted after its foreignkey-references - */ - public void setEnsureFKOrder(boolean ensureFKOrder) - { - _ensureFKOrder = ensureFKOrder; - } - - /** - * Specifies whether DdlUtils should try to find the schema of the tables when reading data - * from a live database. - * - * @param determineSchema Whether to try to find the table's schemas - * @deprecated Will be removed once proper schema support is in place - */ - public void setDetermineSchema(boolean determineSchema) - { - _determineSchema = determineSchema; - } - - /** - * Sets the schema pattern to find the schemas of tables when reading data from a live database. - * - * @param schemaPattern The schema pattern - * @deprecated Will be removed once proper schema support is in place - */ - public void setSchemaPattern(String schemaPattern) - { - _schemaPattern = schemaPattern; - } - - /** - * Registers the converters at the given configuration. - * - * @param converterConf The converter configuration - */ - private void registerConverters(ConverterConfiguration converterConf) throws DdlUtilsException - { - for (Iterator it = _converters.iterator(); it.hasNext();) - { - DataConverterRegistration registrationInfo = (DataConverterRegistration)it.next(); - - if (registrationInfo.getTypeCode() != Integer.MIN_VALUE) - { - converterConf.registerConverter(registrationInfo.getTypeCode(), - registrationInfo.getConverter()); - } - else - { - if ((registrationInfo.getTable() == null) || (registrationInfo.getColumn() == null)) - { - throw new DdlUtilsException("Please specify either the jdbc type or a table/column pair for which the converter shall be defined"); - } - converterConf.registerConverter(registrationInfo.getTable(), - registrationInfo.getColumn(), - registrationInfo.getConverter()); - } - } - } - - /** - * Returns a data writer instance configured to write to the indicated file - * in the specified encoding. - * - * @param path The path to the output XML data file - * @param xmlEncoding The encoding to use for writing the XML - * @return The writer - */ - public DataWriter getConfiguredDataWriter(String path, String xmlEncoding) throws DdlUtilsException - { - try - { - DataWriter writer = new DataWriter(new FileOutputStream(path), xmlEncoding); - - registerConverters(writer.getConverterConfiguration()); - return writer; - } - catch (IOException ex) - { - throw new DdlUtilsException(ex); - } - } - - /** - * Returns a data writer instance configured to write to the given output stream - * in the specified encoding. - * - * @param output The output stream - * @param xmlEncoding The encoding to use for writing the XML - * @return The writer - */ - public DataWriter getConfiguredDataWriter(OutputStream output, String xmlEncoding) throws DdlUtilsException - { - DataWriter writer = new DataWriter(output, xmlEncoding); - - registerConverters(writer.getConverterConfiguration()); - return writer; - } - - /** - * Returns a data writer instance configured to write to the given output writer - * in the specified encoding. - * - * @param output The output writer; needs to be configured with the specified encoding - * @param xmlEncoding The encoding to use for writing the XML - * @return The writer - */ - public DataWriter getConfiguredDataWriter(Writer output, String xmlEncoding) throws DdlUtilsException - { - DataWriter writer = new DataWriter(output, xmlEncoding); - - registerConverters(writer.getConverterConfiguration()); - return writer; - } - - /** - * Writes the data contained in the database to which the given platform is connected, as XML - * to the given output stream (which won't be closed by this method). - * - * @param platform The platform; needs to be connected to a live database - * @param path The path of the output file - * @param xmlEncoding The encoding to use for the XML - */ - public void writeDataToXML(Platform platform, String path, String xmlEncoding) throws DdlUtilsException - { - writeDataToXML(platform, getConfiguredDataWriter(path, xmlEncoding)); - } - - /** - * Writes the data contained in the database to which the given platform is connected, as XML - * to the given output stream (which won't be closed by this method). - * - * @param platform The platform; needs to be connected to a live database - * @param model The model for which to retrieve and write the data - * @param path The path of the output file - * @param xmlEncoding The encoding to use for the XML - */ - public void writeDataToXML(Platform platform, Database model, String path, String xmlEncoding) - { - writeDataToXML(platform, model, getConfiguredDataWriter(path, xmlEncoding)); - } - - /** - * Writes the data contained in the database to which the given platform is connected, as XML - * to the given output stream (which won't be closed by this method). - * - * @param platform The platform; needs to be connected to a live database - * @param output The output stream - * @param xmlEncoding The encoding to use for the XML - */ - public void writeDataToXML(Platform platform, OutputStream output, String xmlEncoding) - { - writeDataToXML(platform, getConfiguredDataWriter(output, xmlEncoding)); - } - - /** - * Writes the data contained in the database to which the given platform is connected, as XML - * to the given output stream (which won't be closed by this method). - * - * @param platform The platform; needs to be connected to a live database - * @param model The model for which to retrieve and write the data - * @param output The output stream - * @param xmlEncoding The encoding to use for the XML - */ - public void writeDataToXML(Platform platform, Database model, OutputStream output, String xmlEncoding) - { - writeDataToXML(platform, model, getConfiguredDataWriter(output, xmlEncoding)); - } - - /** - * Writes the data contained in the database to which the given platform is connected, as XML - * to the given output writer (which won't be closed by this method). - * - * @param platform The platform; needs to be connected to a live database - * @param output The output writer (which needs to be openend with the specified encoding) - * @param xmlEncoding The encoding to use for the XML - */ - public void writeDataToXML(Platform platform, Writer output, String xmlEncoding) - { - writeDataToXML(platform, getConfiguredDataWriter(output, xmlEncoding)); - } - - /** - * Writes the data contained in the database to which the given platform is connected, as XML - * to the given output writer (which won't be closed by this method). - * - * @param platform The platform; needs to be connected to a live database - * @param model The model for which to retrieve and write the data - * @param output The output writer (which needs to be openend with the specified encoding) - * @param xmlEncoding The encoding to use for the XML - */ - public void writeDataToXML(Platform platform, Database model, Writer output, String xmlEncoding) - { - writeDataToXML(platform, model, getConfiguredDataWriter(output, xmlEncoding)); - } - - /** - * Writes the data contained in the database to which the given platform is connected, as XML - * to the given data writer. - * - * @param platform The platform; needs to be connected to a live database - * @param writer The data writer - */ - public void writeDataToXML(Platform platform, DataWriter writer) - { - writeDataToXML(platform, platform.readModelFromDatabase("unnamed"), writer); - } - - /** - * Writes the data contained in the database to which the given platform is connected, as XML - * to the given data writer. - * - * @param platform The platform; needs to be connected to a live database - * @param model The model for which to retrieve and write the data - * @param writer The data writer - */ - public void writeDataToXML(Platform platform, Database model, DataWriter writer) - { - registerConverters(writer.getConverterConfiguration()); - - // TODO: An advanced algorithm could be employed here that writes individual - // objects related by foreign keys, in the correct order - List tables = sortTables(model.getTables()); - - writer.writeDocumentStart(); - for (Iterator it = tables.iterator(); it.hasNext();) - { - writeDataForTableToXML(platform, model, (Table)it.next(), writer); - } - writer.writeDocumentEnd(); - } - - /** - * Sorts the given table according to their foreign key order. - * - * @param tables The tables - * @return The sorted tables - */ - private List sortTables(Table[] tables) - { - ArrayList result = new ArrayList(); - HashSet processed = new HashSet(); - ListOrderedMap pending = new ListOrderedMap(); - - for (int idx = 0; idx < tables.length; idx++) - { - Table table = tables[idx]; - - if (table.getForeignKeyCount() == 0) - { - result.add(table); - processed.add(table); - } - else - { - HashSet waitedFor = new HashSet(); - - for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) - { - Table waitedForTable = table.getForeignKey(fkIdx).getForeignTable(); - - if (!table.equals(waitedForTable)) - { - waitedFor.add(waitedForTable); - } - } - pending.put(table, waitedFor); - } - } - - HashSet newProcessed = new HashSet(); - - while (!processed.isEmpty() && !pending.isEmpty()) - { - newProcessed.clear(); - for (Iterator it = pending.entrySet().iterator(); it.hasNext();) - { - Map.Entry entry = (Map.Entry)it.next(); - Table table = (Table)entry.getKey(); - HashSet waitedFor = (HashSet)entry.getValue(); - - waitedFor.removeAll(processed); - if (waitedFor.isEmpty()) - { - it.remove(); - result.add(table); - newProcessed.add(table); - } - } - processed.clear(); - - HashSet tmp = processed; - - processed = newProcessed; - newProcessed = tmp; - } - // the remaining are within circular dependencies - for (Iterator it = pending.keySet().iterator(); it.hasNext();) - { - result.add(it.next()); - } - return result; - } - - /** - * Writes the data contained in a single table to XML. - * - * @param platform The platform - * @param model The database model - * @param table The table - * @param writer The data writer - */ - private void writeDataForTableToXML(Platform platform, Database model, Table table, DataWriter writer) - { - Table[] tables = { table }; - StringBuffer query = new StringBuffer(); - - query.append("SELECT "); - - Connection connection = null; - String schema = null; - - if (_determineSchema) - { - try - { - // TODO: Remove this once we have full support for schemas - connection = platform.borrowConnection(); - schema = platform.getModelReader().determineSchemaOf(connection, _schemaPattern, tables[0]); - } - catch (SQLException ex) - { - // ignored - } - finally - { - if (connection != null) - { - try - { - connection.close(); - } - catch (SQLException ex) - { - // ignored - } - } - } - } - - Column[] columns = tables[0].getColumns(); - - for (int columnIdx = 0; columnIdx < columns.length; columnIdx++) - { - if (columnIdx > 0) - { - query.append(","); - } - if (platform.isDelimitedIdentifierModeOn()) - { - query.append(platform.getPlatformInfo().getDelimiterToken()); - } - query.append(columns[columnIdx].getName()); - if (platform.isDelimitedIdentifierModeOn()) - { - query.append(platform.getPlatformInfo().getDelimiterToken()); - } - } - query.append(" FROM "); - if (platform.isDelimitedIdentifierModeOn()) - { - query.append(platform.getPlatformInfo().getDelimiterToken()); - } - if (schema != null) - { - query.append(schema); - query.append("."); - } - query.append(tables[0].getName()); - if (platform.isDelimitedIdentifierModeOn()) - { - query.append(platform.getPlatformInfo().getDelimiterToken()); - } - - writer.write(platform.query(model, query.toString(), tables)); - } - - /** - * Returns a data reader instance configured for the given platform (which needs to - * be connected to a live database) and model. - * - * @param platform The database - * @param model The model - * @return The data reader - */ - public DataReader getConfiguredDataReader(Platform platform, Database model) throws DdlUtilsException - { - DataToDatabaseSink sink = new DataToDatabaseSink(platform, model); - DataReader reader = new DataReader(); - - sink.setHaltOnErrors(_failOnError); - sink.setEnsureForeignKeyOrder(_ensureFKOrder); - sink.setUseBatchMode(_useBatchMode); - if (_batchSize != null) - { - sink.setBatchSize(_batchSize.intValue()); - } - - reader.setModel(model); - reader.setSink(sink); - registerConverters(reader.getConverterConfiguration()); - return reader; - } - - /** - * Reads the data from the specified files and writes it to the database to which the given - * platform is connected. - * - * @param platform The platform, must be connected to a live database - * @param files The XML data files - */ - public void writeDataToDatabase(Platform platform, String[] files) throws DdlUtilsException - { - writeDataToDatabase(platform, platform.readModelFromDatabase("unnamed"), files); - } - - /** - * Reads the data from the given input streams and writes it to the database to which the given - * platform is connected. - * - * @param platform The platform, must be connected to a live database - * @param inputs The input streams for the XML data - */ - public void writeDataToDatabase(Platform platform, InputStream[] inputs) throws DdlUtilsException - { - writeDataToDatabase(platform, platform.readModelFromDatabase("unnamed"), inputs); - } - - /** - * Reads the data from the given input readers and writes it to the database to which the given - * platform is connected. - * - * @param platform The platform, must be connected to a live database - * @param inputs The input readers for the XML data - */ - public void writeDataToDatabase(Platform platform, Reader[] inputs) throws DdlUtilsException - { - writeDataToDatabase(platform, platform.readModelFromDatabase("unnamed"), inputs); - } - - /** - * Reads the data from the indicated files and writes it to the database to which the given - * platform is connected. Only data that matches the given model will be written. - * - * @param platform The platform, must be connected to a live database - * @param model The model to which to constrain the written data - * @param files The XML data files - */ - public void writeDataToDatabase(Platform platform, Database model, String[] files) throws DdlUtilsException - { - DataReader dataReader = getConfiguredDataReader(platform, model); - - dataReader.getSink().start(); - for (int idx = 0; (files != null) && (idx < files.length); idx++) - { - writeDataToDatabase(dataReader, files[idx]); - } - dataReader.getSink().end(); - } - - /** - * Reads the data from the given input streams and writes it to the database to which the given - * platform is connected. Only data that matches the given model will be written. - * - * @param platform The platform, must be connected to a live database - * @param model The model to which to constrain the written data - * @param inputs The input streams for the XML data - */ - public void writeDataToDatabase(Platform platform, Database model, InputStream[] inputs) throws DdlUtilsException - { - DataReader dataReader = getConfiguredDataReader(platform, model); - - dataReader.getSink().start(); - for (int idx = 0; (inputs != null) && (idx < inputs.length); idx++) - { - writeDataToDatabase(dataReader, inputs[idx]); - } - dataReader.getSink().end(); - } - - /** - * Reads the data from the given input readers and writes it to the database to which the given - * platform is connected. Only data that matches the given model will be written. - * - * @param platform The platform, must be connected to a live database - * @param model The model to which to constrain the written data - * @param inputs The input readers for the XML data - */ - public void writeDataToDatabase(Platform platform, Database model, Reader[] inputs) throws DdlUtilsException - { - DataReader dataReader = getConfiguredDataReader(platform, model); - - dataReader.getSink().start(); - for (int idx = 0; (inputs != null) && (idx < inputs.length); idx++) - { - writeDataToDatabase(dataReader, inputs[idx]); - } - dataReader.getSink().end(); - } - - /** - * Reads the data from the specified files and writes it to the database via the given data reader. - * Note that the sink that the data reader is configured with, won't be started or ended by - * this method. This has to be done by the code using this method. - * - * @param dataReader The data reader - * @param files The XML data files - */ - public void writeDataToDatabase(DataReader dataReader, String[] files) throws DdlUtilsException - { - for (int idx = 0; (files != null) && (idx < files.length); idx++) - { - writeDataToDatabase(dataReader, files[idx]); - } - } - - /** - * Reads the data from the given input stream and writes it to the database via the given data reader. - * Note that the input stream won't be closed by this method. Note also that the sink that the data - * reader is configured with, won't be started or ended by this method. This has to be done by the - * code using this method. - * - * @param dataReader The data reader - * @param inputs The input streams for the XML data - */ - public void writeDataToDatabase(DataReader dataReader, InputStream[] inputs) throws DdlUtilsException - { - for (int idx = 0; (inputs != null) && (idx < inputs.length); idx++) - { - writeDataToDatabase(dataReader, inputs[idx]); - } - } - - /** - * Reads the data from the given input stream and writes it to the database via the given data reader. - * Note that the input stream won't be closed by this method. Note also that the sink that the data - * reader is configured with, won't be started or ended by this method. This has to be done by the - * code using this method. - * - * @param dataReader The data reader - * @param inputs The input readers for the XML data - */ - public void writeDataToDatabase(DataReader dataReader, Reader[] inputs) throws DdlUtilsException - { - for (int idx = 0; (inputs != null) && (idx < inputs.length); idx++) - { - writeDataToDatabase(dataReader, inputs[idx]); - } - } - - /** - * Reads the data from the indicated XML file and writes it to the database via the given data reader. - * Note that the sink that the data reader is configured with, won't be started or ended by this method. - * This has to be done by the code using this method. - * - * @param dataReader The data reader - * @param path The path to the XML data file - */ - public void writeDataToDatabase(DataReader dataReader, String path) throws DdlUtilsException - { - try - { - dataReader.parse(path); - } - catch (Exception ex) - { - throw new DdlUtilsException(ex); - } - } - - /** - * Reads the data from the given input stream and writes it to the database via the given data reader. - * Note that the input stream won't be closed by this method. Note also that the sink that the data - * reader is configured with, won't be started or ended by this method. This has to be done by the - * code using this method. - * - * @param dataReader The data reader - * @param input The input stream for the XML data - */ - public void writeDataToDatabase(DataReader dataReader, InputStream input) throws DdlUtilsException - { - try - { - dataReader.parse(input); - } - catch (Exception ex) - { - throw new DdlUtilsException(ex); - } - } - - /** - * Reads the data from the given input stream and writes it to the database via the given data reader. - * Note that the input stream won't be closed by this method. Note also that the sink that the data - * reader is configured with, won't be started or ended by this method. This has to be done by the - * code using this method. - * - * @param dataReader The data reader - * @param input The input reader for the XML data - */ - public void writeDataToDatabase(DataReader dataReader, Reader input) throws DdlUtilsException - { - try - { - dataReader.parse(input); - } - catch (Exception ex) - { - throw new DdlUtilsException(ex); - } - } -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DynaSqlCreateRule.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DynaSqlCreateRule.java deleted file mode 100644 index cc5334bfbf..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/io/DynaSqlCreateRule.java +++ /dev/null @@ -1,83 +0,0 @@ -package org.jumpmind.symmetric.ddl.io; - -/* - * 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 org.apache.commons.beanutils.DynaBean; -import org.apache.commons.digester.Rule; -import org.jumpmind.symmetric.ddl.model.Database; -import org.jumpmind.symmetric.ddl.model.Table; -import org.xml.sax.Attributes; - -/** - * A digester rule for creating dyna beans. - * - * @version $Revision: 289996 $ - */ -public class DynaSqlCreateRule extends Rule -{ - /** The database model for which we'l be creating beans. */ - private Database _model; - /** The table that we're creating instances for. */ - private Table _table; - /** The object that will receive the read beans. */ - private DataSink _receiver; - - /** - * Creates a new creation rule that creates dyna bean instances. - * - * @param model The database model that we're operating on - * @param table The table that we're creating instances for - * @param receiver The object that will receive the read beans - */ - public DynaSqlCreateRule(Database model, Table table, DataSink receiver) - { - _model = model; - _table = table; - _receiver = receiver; - } - - /** - * {@inheritDoc} - */ - public void begin(String namespace, String name, Attributes attributes) throws Exception - { - Object instance = _model.createDynaBeanFor(_table); - - if (digester.getLogger().isDebugEnabled()) - { - digester.getLogger().debug("[DynaSqlCreateRule]{" + digester.getMatch() + "} New dyna bean '" + _table.getName() + "' created"); - } - digester.push(instance); - } - - /** - * {@inheritDoc} - */ - public void end(String namespace, String name) throws Exception - { - DynaBean top = (DynaBean)digester.pop(); - - if (digester.getLogger().isDebugEnabled()) - { - digester.getLogger().debug("[DynaSqlCreateRule]{" + digester.getMatch() + "} Pop " + top.getDynaClass().getName()); - } - _receiver.addBean(top); - } -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/model/Database.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/model/Database.java index aba65c357e..db9486a2c9 100644 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/model/Database.java +++ b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/model/Database.java @@ -26,12 +26,8 @@ import java.util.HashSet; import java.util.Iterator; -import org.apache.commons.beanutils.DynaBean; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; -import org.jumpmind.symmetric.ddl.dynabean.DynaClassCache; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaClass; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaException; /** * Represents the database model, ie. the tables in the database. It also @@ -53,8 +49,6 @@ public class Database implements Serializable, Cloneable private String _version; /** The tables. */ private ArrayList _tables = new ArrayList(); - /** The dyna class cache for this model. */ - private transient DynaClassCache _dynaClassCache = null; /** * Adds all tables from the other database to this database. @@ -447,79 +441,6 @@ public Table findTable(String name, boolean caseSensitive) return null; } - /** - * Returns the dyna class cache. If none is available yet, a new one will be created. - * - * @return The dyna class cache - */ - private DynaClassCache getDynaClassCache() - { - if (_dynaClassCache == null) - { - _dynaClassCache = new DynaClassCache(); - } - return _dynaClassCache; - } - - /** - * Resets the dyna class cache. This should be done for instance when a column - * has been added or removed to a table. - */ - public void resetDynaClassCache() - { - _dynaClassCache = null; - } - - /** - * Returns the {@link org.jumpmind.symmetric.ddl.dynabean.SqlDynaClass} for the given table name. If the it does not - * exist yet, a new one will be created based on the Table definition. - * - * @param tableName The name of the table to create the bean for - * @return The SqlDynaClass for the indicated table or null - * if the model contains no such table - */ - public SqlDynaClass getDynaClassFor(String tableName) - { - Table table = findTable(tableName); - - return table != null ? getDynaClassCache().getDynaClass(table) : null; - } - - /** - * Returns the {@link org.jumpmind.symmetric.ddl.dynabean.SqlDynaClass} for the given dyna bean. - * - * @param bean The dyna bean - * @return The SqlDynaClass for the given bean - */ - public SqlDynaClass getDynaClassFor(DynaBean bean) - { - return getDynaClassCache().getDynaClass(bean); - } - - /** - * Creates a new dyna bean for the given table. - * - * @param table The table to create the bean for - * @return The new dyna bean - */ - public DynaBean createDynaBeanFor(Table table) throws SqlDynaException - { - return getDynaClassCache().createNewInstance(table); - } - - /** - * Convenience method that combines {@link #createDynaBeanFor(Table)} and - * {@link #findTable(String, boolean)}. - * - * @param tableName The name of the table to create the bean for - * @param caseSensitive Whether case matters for the names - * @return The new dyna bean - */ - public DynaBean createDynaBeanFor(String tableName, boolean caseSensitive) throws SqlDynaException - { - return getDynaClassCache().createNewInstance(findTable(tableName, caseSensitive)); - } - /** * {@inheritDoc} */ diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/ModelBasedResultSetIterator.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/ModelBasedResultSetIterator.java deleted file mode 100644 index 5a1a370acc..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/ModelBasedResultSetIterator.java +++ /dev/null @@ -1,388 +0,0 @@ -package org.jumpmind.symmetric.ddl.platform; - -/* - * 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.sql.Connection; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.NoSuchElementException; - -import org.apache.commons.beanutils.BasicDynaBean; -import org.apache.commons.beanutils.BasicDynaClass; -import org.apache.commons.beanutils.DynaBean; -import org.apache.commons.beanutils.DynaClass; -import org.apache.commons.beanutils.DynaProperty; -import org.apache.commons.collections.map.ListOrderedMap; -import org.jumpmind.symmetric.ddl.DatabaseOperationException; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaBean; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaClass; -import org.jumpmind.symmetric.ddl.model.Column; -import org.jumpmind.symmetric.ddl.model.Database; -import org.jumpmind.symmetric.ddl.model.Table; - -/** - * This is an iterator that is specifically targeted at traversing result sets. - * If the query is against a known table, then {@link org.jumpmind.symmetric.ddl.dynabean.SqlDynaBean} instances - * are created from the rows, otherwise normal {@link org.apache.commons.beanutils.DynaBean} instances - * are created. - * - * @version $Revision: 289996 $ - */ -public class ModelBasedResultSetIterator implements Iterator -{ - /** The platform. */ - private PlatformImplBase _platform; - /** The base result set. */ - private ResultSet _resultSet; - /** The dyna class to use for creating beans. */ - private DynaClass _dynaClass; - /** Whether the case of identifiers matters. */ - private boolean _caseSensitive; - /** Maps column names to table objects as given by the query hints. */ - private Map _preparedQueryHints; - /** Maps column names to properties. */ - private Map _columnsToProperties = new ListOrderedMap(); - /** Whether the next call to hasNext or next needs advancement. */ - private boolean _needsAdvancing = true; - /** Whether we're already at the end of the result set. */ - private boolean _isAtEnd = false; - /** Whether to close the statement and connection after finishing. */ - private boolean _cleanUpAfterFinish; - - /** - * Creates a new iterator. - * - * @param platform The platform - * @param model The database model - * @param resultSet The result set - * @param queryHints The tables that were queried in the query that produced the given result set - * (optional) - * @param cleanUpAfterFinish Whether to close the statement and connection after finishing - * the iteration, upon on exception, or when this iterator is garbage collected - */ - public ModelBasedResultSetIterator(PlatformImplBase platform, Database model, ResultSet resultSet, Table[] queryHints, boolean cleanUpAfterFinish) throws DatabaseOperationException - { - if (resultSet != null) - { - _platform = platform; - _resultSet = resultSet; - _cleanUpAfterFinish = cleanUpAfterFinish; - _caseSensitive = _platform.isDelimitedIdentifierModeOn(); - _preparedQueryHints = prepareQueryHints(queryHints); - - try - { - initFromMetaData(model); - } - catch (SQLException ex) - { - cleanUp(); - throw new DatabaseOperationException("Could not read the metadata of the result set", ex); - } - } - else - { - _isAtEnd = true; - } - } - - /** - * Initializes this iterator from the resultset metadata. - * - * @param model The database model - */ - private void initFromMetaData(Database model) throws SQLException - { - ResultSetMetaData metaData = _resultSet.getMetaData(); - String tableName = null; - boolean singleKnownTable = true; - - for (int idx = 1; idx <= metaData.getColumnCount(); idx++) - { - String columnName = metaData.getColumnName(idx); - String tableOfColumn = metaData.getTableName(idx); - Table table = null; - - if ((tableOfColumn != null) && (tableOfColumn.length() > 0)) - { - // jConnect might return a table name enclosed in quotes - if (tableOfColumn.startsWith("\"") && tableOfColumn.endsWith("\"") && (tableOfColumn.length() > 1)) - { - tableOfColumn = tableOfColumn.substring(1, tableOfColumn.length() - 1); - } - // the JDBC driver gave us enough meta data info - table = model.findTable(tableOfColumn, _caseSensitive); - } - else - { - // not enough info in the meta data of the result set, lets try the - // user-supplied query hints - table = (Table)_preparedQueryHints.get(_caseSensitive ? columnName : columnName.toLowerCase()); - tableOfColumn = (table == null ? null : table.getName()); - } - if (tableName == null) - { - tableName = tableOfColumn; - } - else if (!tableName.equals(tableOfColumn)) - { - singleKnownTable = false; - } - - String propName = columnName; - - if (table != null) - { - Column column = table.findColumn(columnName, _caseSensitive); - - if (column != null) - { - propName = column.getName(); - } - } - _columnsToProperties.put(columnName, propName); - } - if (singleKnownTable && (tableName != null)) - { - _dynaClass = model.getDynaClassFor(tableName); - } - else - { - DynaProperty[] props = new DynaProperty[_columnsToProperties.size()]; - int idx = 0; - - for (Iterator it = _columnsToProperties.values().iterator(); it.hasNext(); idx++) - { - props[idx] = new DynaProperty((String)it.next()); - } - _dynaClass = new BasicDynaClass("result", BasicDynaBean.class, props); - } - } - - /** - * Prepares the query hints by extracting the column names and using them as keys - * into the resulting map pointing to the corresponding table. - * - * @param queryHints The query hints - * @return The column name -> table map - */ - private Map prepareQueryHints(Table[] queryHints) - { - Map result = new HashMap(); - - for (int tableIdx = 0; (queryHints != null) && (tableIdx < queryHints.length); tableIdx++) - { - for (int columnIdx = 0; columnIdx < queryHints[tableIdx].getColumnCount(); columnIdx++) - { - String columnName = queryHints[tableIdx].getColumn(columnIdx).getName(); - - if (!_caseSensitive) - { - columnName = columnName.toLowerCase(); - } - if (!result.containsKey(columnName)) - { - result.put(columnName, queryHints[tableIdx]); - } - } - } - return result; - } - - /** - * {@inheritDoc} - */ - public boolean hasNext() throws DatabaseOperationException - { - advanceIfNecessary(); - return !_isAtEnd; - } - - /** - * {@inheritDoc} - */ - public Object next() throws DatabaseOperationException - { - advanceIfNecessary(); - if (_isAtEnd) - { - throw new NoSuchElementException("No more elements in the resultset"); - } - else - { - try - { - DynaBean bean = _dynaClass.newInstance(); - Table table = null; - - if (bean instanceof SqlDynaBean) - { - SqlDynaClass dynaClass = (SqlDynaClass)((SqlDynaBean)bean).getDynaClass(); - - table = dynaClass.getTable(); - } - - for (Iterator it = _columnsToProperties.entrySet().iterator(); it.hasNext();) - { - Map.Entry entry = (Map.Entry)it.next(); - String columnName = (String)entry.getKey(); - String propName = (String)entry.getValue(); - Table curTable = table; - - if (curTable == null) - { - curTable = (Table)_preparedQueryHints.get(_caseSensitive ? columnName : columnName.toLowerCase()); - } - - Object value = _platform.getObjectFromResultSet(_resultSet, columnName, curTable); - - bean.set(propName, value); - } - _needsAdvancing = true; - return bean; - } - catch (Exception ex) - { - cleanUp(); - throw new DatabaseOperationException("Exception while reading the row from the resultset", ex); - } - } - } - - /** - * Advances the iterator without materializing the object. This is the same effect as calling - * {@link #next()} except that no object is created and nothing is read from the result set. - */ - public void advance() - { - advanceIfNecessary(); - if (_isAtEnd) - { - throw new NoSuchElementException("No more elements in the resultset"); - } - else - { - _needsAdvancing = true; - } - } - - /** - * Advances the result set if necessary. - */ - private void advanceIfNecessary() throws DatabaseOperationException - { - if (_needsAdvancing && !_isAtEnd) - { - try - { - _isAtEnd = !_resultSet.next(); - _needsAdvancing = false; - } - catch (SQLException ex) - { - cleanUp(); - throw new DatabaseOperationException("Could not retrieve next row from result set", ex); - } - if (_isAtEnd) - { - cleanUp(); - } - } - } - - /** - * {@inheritDoc} - */ - public void remove() throws DatabaseOperationException - { - try - { - _resultSet.deleteRow(); - } - catch (SQLException ex) - { - cleanUp(); - throw new DatabaseOperationException("Failed to delete current row", ex); - } - } - - /** - * Closes the resources (connection, statement, resultset). - */ - public void cleanUp() - { - if (_cleanUpAfterFinish && (_resultSet != null)) - { - Connection conn = null; - try - { - Statement stmt = _resultSet.getStatement(); - - conn = stmt.getConnection(); - - // also closes the resultset - _platform.closeStatement(stmt); - } - catch (SQLException ex) - { - // we ignore it - } - _platform.returnConnection(conn); - _resultSet = null; - } - } - - /** - * {@inheritDoc} - */ - protected void finalize() throws Throwable - { - cleanUp(); - } - - /** - * Determines whether the connection is still open. - * - * @return true if the connection is still open - */ - public boolean isConnectionOpen() - { - if (_resultSet == null) - { - return false; - } - try - { - Statement stmt = _resultSet.getStatement(); - Connection conn = stmt.getConnection(); - - return !conn.isClosed(); - } - catch (SQLException ex) - { - return false; - } - } -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/PlatformImplBase.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/PlatformImplBase.java index 7a4d260f55..2436488125 100644 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/PlatformImplBase.java +++ b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/PlatformImplBase.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.io.StringWriter; -import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.sql.Blob; import java.sql.Clob; @@ -32,26 +31,14 @@ import java.sql.SQLWarning; import java.sql.Statement; import java.sql.Types; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; import java.util.Map; -import org.apache.commons.beanutils.DynaBean; -import org.apache.commons.beanutils.PropertyUtils; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.Predicate; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jumpmind.symmetric.ddl.DatabaseOperationException; import org.jumpmind.symmetric.ddl.DdlUtilsException; import org.jumpmind.symmetric.ddl.Platform; import org.jumpmind.symmetric.ddl.PlatformInfo; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaClass; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaProperty; import org.jumpmind.symmetric.ddl.model.Column; import org.jumpmind.symmetric.ddl.model.Database; import org.jumpmind.symmetric.ddl.model.Table; @@ -861,1002 +848,6 @@ public String getDropTablesSql(Database model, boolean continueOnError) return sql; } - /** - * {@inheritDoc} - */ - public Iterator query(Database model, String sql) throws DatabaseOperationException - { - return query(model, sql, (Table[])null); - } - - /** - * {@inheritDoc} - */ - public Iterator query(Database model, String sql, Collection parameters) throws DatabaseOperationException - { - return query(model, sql, parameters, null); - } - - /** - * {@inheritDoc} - */ - public Iterator query(Database model, String sql, Table[] queryHints) throws DatabaseOperationException - { - Connection connection = borrowConnection(); - Statement statement = null; - ResultSet resultSet = null; - Iterator answer = null; - - try - { - statement = connection.createStatement(); - resultSet = statement.executeQuery(sql); - answer = createResultSetIterator(model, resultSet, queryHints); - return answer; - } - catch (SQLException ex) - { - throw new DatabaseOperationException("Error while performing a query", ex); - } - finally - { - // if any exceptions are thrown, close things down - // otherwise we're leaving it open for the iterator - if (answer == null) - { - closeStatement(statement); - returnConnection(connection); - } - } - } - - /** - * {@inheritDoc} - */ - public Iterator query(Database model, String sql, Collection parameters, Table[] queryHints) throws DatabaseOperationException - { - Connection connection = borrowConnection(); - PreparedStatement statement = null; - ResultSet resultSet = null; - Iterator answer = null; - - try - { - statement = connection.prepareStatement(sql); - - int paramIdx = 1; - - for (Iterator iter = parameters.iterator(); iter.hasNext(); paramIdx++) - { - Object arg = iter.next(); - - if (arg instanceof BigDecimal) - { - // to avoid scale problems because setObject assumes a scale of 0 - statement.setBigDecimal(paramIdx, (BigDecimal)arg); - } - else - { - statement.setObject(paramIdx, arg); - } - } - resultSet = statement.executeQuery(); - answer = createResultSetIterator(model, resultSet, queryHints); - return answer; - } - catch (SQLException ex) - { - throw new DatabaseOperationException("Error while performing a query", ex); - } - finally - { - // if any exceptions are thrown, close things down - // otherwise we're leaving it open for the iterator - if (answer == null) - { - closeStatement(statement); - returnConnection(connection); - } - } - } - - /** - * {@inheritDoc} - */ - public List fetch(Database model, String sql) throws DatabaseOperationException - { - return fetch(model, sql, (Table[])null, 0, -1); - } - - /** - * {@inheritDoc} - */ - public List fetch(Database model, String sql, Table[] queryHints) throws DatabaseOperationException - { - return fetch(model, sql, queryHints, 0, -1); - } - - /** - * {@inheritDoc} - */ - public List fetch(Database model, String sql, int start, int end) throws DatabaseOperationException - { - return fetch(model, sql, (Table[])null, start, end); - } - - /** - * {@inheritDoc} - */ - public List fetch(Database model, String sql, Table[] queryHints, int start, int end) throws DatabaseOperationException - { - Connection connection = borrowConnection(); - Statement statement = null; - ResultSet resultSet = null; - List result = new ArrayList(); - - try - { - statement = connection.createStatement(); - resultSet = statement.executeQuery(sql); - - int rowIdx = 0; - - for (ModelBasedResultSetIterator it = createResultSetIterator(model, resultSet, queryHints); ((end < 0) || (rowIdx <= end)) && it.hasNext(); rowIdx++) - { - if (rowIdx >= start) - { - result.add(it.next()); - } - else - { - it.advance(); - } - } - } - catch (SQLException ex) - { - throw new DatabaseOperationException("Error while fetching data from the database", ex); - } - finally - { - // the iterator should return the connection automatically - // so this is usually not necessary (but just in case) - closeStatement(statement); - returnConnection(connection); - } - return result; - } - - /** - * {@inheritDoc} - */ - public List fetch(Database model, String sql, Collection parameters) throws DatabaseOperationException - { - return fetch(model, sql, parameters, null, 0, -1); - } - - /** - * {@inheritDoc} - */ - public List fetch(Database model, String sql, Collection parameters, int start, int end) throws DatabaseOperationException - { - return fetch(model, sql, parameters, null, start, end); - } - - /** - * {@inheritDoc} - */ - public List fetch(Database model, String sql, Collection parameters, Table[] queryHints) throws DatabaseOperationException - { - return fetch(model, sql, parameters, queryHints, 0, -1); - } - - /** - * {@inheritDoc} - */ - public List fetch(Database model, String sql, Collection parameters, Table[] queryHints, int start, int end) throws DatabaseOperationException - { - Connection connection = borrowConnection(); - PreparedStatement statement = null; - ResultSet resultSet = null; - List result = new ArrayList(); - - try - { - statement = connection.prepareStatement(sql); - - int paramIdx = 1; - - for (Iterator iter = parameters.iterator(); iter.hasNext(); paramIdx++) - { - Object arg = iter.next(); - - if (arg instanceof BigDecimal) - { - // to avoid scale problems because setObject assumes a scale of 0 - statement.setBigDecimal(paramIdx, (BigDecimal)arg); - } - else - { - statement.setObject(paramIdx, arg); - } - } - resultSet = statement.executeQuery(); - - int rowIdx = 0; - - for (ModelBasedResultSetIterator it = createResultSetIterator(model, resultSet, queryHints); ((end < 0) || (rowIdx <= end)) && it.hasNext(); rowIdx++) - { - if (rowIdx >= start) - { - result.add(it.next()); - } - else - { - it.advance(); - } - } - } - catch (SQLException ex) - { - // any other exception comes from the iterator which closes the resources automatically - closeStatement(statement); - returnConnection(connection); - throw new DatabaseOperationException("Error while fetching data from the database", ex); - } - return result; - } - - /** - * Creates the SQL for inserting an object of the given type. If a concrete bean is given, - * then a concrete insert statement is created, otherwise an insert statement usable in a - * prepared statement is build. - * - * @param model The database model - * @param dynaClass The type - * @param properties The properties to write - * @param bean Optionally the concrete bean to insert - * @return The SQL required to insert an instance of the class - */ - protected String createInsertSql(Database model, SqlDynaClass dynaClass, SqlDynaProperty[] properties, DynaBean bean) - { - Table table = model.findTable(dynaClass.getTableName()); - HashMap columnValues = toColumnValues(properties, bean); - - return _builder.getInsertSql(table, columnValues, bean == null); - } - - /** - * Creates the SQL for querying for the id generated by the last insert of an object of the given type. - * - * @param model The database model - * @param dynaClass The type - * @return The SQL required for querying for the id, or null if the database does not - * support this - */ - protected String createSelectLastInsertIdSql(Database model, SqlDynaClass dynaClass) - { - Table table = model.findTable(dynaClass.getTableName()); - - return _builder.getSelectLastIdentityValues(table); - } - - /** - * {@inheritDoc} - */ - public String getInsertSql(Database model, DynaBean dynaBean) - { - SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean); - SqlDynaProperty[] properties = dynaClass.getSqlDynaProperties(); - - if (properties.length == 0) - { - _log.info("Cannot insert instances of type " + dynaClass + " because it has no properties"); - return null; - } - - return createInsertSql(model, dynaClass, properties, dynaBean); - } - - /** - * Returns all properties where the column is not non-autoincrement and for which the bean - * either has a value or the column hasn't got a default value, for the given dyna class. - * - * @param model The database model - * @param dynaClass The dyna class - * @param bean The bean - * @return The properties - */ - private SqlDynaProperty[] getPropertiesForInsertion(Database model, SqlDynaClass dynaClass, final DynaBean bean) - { - SqlDynaProperty[] properties = dynaClass.getSqlDynaProperties(); - - Collection result = CollectionUtils.select(Arrays.asList(properties), new Predicate() { - public boolean evaluate(Object input) { - SqlDynaProperty prop = (SqlDynaProperty)input; - - if (bean.get(prop.getName()) != null) - { - // we ignore properties for which a value is present in the bean - // only if they are identity and identity override is off or - // the platform does not allow the override of the auto-increment - // specification - return !prop.getColumn().isAutoIncrement() || - (isIdentityOverrideOn() && getPlatformInfo().isIdentityOverrideAllowed()); - } - else - { - // we also return properties without a value in the bean - // if they ain't auto-increment and don't have a default value - // in this case, a NULL is inserted - return !prop.getColumn().isAutoIncrement() && - (prop.getColumn().getDefaultValue() == null); - } - } - }); - - return (SqlDynaProperty[])result.toArray(new SqlDynaProperty[result.size()]); - } - - /** - * Returns all identity properties whose value were defined by the database and which - * now need to be read back from the DB. - * - * @param model The database model - * @param dynaClass The dyna class - * @param bean The bean - * @return The columns - */ - private Column[] getRelevantIdentityColumns(Database model, SqlDynaClass dynaClass, final DynaBean bean) - { - SqlDynaProperty[] properties = dynaClass.getSqlDynaProperties(); - - Collection relevantProperties = CollectionUtils.select(Arrays.asList(properties), new Predicate() { - public boolean evaluate(Object input) { - SqlDynaProperty prop = (SqlDynaProperty)input; - - // we only want those identity columns that were really specified by the DB - // if the platform allows specification of values for identity columns - // in INSERT/UPDATE statements, then we need to filter the corresponding - // columns out - return prop.getColumn().isAutoIncrement() && - (!isIdentityOverrideOn() || !getPlatformInfo().isIdentityOverrideAllowed() || (bean.get(prop.getName()) == null)); - } - }); - - Column[] columns = new Column[relevantProperties.size()]; - int idx = 0; - - for (Iterator propIt = relevantProperties.iterator(); propIt.hasNext(); idx++) - { - columns[idx] = ((SqlDynaProperty)propIt.next()).getColumn(); - } - return columns; - } - - /** - * {@inheritDoc} - */ - public void insert(Connection connection, Database model, DynaBean dynaBean) throws DatabaseOperationException - { - SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean); - SqlDynaProperty[] properties = getPropertiesForInsertion(model, dynaClass, dynaBean); - Column[] autoIncrColumns = getRelevantIdentityColumns(model, dynaClass, dynaBean); - - if ((properties.length == 0) && (autoIncrColumns.length == 0)) - { - _log.warn("Cannot insert instances of type " + dynaClass + " because it has no usable properties"); - return; - } - - String insertSql = createInsertSql(model, dynaClass, properties, null); - String queryIdentitySql = null; - - if (_log.isDebugEnabled()) - { - _log.debug("About to execute SQL: " + insertSql); - } - - if (autoIncrColumns.length > 0) - { - if (!getPlatformInfo().isLastIdentityValueReadable()) - { - _log.warn("The database does not support querying for auto-generated column values"); - } - else - { - queryIdentitySql = createSelectLastInsertIdSql(model, dynaClass); - } - } - - boolean autoCommitMode = false; - PreparedStatement statement = null; - - try - { - if (!getPlatformInfo().isAutoCommitModeForLastIdentityValueReading()) - { - autoCommitMode = connection.getAutoCommit(); - connection.setAutoCommit(false); - } - - beforeInsert(connection, dynaClass.getTable()); - - statement = connection.prepareStatement(insertSql); - - for (int idx = 0; idx < properties.length; idx++ ) - { - setObject(statement, idx + 1, dynaBean, properties[idx]); - } - - int count = statement.executeUpdate(); - - afterInsert(connection, dynaClass.getTable()); - - if (count != 1) - { - _log.warn("Attempted to insert a single row " + dynaBean + - " in table " + dynaClass.getTableName() + - " but changed " + count + " row(s)"); - } - } - catch (SQLException ex) - { - throw new DatabaseOperationException("Error while inserting into the database: " + ex.getMessage(), ex); - } - finally - { - closeStatement(statement); - } - if (queryIdentitySql != null) - { - Statement queryStmt = null; - ResultSet lastInsertedIds = null; - - try - { - if (getPlatformInfo().isAutoCommitModeForLastIdentityValueReading()) - { - // we'll commit the statement(s) if no auto-commit is enabled because - // otherwise it is possible that the auto increment hasn't happened yet - // (the db didn't actually perform the insert yet so no triggering of - // sequences did occur) - if (!connection.getAutoCommit()) - { - connection.commit(); - } - } - - queryStmt = connection.createStatement(); - lastInsertedIds = queryStmt.executeQuery(queryIdentitySql); - - lastInsertedIds.next(); - - for (int idx = 0; idx < autoIncrColumns.length; idx++) - { - // we're using the index rather than the name because we cannot know how - // the SQL statement looks like; rather we assume that we get the values - // back in the same order as the auto increment columns - Object value = getObjectFromResultSet(lastInsertedIds, autoIncrColumns[idx], idx + 1); - - PropertyUtils.setProperty(dynaBean, autoIncrColumns[idx].getName(), value); - } - } - catch (NoSuchMethodException ex) - { - // Can't happen because we're using dyna beans - } - catch (IllegalAccessException ex) - { - // Can't happen because we're using dyna beans - } - catch (InvocationTargetException ex) - { - // Can't happen because we're using dyna beans - } - catch (SQLException ex) - { - throw new DatabaseOperationException("Error while retrieving the identity column value(s) from the database", ex); - } - finally - { - if (lastInsertedIds != null) - { - try - { - lastInsertedIds.close(); - } - catch (SQLException ex) - { - // we ignore this one - } - } - closeStatement(statement); - } - } - if (!getPlatformInfo().isAutoCommitModeForLastIdentityValueReading()) - { - try - { - // we need to do a manual commit now - connection.commit(); - connection.setAutoCommit(autoCommitMode); - } - catch (SQLException ex) - { - throw new DatabaseOperationException(ex); - } - } - } - - /** - * {@inheritDoc} - */ - public void insert(Database model, DynaBean dynaBean) throws DatabaseOperationException - { - Connection connection = borrowConnection(); - - try - { - insert(connection, model, dynaBean); - } - finally - { - returnConnection(connection); - } - } - - /** - * {@inheritDoc} - */ - public void insert(Connection connection, Database model, Collection dynaBeans) throws DatabaseOperationException - { - SqlDynaClass dynaClass = null; - SqlDynaProperty[] properties = null; - PreparedStatement statement = null; - int addedStmts = 0; - boolean identityWarningPrinted = false; - - for (Iterator it = dynaBeans.iterator(); it.hasNext();) - { - DynaBean dynaBean = (DynaBean)it.next(); - SqlDynaClass curDynaClass = model.getDynaClassFor(dynaBean); - - if (curDynaClass != dynaClass) - { - if (dynaClass != null) - { - executeBatch(statement, addedStmts, dynaClass.getTable()); - addedStmts = 0; - } - - dynaClass = curDynaClass; - properties = getPropertiesForInsertion(model, curDynaClass, dynaBean); - - if (properties.length == 0) - { - _log.warn("Cannot insert instances of type " + dynaClass + " because it has no usable properties"); - continue; - } - if (!identityWarningPrinted && - (getRelevantIdentityColumns(model, curDynaClass, dynaBean).length > 0)) - { - _log.warn("Updating the bean properties corresponding to auto-increment columns is not supported in batch mode"); - identityWarningPrinted = true; - } - - String insertSql = createInsertSql(model, dynaClass, properties, null); - - if (_log.isDebugEnabled()) - { - _log.debug("Starting new batch with SQL: " + insertSql); - } - try - { - statement = connection.prepareStatement(insertSql); - } - catch (SQLException ex) - { - throw new DatabaseOperationException("Error while preparing insert statement", ex); - } - } - try - { - for (int idx = 0; idx < properties.length; idx++ ) - { - setObject(statement, idx + 1, dynaBean, properties[idx]); - } - statement.addBatch(); - addedStmts++; - } - catch (SQLException ex) - { - throw new DatabaseOperationException("Error while adding batch insert", ex); - } - } - if (dynaClass != null) - { - executeBatch(statement, addedStmts, dynaClass.getTable()); - } - } - - /** - * Performs the batch for the given statement, and checks that the specified amount of rows have been changed. - * - * @param statement The prepared statement - * @param numRows The number of rows that should change - * @param table The changed table - */ - private void executeBatch(PreparedStatement statement, int numRows, Table table) throws DatabaseOperationException - { - if (statement != null) - { - try - { - Connection connection = statement.getConnection(); - - beforeInsert(connection, table); - - int[] results = statement.executeBatch(); - - closeStatement(statement); - afterInsert(connection, table); - - boolean hasSum = true; - int sum = 0; - - for (int idx = 0; (results != null) && (idx < results.length); idx++) - { - if (results[idx] < 0) - { - hasSum = false; - if (Jdbc3Utils.supportsJava14BatchResultCodes()) - { - String msg = Jdbc3Utils.getBatchResultMessage(table.getName(), idx, results[idx]); - - if (msg != null) - { - _log.warn(msg); - } - } - } - else - { - sum += results[idx]; - } - } - if (hasSum && (sum != numRows)) - { - _log.warn("Attempted to insert " + numRows + " rows into table " + table.getName() + " but changed " + sum + " rows"); - } - } - catch (SQLException ex) - { - throw new DatabaseOperationException("Error while inserting into the database", ex); - } - } - } - - /** - * {@inheritDoc} - */ - public void insert(Database model, Collection dynaBeans) throws DatabaseOperationException - { - Connection connection = borrowConnection(); - - try - { - insert(connection, model, dynaBeans); - } - finally - { - returnConnection(connection); - } - } - - /** - * Allows platforms to issue statements directly before rows are inserted into - * the specified table. - * - * @param connection The connection used for the insertion - * @param table The table that the rows are inserted into - */ - protected void beforeInsert(Connection connection, Table table) throws SQLException - { - } - - /** - * Allows platforms to issue statements directly after rows have been inserted into - * the specified table. - * - * @param connection The connection used for the insertion - * @param table The table that the rows have been inserted into - */ - protected void afterInsert(Connection connection, Table table) throws SQLException - { - } - - /** - * Creates the SQL for updating an object of the given type. If a concrete bean is given, - * then a concrete update statement is created, otherwise an update statement usable in a - * prepared statement is build. - * - * @param model The database model - * @param dynaClass The type - * @param primaryKeys The primary keys - * @param properties The properties to write - * @param bean Optionally the concrete bean to update - * @return The SQL required to update the instance - */ - protected String createUpdateSql(Database model, SqlDynaClass dynaClass, SqlDynaProperty[] primaryKeys, SqlDynaProperty[] properties, DynaBean bean) - { - Table table = model.findTable(dynaClass.getTableName()); - HashMap columnValues = toColumnValues(properties, bean); - - columnValues.putAll(toColumnValues(primaryKeys, bean)); - - return _builder.getUpdateSql(table, columnValues, bean == null); - } - - /** - * {@inheritDoc} - */ - public String getUpdateSql(Database model, DynaBean dynaBean) - { - SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean); - SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties(); - - if (primaryKeys.length == 0) - { - _log.info("Cannot update instances of type " + dynaClass + " because it has no primary keys"); - return null; - } - - return createUpdateSql(model, dynaClass, primaryKeys, dynaClass.getNonPrimaryKeyProperties(), dynaBean); - } - - /** - * {@inheritDoc} - */ - public void update(Connection connection, Database model, DynaBean dynaBean) throws DatabaseOperationException - { - SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean); - SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties(); - - if (primaryKeys.length == 0) - { - _log.info("Cannot update instances of type " + dynaClass + " because it has no primary keys"); - return; - } - - SqlDynaProperty[] properties = dynaClass.getNonPrimaryKeyProperties(); - String sql = createUpdateSql(model, dynaClass, primaryKeys, properties, null); - PreparedStatement statement = null; - - if (_log.isDebugEnabled()) - { - _log.debug("About to execute SQL: " + sql); - } - try - { - beforeUpdate(connection, dynaClass.getTable()); - - statement = connection.prepareStatement(sql); - - int sqlIndex = 1; - - for (int idx = 0; idx < properties.length; idx++) - { - setObject(statement, sqlIndex++, dynaBean, properties[idx]); - } - for (int idx = 0; idx < primaryKeys.length; idx++) - { - setObject(statement, sqlIndex++, dynaBean, primaryKeys[idx]); - } - - int count = statement.executeUpdate(); - - afterUpdate(connection, dynaClass.getTable()); - - if (count != 1) - { - _log.warn("Attempted to insert a single row " + dynaBean + - " into table " + dynaClass.getTableName() + - " but changed " + count + " row(s)"); - } - } - catch (SQLException ex) - { - throw new DatabaseOperationException("Error while updating in the database", ex); - } - finally - { - closeStatement(statement); - } - } - - /** - * {@inheritDoc} - */ - public void update(Database model, DynaBean dynaBean) throws DatabaseOperationException - { - Connection connection = borrowConnection(); - - try - { - update(connection, model, dynaBean); - } - finally - { - returnConnection(connection); - } - } - - /** - * Allows platforms to issue statements directly before rows are updated in - * the specified table. - * - * @param connection The connection used for the update - * @param table The table that the rows are updateed into - */ - protected void beforeUpdate(Connection connection, Table table) throws SQLException - { - } - - /** - * Allows platforms to issue statements directly after rows have been updated in - * the specified table. - * - * @param connection The connection used for the update - * @param table The table that the rows have been updateed into - */ - protected void afterUpdate(Connection connection, Table table) throws SQLException - { - } - - /** - * Determines whether the given dyna bean is stored in the database. - * - * @param dynaBean The bean - * @param connection The connection - * @return true if this dyna bean has a primary key - */ - protected boolean exists(Connection connection, DynaBean dynaBean) - { - // TODO: check for the pk value, and if present, query against database - return false; - } - - /** - * {@inheritDoc} - */ - public void store(Database model, DynaBean dynaBean) throws DatabaseOperationException - { - Connection connection = borrowConnection(); - - try - { - if (exists(connection, dynaBean)) - { - update(connection, model, dynaBean); - } - else - { - insert(connection, model, dynaBean); - } - } - finally - { - returnConnection(connection); - } - } - - /** - * Creates the SQL for deleting an object of the given type. If a concrete bean is given, - * then a concrete delete statement is created, otherwise a delete statement usable in a - * prepared statement is build. - * - * @param model The database model - * @param dynaClass The type - * @param primaryKeys The primary keys - * @param bean Optionally the concrete bean to update - * @return The SQL required to delete the instance - */ - protected String createDeleteSql(Database model, SqlDynaClass dynaClass, SqlDynaProperty[] primaryKeys, DynaBean bean) - { - Table table = model.findTable(dynaClass.getTableName()); - HashMap pkValues = toColumnValues(primaryKeys, bean); - - return _builder.getDeleteSql(table, pkValues, bean == null); - } - - /** - * {@inheritDoc} - */ - public String getDeleteSql(Database model, DynaBean dynaBean) - { - SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean); - SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties(); - - if (primaryKeys.length == 0) - { - _log.warn("Cannot delete instances of type " + dynaClass + " because it has no primary keys"); - return null; - } - else - { - return createDeleteSql(model, dynaClass, primaryKeys, dynaBean); - } - } - - /** - * {@inheritDoc} - */ - public void delete(Database model, DynaBean dynaBean) throws DatabaseOperationException - { - Connection connection = borrowConnection(); - - try - { - delete(connection, model, dynaBean); - } - finally - { - returnConnection(connection); - } - } - - /** - * {@inheritDoc} - */ - public void delete(Connection connection, Database model, DynaBean dynaBean) throws DatabaseOperationException - { - PreparedStatement statement = null; - - try - { - SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean); - SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties(); - - if (primaryKeys.length == 0) - { - _log.warn("Cannot delete instances of type " + dynaClass + " because it has no primary keys"); - return; - } - - String sql = createDeleteSql(model, dynaClass, primaryKeys, null); - - if (_log.isDebugEnabled()) - { - _log.debug("About to execute SQL " + sql); - } - - statement = connection.prepareStatement(sql); - - for (int idx = 0; idx < primaryKeys.length; idx++) - { - setObject(statement, idx + 1, dynaBean, primaryKeys[idx]); - } - - int count = statement.executeUpdate(); - - if (count != 1) - { - _log.warn("Attempted to delete a single row " + dynaBean + - " in table " + dynaClass.getTableName() + - " but changed " + count + " row(s)."); - } - } - catch (SQLException ex) - { - throw new DatabaseOperationException("Error while deleting from the database", ex); - } - finally - { - closeStatement(statement); - } - } - /** * {@inheritDoc} */ @@ -1965,41 +956,6 @@ protected void postprocessModelFromDatabase(Database model) } } - /** - * Derives the column values for the given dyna properties from the dyna bean. - * - * @param properties The properties - * @param bean The bean - * @return The values indexed by the column names - */ - protected HashMap toColumnValues(SqlDynaProperty[] properties, DynaBean bean) - { - HashMap result = new HashMap(); - - for (int idx = 0; idx < properties.length; idx++) - { - result.put(properties[idx].getName(), - bean == null ? null : bean.get(properties[idx].getName())); - } - return result; - } - - /** - * Sets a parameter of the prepared statement based on the type of the column of the property. - * - * @param statement The statement - * @param sqlIndex The index of the parameter to set in the statement - * @param dynaBean The bean of which to take the value - * @param property The property of the bean, which also defines the corresponding column - */ - protected void setObject(PreparedStatement statement, int sqlIndex, DynaBean dynaBean, SqlDynaProperty property) throws SQLException - { - int typeCode = property.getColumn().getTypeCode(); - Object value = dynaBean.get(property.getName()); - - setStatementParameterValue(statement, sqlIndex, typeCode, value); - } - /** * This is the core method to set the parameter of a prepared statement to a given value. * The primary purpose of this method is to call the appropriate method on the statement, @@ -2281,18 +1237,4 @@ else if (length == 0) return resultSet.wasNull() ? null : value; } - - /** - * Creates an iterator over the given result set. - * - * @param model The database model - * @param resultSet The result set to iterate over - * @param queryHints The tables that were queried in the query that produced the - * given result set (optional) - * @return The iterator - */ - protected ModelBasedResultSetIterator createResultSetIterator(Database model, ResultSet resultSet, Table[] queryHints) - { - return new ModelBasedResultSetIterator(this, model, resultSet, queryHints, true); - } } diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/postgresql/PostgreSqlPlatform.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/postgresql/PostgreSqlPlatform.java index 77d1dac42e..bf87311072 100644 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/postgresql/PostgreSqlPlatform.java +++ b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/postgresql/PostgreSqlPlatform.java @@ -21,17 +21,14 @@ import java.sql.Connection; import java.sql.DriverManager; -import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; import java.util.Iterator; import java.util.Map; -import org.apache.commons.beanutils.DynaBean; import org.jumpmind.symmetric.ddl.DatabaseOperationException; import org.jumpmind.symmetric.ddl.PlatformInfo; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaProperty; import org.jumpmind.symmetric.ddl.platform.PlatformImplBase; /** @@ -211,33 +208,4 @@ public void dropDatabase(String jdbcDriverClassName, String connectionUrl, Strin createOrDropDatabase(jdbcDriverClassName, connectionUrl, username, password, null, false); } - /** - * {@inheritDoc} - */ - protected void setObject(PreparedStatement statement, int sqlIndex, DynaBean dynaBean, SqlDynaProperty property) throws SQLException - { - int typeCode = property.getColumn().getTypeCode(); - Object value = dynaBean.get(property.getName()); - - // PostgreSQL doesn't like setNull for BYTEA columns - if (value == null) - { - switch (typeCode) - { - case Types.BINARY: - case Types.VARBINARY: - case Types.LONGVARBINARY: - case Types.BLOB: - statement.setBytes(sqlIndex, null); - break; - default: - statement.setNull(sqlIndex, typeCode); - break; - } - } - else - { - super.setObject(statement, sqlIndex, dynaBean, property); - } - } } diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/sybase/SybasePlatform.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/sybase/SybasePlatform.java index b8358e45dc..d20ce2dfa7 100644 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/sybase/SybasePlatform.java +++ b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/platform/sybase/SybasePlatform.java @@ -28,13 +28,9 @@ import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; import org.jumpmind.symmetric.ddl.DatabaseOperationException; import org.jumpmind.symmetric.ddl.PlatformInfo; -import org.jumpmind.symmetric.ddl.model.Database; import org.jumpmind.symmetric.ddl.model.Table; import org.jumpmind.symmetric.ddl.model.TypeMap; import org.jumpmind.symmetric.ddl.platform.PlatformImplBase; @@ -225,43 +221,6 @@ else if (typeCode == Types.CLOB) } } - /** - * {@inheritDoc} - */ - public List fetch(Database model, String sql, Collection parameters, Table[] queryHints, int start, int end) throws DatabaseOperationException - { - setTextSize(MAX_TEXT_SIZE); - return super.fetch(model, sql, parameters, queryHints, start, end); - } - - /** - * {@inheritDoc} - */ - public List fetch(Database model, String sql, Table[] queryHints, int start, int end) throws DatabaseOperationException - { - setTextSize(MAX_TEXT_SIZE); - return super.fetch(model, sql, queryHints, start, end); - } - - /** - * {@inheritDoc} - */ - public Iterator query(Database model, String sql, Collection parameters, Table[] queryHints) throws DatabaseOperationException - { - setTextSize(MAX_TEXT_SIZE); - return super.query(model, sql, parameters, queryHints); - } - - /** - * {@inheritDoc} - */ - public Iterator query(Database model, String sql, Table[] queryHints) throws DatabaseOperationException - { - setTextSize(MAX_TEXT_SIZE); - return super.query(model, sql, queryHints); - } - - /** * Determines whether we need to use identity override mode for the given table. * diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/ConvertingDatabaseCommand.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/ConvertingDatabaseCommand.java deleted file mode 100644 index 1e6271292e..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/ConvertingDatabaseCommand.java +++ /dev/null @@ -1,55 +0,0 @@ -package org.jumpmind.symmetric.ddl.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 org.jumpmind.symmetric.ddl.io.DataConverterRegistration; -import org.jumpmind.symmetric.ddl.io.DatabaseDataIO; - -/** - * Base type for database commands that use converters. - * - * @version $Revision: 289996 $ - * @ant.type ignore="true" - */ -public abstract class ConvertingDatabaseCommand extends DatabaseCommand -{ - /** The database data io object. */ - private DatabaseDataIO _dataIO = new DatabaseDataIO(); - - /** - * Returns the database data io object. - * - * @return The data io object - */ - protected DatabaseDataIO getDataIO() - { - return _dataIO; - } - - /** - * Registers a converter. - * - * @param converterRegistration The registration info - */ - public void addConfiguredConverter(DataConverterRegistration converterRegistration) - { - _dataIO.registerConverter(converterRegistration); - } -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/DatabaseToDdlTask.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/DatabaseToDdlTask.java index 7a909a9f9a..7e8cad977c 100644 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/DatabaseToDdlTask.java +++ b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/DatabaseToDdlTask.java @@ -119,26 +119,6 @@ public void addWriteSchemaSqlToFile(WriteSchemaSqlToFileCommand command) addCommand(command); } - /** - * Adds the "write data into database"-command. - * - * @param command The command - */ - public void addWriteDataToDatabase(WriteDataToDatabaseCommand command) - { - addCommand(command); - } - - /** - * Adds the "write data into file"-command. - * - * @param command The command - */ - public void addWriteDataToFile(WriteDataToFileCommand command) - { - addCommand(command); - } - /** * Returns the table types to recognize. * diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/DdlToDatabaseTask.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/DdlToDatabaseTask.java index 0003262b34..649949030d 100644 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/DdlToDatabaseTask.java +++ b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/DdlToDatabaseTask.java @@ -170,26 +170,6 @@ public void addWriteSchemaSqlToFile(WriteSchemaSqlToFileCommand command) addCommand(command); } - /** - * Adds the "write data to database"-command. - * - * @param command The command - */ - public void addWriteDataToDatabase(WriteDataToDatabaseCommand command) - { - addCommand(command); - } - - /** - * Adds the "write data to file"-command. - * - * @param command The command - */ - public void addWriteDataToFile(WriteDataToFileCommand command) - { - addCommand(command); - } - /** * {@inheritDoc} */ diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/WriteDataToDatabaseCommand.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/WriteDataToDatabaseCommand.java deleted file mode 100644 index faac9d6e95..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/WriteDataToDatabaseCommand.java +++ /dev/null @@ -1,226 +0,0 @@ -package org.jumpmind.symmetric.ddl.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.util.ArrayList; -import java.util.Iterator; - -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.Task; -import org.apache.tools.ant.types.FileSet; -import org.jumpmind.symmetric.ddl.Platform; -import org.jumpmind.symmetric.ddl.io.DataReader; -import org.jumpmind.symmetric.ddl.model.Database; - -/** - * Inserts the data defined by the data XML file(s) into the database. This requires the schema - * in the database to match the schema defined by the XML files specified at the enclosing task.
- * DdlUtils will honor the order imposed by the foreign keys. Ie. first all required entries are - * inserted, then the dependent ones. Obviously this requires that no circular references exist - * in the schema (DdlUtils currently does not check this). Also, the referenced entries must be - * present in the data, otherwise the task will fail. This behavior can be turned off via the - * ensureForeignKeyOrder attribute.
- * In order to define data for foreign key dependencies that use auto-incrementing primary keys, - * simply use some unique values for their columns. DdlUtils then will automatically use the real - * primary key values when inserting the data. Note though that not every database supports the - * retrieval of auto-increment values which is necessary for this to work. - * - * @version $Revision: 289996 $ - * @ant.task name="writeDataToDatabase" - */ -public class WriteDataToDatabaseCommand extends ConvertingDatabaseCommand -{ - /** A single data file to insert. */ - private File _singleDataFile = null; - /** The input files. */ - private ArrayList _fileSets = new ArrayList(); - /** Whether explicit values for identity columns will be used. */ - private boolean _useExplicitIdentityValues; - - /** - * Defines whether values for identity columns in the data XML shall be used instead of - * letting the database define the value. Unless ensureForeignKeyOrder is - * set to false, setting this to false (the default) does not affect foreign - * keys as DdlUtils will automatically update the values of the columns of foreign keys - * pointing to the inserted row with the database-created values. - * - * @param useExplicitIdentityValues true if explicitly specified identity - * column values should be inserted instead of letting - * the database define the values for these columns - * @ant.not-required Default is false - */ - public void setUseExplicitIdentityValues(boolean useExplicitIdentityValues) - { - _useExplicitIdentityValues = useExplicitIdentityValues; - } - - /** - * Adds a fileset. - * - * @param fileset The additional input files - */ - public void addConfiguredFileset(FileSet fileset) - { - _fileSets.add(fileset); - } - - /** - * Specifies the name of the single XML file that contains the data to insert into the database. - * - * @param dataFile The data file - * @ant.not-required Use either this or fileset sub elements. - */ - public void setDataFile(File dataFile) - { - _singleDataFile = dataFile; - } - - /** - * The maximum number of insert statements to combine in one batch. The number typically - * depends on the JDBC driver and the amount of available memory.
- * This value is only used if useBatchMode is true. - * - * @param batchSize The number of objects - * @ant.not-required The default value is 1. - */ - public void setBatchSize(int batchSize) - { - getDataIO().setBatchSize(new Integer(batchSize)); - } - - /** - * Specifies whether batch mode shall be used for inserting the data. In batch mode, insert statements - * for the same table are bundled together and executed as one statement. This can be a lot faster - * than single insert statements but is not supported by all JDBC drivers/databases. To achieve the - * highest performance, you should group the data in the XML file according to the tables. This is - * because a batch insert only works for one table at a time. Thus when the table changes in an - * entry in the XML file, the batch is committed and then a new one is started. - * - * @param useBatchMode true if batch mode shall be used - * @ant.not-required Per default batch mode is not used. - */ - public void setUseBatchMode(boolean useBatchMode) - { - getDataIO().setUseBatchMode(useBatchMode); - } - - /** - * Specifies whether the foreign key order shall be honored when inserting data into the database. - * If not, DdlUtils will simply assume that the entry order is correct, i.e. that referenced rows - * come before referencing rows in the data XML. Note that execution will be slower when DdlUtils - * has to ensure the foreign-key order of the data. Thus if you know that the data is specified in - * foreign key order turn this off. - * - * @param ensureFKOrder true if the foreign key order shall be followed - * @ant.not-required Per default foreign key order is honored. - */ - public void setEnsureForeignKeyOrder(boolean ensureFKOrder) - { - getDataIO().setEnsureFKOrder(ensureFKOrder); - } - - /** - * {@inheritDoc} - */ - public void execute(DatabaseTaskBase task, Database model) throws BuildException - { - if ((_singleDataFile != null) && !_fileSets.isEmpty()) - { - throw new BuildException("Please use either the datafile attribute or the sub fileset element, but not both"); - } - - Platform platform = getPlatform(); - DataReader dataReader = null; - - platform.setIdentityOverrideOn(_useExplicitIdentityValues); - try - { - dataReader = getDataIO().getConfiguredDataReader(platform, model); - dataReader.getSink().start(); - if (_singleDataFile != null) - { - readSingleDataFile(task, dataReader, _singleDataFile); - } - else - { - for (Iterator it = _fileSets.iterator(); it.hasNext();) - { - FileSet fileSet = (FileSet)it.next(); - File fileSetDir = fileSet.getDir(task.getProject()); - DirectoryScanner scanner = fileSet.getDirectoryScanner(task.getProject()); - String[] files = scanner.getIncludedFiles(); - - for (int idx = 0; (files != null) && (idx < files.length); idx++) - { - readSingleDataFile(task, dataReader, new File(fileSetDir, files[idx])); - } - } - } - } - catch (Exception ex) - { - handleException(ex, ex.getMessage()); - } - finally - { - if (dataReader != null) - { - dataReader.getSink().end(); - } - } - } - - /** - * Reads a single data file. - * - * @param task The parent task - * @param reader The data reader - * @param dataFile The schema file - */ - private void readSingleDataFile(Task task, DataReader reader, File dataFile) throws BuildException - { - if (!dataFile.exists()) - { - _log.error("Could not find data file " + dataFile.getAbsolutePath()); - } - else if (!dataFile.isFile()) - { - _log.error("Path " + dataFile.getAbsolutePath() + " does not denote a data file"); - } - else if (!dataFile.canRead()) - { - _log.error("Could not read data file " + dataFile.getAbsolutePath()); - } - else - { - try - { - getDataIO().writeDataToDatabase(reader, dataFile.getAbsolutePath()); - _log.info("Written data from file " + dataFile.getAbsolutePath() + " to database"); - } - catch (Exception ex) - { - handleException(ex, "Could not parse or write data file " + dataFile.getAbsolutePath()); - } - } - } -} diff --git a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/WriteDataToFileCommand.java b/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/WriteDataToFileCommand.java deleted file mode 100644 index 6e006d813a..0000000000 --- a/symmetric/symmetric-ddl/src/main/java/org/jumpmind/symmetric/ddl/task/WriteDataToFileCommand.java +++ /dev/null @@ -1,98 +0,0 @@ -package org.jumpmind.symmetric.ddl.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.FileOutputStream; - -import org.apache.tools.ant.BuildException; -import org.jumpmind.symmetric.ddl.model.Database; - -/** - * Reads the data currently in the table in the live database (as specified by the - * enclosing task), and writes it as XML to a file. - * - * @version $Revision: 289996 $ - * @ant.task name="writeDataToFile" - */ -public class WriteDataToFileCommand extends ConvertingDatabaseCommand -{ - /** The file to output the data to. */ - private File _outputFile; - /** The character encoding to use. */ - private String _encoding; - - /** Whether DdlUtils should search for the schema of the tables. @deprecated */ - private boolean _determineSchema; - - /** - * Specifies the file to write the data XML to. - * - * @param outputFile The output file - * @ant.required - */ - public void setOutputFile(File outputFile) - { - _outputFile = outputFile; - } - - /** - * Specifies the encoding of the XML file. - * - * @param encoding The encoding - * @ant.not-required The default encoding is UTF-8. - */ - public void setEncoding(String encoding) - { - _encoding = encoding; - } - - /** - * Specifies whether DdlUtils should try to find the schema of the tables when reading data - * from a live database. - * - * @param determineSchema Whether to try to find the table's schemas - * @deprecated Will be removed once proper schema support is in place - */ - public void setDetermineSchema(boolean determineSchema) - { - _determineSchema = determineSchema; - } - - /** - * {@inheritDoc} - */ - public void execute(DatabaseTaskBase task, Database model) throws BuildException - { - try - { - getDataIO().setDetermineSchema(_determineSchema); - getDataIO().setSchemaPattern(task.getPlatformConfiguration().getSchemaPattern()); - getDataIO().writeDataToXML(getPlatform(), - new FileOutputStream(_outputFile), _encoding); - _log.info("Written data XML to file" + _outputFile.getAbsolutePath()); - } - catch (Exception ex) - { - handleException(ex, ex.getMessage()); - } - } - -} diff --git a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/RunAllTests.java b/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/RunAllTests.java deleted file mode 100644 index f683137f42..0000000000 --- a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/RunAllTests.java +++ /dev/null @@ -1,134 +0,0 @@ -package org.jumpmind.symmetric.ddl; - -/* - * 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 org.jumpmind.symmetric.ddl.alteration.TestAlterationAlgorithm; -import org.jumpmind.symmetric.ddl.alteration.TestModelComparator; -import org.jumpmind.symmetric.ddl.dynabean.TestDynaSqlQueries; -import org.jumpmind.symmetric.ddl.io.TestAlteration; -import org.jumpmind.symmetric.ddl.io.TestConstraints; -import org.jumpmind.symmetric.ddl.io.TestDataReaderAndWriter; -import org.jumpmind.symmetric.ddl.io.TestDatabaseIO; -import org.jumpmind.symmetric.ddl.io.TestDatatypes; -import org.jumpmind.symmetric.ddl.io.TestMisc; -import org.jumpmind.symmetric.ddl.io.converters.TestDateConverter; -import org.jumpmind.symmetric.ddl.io.converters.TestTimeConverter; -import org.jumpmind.symmetric.ddl.model.TestArrayAccessAtTable; -import org.jumpmind.symmetric.ddl.platform.TestAxionPlatform; -import org.jumpmind.symmetric.ddl.platform.TestCloudscapePlatform; -import org.jumpmind.symmetric.ddl.platform.TestDB2Platform; -import org.jumpmind.symmetric.ddl.platform.TestDerbyPlatform; -import org.jumpmind.symmetric.ddl.platform.TestFirebirdPlatform; -import org.jumpmind.symmetric.ddl.platform.TestHsqlDbPlatform; -import org.jumpmind.symmetric.ddl.platform.TestInterbasePlatform; -import org.jumpmind.symmetric.ddl.platform.TestMSSqlPlatform; -import org.jumpmind.symmetric.ddl.platform.TestMaxDbPlatform; -import org.jumpmind.symmetric.ddl.platform.TestMcKoiPlatform; -import org.jumpmind.symmetric.ddl.platform.TestMySql50Platform; -import org.jumpmind.symmetric.ddl.platform.TestMySqlPlatform; -import org.jumpmind.symmetric.ddl.platform.TestOracle8Platform; -import org.jumpmind.symmetric.ddl.platform.TestOracle9Platform; -import org.jumpmind.symmetric.ddl.platform.TestPlatformUtils; -import org.jumpmind.symmetric.ddl.platform.TestPostgresqlPlatform; -import org.jumpmind.symmetric.ddl.platform.TestSapDbPlatform; -import org.jumpmind.symmetric.ddl.platform.TestSqlBuilder; -import org.jumpmind.symmetric.ddl.platform.TestSybasePlatform; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Helper class to run all DdlUtils tests. - * - * @version $Revision: 289996 $ - */ -public class RunAllTests extends TestCase -{ - /** - * Creates a new instance. - * - * @param name The name of the test case - */ - public RunAllTests(String name) - { - super(name); - } - - /** - * Runs the test cases on the commandline using the text ui. - * - * @param args The invocation arguments - */ - public static void main(String[] args) - { - junit.textui.TestRunner.run(suite()); - } - - /** - * Returns a test suite containing all test cases. - * - * @return The test suite - */ - public static Test suite() - { - TestSuite suite = new TestSuite("DdlUtils tests"); - - // tests that don't need a live database - suite.addTestSuite(TestArrayAccessAtTable.class); - suite.addTestSuite(TestSqlBuilder.class); - suite.addTestSuite(TestPlatformUtils.class); - suite.addTestSuite(TestDatabaseIO.class); - suite.addTestSuite(TestDataReaderAndWriter.class); - suite.addTestSuite(TestDateConverter.class); - suite.addTestSuite(TestTimeConverter.class); - suite.addTestSuite(TestAxionPlatform.class); - suite.addTestSuite(TestCloudscapePlatform.class); - suite.addTestSuite(TestDB2Platform.class); - suite.addTestSuite(TestDerbyPlatform.class); - suite.addTestSuite(TestFirebirdPlatform.class); - suite.addTestSuite(TestHsqlDbPlatform.class); - suite.addTestSuite(TestInterbasePlatform.class); - suite.addTestSuite(TestMaxDbPlatform.class); - suite.addTestSuite(TestMcKoiPlatform.class); - suite.addTestSuite(TestMSSqlPlatform.class); - suite.addTestSuite(TestMySqlPlatform.class); - suite.addTestSuite(TestMySql50Platform.class); - suite.addTestSuite(TestOracle8Platform.class); - suite.addTestSuite(TestOracle9Platform.class); - suite.addTestSuite(TestPostgresqlPlatform.class); - suite.addTestSuite(TestSapDbPlatform.class); - suite.addTestSuite(TestSybasePlatform.class); - suite.addTestSuite(TestModelComparator.class); - suite.addTestSuite(TestAlterationAlgorithm.class); - - // tests that need a live database - if (System.getProperty(TestDatabaseWriterBase.JDBC_PROPERTIES_PROPERTY) != null) - { - suite.addTestSuite(TestDynaSqlQueries.class); - suite.addTestSuite(TestDatatypes.class); - suite.addTestSuite(TestConstraints.class); - suite.addTestSuite(TestAlteration.class); - suite.addTestSuite(TestMisc.class); - } - - return suite; - } -} diff --git a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/TestDatabaseWriterBase.java b/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/TestDatabaseWriterBase.java deleted file mode 100644 index 12fcb20f22..0000000000 --- a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/TestDatabaseWriterBase.java +++ /dev/null @@ -1,410 +0,0 @@ -package org.jumpmind.symmetric.ddl; - -/* - * 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.FileInputStream; -import java.io.InputStream; -import java.io.StringReader; -import java.util.Iterator; -import java.util.Map; -import java.util.Properties; - -import javax.sql.DataSource; - -import org.apache.commons.beanutils.BeanUtils; -import org.apache.commons.beanutils.DynaBean; -import org.apache.commons.beanutils.DynaProperty; -import org.apache.commons.dbcp.BasicDataSource; -import org.jumpmind.symmetric.ddl.DatabaseOperationException; -import org.jumpmind.symmetric.ddl.PlatformUtils; -import org.jumpmind.symmetric.ddl.io.DataReader; -import org.jumpmind.symmetric.ddl.io.DataToDatabaseSink; -import org.jumpmind.symmetric.ddl.model.Database; -import org.jumpmind.symmetric.ddl.platform.CreationParameters; - -/** - * Base class for database writer tests. - * - * @version $Revision: 289996 $ - */ -public abstract class TestDatabaseWriterBase extends TestPlatformBase -{ - /** The name of the property that specifies properties file with the settings for the connection to test against. */ - public static final String JDBC_PROPERTIES_PROPERTY = "jdbc.properties.file"; - /** The prefix for properties of the datasource. */ - public static final String DATASOURCE_PROPERTY_PREFIX = "datasource."; - /** The prefix for properties for ddlutils. */ - public static final String DDLUTILS_PROPERTY_PREFIX = "ddlutils."; - /** The property for specifying the platform. */ - public static final String DDLUTILS_PLATFORM_PROPERTY = DDLUTILS_PROPERTY_PREFIX + "platform"; - /** The property specifying the catalog for the tests. */ - public static final String DDLUTILS_CATALOG_PROPERTY = DDLUTILS_PROPERTY_PREFIX + "catalog"; - /** The property specifying the schema for the tests. */ - public static final String DDLUTILS_SCHEMA_PROPERTY = DDLUTILS_PROPERTY_PREFIX + "schema"; - /** The prefix for table creation properties. */ - public static final String DDLUTILS_TABLE_CREATION_PREFIX = DDLUTILS_PROPERTY_PREFIX + "tableCreation."; - - /** The test properties as defined by an external properties file. */ - private static Properties _testProps; - /** The data source to test against. */ - private static DataSource _dataSource; - /** The database name. */ - private static String _databaseName; - /** The database model. */ - private Database _model; - - /** - * Creates a new test case instance. - */ - public TestDatabaseWriterBase() - { - super(); - init(); - } - - /** - * Returns the test properties. - * - * @return The properties - */ - protected Properties getTestProperties() - { - if (_testProps == null) - { - String propFile = System.getProperty(JDBC_PROPERTIES_PROPERTY); - - if (propFile == null) - { - throw new RuntimeException("Please specify the properties file via the jdbc.properties.file environment variable"); - } - try - { - InputStream propStream = getClass().getResourceAsStream(propFile); - - if (propStream == null) - { - propStream = new FileInputStream(propFile); - } - Properties props = new Properties(); - - props.load(propStream); - _testProps = props; - } - catch (Exception ex) - { - throw new RuntimeException(ex); - } - } - return _testProps; - } - - /** - * Initializes the test datasource and the platform. - */ - private void init() - { - // the data source won't change during the tests, hence - // it is static and needs to be initialized only once - if (_dataSource != null) - { - return; - } - - Properties props = getTestProperties(); - - try - { - String dataSourceClass = props.getProperty(DATASOURCE_PROPERTY_PREFIX + "class", BasicDataSource.class.getName()); - - _dataSource = (DataSource)Class.forName(dataSourceClass).newInstance(); - - for (Iterator it = props.entrySet().iterator(); it.hasNext();) - { - Map.Entry entry = (Map.Entry)it.next(); - String propName = (String)entry.getKey(); - - if (propName.startsWith(DATASOURCE_PROPERTY_PREFIX) && !propName.equals(DATASOURCE_PROPERTY_PREFIX +"class")) - { - BeanUtils.setProperty(_dataSource, - propName.substring(DATASOURCE_PROPERTY_PREFIX.length()), - entry.getValue()); - } - } - } - catch (Exception ex) - { - throw new DatabaseOperationException(ex); - } - - _databaseName = props.getProperty(DDLUTILS_PLATFORM_PROPERTY); - if (_databaseName == null) - { - // property not set, then try to determine - _databaseName = new PlatformUtils().determineDatabaseType(_dataSource); - if (_databaseName == null) - { - throw new DatabaseOperationException("Could not determine platform from datasource, please specify it in the jdbc.properties via the ddlutils.platform property"); - } - } - } - - /** - * Returns the test table creation parameters for the given model. - * - * @param model The model - * @return The creation parameters - */ - protected CreationParameters getTableCreationParameters(Database model) - { - CreationParameters params = new CreationParameters(); - - for (Iterator entryIt = _testProps.entrySet().iterator(); entryIt.hasNext();) - { - Map.Entry entry = (Map.Entry)entryIt.next(); - String name = (String)entry.getKey(); - String value = (String)entry.getValue(); - - if (name.startsWith(DDLUTILS_TABLE_CREATION_PREFIX)) - { - name = name.substring(DDLUTILS_TABLE_CREATION_PREFIX.length()); - for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++) - { - params.addParameter(model.getTable(tableIdx), name, value); - } - } - } - return params; - } - - /** - * Returns the data source. - * - * @return The data source - */ - protected DataSource getDataSource() - { - return _dataSource; - } - - /** - * {@inheritDoc} - */ - protected String getDatabaseName() - { - return _databaseName; - } - - /** - * Returns the database model. - * - * @return The model - */ - protected Database getModel() - { - return _model; - } - - /** - * {@inheritDoc} - */ - protected void setUp() throws Exception - { - super.setUp(); - getPlatform().setDataSource(getDataSource()); - } - - /** - * {@inheritDoc} - */ - protected void tearDown() throws Exception - { - if (_model != null) - { - dropDatabase(); - _model = null; - } - super.tearDown(); - } - - /** - * Creates a new database from the given XML database schema. - * - * @param schemaXml The XML database schema - * @return The parsed database model - */ - protected Database createDatabase(String schemaXml) throws DatabaseOperationException - { - Database model = parseDatabaseFromString(schemaXml); - - createDatabase(model); - return model; - } - - /** - * Creates a new database from the given model. - * - * @param model The model - */ - protected void createDatabase(Database model) throws DatabaseOperationException - { - try - { - _model = model; - - getPlatform().setSqlCommentsOn(false); - getPlatform().createTables(_model, getTableCreationParameters(_model), false, false); - } - catch (Exception ex) - { - throw new DatabaseOperationException(ex); - } - } - - /** - * Alters the database to match the given model. - * - * @param schemaXml The model XML - * @return The model object - */ - protected Database alterDatabase(String schemaXml) throws DatabaseOperationException - { - Database model = parseDatabaseFromString(schemaXml); - - alterDatabase(model); - return model; - } - - /** - * Alters the database to match the given model. - * - * @param model The model - */ - protected void alterDatabase(Database model) throws DatabaseOperationException - { - Properties props = getTestProperties(); - String catalog = props.getProperty(DDLUTILS_CATALOG_PROPERTY); - String schema = props.getProperty(DDLUTILS_SCHEMA_PROPERTY); - - try - { - _model = model; - _model.resetDynaClassCache(); - - getPlatform().setSqlCommentsOn(false); - getPlatform().alterTables(catalog, schema, null, _model, getTableCreationParameters(_model), false); - } - catch (Exception ex) - { - throw new DatabaseOperationException(ex); - } - } - - /** - * Inserts data into the database. - * - * @param dataXml The data xml - * @return The database - */ - protected Database insertData(String dataXml) throws DatabaseOperationException - { - try - { - DataReader dataReader = new DataReader(); - - dataReader.setModel(_model); - dataReader.setSink(new DataToDatabaseSink(getPlatform(), _model)); - dataReader.getSink().start(); - dataReader.parse(new StringReader(dataXml)); - dataReader.getSink().end(); - return _model; - } - catch (Exception ex) - { - throw new DatabaseOperationException(ex); - } - } - - /** - * Drops the tables defined in the database model. - */ - protected void dropDatabase() throws DatabaseOperationException - { - getPlatform().dropTables(_model, true); - } - - /** - * Reads the database model from a live database. - * - * @param databaseName The name of the resulting database - * @return The model - */ - protected Database readModelFromDatabase(String databaseName) - { - Properties props = getTestProperties(); - String catalog = props.getProperty(DDLUTILS_CATALOG_PROPERTY); - String schema = props.getProperty(DDLUTILS_SCHEMA_PROPERTY); - - return getPlatform().readModelFromDatabase(databaseName, catalog, schema, null); - } - - /** - * Returns the SQL for altering the live database so that it matches the given model. - * - * @param desiredModel The desired model - * @return The alteration SQL - */ - protected String getAlterTablesSql(Database desiredModel) - { - Properties props = getTestProperties(); - String catalog = props.getProperty(DDLUTILS_CATALOG_PROPERTY); - String schema = props.getProperty(DDLUTILS_SCHEMA_PROPERTY); - - return getPlatform().getAlterTablesSql(catalog, schema, null, desiredModel); - } - - /** - * Determines the value of the bean's property that has the given name. Depending on the - * case-setting of the current builder, the case of teh name is considered or not. - * - * @param bean The bean - * @param propName The name of the property - * @return The value - */ - protected Object getPropertyValue(DynaBean bean, String propName) - { - if (getPlatform().isDelimitedIdentifierModeOn()) - { - return bean.get(propName); - } - else - { - DynaProperty[] props = bean.getDynaClass().getDynaProperties(); - - for (int idx = 0; idx < props.length; idx++) - { - if (propName.equalsIgnoreCase(props[idx].getName())) - { - return bean.get(props[idx].getName()); - } - } - throw new IllegalArgumentException("The bean has no property with the name "+propName); - } - } -} diff --git a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/TestSummaryCreatorTask.java b/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/TestSummaryCreatorTask.java deleted file mode 100644 index bd565029e8..0000000000 --- a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/TestSummaryCreatorTask.java +++ /dev/null @@ -1,447 +0,0 @@ -package org.jumpmind.symmetric.ddl; - -/* - * 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.FileInputStream; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Properties; - -import javax.sql.DataSource; - -import org.apache.commons.beanutils.BeanUtils; -import org.apache.commons.dbcp.BasicDataSource; -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.Project; -import org.apache.tools.ant.Task; -import org.apache.tools.ant.types.FileSet; -import org.dom4j.Attribute; -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.DocumentHelper; -import org.dom4j.Element; -import org.dom4j.Node; -import org.dom4j.io.OutputFormat; -import org.dom4j.io.SAXReader; -import org.dom4j.io.XMLWriter; -import org.jumpmind.symmetric.ddl.PlatformUtils; -import org.xml.sax.InputSource; - -/** - * Creates a test summary snippet that can be put onto the DdlUtils web site. - * - * @version $Revision: $ - */ -public class TestSummaryCreatorTask extends Task -{ - /** The DdlUtils version. */ - private String _version; - /** The file to write the snippet to. */ - private File _outputFile; - /** The input files. */ - private ArrayList _fileSets = new ArrayList(); - - /** - * Set the DdlUtils version used to run the tests. - * - * @param version The version - */ - public void setVersion(String version) - { - _version = version; - } - - /** - * Set the output file. - * - * @param outputFile The output file - */ - public void setOutputFile(File outputFile) - { - _outputFile = outputFile; - } - - /** - * Adds a fileset. - * - * @param fileset The additional input files - */ - public void addConfiguredFileset(FileSet fileset) - { - _fileSets.add(fileset); - } - - /** - * Returns the list of input files ({@link java.io.File} objects) to process. - * Note that this does not check that the file is a valid and useful XML file. - * - * @return The input files - */ - private List getInputFiles() - { - ArrayList result = new ArrayList(); - - for (Iterator it = _fileSets.iterator(); it.hasNext();) - { - FileSet fileSet = (FileSet)it.next(); - File fileSetDir = fileSet.getDir(getProject()); - DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject()); - String[] files = scanner.getIncludedFiles(); - - for (int idx = 0; (files != null) && (idx < files.length); idx++) - { - File file = new File(fileSetDir, files[idx]); - - if (file.isFile() && file.canRead()) - { - result.add(file); - } - } - } - return result; - } - - /** - * Processes all input files and gathers the relevant data. - * - * @return The Dom4j document object containing the summary - */ - private Document processInputFiles() throws IOException - { - Document summaryDoc = DocumentHelper.createDocument(); - - summaryDoc.addElement("summary"); - - for (Iterator it = getInputFiles().iterator(); it.hasNext();) - { - processInputFile(summaryDoc, (File)it.next()); - } - return summaryDoc; - } - - /** - * Returns the value of the specified attribute of the node determined by the given xpath. - * - * @param doc The XML document - * @param xpath The xpath selecting the node whose attribute we want - * @param attrName The name of the attribute - * @return The attribute value - */ - private String getAttrValue(Document doc, String xpath, String attrName) - { - Node node = doc.selectSingleNode(xpath); - - if (node instanceof Attribute) - { - // we ignore the attribute name then - return ((Attribute)node).getValue(); - } - else if (node != null) - { - return node.valueOf("@" + attrName); - } - else - { - return null; - } - } - - /** - * Processes the given input file. - * - * @param summaryDoc The document object to add data to - * @param inputFile The input file - */ - private void processInputFile(Document summaryDoc, File inputFile) throws IOException - { - try - { - // First we check whether it is an XML file - SAXReader reader = new SAXReader(); - Document testDoc = reader.read(new InputSource(new FileReader(inputFile))); - - // Then we check whether it is one that we're interested in - if (testDoc.selectSingleNode("/testsuite") == null) - { - return; - } - - // Ok, it is, so lets extract the data that we want: - Element generalElement = (Element)summaryDoc.selectSingleNode("//general"); - int totalNumTests = 0; - int totalNumErrors = 0; - int totalNumFailures = 0; - - if (generalElement == null) - { - generalElement = summaryDoc.getRootElement().addElement("general"); - - // General run info (we only need this once) - generalElement.addAttribute("ddlUtilsVersion", - _version); - generalElement.addAttribute("date", - getAttrValue(testDoc, "//property[@name='TODAY']", "value")); - generalElement.addAttribute("lang", - getAttrValue(testDoc, "//property[@name='env.LANG']", "value")); - generalElement.addAttribute("jre", - getAttrValue(testDoc, "//property[@name='java.runtime.name']", "value") + " " + - getAttrValue(testDoc, "//property[@name='java.runtime.version']", "value") + " (" + - getAttrValue(testDoc, "//property[@name='java.vendor']", "value") + ")"); - generalElement.addAttribute("os", - getAttrValue(testDoc, "//property[@name='os.name']", "value") + " " + - getAttrValue(testDoc, "//property[@name='os.version']", "value") + ", arch " + - getAttrValue(testDoc, "//property[@name='os.arch']", "value")); - addTargetDatabaseInfo(generalElement, - getAttrValue(testDoc, "//property[@name='jdbc.properties.file']", "value")); - } - else - { - totalNumTests = Integer.parseInt(generalElement.attributeValue("tests")); - totalNumErrors = Integer.parseInt(generalElement.attributeValue("errors")); - totalNumFailures = Integer.parseInt(generalElement.attributeValue("failures")); - } - - int numTests = Integer.parseInt(getAttrValue(testDoc, "/testsuite", "tests")); - int numErrors = Integer.parseInt(getAttrValue(testDoc, "/testsuite", "errors")); - int numFailures = Integer.parseInt(getAttrValue(testDoc, "/testsuite", "failures")); - - totalNumTests += numTests; - totalNumErrors += numErrors; - totalNumFailures += numFailures; - - generalElement.addAttribute("tests", String.valueOf(totalNumTests)); - generalElement.addAttribute("errors", String.valueOf(totalNumErrors)); - generalElement.addAttribute("failures", String.valueOf(totalNumErrors)); - - if ((numErrors > 0) || (numFailures > 0)) - { - Element testSuiteNode = (Element)testDoc.selectSingleNode("/testsuite"); - String testSuiteName = testSuiteNode.attributeValue("name"); - - // since tests have failed, we add it to the summary - for (Iterator it = testSuiteNode.selectNodes("testcase[failure or error]").iterator(); it.hasNext();) - { - Element failedTestElement = (Element)it.next(); - Element newTestElement = summaryDoc.getRootElement().addElement("failedTest"); - - // Test setup failure, so the test was not actually run ? - if (!failedTestElement.attributeValue("classname").startsWith("junit.framework.TestSuite")) - { - newTestElement.addAttribute("name", failedTestElement.attributeValue("classname") + "#" + failedTestElement.attributeValue("name")); - } - newTestElement.addAttribute("testsuite", testSuiteName); - } - } - } - catch (DocumentException ex) - { - // No, apparently it's not an XML document, so we ignore it - } - } - - /** - * Reads the database properties from the used properties file. - * - * @param element The element to add the relevant database properties to - * @param jdbcPropertiesFile The path of the properties file - */ - protected void addTargetDatabaseInfo(Element element, String jdbcPropertiesFile) throws IOException, BuildException - { - if (jdbcPropertiesFile == null) - { - return; - } - - Properties props = new Properties(); - Connection conn = null; - DatabaseMetaData metaData = null; - - try - { - props.load(getClass().getResourceAsStream(jdbcPropertiesFile)); - } - catch (Exception ex) - { - // not on the classpath ? let's try a file - File baseDir = getProject().getBaseDir(); - File propFile = new File(baseDir, jdbcPropertiesFile); - - if (propFile.exists() && propFile.isFile() && propFile.canRead()) - { - props.load(new FileInputStream(propFile)); - } - else - { - throw new BuildException("Cannot load database properties from file " + jdbcPropertiesFile); - } - } - - try - { - String dataSourceClass = props.getProperty(TestDatabaseWriterBase.DATASOURCE_PROPERTY_PREFIX + "class", BasicDataSource.class.getName()); - DataSource dataSource = (DataSource)Class.forName(dataSourceClass).newInstance(); - - for (Iterator it = props.entrySet().iterator(); it.hasNext();) - { - Map.Entry entry = (Map.Entry)it.next(); - String propName = (String)entry.getKey(); - - if (propName.startsWith(TestDatabaseWriterBase.DATASOURCE_PROPERTY_PREFIX) && !propName.equals(TestDatabaseWriterBase.DATASOURCE_PROPERTY_PREFIX +"class")) - { - BeanUtils.setProperty(dataSource, - propName.substring(TestDatabaseWriterBase.DATASOURCE_PROPERTY_PREFIX.length()), - entry.getValue()); - } - } - - String platformName = props.getProperty(TestDatabaseWriterBase.DDLUTILS_PLATFORM_PROPERTY); - - if (platformName == null) - { - platformName = new PlatformUtils().determineDatabaseType(dataSource); - if (platformName == null) - { - throw new BuildException("Could not determine platform from datasource, please specify it in the jdbc.properties via the ddlutils.platform property"); - } - } - - element.addAttribute("platform", platformName); - element.addAttribute("dataSourceClass", dataSourceClass); - - conn = dataSource.getConnection(); - metaData = conn.getMetaData(); - - try - { - element.addAttribute("dbProductName", metaData.getDatabaseProductName()); - } - catch (Throwable ex) - { - // we ignore it - } - try - { - element.addAttribute("dbProductVersion", metaData.getDatabaseProductVersion()); - } - catch (Throwable ex) - { - // we ignore it - } - try - { - int databaseMajorVersion = metaData.getDatabaseMajorVersion(); - int databaseMinorVersion = metaData.getDatabaseMinorVersion(); - - element.addAttribute("dbVersion", databaseMajorVersion + "." + databaseMinorVersion); - } - catch (Throwable ex) - { - // we ignore it - } - try - { - element.addAttribute("driverName", metaData.getDriverName()); - } - catch (Throwable ex) - { - // we ignore it - } - try - { - element.addAttribute("driverVersion", metaData.getDriverVersion()); - } - catch (Throwable ex) - { - // we ignore it - } - try - { - int jdbcMajorVersion = metaData.getJDBCMajorVersion(); - int jdbcMinorVersion = metaData.getJDBCMinorVersion(); - - element.addAttribute("jdbcVersion", jdbcMajorVersion + "." + jdbcMinorVersion); - } - catch (Throwable ex) - { - // we ignore it - } - } - catch (Exception ex) - { - throw new BuildException(ex); - } - finally - { - if (conn != null) - { - try - { - conn.close(); - } - catch (SQLException ex) - { - // we ignore it - } - } - } - } - - /** - * {@inheritDoc} - */ - public void execute() throws BuildException - { - try - { - log("Processing test results", Project.MSG_INFO); - - Document doc = processInputFiles(); - XMLWriter writer = null; - - if (_outputFile != null) - { - writer = new XMLWriter(new FileWriter(_outputFile), OutputFormat.createPrettyPrint()); - } - else - { - writer = new XMLWriter(System.out, OutputFormat.createPrettyPrint()); - } - - writer.write(doc); - writer.close(); - - log("Processing finished", Project.MSG_INFO); - } - catch (Exception ex) - { - throw new BuildException("Error while processing the test results: "+ex.getLocalizedMessage(), ex); - } - } -} diff --git a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/dynabean/TestDynaSqlQueries.java b/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/dynabean/TestDynaSqlQueries.java deleted file mode 100644 index cc4c60cd3a..0000000000 --- a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/dynabean/TestDynaSqlQueries.java +++ /dev/null @@ -1,315 +0,0 @@ -package org.jumpmind.symmetric.ddl.dynabean; - -/* - * 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.List; - -import org.apache.commons.beanutils.DynaBean; -import org.jumpmind.symmetric.ddl.TestDatabaseWriterBase; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaClass; -import org.jumpmind.symmetric.ddl.model.Table; -import org.jumpmind.symmetric.ddl.platform.ModelBasedResultSetIterator; -import org.jumpmind.symmetric.ddl.platform.sybase.SybasePlatform; - -/** - * Tests the sql querying. - * - * @version $Revision: 289996 $ - */ -public class TestDynaSqlQueries extends TestDatabaseWriterBase -{ - /** - * {@inheritDoc} - */ - protected void setUp() throws Exception - { - super.setUp(); - getPlatform().setDelimitedIdentifierModeOn(false); - } - - /** - * Tests a simple SELECT query. - */ - public void testSimpleQuery() throws Exception - { - createDatabase( - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"); - - insertData( - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " "+ - ""); - - ModelBasedResultSetIterator it = (ModelBasedResultSetIterator)getPlatform().query(getModel(), - "SELECT * FROM TestTable", - new Table[] { getModel().getTable(0) }); - - assertTrue(it.hasNext()); - // we call the method a second time to assert that the result set does not get advanced twice - assertTrue(it.hasNext()); - - DynaBean bean = (DynaBean)it.next(); - - assertEquals(new Integer(1), - getPropertyValue(bean, "TheId")); - assertEquals("Text 1", - getPropertyValue(bean, "TheText")); - - assertTrue(it.hasNext()); - - bean = (DynaBean)it.next(); - - assertEquals(new Integer(2), - getPropertyValue(bean, "TheId")); - assertEquals("Text 2", - getPropertyValue(bean, "TheText")); - - assertTrue(it.hasNext()); - - bean = (DynaBean)it.next(); - - assertEquals(new Integer(3), - getPropertyValue(bean, "TheId")); - assertEquals("Text 3", - getPropertyValue(bean, "TheText")); - - assertFalse(it.hasNext()); - assertFalse(it.isConnectionOpen()); - } - - /** - * Tests a simple SELECT fetch. - */ - public void testSimpleFetch() throws Exception - { - createDatabase( - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"); - - insertData( - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " "+ - ""); - - List beans = getPlatform().fetch(getModel(), - "SELECT * FROM TestTable", - new Table[] { getModel().getTable(0) }); - - assertEquals(3, - beans.size()); - - DynaBean bean = (DynaBean)beans.get(0); - - assertEquals(new Integer(1), - getPropertyValue(bean, "TheId")); - assertEquals("Text 1", - getPropertyValue(bean, "TheText")); - - bean = (DynaBean)beans.get(1); - - assertEquals(new Integer(2), - getPropertyValue(bean, "TheId")); - assertEquals("Text 2", - getPropertyValue(bean, "TheText")); - - bean = (DynaBean)beans.get(2); - - assertEquals(new Integer(3), - getPropertyValue(bean, "TheId")); - assertEquals("Text 3", - getPropertyValue(bean, "TheText")); - } - - /** - * Tests insertion & reading of auto-increment columns. - */ - public void testAutoIncrement() throws Exception - { - // we need special catering for Sybase which does not support identity for INTEGER columns - final String modelXml; - - if (SybasePlatform.DATABASENAME.equals(getPlatform().getName())) - { - modelXml = "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - else - { - modelXml = "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - - createDatabase(modelXml); - - // we're inserting the rows manually via beans since we do want to - // check the back-reading of the auto-increment columns - SqlDynaClass dynaClass = getModel().getDynaClassFor("TestTable"); - DynaBean bean = null; - Object id1 = null; - Object id2 = null; - Object id3 = null; - - bean = dynaClass.newInstance(); - bean.set("TheText", "Text 1"); - getPlatform().insert(getModel(), bean); - if (getPlatformInfo().isLastIdentityValueReadable()) - { - // we cannot know the value for sure (though it usually will be 1) - id1 = getPropertyValue(bean, "TheId"); - assertNotNull(id1); - } - bean = dynaClass.newInstance(); - bean.set("TheText", "Text 2"); - getPlatform().insert(getModel(), bean); - if (getPlatformInfo().isLastIdentityValueReadable()) - { - // we cannot know the value for sure (though it usually will be 2) - id2 = getPropertyValue(bean, "TheId"); - assertNotNull(id2); - } - bean = dynaClass.newInstance(); - bean.set("TheText", "Text 3"); - getPlatform().insert(getModel(), bean); - if (getPlatformInfo().isLastIdentityValueReadable()) - { - // we cannot know the value for sure (though it usually will be 3) - id3 = getPropertyValue(bean, "TheId"); - assertNotNull(id3); - } - - List beans = getPlatform().fetch(getModel(), - "SELECT * FROM TestTable", - new Table[] { getModel().getTable(0) }); - - assertEquals(3, - beans.size()); - - bean = (DynaBean)beans.get(0); - if (getPlatformInfo().isLastIdentityValueReadable()) - { - assertEquals(id1, - getPropertyValue(bean, "TheId")); - } - else - { - assertNotNull(getPropertyValue(bean, "TheId")); - } - assertEquals("Text 1", - getPropertyValue(bean, "TheText")); - - bean = (DynaBean)beans.get(1); - if (getPlatformInfo().isLastIdentityValueReadable()) - { - assertEquals(id2, - getPropertyValue(bean, "TheId")); - } - else - { - assertNotNull(getPropertyValue(bean, "TheId")); - } - assertEquals("Text 2", - getPropertyValue(bean, "TheText")); - - bean = (DynaBean)beans.get(2); - if (getPlatformInfo().isLastIdentityValueReadable()) - { - assertEquals(id3, - getPropertyValue(bean, "TheId")); - } - else - { - assertNotNull(getPropertyValue(bean, "TheId")); - } - assertEquals("Text 3", - getPropertyValue(bean, "TheText")); - } - - /** - * Tests a more complicated SELECT query that leads to a JOIN in the database. - */ - public void testJoinQuery() throws Exception - { - createDatabase( - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"); - - insertData( - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " "+ - ""); - - ModelBasedResultSetIterator it = (ModelBasedResultSetIterator)getPlatform().query(getModel(), - "SELECT Id1, Avalue FROM TestTable1, TestTable2 WHERE Id2 = Id", - new Table[] { getModel().getTable(0), getModel().getTable(1) }); - - assertTrue(it.hasNext()); - - DynaBean bean = (DynaBean)it.next(); - - assertEquals(new Integer(2), - getPropertyValue(bean, "Id1")); - assertEquals("Text 3", - getPropertyValue(bean, "Avalue")); - - assertFalse(it.hasNext()); - assertFalse(it.isConnectionOpen()); - } -} diff --git a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/dynabean/package.html b/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/dynabean/package.html deleted file mode 100644 index 4de9f94e66..0000000000 --- a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/dynabean/package.html +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - -

- This package contains the tests for the dynabean handling. -

- - diff --git a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/RoundtripTestBase.java b/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/RoundtripTestBase.java deleted file mode 100644 index 69eebd56c1..0000000000 --- a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/RoundtripTestBase.java +++ /dev/null @@ -1,642 +0,0 @@ -package org.jumpmind.symmetric.ddl.io; - -/* - * 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.StringWriter; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.sql.Types; -import java.util.List; - -import junit.framework.TestSuite; - -import org.apache.commons.beanutils.DynaBean; -import org.jumpmind.symmetric.ddl.DdlUtilsException; -import org.jumpmind.symmetric.ddl.PlatformFactory; -import org.jumpmind.symmetric.ddl.PlatformInfo; -import org.jumpmind.symmetric.ddl.TestDatabaseWriterBase; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaBean; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaClass; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaProperty; -import org.jumpmind.symmetric.ddl.io.BinaryObjectsHelper; -import org.jumpmind.symmetric.ddl.io.DatabaseIO; -import org.jumpmind.symmetric.ddl.model.Column; -import org.jumpmind.symmetric.ddl.model.Database; -import org.jumpmind.symmetric.ddl.model.ForeignKey; -import org.jumpmind.symmetric.ddl.model.Index; -import org.jumpmind.symmetric.ddl.model.IndexColumn; -import org.jumpmind.symmetric.ddl.model.Reference; -import org.jumpmind.symmetric.ddl.model.Table; -import org.jumpmind.symmetric.ddl.model.TypeMap; -import org.jumpmind.symmetric.ddl.platform.DefaultValueHelper; - -/** - * Base class for database roundtrip (creation & reconstruction from the database). - * - * @version $Revision: 289996 $ - */ -public abstract class RoundtripTestBase extends TestDatabaseWriterBase -{ - /** - * Creates the test suite for the given test class which must be a sub class of - * {@link RoundtripTestBase}. If the platform supports it, it will be tested - * with both delimited and undelimited identifiers. - * - * @param testedClass The tested class - * @return The tests - */ - protected static TestSuite getTests(Class testedClass) - { - if (!RoundtripTestBase.class.isAssignableFrom(testedClass) || - Modifier.isAbstract(testedClass.getModifiers())) - { - throw new DdlUtilsException("Cannot create parameterized tests for class "+testedClass.getName()); - } - - TestSuite suite = new TestSuite(); - - try - { - Method[] methods = testedClass.getMethods(); - PlatformInfo info = null; - RoundtripTestBase newTest; - - for (int idx = 0; (methods != null) && (idx < methods.length); idx++) - { - if (methods[idx].getName().startsWith("test") && - ((methods[idx].getParameterTypes() == null) || (methods[idx].getParameterTypes().length == 0))) - { - newTest = (RoundtripTestBase)testedClass.newInstance(); - newTest.setName(methods[idx].getName()); - newTest.setUseDelimitedIdentifiers(false); - suite.addTest(newTest); - - if (info == null) - { - info = PlatformFactory.createNewPlatformInstance(newTest.getDatabaseName()).getPlatformInfo(); - } - if (info.isDelimitedIdentifiersSupported()) - { - newTest = (RoundtripTestBase)testedClass.newInstance(); - newTest.setName(methods[idx].getName()); - newTest.setUseDelimitedIdentifiers(true); - suite.addTest(newTest); - } - } - } - } - catch (Exception ex) - { - throw new DdlUtilsException(ex); - } - - return suite; - } - - /** Whether to use delimited identifiers for the test. */ - private boolean _useDelimitedIdentifiers; - - /** - * Specifies whether the test shall use delimited identifiers. - * - * @param useDelimitedIdentifiers Whether to use delimited identifiers - */ - protected void setUseDelimitedIdentifiers(boolean useDelimitedIdentifiers) - { - _useDelimitedIdentifiers = useDelimitedIdentifiers; - } - - /** - * {@inheritDoc} - */ - protected void setUp() throws Exception - { - super.setUp(); - getPlatform().setDelimitedIdentifierModeOn(_useDelimitedIdentifiers); - } - - /** - * 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++) - { - Column column = table.getColumn(idx); - - bean.set(column.getName(), columnValues[idx]); - } - getPlatform().insert(getModel(), bean); - } - - /** - * Returns a "SELECT * FROM [table name]" statement. It also takes - * delimited identifier mode into account if enabled. - * - * @param table The table - * @param orderColumn The column to order the rows by (can be null) - * @return The statement - */ - protected String getSelectQueryForAllString(Table table, String orderColumn) - { - StringBuffer query = new StringBuffer(); - - query.append("SELECT * FROM "); - if (getPlatform().isDelimitedIdentifierModeOn()) - { - query.append(getPlatformInfo().getDelimiterToken()); - } - query.append(table.getName()); - if (getPlatform().isDelimitedIdentifierModeOn()) - { - query.append(getPlatformInfo().getDelimiterToken()); - } - if (orderColumn != null) - { - query.append(" ORDER BY "); - if (getPlatform().isDelimitedIdentifierModeOn()) - { - query.append(getPlatformInfo().getDelimiterToken()); - } - query.append(orderColumn); - if (getPlatform().isDelimitedIdentifierModeOn()) - { - query.append(getPlatformInfo().getDelimiterToken()); - } - } - return query.toString(); - } - - /** - * Retrieves all rows from the given table. - * - * @param tableName The table - * @return The rows - */ - protected List getRows(String tableName) - { - Table table = getModel().findTable(tableName, getPlatform().isDelimitedIdentifierModeOn()); - - return getPlatform().fetch(getModel(), - getSelectQueryForAllString(table, null), - new Table[] { table }); - } - - /** - * Retrieves all rows from the given table. - * - * @param tableName The table - * @param orderColumn The column to order the rows by - * @return The rows - */ - protected List getRows(String tableName, String orderColumn) - { - Table table = getModel().findTable(tableName, getPlatform().isDelimitedIdentifierModeOn()); - - return getPlatform().fetch(getModel(), - getSelectQueryForAllString(table, orderColumn), - new Table[] { table }); - } - - /** - * 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. - * - * @return The adjusted model - */ - protected Database getAdjustedModel() - { - try - { - Database model = (Database)getModel().clone(); - - for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++) - { - Table table = model.getTable(tableIdx); - - for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++) - { - Column column = table.getColumn(columnIdx); - int origType = column.getTypeCode(); - int targetType = getPlatformInfo().getTargetJdbcType(origType); - - // we adjust the column types if the native type would back-map to a - // different jdbc type - if (targetType != origType) - { - column.setTypeCode(targetType); - // we should also adapt the default value - if (column.getDefaultValue() != null) - { - DefaultValueHelper helper = getPlatform().getSqlBuilder().getDefaultValueHelper(); - - column.setDefaultValue(helper.convert(column.getDefaultValue(), origType, targetType)); - } - } - // we also promote the default size if the column has no size - // spec of its own - if ((column.getSize() == null) && getPlatformInfo().hasSize(targetType)) - { - Integer defaultSize = getPlatformInfo().getDefaultSize(targetType); - - if (defaultSize != null) - { - column.setSize(defaultSize.toString()); - } - } - // finally the platform might return a synthetic default value if the column - // is a primary key column - if (getPlatformInfo().isSyntheticDefaultValueForRequiredReturned() && - (column.getDefaultValue() == null) && column.isRequired() && !column.isAutoIncrement()) - { - switch (column.getTypeCode()) - { - case Types.TINYINT: - case Types.SMALLINT: - case Types.INTEGER: - case Types.BIGINT: - column.setDefaultValue("0"); - break; - case Types.REAL: - case Types.FLOAT: - case Types.DOUBLE: - column.setDefaultValue("0.0"); - break; - case Types.BIT: - column.setDefaultValue("false"); - break; - default: - column.setDefaultValue(""); - break; - } - } - } - // 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; - } - catch (CloneNotSupportedException ex) - { - throw new RuntimeException(ex); - } - } - - /** - * Compares the specified attribute value of the given bean with 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; - Object value = dynaBean.get(attrName); - - if ((value instanceof byte[]) && !(expected instanceof byte[]) && (dynaBean instanceof SqlDynaBean)) - { - SqlDynaClass dynaClass = (SqlDynaClass)((SqlDynaBean)dynaBean).getDynaClass(); - Column column = ((SqlDynaProperty)dynaClass.getDynaProperty(attrName)).getColumn(); - - if (TypeMap.isBinaryType(column.getTypeCode())) - { - value = new BinaryObjectsHelper().deserialize((byte[])value); - } - } - if (expected == null) - { - assertNull(value); - } - else - { - assertEquals(expected, value); - } - } - - /** - * Asserts that the two given database models are equal, and if not, writes both of them - * in XML form to stderr. - * - * @param expected The expected model - * @param actual The actual model - */ - protected void assertEquals(Database expected, Database actual) - { - try - { - assertEquals("Model names do not match.", - expected.getName(), - actual.getName()); - assertEquals("Not the same number of tables.", - expected.getTableCount(), - actual.getTableCount()); - for (int tableIdx = 0; tableIdx < actual.getTableCount(); tableIdx++) - { - assertEquals(expected.getTable(tableIdx), - actual.getTable(tableIdx)); - } - } - catch (Throwable ex) - { - StringWriter writer = new StringWriter(); - DatabaseIO dbIo = new DatabaseIO(); - - dbIo.write(expected, writer); - - getLog().error("Expected model:\n" + writer.toString()); - - writer = new StringWriter(); - dbIo.write(actual, writer); - - getLog().error("Actual model:\n" + writer.toString()); - - if (ex instanceof Error) - { - throw (Error)ex; - } - else - { - throw new DdlUtilsException(ex); - } - } - } - - /** - * Asserts that the two given database tables are equal. - * - * @param expected The expected table - * @param actual The actual table - */ - protected void assertEquals(Table expected, Table actual) - { - if (_useDelimitedIdentifiers) - { - assertEquals("Table names do not match.", - getPlatform().getSqlBuilder().shortenName(expected.getName(), getSqlBuilder().getMaxTableNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getName(), getSqlBuilder().getMaxTableNameLength())); - } - else - { - assertEquals("Table names do not match (ignoring case).", - getPlatform().getSqlBuilder().shortenName(expected.getName().toUpperCase(), getSqlBuilder().getMaxTableNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getName().toUpperCase(), getSqlBuilder().getMaxTableNameLength())); - } - assertEquals("Not the same number of columns in table "+actual.getName()+".", - expected.getColumnCount(), - actual.getColumnCount()); - for (int columnIdx = 0; columnIdx < actual.getColumnCount(); columnIdx++) - { - assertEquals(expected.getColumn(columnIdx), - actual.getColumn(columnIdx)); - } - assertEquals("Not the same number of foreign keys in table "+actual.getName()+".", - expected.getForeignKeyCount(), - actual.getForeignKeyCount()); - // order is not assumed with the way foreignkeys are returned. - for (int expectedFkIdx = 0; expectedFkIdx < expected.getForeignKeyCount(); expectedFkIdx++) - { - ForeignKey expectedFk = expected.getForeignKey(expectedFkIdx); - String expectedName = getPlatform().getSqlBuilder().shortenName(expectedFk.getName(), getSqlBuilder().getMaxForeignKeyNameLength()); - - for (int actualFkIdx = 0; actualFkIdx < actual.getForeignKeyCount(); actualFkIdx++) - { - ForeignKey actualFk = actual.getForeignKey(actualFkIdx); - String actualName = getPlatform().getSqlBuilder().shortenName(actualFk.getName(), getSqlBuilder().getMaxForeignKeyNameLength()); - - if ((_useDelimitedIdentifiers && expectedName.equals(actualName)) || - (!_useDelimitedIdentifiers && expectedName.equalsIgnoreCase(actualName))) - { - assertEquals(expectedFk, actualFk); - } - } - } - assertEquals("Not the same number of indices in table "+actual.getName()+".", - expected.getIndexCount(), - actual.getIndexCount()); - for (int indexIdx = 0; indexIdx < actual.getIndexCount(); indexIdx++) - { - assertEquals(expected.getIndex(indexIdx), - actual.getIndex(indexIdx)); - } - } - - /** - * Asserts that the two given columns are equal. - * - * @param expected The expected column - * @param actual The actual column - */ - protected void assertEquals(Column expected, Column actual) - { - if (_useDelimitedIdentifiers) - { - assertEquals("Column names do not match.", - getPlatform().getSqlBuilder().shortenName(expected.getName(), getSqlBuilder().getMaxColumnNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getName(), getSqlBuilder().getMaxColumnNameLength())); - } - else - { - assertEquals("Column names do not match (ignoring case).", - getPlatform().getSqlBuilder().shortenName(expected.getName().toUpperCase(), getSqlBuilder().getMaxColumnNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getName().toUpperCase(), getSqlBuilder().getMaxColumnNameLength())); - } - assertEquals("Primary key status not the same for column "+actual.getName()+".", - expected.isPrimaryKey(), - actual.isPrimaryKey()); - assertEquals("Required status not the same for column "+actual.getName()+".", - expected.isRequired(), - actual.isRequired()); - if (getPlatformInfo().getIdentityStatusReadingSupported()) - { - // we're only comparing this if the platform can actually read the - // auto-increment status back from an existing database - assertEquals("Auto-increment status not the same for column "+actual.getName()+".", - expected.isAutoIncrement(), - actual.isAutoIncrement()); - } - assertEquals("Type not the same for column "+actual.getName()+".", - expected.getType(), - actual.getType()); - assertEquals("Type code not the same for column "+actual.getName()+".", - expected.getTypeCode(), - actual.getTypeCode()); - assertEquals("Parsed default values do not match for column "+actual.getName()+".", - expected.getParsedDefaultValue(), - actual.getParsedDefaultValue()); - - // comparing the size makes only sense for types where it is relevant - if ((expected.getTypeCode() == Types.NUMERIC) || - (expected.getTypeCode() == Types.DECIMAL)) - { - assertEquals("Precision not the same for column "+actual.getName()+".", - expected.getSizeAsInt(), - actual.getSizeAsInt()); - assertEquals("Scale not the same for column "+actual.getName()+".", - expected.getScale(), - actual.getScale()); - } - else if ((expected.getTypeCode() == Types.CHAR) || - (expected.getTypeCode() == Types.VARCHAR) || - (expected.getTypeCode() == Types.BINARY) || - (expected.getTypeCode() == Types.VARBINARY)) - { - assertEquals("Size not the same for column "+actual.getName()+".", - expected.getSize(), - actual.getSize()); - } - } - - /** - * Asserts that the two given foreign keys are equal. - * - * @param expected The expected foreign key - * @param actual The actual foreign key - */ - protected void assertEquals(ForeignKey expected, ForeignKey actual) - { - if (_useDelimitedIdentifiers) - { - assertEquals("Foreign key names do not match.", - getPlatform().getSqlBuilder().shortenName(expected.getName(), getSqlBuilder().getMaxForeignKeyNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getName(), getSqlBuilder().getMaxForeignKeyNameLength())); - assertEquals("Referenced table names do not match.", - getPlatform().getSqlBuilder().shortenName(expected.getForeignTableName(), getSqlBuilder().getMaxTableNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getForeignTableName(), getSqlBuilder().getMaxTableNameLength())); - } - else - { - assertEquals("Foreign key names do not match (ignoring case).", - getPlatform().getSqlBuilder().shortenName(expected.getName().toUpperCase(), getSqlBuilder().getMaxForeignKeyNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getName().toUpperCase(), getSqlBuilder().getMaxForeignKeyNameLength())); - assertEquals("Referenced table names do not match (ignoring case).", - getPlatform().getSqlBuilder().shortenName(expected.getForeignTableName().toUpperCase(), getSqlBuilder().getMaxTableNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getForeignTableName().toUpperCase(), getSqlBuilder().getMaxTableNameLength())); - } - assertEquals("Not the same number of references in foreign key "+actual.getName()+".", - expected.getReferenceCount(), - actual.getReferenceCount()); - for (int refIdx = 0; refIdx < actual.getReferenceCount(); refIdx++) - { - assertEquals(expected.getReference(refIdx), - actual.getReference(refIdx)); - } - } - - /** - * Asserts that the two given references are equal. - * - * @param expected The expected reference - * @param actual The actual reference - */ - protected void assertEquals(Reference expected, Reference actual) - { - if (_useDelimitedIdentifiers) - { - assertEquals("Local column names do not match.", - getPlatform().getSqlBuilder().shortenName(expected.getLocalColumnName(), getSqlBuilder().getMaxColumnNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getLocalColumnName(), getSqlBuilder().getMaxColumnNameLength())); - assertEquals("Foreign column names do not match.", - getPlatform().getSqlBuilder().shortenName(expected.getForeignColumnName(), getSqlBuilder().getMaxColumnNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getForeignColumnName(), getSqlBuilder().getMaxColumnNameLength())); - } - else - { - assertEquals("Local column names do not match (ignoring case).", - getPlatform().getSqlBuilder().shortenName(expected.getLocalColumnName().toUpperCase(), getSqlBuilder().getMaxColumnNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getLocalColumnName().toUpperCase(), getSqlBuilder().getMaxColumnNameLength())); - assertEquals("Foreign column names do not match (ignoring case).", - getPlatform().getSqlBuilder().shortenName(expected.getForeignColumnName().toUpperCase(), getSqlBuilder().getMaxColumnNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getForeignColumnName().toUpperCase(), getSqlBuilder().getMaxColumnNameLength())); - } - } - - /** - * Asserts that the two given indices are equal. - * - * @param expected The expected index - * @param actual The actual index - */ - protected void assertEquals(Index expected, Index actual) - { - if (_useDelimitedIdentifiers) - { - assertEquals("Index names do not match.", - getPlatform().getSqlBuilder().shortenName(expected.getName(), getSqlBuilder().getMaxConstraintNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getName(), getSqlBuilder().getMaxConstraintNameLength())); - } - else - { - assertEquals("Index names do not match (ignoring case).", - getPlatform().getSqlBuilder().shortenName(expected.getName().toUpperCase(), getSqlBuilder().getMaxConstraintNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getName().toUpperCase(), getSqlBuilder().getMaxConstraintNameLength())); - } - assertEquals("Unique status not the same for index "+actual.getName()+".", - expected.isUnique(), - actual.isUnique()); - assertEquals("Not the same number of columns in index "+actual.getName()+".", - expected.getColumnCount(), - actual.getColumnCount()); - for (int columnIdx = 0; columnIdx < actual.getColumnCount(); columnIdx++) - { - assertEquals(expected.getColumn(columnIdx), - actual.getColumn(columnIdx)); - } - } - - /** - * Asserts that the two given index columns are equal. - * - * @param expected The expected index column - * @param actual The actual index column - */ - protected void assertEquals(IndexColumn expected, IndexColumn actual) - { - if (_useDelimitedIdentifiers) - { - assertEquals("Index column names do not match.", - getPlatform().getSqlBuilder().shortenName(expected.getName(), getSqlBuilder().getMaxColumnNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getName(), getSqlBuilder().getMaxColumnNameLength())); - } - else - { - assertEquals("Index column names do not match (ignoring case).", - getPlatform().getSqlBuilder().shortenName(expected.getName().toUpperCase(), getSqlBuilder().getMaxColumnNameLength()), - getPlatform().getSqlBuilder().shortenName(actual.getName().toUpperCase(), getSqlBuilder().getMaxColumnNameLength())); - } - assertEquals("Size not the same for index column "+actual.getName()+".", - expected.getSize(), - actual.getSize()); - } -} diff --git a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestAlteration.java b/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestAlteration.java deleted file mode 100644 index 51f7473b82..0000000000 --- a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestAlteration.java +++ /dev/null @@ -1,2199 +0,0 @@ -package org.jumpmind.symmetric.ddl.io; - -/* - * 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.math.BigDecimal; -import java.util.List; -import java.util.Properties; - -import junit.framework.Test; - -import org.apache.commons.beanutils.DynaBean; -import org.jumpmind.symmetric.ddl.model.Database; -import org.jumpmind.symmetric.ddl.platform.sybase.SybasePlatform; - -/** - * Performs tests for the alteration of databases. - * - * @version $Revision: $ - */ -public class TestAlteration extends RoundtripTestBase -{ - /** - * Parameterized test case pattern. - * - * @return The tests - */ - public static Test suite() throws Exception - { - return getTests(TestAlteration.class); - } - - /** - * Tests the alteration of a column datatype. - */ - public void testChangeDatatype1() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), new Integer(2) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Double(2.0), beans.get(0), "avalue"); - } - - /** - * Tests the alteration of a column datatype. - */ - public void testChangeDatatype2() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), new Short((short)2) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - DynaBean bean = (DynaBean)beans.get(0); - - // Some databases (e.g. DB2) pad the string for some reason, so we manually trim it - if (bean.get("avalue") instanceof String) - { - bean.set("avalue", ((String)bean.get("avalue")).trim()); - } - assertEquals((Object)"2", beans.get(0), "avalue"); - } - - /** - * Tests the alteration of the datatypes of PK and FK columns. - */ - public void testChangePKAndFKDatatypes() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip1", new Object[] { new Integer(1) }); - insertRow("roundtrip2", new Object[] { new Integer(1), new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip2"); - DynaBean bean = (DynaBean)beans.get(0); - - // Some databases (e.g. DB2) pad the string for some reason, so we manually trim it - if (bean.get("fk") instanceof String) - { - bean.set("fk", ((String)bean.get("fk")).trim()); - } - assertEquals((Object)"1", bean, "fk"); - } - - /** - * Tests the alteration of the datatypes of columns of a PK and FK that - * will be dropped. - */ - public void testChangeDroppedPKAndFKDatatypes() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip1", new Object[] { new Integer(1) }); - insertRow("roundtrip2", new Object[] { new Integer(1), new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip2"); - DynaBean bean = (DynaBean)beans.get(0); - - // Some databases (e.g. DB2) pad the string for some reason, so we manually trim it - if (bean.get("fk") instanceof String) - { - bean.set("fk", ((String)bean.get("fk")).trim()); - } - assertEquals((Object)"1", bean, "fk"); - } - - /** - * Tests the alteration of the datatypes of a column that is indexed. - */ - public void testChangeIndexColumnDatatype() - { - if (!getPlatformInfo().isIndicesSupported()) - { - return; - } - - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), new Integer(1) }); - insertRow("roundtrip", new Object[] { new Integer(2), new Integer(10) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Integer(1), beans.get(0), "avalue"); - assertEquals(new Integer(10), beans.get(1), "avalue"); - } - - /** - * Tests the alteration of the datatypes of an indexed column where - * the index will be dropped. - */ - public void testChangeDroppedIndexColumnDatatype() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), new Integer(1) }); - insertRow("roundtrip", new Object[] { new Integer(2), new Integer(10) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Integer(1), beans.get(0), "avalue"); - assertEquals(new Integer(10), beans.get(1), "avalue"); - } - - /** - * Tests the alteration of a column size. - */ - public void testChangeSize() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), "test" }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals((Object)"test", beans.get(0), "avalue"); - } - - /** - * Tests the alteration of a column's datatype and size. - */ - public void testChangeDatatypeAndSize() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), "test" }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals((Object)"test", beans.get(0), "avalue"); - } - - /** - * Tests the alteration of a column null constraint. - */ - public void testChangeNull() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), new Integer(2) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Integer(2), beans.get(0), "avalue"); - } - - /** - * Tests the addition of a column's default value. - */ - public void testAddDefault() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), new Double(2.0) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Double(2.0), beans.get(0), "avalue"); - } - - /** - * Tests the change of a column default value. - */ - public void testChangeDefault() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), new Integer(2) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Integer(2), beans.get(0), "avalue"); - } - - /** - * Tests the removal of a column default value. - */ - public void testDropDefault() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals((Object)"test", beans.get(0), "avalue"); - } - - /** - * Tests the change of a column's auto-increment state. - */ - public void testMakeAutoIncrement() - { - if (!getPlatformInfo().isNonPKIdentityColumnsSupported()) - { - return; - } - // Sybase does not like INTEGER auto-increment columns - if (SybasePlatform.DATABASENAME.equals(getPlatform().getName())) - { - String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), new BigDecimal(2) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new BigDecimal(2), beans.get(0), "avalue"); - } - else - { - String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - String model2Xml= - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), new Integer(2) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Integer(2), beans.get(0), "avalue"); - } - } - - /** - * Tests the removal the column auto-increment status. - */ - public void testDropAutoIncrement() - { - if (!getPlatformInfo().isNonPKIdentityColumnsSupported()) - { - return; - } - - boolean isSybase = SybasePlatform.DATABASENAME.equals(getPlatform().getName()); - final String model1Xml; - final String model2Xml; - - if (isSybase) - { - model1Xml = "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - model2Xml = "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - else - { - model1Xml = "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - model2Xml = "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - if (isSybase) - { - assertEquals(new BigDecimal(1), beans.get(0), "avalue"); - } - else - { - assertEquals(new Integer(1), beans.get(0), "avalue"); - } - } - - /** - * Tests the addition of a column. - */ - public void testAddColumn() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals((Object)null, beans.get(0), "avalue"); - } - - /** - * Tests the addition of an auto-increment column. - */ - public void testAddAutoIncrementColumn() - { - if (!getPlatformInfo().isNonPKIdentityColumnsSupported()) - { - return; - } - - // we need special catering for Sybase which does not support identity for INTEGER columns - boolean isSybase = SybasePlatform.DATABASENAME.equals(getPlatform().getName()); - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml; - - if (isSybase) - { - model2Xml = "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - else - { - model2Xml = "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - if (isSybase) - { - assertEquals(new BigDecimal(1), beans.get(0), "avalue"); - } - else - { - Object avalue = ((DynaBean)beans.get(0)).get("avalue"); - - assertTrue((avalue == null) || new Integer(1).equals(avalue)); - } - } - - /** - * Tests the addition of several columns. - */ - public void testAddColumns() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), new Double(3.0) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals((Object)null, beans.get(0), "avalue1"); - assertEquals((Object)null, beans.get(0), "avalue2"); - assertEquals(new Double(3.0), beans.get(0), "avalue3"); - assertEquals((Object)null, beans.get(0), "avalue4"); - } - - /** - * Tests the addition of several columns at the end of the table. - */ - public void testAddColumnsAtTheEnd() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), "test", new Integer(3) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals((Object)"test", beans.get(0), "avalue1"); - assertEquals(new Integer(3), beans.get(0), "avalue2"); - - // we cannot be sure whether the default algorithm is used (which will apply the - // default value even to existing columns with NULL in it) or the database supports - // it dircetly (in which case it might still be NULL) - Object avalue3 = ((DynaBean)beans.get(0)).get("avalue3"); - - assertTrue((avalue3 == null) || new Double(1.0).equals(avalue3)); - - assertEquals((Object)null, beans.get(0), "avalue4"); - } - - /** - * Tests the addition of a column with a default value. Note that depending - * on whether the database supports this via a statement, this test may fail. - * For instance, Sql Server has a statement for this which means that the - * existing value in column avalue won't be changed and thus the test fails. - */ - public void testAddColumnWithDefault() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - // we cannot be sure whether the default algorithm is used (which will apply the - // default value even to existing columns with NULL in it) or the database supports - // it dircetly (in which case it might still be NULL) - Object avalue = ((DynaBean)beans.get(0)).get("avalue"); - - assertTrue((avalue == null) || new Integer(2).equals(avalue)); - } - - /** - * Tests the addition of a column that is set to NOT NULL. - */ - public void testAddRequiredColumn() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Integer(2), beans.get(0), "avalue"); - } - - /** - * Tests the change of the order of the columns of a table. - */ - public void testChangeColumnOrder() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), "test", "value", null, null }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals((Object)"test", beans.get(0), "avalue1"); - assertEquals((Object)null, beans.get(0), "avalue2"); - assertEquals(new Double(1.0), beans.get(0), "avalue3"); - assertEquals((Object)"value", beans.get(0), "avalue4"); - } - - /** - * Tests the removal of a column. - */ - public void testDropColumn() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), "test" }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Integer(1), beans.get(0), "pk"); - } - - /** - * Tests the removal of an auto-increment column. - */ - public void testDropAutoIncrementColumn() - { - if (!getPlatformInfo().isNonPKIdentityColumnsSupported()) - { - return; - } - - boolean isSybase = SybasePlatform.DATABASENAME.equals(getPlatform().getName()); - final String model1Xml; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - if (isSybase) - { - model1Xml = "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - else - { - model1Xml = "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Integer(1), beans.get(0), "pk"); - } - - /** - * Tests the addition of a column to the pk. - */ - public void testAddColumnToPK() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), "test" }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals((Object)"test", beans.get(0), "avalue"); - } - - /** - * Tests the removal of a column from the pk. - */ - public void testRemoveColumnFromPK() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), "test" }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals((Object)"test", beans.get(0), "avalue"); - } - - /** - * Tests the addition of a pk column. - */ - public void testAddPKColumn() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Integer(0), beans.get(0), "avalue"); - } - - /** - * Tests the addition of a primary key and a column. - */ - public void testAddPKAndColumn() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - // we cannot be sure whether the default algorithm is used (which will apply the - // default value even to existing columns with NULL in it) or the database supports - // it dircetly (in which case it might still be NULL) - Object avalue = ((DynaBean)beans.get(0)).get("avalue"); - - assertTrue((avalue == null) || new Integer(0).equals(avalue)); - } - - /** - * Tests the addition of a primary key and a primary key column. - */ - public void testAddPKAndPKColumn() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Integer(0), beans.get(0), "avalue"); - } - - /** - * Tests the removal of a pk column. - */ - public void testDropPKColumn() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), "test" }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Integer(1), beans.get(0), "pk"); - } - - /** - * Tests the addition of an index. - */ - public void testAddIndex() - { - if (!getPlatformInfo().isIndicesSupported()) - { - return; - } - - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), null, new Integer(2) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals((Object)null, beans.get(0), "avalue1"); - assertEquals(new Integer(2), beans.get(0), "avalue2"); - } - - /** - * Tests the addition of an unique index. - */ - public void testAddUniqueIndex() - { - if (!getPlatformInfo().isIndicesSupported()) - { - return; - } - - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), new Integer(2) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Integer(2), beans.get(0), "avalue"); - } - - /** - * Tests the removal of an unique index. - */ - public void testDropUniqueIndex() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), new Double(2.0), "test" }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Double(2.0), beans.get(0), "avalue1"); - assertEquals((Object)"test", beans.get(0), "avalue2"); - } - - /** - * Tests the addition of a column to an index. - */ - public void testAddColumnToIndex() - { - if (!getPlatformInfo().isIndicesSupported()) - { - return; - } - - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), new Double(2.0), "test" }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Double(2.0), beans.get(0), "avalue1"); - assertEquals((Object)"test", beans.get(0), "avalue2"); - } - - /** - * Tests the removal of a column from an index. - */ - public void testRemoveColumnFromUniqueIndex() - { - if (!getPlatformInfo().isIndicesSupported()) - { - return; - } - - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), new Double(2.0), new Integer(3) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip"); - - assertEquals(new Double(2.0), beans.get(0), "avalue1"); - assertEquals(new Integer(3), beans.get(0), "avalue2"); - } - - /** - * Tests the addition of a foreign key. - */ - public void testAddFK() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip1", new Object[] { new Integer(1) }); - insertRow("roundtrip2", new Object[] { "2", new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans1 = getRows("roundtrip1"); - List beans2 = getRows("roundtrip2"); - - assertEquals(new Integer(1), beans1.get(0), "pk"); - assertEquals((Object)"2", beans2.get(0), "pk"); - assertEquals(new Integer(1), beans2.get(0), "avalue"); - } - - /** - * Tests the removal of a foreign key. - */ - public void testDropFK() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip1", new Object[] { new Integer(1), new Double(2.0) }); - insertRow("roundtrip2", new Object[] { new Integer(2), new Double(2.0), new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans1 = getRows("roundtrip1"); - List beans2 = getRows("roundtrip2"); - - assertEquals(new Integer(1), beans1.get(0), "pk1"); - assertEquals(new Double(2.0), beans1.get(0), "pk2"); - assertEquals(new Integer(2), beans2.get(0), "pk"); - assertEquals(new Double(2.0), beans2.get(0), "avalue1"); - assertEquals(new Integer(1), beans2.get(0), "avalue2"); - } - - /** - * Tests the removal of several foreign keys. Test for DDLUTILS-150. - */ - public void testDropFKs() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip1", new Object[] { new Integer(1) }); - insertRow("roundtrip2", new Object[] { new Integer(2) }); - insertRow("roundtrip2", new Object[] { new Integer(3) }); - insertRow("roundtrip3", new Object[] { new Integer(1), new Integer(2) }); - insertRow("roundtrip4", new Object[] { new Integer(1), new Integer(3), new Integer(2) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans1 = getRows("roundtrip1"); - List beans2 = getRows("roundtrip2"); - List beans3 = getRows("roundtrip3"); - List beans4 = getRows("roundtrip4"); - - assertEquals(new Integer(1), beans1.get(0), "pk"); - assertEquals(new Integer(2), beans2.get(0), "pk"); - assertEquals(new Integer(3), beans2.get(1), "pk"); - assertEquals(new Integer(1), beans3.get(0), "pk1"); - assertEquals(new Integer(2), beans3.get(0), "pk2"); - assertEquals(new Integer(1), beans4.get(0), "pk"); - assertEquals(new Integer(3), beans4.get(0), "fk1"); - assertEquals(new Integer(2), beans4.get(0), "fk2"); - } - - /** - * Tests the addition of a reference to a foreign key. - */ - public void testAddReferenceToFK() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip1", new Object[] { new Integer(1) }); - insertRow("roundtrip2", new Object[] { new Integer(2), new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans1 = getRows("roundtrip1"); - List beans2 = getRows("roundtrip2"); - - assertEquals(new Integer(1), beans1.get(0), "pk1"); - assertEquals(new Double(0.0), beans1.get(0), "pk2"); - assertEquals(new Integer(2), beans2.get(0), "pk"); - assertEquals(new Integer(1), beans2.get(0), "avalue1"); - assertEquals(new Double(0.0), beans2.get(0), "avalue2"); - } - - /** - * Tests the removal of a reference from a foreign key. - */ - public void testRemoveReferenceFromFK() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip1", new Object[] { new Integer(1), "test" }); - insertRow("roundtrip2", new Object[] { new Integer(2), "test", new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans1 = getRows("roundtrip1"); - List beans2 = getRows("roundtrip2"); - - assertEquals(new Integer(1), beans1.get(0), "pk1"); - assertEquals(new Integer(2), beans2.get(0), "pk"); - assertEquals(new Integer(1), beans2.get(0), "avalue2"); - } - - /** - * Tests the addition of a table. - */ - public void testAddTable1() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip1", new Object[] { new Integer(1) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip1"); - - assertEquals(new Integer(1), beans.get(0), "pk"); - } - - /** - * Tests the addition of a table. - */ - public void testAddTable2() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model3Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip1", new Object[] { new Integer(1), "test" }); - - alterDatabase(model2Xml); - - // note that we have to split the alteration because we can only add the foreign key if - // there is a corresponding row in the new table - - insertRow("roundtrip2", new Object[] { "test" }); - - alterDatabase(model3Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans1 = getRows("roundtrip1"); - List beans2 = getRows("roundtrip2"); - - assertEquals(new Integer(1), beans1.get(0), "pk"); - assertEquals((Object)"test", beans1.get(0), "avalue"); - assertEquals((Object)"test", beans2.get(0), "pk"); - } - - /** - * Tests the addition of a table with an auto-increment primary key. - */ - public void testAddAutoIncrementTable() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml; - - // Sybase does not like INTEGER auto-increment columns - if (SybasePlatform.DATABASENAME.equals(getPlatform().getName())) - { - model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - else - { - model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - createDatabase(model1Xml); - - insertRow("roundtrip1", new Object[] { "1" }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip1"); - - assertEquals((Object)"1", beans.get(0), "pk"); - } - - /** - * Tests the removal of a table. - */ - public void testRemoveTable1() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip1", new Object[] { new Integer(1) }); - insertRow("roundtrip2", new Object[] { new Integer(2), new Double(2.0) }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip1"); - - assertEquals(new Integer(1), beans.get(0), "pk"); - } - - /** - * Tests the removal of a table. - */ - public void testRemoveTable2() - { - final String model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String model2Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip1", new Object[] { "test" }); - insertRow("roundtrip2", new Object[] { new Integer(1), "test" }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - List beans = getRows("roundtrip2"); - - assertEquals(new Integer(1), beans.get(0), "pk"); - assertEquals((Object)"test", beans.get(0), "avalue"); - } - - /** - * Tests the removal of a table with an auto-increment column. - */ - public void testRemoveTable3() - { - final String model1Xml; - final String model2Xml = - "\n"+ - "\n"+ - ""; - - // Sybase does not like INTEGER auto-increment columns - if (SybasePlatform.DATABASENAME.equals(getPlatform().getName())) - { - model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - else - { - model1Xml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { null, "1" }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - } - - /** - * Test for DDLUTILS-54. - */ - public void testIssue54() throws Exception - { - final String modelXml = - "\n" + - "\n" + - " \n" + - " \n" + - " \n" + - "
\n" + - "
"; - - createDatabase(modelXml); - - Properties props = getTestProperties(); - String catalog = props.getProperty(DDLUTILS_CATALOG_PROPERTY); - String schema = props.getProperty(DDLUTILS_SCHEMA_PROPERTY); - Database model = parseDatabaseFromString(modelXml); - - getPlatform().setSqlCommentsOn(false); - - String alterationSql = getPlatform().getAlterTablesSql(catalog, schema, null, model); - - assertEqualsIgnoringWhitespaces("", alterationSql); - } - - /** - * Test for DDLUTILS-159. - */ - public void testRenamePK() throws Exception - { - final String model1Xml = - "\n" + - "\n" + - " \n" + - " \n" + - " \n" + - "
\n" + - "
"; - final String model2Xml = - "\n" + - "\n" + - " \n" + - " \n" + - " \n" + - "
\n" + - "
"; - - createDatabase(model1Xml); - - insertRow("roundtrip", new Object[] { new Integer(1), "test" }); - - alterDatabase(model2Xml); - - assertEquals(getAdjustedModel(), - readModelFromDatabase("roundtriptest")); - - assertTrue(getRows("roundtrip").isEmpty()); - } -} diff --git a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestConstraints.java b/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestConstraints.java deleted file mode 100644 index 7274c6dc5e..0000000000 --- a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestConstraints.java +++ /dev/null @@ -1,442 +0,0 @@ -package org.jumpmind.symmetric.ddl.io; - -/* - * 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 org.apache.commons.lang.StringUtils; -import org.jumpmind.symmetric.ddl.model.Database; -import org.jumpmind.symmetric.ddl.platform.sybase.SybasePlatform; - -import junit.framework.Test; - -/** - * Performs the constraint tests. - * - * @version $Revision: 289996 $ - */ -public class TestConstraints extends RoundtripTestBase -{ - /** - * Parameterized test case pattern. - * - * @return The tests - */ - public static Test suite() throws Exception - { - return getTests(TestConstraints.class); - } - - /** - * Tests a nullable column. Basically we're creating the test database - * and then read it back and compare the original with the read one. - * In addition we can also check that DdlUtils does not try to alter the new - * database when using the alterTables/getAlterTablesSql - * methods of the {@link org.jumpmind.symmetric.ddl.Platform} with the read-back model. - * - * @param modelXml The model to be tested in XML form - * @param checkAlteration Whether to also check the alter tables sql - */ - protected void performConstraintsTest(String modelXml, boolean checkAlteration) - { - createDatabase(modelXml); - - Database modelFromDb = readModelFromDatabase("roundtriptest"); - - assertEquals(getAdjustedModel(), - modelFromDb); - - if (checkAlteration) - { - String alterTablesSql = getAlterTablesSql(modelFromDb).trim(); - - assertTrue(alterTablesSql.length() == 0); - } - } - - /** - * Tests a table name that is longer than the maximum allowed. - */ - public void testLongTableName() - { - if (getSqlBuilder().getMaxTableNameLength() == -1) - { - return; - } - - String tableName = StringUtils.repeat("Test", (getSqlBuilder().getMaxTableNameLength() / 4) + 3); - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performConstraintsTest(modelXml, true); - } - - /** - * Tests a column name that is longer than the maximum allowed. - */ - public void testLongColumnName() - { - if (getPlatformInfo().getMaxColumnNameLength() == -1) - { - return; - } - - String columnName = StringUtils.repeat("Test", (getSqlBuilder().getMaxColumnNameLength() / 4) + 3); - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performConstraintsTest(modelXml, true); - } - - /** - * Tests a constraint name that is longer than the maximum allowed. - */ - public void testLongConstraintName() - { - if (getSqlBuilder().getMaxConstraintNameLength() == -1) - { - return; - } - - String constraintName = StringUtils.repeat("Test", (getSqlBuilder().getMaxConstraintNameLength() / 4) + 3); - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performConstraintsTest(modelXml, true); - } - - /** - * Tests a foreign key name that is longer than the maximum allowed. - */ - public void testLongForeignKeyName() - { - if (getSqlBuilder().getMaxForeignKeyNameLength() == -1) - { - return; - } - - String fkName = StringUtils.repeat("Test", (getSqlBuilder().getMaxForeignKeyNameLength() / 4) + 3); - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performConstraintsTest(modelXml, true); - } - - /** - * Tests a nullable column. - */ - public void testNullableColumn() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performConstraintsTest(modelXml, true); - } - - /** - * Tests a not-nullable column. - */ - public void testNotNullableColumn() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performConstraintsTest(modelXml, true); - } - - /** - * Tests an auto-increment INTEGER column. - */ - public void testAutoIncrementIntegerColumn() - { - // only test this if the platform supports it - if (!getPlatformInfo().isNonPKIdentityColumnsSupported()) - { - return; - } - - // we need special catering for Sybase which does not support identity for INTEGER columns - final String modelXml; - - if (SybasePlatform.DATABASENAME.equals(getPlatform().getName())) - { - modelXml = "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - else - { - modelXml = "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - - performConstraintsTest(modelXml, - getPlatformInfo().getIdentityStatusReadingSupported()); - } - - /** - * Tests an auto-increment primary key column. - */ - public void testPrimaryKeyAutoIncrementColumn() - { - // we need special catering for Sybase which does not support identity for INTEGER columns - final String modelXml; - - if (SybasePlatform.DATABASENAME.equals(getPlatform().getName())) - { - modelXml = "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - else - { - modelXml = "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - - performConstraintsTest(modelXml, - getPlatformInfo().getIdentityStatusReadingSupported()); - } - - /** - * Tests a simple index. - */ - public void testIndex() - { - if (!getPlatformInfo().isIndicesSupported()) - { - return; - } - - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performConstraintsTest(modelXml, true); - } - - /** - * Tests an unique index for two columns. - */ - public void testUniqueIndex() - { - if (!getPlatformInfo().isIndicesSupported()) - { - return; - } - - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performConstraintsTest(modelXml, true); - } - - /** - * Tests an index for two columns, one of which a pk column. - */ - public void testPrimaryKeyIndex() - { - if (!getPlatformInfo().isIndicesSupported()) - { - return; - } - - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performConstraintsTest(modelXml, true); - } - - /** - * Tests two tables with a simple foreign key relationship between them. - */ - public void testSimpleForeignKey() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performConstraintsTest(modelXml, true); - } - - /** - * Tests two tables with overlapping foreign key relationships between them. - */ - public void testOverlappingForeignKeys() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performConstraintsTest(modelXml, true); - } - - /** - * Tests two tables with circular foreign key relationships between them. - */ - public void testCircularForeignKeys() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performConstraintsTest(modelXml, true); - } -} diff --git a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestDataReaderAndWriter.java b/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestDataReaderAndWriter.java deleted file mode 100644 index ff63663339..0000000000 --- a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestDataReaderAndWriter.java +++ /dev/null @@ -1,324 +0,0 @@ -package org.jumpmind.symmetric.ddl.io; - -/* - * 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.StringReader; -import java.io.StringWriter; -import java.util.ArrayList; - -import junit.framework.TestCase; - -import org.apache.commons.beanutils.DynaBean; -import org.apache.commons.lang.StringUtils; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaBean; -import org.jumpmind.symmetric.ddl.io.DataReader; -import org.jumpmind.symmetric.ddl.io.DataSink; -import org.jumpmind.symmetric.ddl.io.DataSinkException; -import org.jumpmind.symmetric.ddl.io.DataWriter; -import org.jumpmind.symmetric.ddl.io.DatabaseIO; -import org.jumpmind.symmetric.ddl.model.Database; - -/** - * Tests the {@link org.jumpmind.symmetric.ddl.io.DataReader} and {@link org.jumpmind.symmetric.ddl.io.DataWriter} classes. - * - * @version $Revision: 289996 $ - */ -public class TestDataReaderAndWriter extends TestCase -{ - /** - * Tests reading the data from XML. - */ - public void testRead() throws Exception - { - final String testSchemaXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String testDataXml = - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " 0684830493\n"+ - " Old Man And The Sea\n"+ - " 1952\n"+ - " \n"+ - " \n"+ - " 0198321465\n"+ - " Macbeth\n"+ - " 1606\n"+ - " \n"+ - " \n"+ - " 0140707026\n"+ - " A Midsummer Night's Dream\n"+ - " 1595\n"+ - " \n"+ - ""; - - DatabaseIO modelReader = new DatabaseIO(); - - modelReader.setUseInternalDtd(true); - modelReader.setValidateXml(false); - - Database model = modelReader.read(new StringReader(testSchemaXml)); - final ArrayList readObjects = new ArrayList(); - DataReader dataReader = new DataReader(); - - dataReader.setModel(model); - dataReader.setSink(new DataSink() { - public void start() throws DataSinkException - {} - - public void addBean(DynaBean bean) throws DataSinkException - { - readObjects.add(bean); - } - - public void end() throws DataSinkException - {} - }); - // no need to call start/end as the don't do anything anyways - dataReader.parse(new StringReader(testDataXml)); - - assertEquals(5, readObjects.size()); - - DynaBean obj1 = (DynaBean)readObjects.get(0); - DynaBean obj2 = (DynaBean)readObjects.get(1); - DynaBean obj3 = (DynaBean)readObjects.get(2); - DynaBean obj4 = (DynaBean)readObjects.get(3); - DynaBean obj5 = (DynaBean)readObjects.get(4); - - assertEquals("author", - obj1.getDynaClass().getName()); - assertEquals("1", - obj1.get("author_id").toString()); - assertEquals("Ernest Hemingway", - obj1.get("name").toString()); - assertEquals("author", - obj2.getDynaClass().getName()); - assertEquals("2", - obj2.get("author_id").toString()); - assertEquals("William Shakespeare", - obj2.get("name").toString()); - assertEquals("book", - obj3.getDynaClass().getName()); - assertEquals("1", - obj3.get("book_id").toString()); - assertEquals("1", - obj3.get("author_id").toString()); - assertEquals("0684830493", - obj3.get("isbn").toString()); - assertEquals("Old Man And The Sea", - obj3.get("title").toString()); - assertEquals("1952-01-01", - obj3.get("issue_date").toString()); // parsed as a java.sql.Date - assertEquals("book", - obj4.getDynaClass().getName()); - assertEquals("2", - obj4.get("book_id").toString()); - assertEquals("2", - obj4.get("author_id").toString()); - assertEquals("0198321465", - obj4.get("isbn").toString()); - assertEquals("Macbeth", - obj4.get("title").toString()); - assertEquals("1606-01-01", - obj4.get("issue_date").toString()); // parsed as a java.sql.Date - assertEquals("book", - obj5.getDynaClass().getName()); - assertEquals("3", - obj5.get("book_id").toString()); - assertEquals("2", - obj5.get("author_id").toString()); - assertEquals("0140707026", - obj5.get("isbn").toString()); - assertEquals("A Midsummer Night's Dream", - obj5.get("title").toString()); - assertEquals("1595-01-01", - obj5.get("issue_date").toString()); // parsed as a java.sql.Date - } - - /** - * Tests special characters in the data XML (for DDLUTILS-63). - */ - public void testSpecialCharacters() throws Exception - { - final String testSchemaXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String testedValue = "Some Special Characters: \u0001\u0009\u0010"; - - DatabaseIO modelIO = new DatabaseIO(); - - modelIO.setUseInternalDtd(true); - modelIO.setValidateXml(false); - - Database model = modelIO.read(new StringReader(testSchemaXml)); - StringWriter output = new StringWriter(); - DataWriter dataWriter = new DataWriter(output, "UTF-8"); - SqlDynaBean bean = (SqlDynaBean)model.createDynaBeanFor(model.getTable(0)); - - bean.set("id", new Integer(1)); - bean.set("value", testedValue); - dataWriter.writeDocumentStart(); - dataWriter.write(bean); - dataWriter.writeDocumentEnd(); - - String dataXml = output.toString(); - - final ArrayList readObjects = new ArrayList(); - DataReader dataReader = new DataReader(); - - dataReader.setModel(model); - dataReader.setSink(new DataSink() { - public void start() throws DataSinkException - {} - - public void addBean(DynaBean bean) throws DataSinkException - { - readObjects.add(bean); - } - - public void end() throws DataSinkException - {} - }); - // no need to call start/end as they don't do anything anyways - dataReader.parse(new StringReader(dataXml)); - - assertEquals(1, readObjects.size()); - - DynaBean obj = (DynaBean)readObjects.get(0); - - assertEquals("test", - obj.getDynaClass().getName()); - assertEquals("1", - obj.get("id").toString()); - assertEquals(testedValue, - obj.get("value").toString()); - } - - - /** - * Tests a cdata section (see DDLUTILS-174). - */ - public void testCData() throws Exception - { - final String testSchemaXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String testedValue1 = ""; - final String testedValue2 = StringUtils.repeat("a ", 1000) + testedValue1; - final String testedValue3 = "
\n

\n" + StringUtils.repeat("Make it longer\n", 99) + "
"; - final String testedValue4 = ""; - final String testedValue5 = "<>"; - - DatabaseIO modelIO = new DatabaseIO(); - - modelIO.setUseInternalDtd(true); - modelIO.setValidateXml(false); - - Database model = modelIO.read(new StringReader(testSchemaXml)); - StringWriter output = new StringWriter(); - DataWriter dataWriter = new DataWriter(output, "UTF-8"); - SqlDynaBean bean = (SqlDynaBean)model.createDynaBeanFor(model.getTable(0)); - - bean.set("id", new Integer(1)); - bean.set("value1", testedValue1); - bean.set("value2", testedValue2); - bean.set("value3", testedValue3); - bean.set("value4", testedValue4); - bean.set("value5", testedValue5); - dataWriter.writeDocumentStart(); - dataWriter.write(bean); - dataWriter.writeDocumentEnd(); - - String dataXml = output.toString(); - - final ArrayList readObjects = new ArrayList(); - DataReader dataReader = new DataReader(); - - dataReader.setModel(model); - dataReader.setSink(new DataSink() { - public void start() throws DataSinkException - {} - - public void addBean(DynaBean bean) throws DataSinkException - { - readObjects.add(bean); - } - - public void end() throws DataSinkException - {} - }); - // no need to call start/end as they don't do anything anyways - dataReader.parse(new StringReader(dataXml)); - - assertEquals(1, readObjects.size()); - - DynaBean obj = (DynaBean)readObjects.get(0); - - assertEquals("test", - obj.getDynaClass().getName()); - assertEquals("1", - obj.get("id").toString()); - assertEquals(testedValue1, - obj.get("value1").toString()); - assertEquals(testedValue2, - obj.get("value2").toString()); - assertEquals(testedValue3, - obj.get("value3").toString()); - assertEquals(testedValue4, - obj.get("value4").toString()); - assertEquals(testedValue5, - obj.get("value5").toString()); - } -} diff --git a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestDatatypes.java b/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestDatatypes.java deleted file mode 100644 index 1dddf16a9e..0000000000 --- a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestDatatypes.java +++ /dev/null @@ -1,978 +0,0 @@ -package org.jumpmind.symmetric.ddl.io; - -/* - * 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.Reader; -import java.io.StringReader; -import java.io.StringWriter; -import java.math.BigDecimal; -import java.sql.Date; -import java.sql.Time; -import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.TreeSet; - -import junit.framework.Test; - -import org.jumpmind.symmetric.ddl.io.BinaryObjectsHelper; -import org.jumpmind.symmetric.ddl.io.DatabaseDataIO; -import org.jumpmind.symmetric.ddl.model.Database; -import org.jumpmind.symmetric.ddl.platform.interbase.InterbasePlatform; - -/** - * Performs roundtrip datatype tests. - * - * @version $Revision: $ - */ -public class TestDatatypes extends RoundtripTestBase -{ - // TODO: special columns (java_object, array, distinct, ...) - - /** - * Parameterized test case pattern. - * - * @return The tests - */ - public static Test suite() throws Exception - { - return getTests(TestDatatypes.class); - } - - /** - * Performs a data type test. - * - * @param modelXml The model as XML - * @param value1 The non-pk value for the first row - * @param value2 The non-pk value for the second row - */ - protected void performDataTypeTest(String modelXml, Object value1, Object value2) - { - performDataTypeTest(modelXml, value1, value2, value1, value2); - } - - /** - * Performs a data type test for a model with a default value. - * - * @param modelXml The model as XML - * @param value1 The non-pk value for the first row; use null for - * the default value - * @param value2 The non-pk value for the second row; use null for - * the default value - * @param defaultValue The default value - */ - protected void performDataTypeTest(String modelXml, Object value1, Object value2, Object defaultValue) - { - performDataTypeTest(modelXml, - value1, - value2, - value1 == null ? defaultValue : value1, - value2 == null ? defaultValue : value2); - } - - /** - * Performs a data type test. In short, we're testing creation of a database, insertion of values - * into it, and reading the model back. In addition we also check that DdlUtils does not try to - * alter the new database when using the alterTables/getAlterTablesSql - * methods of the {@link org.jumpmind.symmetric.ddl.Platform} with the read-back model. - * - * @param modelXml The model as XML - * @param inserted1 The non-pk value to insert for the first row - * @param inserted2 The non-pk value to insert for the second row - * @param expected1 The expected non-pk value for the first row - * @param expected2 The expected non-pk value for the second row - */ - protected void performDataTypeTest(String modelXml, Object inserted1, Object inserted2, Object expected1, Object expected2) - { - createDatabase(modelXml); - insertRow("roundtrip", new Object[] { new Integer(1), inserted1 }); - insertRow("roundtrip", new Object[] { new Integer(2), inserted2 }); - - List beans = getRows("roundtrip"); - - assertEquals(expected1, beans.get(0), "avalue"); - assertEquals(expected2, beans.get(1), "avalue"); - - Database modelFromDb = readModelFromDatabase("roundtriptest"); - - assertEquals(getAdjustedModel(), - modelFromDb); - - String alterTablesSql = getAlterTablesSql(modelFromDb).trim(); - - assertEquals("", - alterTablesSql); - - StringWriter stringWriter = new StringWriter(); - DatabaseDataIO dataIO = new DatabaseDataIO(); - - dataIO.writeDataToXML(getPlatform(), getModel(), stringWriter, "UTF-8"); - - String dataSql = stringWriter.toString(); - - assertTrue((dataSql != null) && (dataSql.length() > 0)); - - getPlatform().dropTables(getModel(), false); - - createDatabase(modelXml); - - dataIO.writeDataToDatabase(getPlatform(), getModel(), new Reader[] { new StringReader(dataSql) }); - - beans = getRows("roundtrip"); - - assertEquals(expected1, beans.get(0), "avalue"); - assertEquals(expected2, beans.get(1), "avalue"); - } - - /** - * Tests a simple BIT column. - */ - public void testBit() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, Boolean.TRUE, Boolean.FALSE); - } - - /** - * Tests a BIT column with a default value. - */ - public void testBitWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, null, Boolean.TRUE, Boolean.FALSE); - } - - /** - * Tests a simple BOOLEAN column. - */ - public void testBoolean() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, Boolean.FALSE, Boolean.TRUE); - } - - /** - * Tests a BOOLEAN column with a default value. - */ - public void testBooleanWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, Boolean.TRUE, null, Boolean.TRUE); - } - - /** - * Tests a simple TINYINT column. - */ - public void testTinyInt() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new Integer(254), new Integer(-254)); - } - - /** - * Tests a TINYINT column with a default value. - */ - public void testTinyIntWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new Integer(128), null, new Integer(-200)); - } - - /** - * Tests a simple SMALLINT column. - */ - public void testSmallInt() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new Integer(Short.MIN_VALUE), new Integer(Short.MAX_VALUE)); - } - - /** - * Tests a SMALLINT column with a default value. - */ - public void testSmallIntWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new Integer(256), null, new Integer(-30000)); - } - - /** - * Tests a simple INTEGER column. - */ - public void testInteger() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new Integer(0), new Integer(-2147483648)); - } - - /** - * Tests a INTEGER column with a default value. - */ - public void testIntegerWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, null, new Integer(2147483646), new Integer(2147483647)); - } - - /** - * Tests a simple BIGINT column. - */ - public void testBigInt() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new Long(Long.MAX_VALUE), new Long(0l)); - } - - /** - * Tests a BIGINT column with a default value. - */ - public void testBigIntWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, null, new Long(-1l), new Long(-9000000000000000000l)); - } - - /** - * Tests a simple REAL column. - */ - public void testReal() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new Float(12345.6f), new Float(0.0f)); - } - - /** - * Tests a REAL column with a default value. - */ - public void testRealWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new Float(1e+20f), null, new Float(-1.01234f)); - } - - /** - * Tests a simple FLOAT column. - */ - public void testFloat() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new Double(-1.0), new Double(1e-45)); - } - - /** - * Tests a FLOAT column with a default value. - */ - public void testFloatWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, null, new Double(1e+25), new Double(12345678.9012345)); - } - - /** - * Tests a simple DOUBLE column. - */ - public void testDouble() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new Double(1e+38), new Double(1.01)); - } - - /** - * Tests a DOUBLE column with a default value. - */ - public void testDoubleWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new Double(-1e+25), null, new Double(-987654321.098765)); - } - - /** - * Tests a simple DECIMAL column. - */ - public void testDecimal() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new BigDecimal("0"), new BigDecimal("-1234567890123")); - } - - /** - * Tests a DECIMAL column with a default value. - */ - public void testDecimalWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, null, new BigDecimal("-1"), new BigDecimal("123456789012345")); - } - - /** - * Tests a simple DECIMAL column with a scale. - */ - public void testDecimalWithScale() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new BigDecimal("0.0100001"), new BigDecimal("-87654321.1234567")); - } - - /** - * Tests a DECIMAL column with a scale and default value. - */ - public void testDecimalWithScaleAndDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new BigDecimal("1.0000001"), null, new BigDecimal("12345678.7654321")); - } - - /** - * Tests a simple NUMERIC column. - */ - public void testNumeric() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new BigDecimal("210987654321"), new BigDecimal("-2")); - } - - /** - * Tests a NUMERIC column with a default value. - */ - public void testNumericWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, null, new BigDecimal("100"), new BigDecimal("-123456789012345")); - } - - /** - * Tests a simple NUMERIC column with a scale. - */ - public void testNumericWithScale() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, new BigDecimal("1234567.89012345"), new BigDecimal("1.00000001")); - } - - /** - * Tests a NUMERIC column with a scale and default value. - */ - public void testNumericWithScaleAndDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, null, new BigDecimal("1e-8"), new BigDecimal("-1234567.87654321")); - } - - /** - * Tests a simple CHAR column. - */ - public void testChar() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, null, "1234567890"); - } - - /** - * Tests a CHAR column with a default value. - */ - public void testCharWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, null, "123456789012345", "543210987654321", "123456789012345"); - } - - /** - * Tests a simple VARCHAR column. - */ - public void testVarChar() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, "123456789012345678", null); - } - - /** - * Tests a VARCHAR column with a default value. - */ - public void testVarCharWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String value = - "1234567890123456789012345678901234567890123456789012345678901234"+ - "1234567890123456789012345678901234567890123456789012345678901234"+ - "1234567890123456789012345678901234567890123456789012345678901234"+ - "12345678901234567890123456789012345678901234567890123456789012"; - - performDataTypeTest(modelXml, null, value, "some value"); - } - - /** - * Tests a VARCHAR column with a default value that contains a single quote. - */ - public void testVarCharWithDefaultValueWithQuote() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, null, "123456", "someone's"); - } - - /** - * Tests a VARCHAR column with a single quote as the default value. - */ - public void testVarCharWithSingleQuoteAsDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, null, "123456", "'"); - } - - /** - * Tests a simple LONGVARCHAR column. - */ - public void testLongVarChar() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - performDataTypeTest(modelXml, null, "some not too long text"); - } - - /** - * Tests a LONGVARCHAR column with a default value. - */ - public void testLongVarCharWithDefault() - { - if (!getPlatformInfo().isDefaultValuesForLongTypesSupported() || - InterbasePlatform.DATABASENAME.equals(getPlatform().getName())) - { - // Some Interbase versions do not like default values for LOB objects - return; - } - - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String value = - "1234567890123456789012345678901234567890123456789012345678901234"+ - "1234567890123456789012345678901234567890123456789012345678901234"+ - "1234567890123456789012345678901234567890123456789012345678901234"+ - "1234567890123456789012345678901234567890123456789012345678901234"; - - performDataTypeTest(modelXml, null, value, "some value"); - } - - /** - * Tests a simple DATE column. - */ - public void testDate() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - // we would use Calendar but that might give Locale problems - performDataTypeTest(modelXml, null, new Date(103, 12, 25)); - } - - /** - * Tests a DATE column with a default value. - */ - public void testDateWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - // we would use Calendar but that might give Locale problems - performDataTypeTest(modelXml, new Date(105, 0, 1), null, new Date(100, 0, 1)); - } - - /** - * Tests a simple TIME column. - */ - public void testTime() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - // we would use Calendar but that might give Locale problems - performDataTypeTest(modelXml, new Time(03, 47, 15), null); - } - - /** - * Tests a TIME column with a default value. - */ - public void testTimeWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - // we would use Calendar but that might give Locale problems - performDataTypeTest(modelXml, new Time(23, 59, 59), null, new Time(11, 27, 03)); - } - - /** - * Tests a simple TIMESTAMP column. - */ - public void testTimestamp() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - // we would use Calendar but that might give Locale problems - // also we leave out the fractional part because databases differ - // in their support here - performDataTypeTest(modelXml, new Timestamp(70, 0, 1, 0, 0, 0, 0), new Timestamp(100, 10, 11, 10, 10, 10, 0)); - } - - /** - * Tests a TIMESTAMP column with a default value. - */ - public void testTimestampWithDefault() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - // we would use Calendar but that might give Locale problems - // also we leave out the fractional part because databases differ - // in their support here - performDataTypeTest(modelXml, new Timestamp(90, 9, 21, 20, 25, 39, 0), null, new Timestamp(85, 5, 17, 16, 17, 18, 0)); - } - - /** - * Tests a simple BINARY column. - */ - public void testBinary() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - HashMap value1 = new HashMap(); - ArrayList value2 = new ArrayList(); - - value1.put("test", "some value"); - value2.add("some other value"); - - BinaryObjectsHelper helper = new BinaryObjectsHelper(); - - performDataTypeTest(modelXml, - helper.serialize(value1), helper.serialize(value2), - value1, value2); - } - - /** - * Tests a simple VARBINARY column. - */ - public void testVarBinary() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - TreeSet value1 = new TreeSet(); - String value2 = "a value, nothing special"; - - value1.add("o look, a value !"); - - BinaryObjectsHelper helper = new BinaryObjectsHelper(); - - performDataTypeTest(modelXml, - helper.serialize(value1), helper.serialize(value2), - value1, value2); - } - - /** - * Tests a simple LONGVARBINARY column. - */ - public void testLongVarBinary() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - HashMap value = new HashMap(); - - value.put("test1", "some value"); - value.put(null, "some other value"); - - BinaryObjectsHelper helper = new BinaryObjectsHelper(); - - performDataTypeTest(modelXml, - helper.serialize(value), null, - value, null); - } - - /** - * Tests a simple BLOB column. - */ - public void testBlob() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - HashMap value = new HashMap(); - - value.put("test1", "some value"); - value.put(null, "some other value"); - - BinaryObjectsHelper helper = new BinaryObjectsHelper(); - - performDataTypeTest(modelXml, - helper.serialize(value), null, - value, null); - } - - /** - * Tests a simple CLOB column. - */ - public void testClob() - { - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - final String value = - "1234567890123456789012345678901234567890123456789012345678901234"+ - "1234567890123456789012345678901234567890123456789012345678901234"+ - "1234567890123456789012345678901234567890123456789012345678901234"+ - "1234567890123456789012345678901234567890123456789012345678901234"; - - performDataTypeTest(modelXml, null, value); - } -} diff --git a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestMisc.java b/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestMisc.java deleted file mode 100644 index 21b2c0ab4c..0000000000 --- a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/io/TestMisc.java +++ /dev/null @@ -1,1043 +0,0 @@ -package org.jumpmind.symmetric.ddl.io; - -/* - * 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.Reader; -import java.io.StringReader; -import java.io.StringWriter; -import java.math.BigDecimal; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; - -import junit.framework.Test; - -import org.apache.commons.beanutils.DynaBean; -import org.dom4j.Document; -import org.dom4j.Element; -import org.dom4j.io.SAXReader; -import org.jumpmind.symmetric.ddl.io.DatabaseDataIO; -import org.jumpmind.symmetric.ddl.platform.hsqldb.HsqlDbPlatform; -import org.jumpmind.symmetric.ddl.platform.sybase.SybasePlatform; -import org.xml.sax.InputSource; - -/** - * Contains misc tests. - * - * @version $Revision: $ - */ -public class TestMisc extends RoundtripTestBase -{ - /** - * Parameterized test case pattern. - * - * @return The tests - */ - public static Test suite() throws Exception - { - return getTests(TestMisc.class); - } - - /** - * Tests the backup and restore of a table with an identity column and a foreign key to - * it when identity override is turned on. - */ - public void testIdentityOverrideOn() throws Exception - { - if (!getPlatformInfo().isIdentityOverrideAllowed()) - { - // TODO: for testing these platforms, we need deleteRows - return; - } - - // Sybase does not like INTEGER auto-increment columns - boolean isSybase = SybasePlatform.DATABASENAME.equals(getPlatform().getName()); - final String modelXml; - - if (isSybase) - { - modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - else - { - modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - - createDatabase(modelXml); - - getPlatform().setIdentityOverrideOn(true); - - if (isSybase) - { - insertRow("misc1", new Object[] { new BigDecimal(10), new Integer(1) }); - insertRow("misc1", new Object[] { new BigDecimal(12), new Integer(2) }); - insertRow("misc1", new Object[] { new BigDecimal(13), new Integer(3) }); - insertRow("misc2", new Object[] { new Integer(1), new BigDecimal(10) }); - insertRow("misc2", new Object[] { new Integer(2), new BigDecimal(13) }); - } - else - { - insertRow("misc1", new Object[] { new Integer(10), new Integer(1) }); - insertRow("misc1", new Object[] { new Integer(12), new Integer(2) }); - insertRow("misc1", new Object[] { new Integer(13), new Integer(3) }); - insertRow("misc2", new Object[] { new Integer(1), new Integer(10) }); - insertRow("misc2", new Object[] { new Integer(2), new Integer(13) }); - } - - StringWriter stringWriter = new StringWriter(); - DatabaseDataIO dataIO = new DatabaseDataIO(); - - dataIO.writeDataToXML(getPlatform(), getModel(), stringWriter, "UTF-8"); - - String dataAsXml = stringWriter.toString(); - SAXReader reader = new SAXReader(); - Document testDoc = reader.read(new InputSource(new StringReader(dataAsXml))); - - List misc1Rows = testDoc.selectNodes("//misc1"); - List misc2Rows = testDoc.selectNodes("//misc2"); - String pkColumnName = "pk"; - String fkColumnName = "fk"; - String valueColumnName = "avalue"; - - if (misc1Rows.size() == 0) - { - misc1Rows = testDoc.selectNodes("//MISC1"); - misc2Rows = testDoc.selectNodes("//MISC2"); - pkColumnName = pkColumnName.toUpperCase(); - fkColumnName = fkColumnName.toUpperCase(); - valueColumnName = valueColumnName.toUpperCase(); - } - - assertEquals(3, misc1Rows.size()); - assertEquals("10", ((Element)misc1Rows.get(0)).attributeValue(pkColumnName)); - assertEquals("1", ((Element)misc1Rows.get(0)).attributeValue(valueColumnName)); - assertEquals("12", ((Element)misc1Rows.get(1)).attributeValue(pkColumnName)); - assertEquals("2", ((Element)misc1Rows.get(1)).attributeValue(valueColumnName)); - assertEquals("13", ((Element)misc1Rows.get(2)).attributeValue(pkColumnName)); - assertEquals("3", ((Element)misc1Rows.get(2)).attributeValue(valueColumnName)); - assertEquals(2, misc2Rows.size()); - assertEquals("1", ((Element)misc2Rows.get(0)).attributeValue(pkColumnName)); - assertEquals("10", ((Element)misc2Rows.get(0)).attributeValue(fkColumnName)); - assertEquals("2", ((Element)misc2Rows.get(1)).attributeValue(pkColumnName)); - assertEquals("13", ((Element)misc2Rows.get(1)).attributeValue(fkColumnName)); - - dropDatabase(); - createDatabase(modelXml); - - StringReader stringReader = new StringReader(dataAsXml); - - dataIO.writeDataToDatabase(getPlatform(), getModel(), new Reader[] { stringReader }); - - List beans = getRows("misc1"); - - if (isSybase) - { - assertEquals(new BigDecimal(10), beans.get(0), "pk"); - assertEquals(new BigDecimal(12), beans.get(1), "pk"); - assertEquals(new BigDecimal(13), beans.get(2), "pk"); - } - else - { - assertEquals(new Integer(10), beans.get(0), "pk"); - assertEquals(new Integer(12), beans.get(1), "pk"); - assertEquals(new Integer(13), beans.get(2), "pk"); - } - assertEquals(new Integer(1), beans.get(0), "avalue"); - assertEquals(new Integer(2), beans.get(1), "avalue"); - assertEquals(new Integer(3), beans.get(2), "avalue"); - - beans = getRows("misc2"); - - assertEquals(new Integer(1), beans.get(0), "pk"); - assertEquals(new Integer(2), beans.get(1), "pk"); - if (isSybase) - { - assertEquals(new BigDecimal(10), beans.get(0), "fk"); - assertEquals(new BigDecimal(13), beans.get(1), "fk"); - } - else - { - assertEquals(new Integer(10), beans.get(0), "fk"); - assertEquals(new Integer(13), beans.get(1), "fk"); - } - } - - /** - * Tests the backup and restore of a table with an identity column and a foreign key to - * it when identity override is turned off. - */ - public void testIdentityOverrideOff() throws Exception - { - if (!getPlatformInfo().isIdentityOverrideAllowed()) - { - // TODO: for testing these platforms, we need deleteRows - return; - } - - // Sybase does not like INTEGER auto-increment columns - boolean isSybase = SybasePlatform.DATABASENAME.equals(getPlatform().getName()); - final String modelXml; - - if (isSybase) - { - modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - else - { - modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - - createDatabase(modelXml); - - getPlatform().setIdentityOverrideOn(true); - - if (isSybase) - { - insertRow("misc1", new Object[] { new BigDecimal(10), new Integer(1) }); - insertRow("misc1", new Object[] { new BigDecimal(12), new Integer(2) }); - insertRow("misc1", new Object[] { new BigDecimal(13), new Integer(3) }); - insertRow("misc2", new Object[] { new Integer(1), new BigDecimal(10) }); - insertRow("misc2", new Object[] { new Integer(2), new BigDecimal(13) }); - } - else - { - insertRow("misc1", new Object[] { new Integer(10), new Integer(1) }); - insertRow("misc1", new Object[] { new Integer(12), new Integer(2) }); - insertRow("misc1", new Object[] { new Integer(13), new Integer(3) }); - insertRow("misc2", new Object[] { new Integer(1), new Integer(10) }); - insertRow("misc2", new Object[] { new Integer(2), new Integer(13) }); - } - - StringWriter stringWriter = new StringWriter(); - DatabaseDataIO dataIO = new DatabaseDataIO(); - - dataIO.writeDataToXML(getPlatform(), getModel(), stringWriter, "UTF-8"); - - String dataAsXml = stringWriter.toString(); - SAXReader reader = new SAXReader(); - Document testDoc = reader.read(new InputSource(new StringReader(dataAsXml))); - - List misc1Rows = testDoc.selectNodes("//misc1"); - List misc2Rows = testDoc.selectNodes("//misc2"); - String pkColumnName = "pk"; - String fkColumnName = "fk"; - String valueColumnName = "avalue"; - - if (misc1Rows.size() == 0) - { - misc1Rows = testDoc.selectNodes("//MISC1"); - misc2Rows = testDoc.selectNodes("//MISC2"); - pkColumnName = pkColumnName.toUpperCase(); - fkColumnName = fkColumnName.toUpperCase(); - valueColumnName = valueColumnName.toUpperCase(); - } - - assertEquals(3, misc1Rows.size()); - assertEquals("10", ((Element)misc1Rows.get(0)).attributeValue(pkColumnName)); - assertEquals("1", ((Element)misc1Rows.get(0)).attributeValue(valueColumnName)); - assertEquals("12", ((Element)misc1Rows.get(1)).attributeValue(pkColumnName)); - assertEquals("2", ((Element)misc1Rows.get(1)).attributeValue(valueColumnName)); - assertEquals("13", ((Element)misc1Rows.get(2)).attributeValue(pkColumnName)); - assertEquals("3", ((Element)misc1Rows.get(2)).attributeValue(valueColumnName)); - assertEquals(2, misc2Rows.size()); - assertEquals("1", ((Element)misc2Rows.get(0)).attributeValue(pkColumnName)); - assertEquals("10", ((Element)misc2Rows.get(0)).attributeValue(fkColumnName)); - assertEquals("2", ((Element)misc2Rows.get(1)).attributeValue(pkColumnName)); - assertEquals("13", ((Element)misc2Rows.get(1)).attributeValue(fkColumnName)); - - dropDatabase(); - createDatabase(modelXml); - - getPlatform().setIdentityOverrideOn(false); - - StringReader stringReader = new StringReader(dataAsXml); - - dataIO.writeDataToDatabase(getPlatform(), getModel(), new Reader[] { stringReader }); - - List beans = getRows("misc1"); - - if (isSybase) - { - assertEquals(new BigDecimal(1), beans.get(0), "pk"); - assertEquals(new BigDecimal(2), beans.get(1), "pk"); - assertEquals(new BigDecimal(3), beans.get(2), "pk"); - } - else - { - assertEquals(new Integer(1), beans.get(0), "pk"); - assertEquals(new Integer(2), beans.get(1), "pk"); - assertEquals(new Integer(3), beans.get(2), "pk"); - } - assertEquals(new Integer(1), beans.get(0), "avalue"); - assertEquals(new Integer(2), beans.get(1), "avalue"); - assertEquals(new Integer(3), beans.get(2), "avalue"); - - beans = getRows("misc2"); - - assertEquals(new Integer(1), beans.get(0), "pk"); - assertEquals(new Integer(2), beans.get(1), "pk"); - if (isSybase) - { - assertEquals(new BigDecimal(1), beans.get(0), "fk"); - assertEquals(new BigDecimal(3), beans.get(1), "fk"); - } - else - { - assertEquals(new Integer(1), beans.get(0), "fk"); - assertEquals(new Integer(3), beans.get(1), "fk"); - } - } - - /** - * Tests the backup and restore of a table with an identity column and a foreign key to - * itself while identity override is off. - */ - public void testSelfReferenceIdentityOverrideOff() throws Exception - { - // Hsqldb does not allow rows to reference themselves - if (HsqlDbPlatform.DATABASENAME.equals(getPlatform().getName())) - { - return; - } - - // Sybase does not like INTEGER auto-increment columns - boolean isSybase = SybasePlatform.DATABASENAME.equals(getPlatform().getName()); - final String modelXml; - - if (isSybase) - { - modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - else - { - modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - - createDatabase(modelXml); - - getPlatform().setIdentityOverrideOn(false); - - if (isSybase) - { - insertRow("misc", new Object[] { new BigDecimal(1), null }); - insertRow("misc", new Object[] { new BigDecimal(2), new BigDecimal(1) }); - insertRow("misc", new Object[] { new BigDecimal(3), new BigDecimal(2) }); - insertRow("misc", new Object[] { new BigDecimal(4), new BigDecimal(4) }); - } - else - { - insertRow("misc", new Object[] { new Integer(1), null }); - insertRow("misc", new Object[] { new Integer(2), new Integer(1) }); - insertRow("misc", new Object[] { new Integer(3), new Integer(2) }); - insertRow("misc", new Object[] { new Integer(4), new Integer(4) }); - } - - StringWriter stringWriter = new StringWriter(); - DatabaseDataIO dataIO = new DatabaseDataIO(); - - dataIO.writeDataToXML(getPlatform(), getModel(), stringWriter, "UTF-8"); - - String dataAsXml = stringWriter.toString(); - SAXReader reader = new SAXReader(); - Document testDoc = reader.read(new InputSource(new StringReader(dataAsXml))); - - List miscRows = testDoc.selectNodes("//misc"); - String pkColumnName = "pk"; - String fkColumnName = "fk"; - - if (miscRows.size() == 0) - { - miscRows = testDoc.selectNodes("//MISC"); - pkColumnName = pkColumnName.toUpperCase(); - fkColumnName = fkColumnName.toUpperCase(); - } - - assertEquals(4, miscRows.size()); - assertEquals("1", ((Element)miscRows.get(0)).attributeValue(pkColumnName)); - assertNull(((Element)miscRows.get(0)).attributeValue(fkColumnName)); - assertEquals("2", ((Element)miscRows.get(1)).attributeValue(pkColumnName)); - assertEquals("1", ((Element)miscRows.get(1)).attributeValue(fkColumnName)); - assertEquals("3", ((Element)miscRows.get(2)).attributeValue(pkColumnName)); - assertEquals("2", ((Element)miscRows.get(2)).attributeValue(fkColumnName)); - assertEquals("4", ((Element)miscRows.get(3)).attributeValue(pkColumnName)); - assertEquals("4", ((Element)miscRows.get(3)).attributeValue(fkColumnName)); - - dropDatabase(); - createDatabase(modelXml); - - StringReader stringReader = new StringReader(dataAsXml); - - dataIO.writeDataToDatabase(getPlatform(), getModel(), new Reader[] { stringReader }); - - List beans = getRows("misc"); - - if (isSybase) - { - assertEquals(new BigDecimal(1), beans.get(0), "pk"); - assertNull(((DynaBean)beans.get(0)).get("fk")); - assertEquals(new BigDecimal(2), beans.get(1), "pk"); - assertEquals(new BigDecimal(1), beans.get(1), "fk"); - assertEquals(new BigDecimal(3), beans.get(2), "pk"); - assertEquals(new BigDecimal(2), beans.get(2), "fk"); - assertEquals(new BigDecimal(4), beans.get(3), "pk"); - assertEquals(new BigDecimal(4), beans.get(3), "fk"); - } - else - { - assertEquals(new Integer(1), beans.get(0), "pk"); - assertNull(((DynaBean)beans.get(0)).get("fk")); - assertEquals(new Integer(2), beans.get(1), "pk"); - assertEquals(new Integer(1), beans.get(1), "fk"); - assertEquals(new Integer(3), beans.get(2), "pk"); - assertEquals(new Integer(2), beans.get(2), "fk"); - assertEquals(new Integer(4), beans.get(3), "pk"); - assertEquals(new Integer(4), beans.get(3), "fk"); - } - } - - /** - * Tests the backup and restore of a table with an identity column and a foreign key to - * itself while identity override is off. - */ - public void testSelfReferenceIdentityOverrideOn() throws Exception - { - if (!getPlatformInfo().isIdentityOverrideAllowed()) - { - // TODO: for testing these platforms, we need deleteRows - return; - } - - // Sybase does not like INTEGER auto-increment columns - boolean isSybase = SybasePlatform.DATABASENAME.equals(getPlatform().getName()); - final String modelXml; - - if (isSybase) - { - modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - else - { - modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - - createDatabase(modelXml); - - getPlatform().setIdentityOverrideOn(true); - - if (isSybase) - { - insertRow("misc", new Object[] { new BigDecimal(10), null }); - insertRow("misc", new Object[] { new BigDecimal(11), new BigDecimal(10) }); - insertRow("misc", new Object[] { new BigDecimal(12), new BigDecimal(11) }); - insertRow("misc", new Object[] { new BigDecimal(13), new BigDecimal(13) }); - } - else - { - insertRow("misc", new Object[] { new Integer(10), null }); - insertRow("misc", new Object[] { new Integer(11), new Integer(10) }); - insertRow("misc", new Object[] { new Integer(12), new Integer(11) }); - insertRow("misc", new Object[] { new Integer(13), new Integer(13) }); - } - - StringWriter stringWriter = new StringWriter(); - DatabaseDataIO dataIO = new DatabaseDataIO(); - - dataIO.writeDataToXML(getPlatform(), getModel(), stringWriter, "UTF-8"); - - String dataAsXml = stringWriter.toString(); - SAXReader reader = new SAXReader(); - Document testDoc = reader.read(new InputSource(new StringReader(dataAsXml))); - - List miscRows = testDoc.selectNodes("//misc"); - String pkColumnName = "pk"; - String fkColumnName = "fk"; - - if (miscRows.size() == 0) - { - miscRows = testDoc.selectNodes("//MISC"); - pkColumnName = pkColumnName.toUpperCase(); - fkColumnName = fkColumnName.toUpperCase(); - } - - assertEquals(4, miscRows.size()); - assertEquals("10", ((Element)miscRows.get(0)).attributeValue(pkColumnName)); - assertNull(((Element)miscRows.get(0)).attributeValue(fkColumnName)); - assertEquals("11", ((Element)miscRows.get(1)).attributeValue(pkColumnName)); - assertEquals("10", ((Element)miscRows.get(1)).attributeValue(fkColumnName)); - assertEquals("12", ((Element)miscRows.get(2)).attributeValue(pkColumnName)); - assertEquals("11", ((Element)miscRows.get(2)).attributeValue(fkColumnName)); - assertEquals("13", ((Element)miscRows.get(3)).attributeValue(pkColumnName)); - assertEquals("13", ((Element)miscRows.get(3)).attributeValue(fkColumnName)); - - dropDatabase(); - createDatabase(modelXml); - - StringReader stringReader = new StringReader(dataAsXml); - - dataIO.writeDataToDatabase(getPlatform(), getModel(), new Reader[] { stringReader }); - - List beans = getRows("misc"); - - if (isSybase) - { - assertEquals(new BigDecimal(10), beans.get(0), "pk"); - assertNull(((DynaBean)beans.get(0)).get("fk")); - assertEquals(new BigDecimal(11), beans.get(1), "pk"); - assertEquals(new BigDecimal(10), beans.get(1), "fk"); - assertEquals(new BigDecimal(12), beans.get(2), "pk"); - assertEquals(new BigDecimal(11), beans.get(2), "fk"); - assertEquals(new BigDecimal(13), beans.get(3), "pk"); - assertEquals(new BigDecimal(13), beans.get(3), "fk"); - } - else - { - assertEquals(new Integer(10), beans.get(0), "pk"); - assertNull(((DynaBean)beans.get(0)).get("fk")); - assertEquals(new Integer(11), beans.get(1), "pk"); - assertEquals(new Integer(10), beans.get(1), "fk"); - assertEquals(new Integer(12), beans.get(2), "pk"); - assertEquals(new Integer(11), beans.get(2), "fk"); - assertEquals(new Integer(13), beans.get(3), "pk"); - assertEquals(new Integer(13), beans.get(3), "fk"); - } - } - - /** - * Tests the backup and restore of a self-referencing data set. - */ - public void testSelfReferences() throws Exception - { - if (!getPlatformInfo().isIdentityOverrideAllowed()) - { - // TODO: for testing these platforms, we need deleteRows - return; - } - - // Sybase does not like INTEGER auto-increment columns - boolean isSybase = SybasePlatform.DATABASENAME.equals(getPlatform().getName()); - final String modelXml; - - if (isSybase) - { - modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - else - { - modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - - final String dataXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - ""; - - createDatabase(modelXml); - - getPlatform().setIdentityOverrideOn(true); - - DatabaseDataIO dataIO = new DatabaseDataIO(); - StringReader stringReader = new StringReader(dataXml); - - dataIO.writeDataToDatabase(getPlatform(), getModel(), new Reader[] { stringReader }); - - List beans = getRows("misc", "id"); - - if (isSybase) - { - assertEquals(12, beans.size()); - assertEquals(new BigDecimal(1), beans.get(0), "id"); - assertNull(((DynaBean)beans.get(0)).get("parent_id")); - assertEquals(new BigDecimal(2), beans.get(1), "id"); - assertNull(((DynaBean)beans.get(1)).get("parent_id")); - assertEquals(new BigDecimal(3), beans.get(2), "id"); - assertEquals(new BigDecimal(2), beans.get(2), "parent_id"); - assertEquals(new BigDecimal(4), beans.get(3), "id"); - assertEquals(new BigDecimal(1), beans.get(3), "parent_id"); - assertEquals(new BigDecimal(5), beans.get(4), "id"); - assertEquals(new BigDecimal(3), beans.get(4), "parent_id"); - assertEquals(new BigDecimal(6), beans.get(5), "id"); - assertNull(((DynaBean)beans.get(5)).get("parent_id")); - assertEquals(new BigDecimal(7), beans.get(6), "id"); - assertEquals(new BigDecimal(1), beans.get(6), "parent_id"); - assertEquals(new BigDecimal(8), beans.get(7), "id"); - assertEquals(new BigDecimal(7), beans.get(7), "parent_id"); - assertEquals(new BigDecimal(9), beans.get(8), "id"); - assertEquals(new BigDecimal(6), beans.get(8), "parent_id"); - assertEquals(new BigDecimal(10), beans.get(9), "id"); - assertEquals(new BigDecimal(4), beans.get(9), "parent_id"); - assertEquals(new BigDecimal(11), beans.get(10), "id"); - assertNull(((DynaBean)beans.get(10)).get("parent_id")); - assertEquals(new BigDecimal(12), beans.get(11), "id"); - assertEquals(new BigDecimal(11), beans.get(11), "parent_id"); - } - else - { - assertEquals(12, beans.size()); - assertEquals(new Integer(1), beans.get(0), "id"); - assertNull(((DynaBean)beans.get(0)).get("parent_id")); - assertEquals(new Integer(2), beans.get(1), "id"); - assertNull(((DynaBean)beans.get(1)).get("parent_id")); - assertEquals(new Integer(3), beans.get(2), "id"); - assertEquals(new Integer(2), beans.get(2), "parent_id"); - assertEquals(new Integer(4), beans.get(3), "id"); - assertEquals(new Integer(1), beans.get(3), "parent_id"); - assertEquals(new Integer(5), beans.get(4), "id"); - assertEquals(new Integer(3), beans.get(4), "parent_id"); - assertEquals(new Integer(6), beans.get(5), "id"); - assertNull(((DynaBean)beans.get(5)).get("parent_id")); - assertEquals(new Integer(7), beans.get(6), "id"); - assertEquals(new Integer(1), beans.get(6), "parent_id"); - assertEquals(new Integer(8), beans.get(7), "id"); - assertEquals(new Integer(7), beans.get(7), "parent_id"); - assertEquals(new Integer(9), beans.get(8), "id"); - assertEquals(new Integer(6), beans.get(8), "parent_id"); - assertEquals(new Integer(10), beans.get(9), "id"); - assertEquals(new Integer(4), beans.get(9), "parent_id"); - assertEquals(new Integer(11), beans.get(10), "id"); - assertNull(((DynaBean)beans.get(10)).get("parent_id")); - assertEquals(new Integer(12), beans.get(11), "id"); - assertEquals(new Integer(11), beans.get(11), "parent_id"); - } - } - - /** - * Tests the backup and restore of a self-referencing data set (with multiple self references - * in the same table). - */ - public void testMultiSelfReferences() throws Exception - { - if (!getPlatformInfo().isIdentityOverrideAllowed()) - { - // TODO: for testing these platforms, we need deleteRows - return; - } - - // Sybase does not like INTEGER auto-increment columns - boolean isSybase = SybasePlatform.DATABASENAME.equals(getPlatform().getName()); - final String modelXml; - - if (isSybase) - { - modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - else - { - modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - } - - final String dataXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - ""; - - createDatabase(modelXml); - - getPlatform().setIdentityOverrideOn(true); - - DatabaseDataIO dataIO = new DatabaseDataIO(); - StringReader stringReader = new StringReader(dataXml); - - dataIO.writeDataToDatabase(getPlatform(), getModel(), new Reader[] { stringReader }); - - List beans = getRows("misc", "id"); - - assertEquals(6, beans.size()); - if (isSybase) - { - assertEquals(new BigDecimal(1), beans.get(0), "id"); - assertEquals(new BigDecimal(2), beans.get(0), "left_id"); - assertEquals(new BigDecimal(3), beans.get(0), "right_id"); - assertEquals(new BigDecimal(2), beans.get(1), "id"); - assertEquals(new BigDecimal(5), beans.get(1), "left_id"); - assertEquals(new BigDecimal(4), beans.get(1), "right_id"); - assertEquals(new BigDecimal(3), beans.get(2), "id"); - assertEquals(new BigDecimal(2), beans.get(2), "left_id"); - assertEquals(new BigDecimal(4), beans.get(2), "right_id"); - assertEquals(new BigDecimal(4), beans.get(3), "id"); - assertEquals(new BigDecimal(6), beans.get(3), "left_id"); - assertEquals((Object)null, beans.get(3), "right_id"); - assertEquals(new BigDecimal(5), beans.get(4), "id"); - assertEquals((Object)null, beans.get(4), "left_id"); - assertEquals(new BigDecimal(6), beans.get(4), "right_id"); - assertEquals(new BigDecimal(6), beans.get(5), "id"); - assertEquals((Object)null, beans.get(5), "left_id"); - assertEquals((Object)null, beans.get(5), "right_id"); - } - else - { - assertEquals(new Integer(1), beans.get(0), "id"); - assertEquals(new Integer(2), beans.get(0), "left_id"); - assertEquals(new Integer(3), beans.get(0), "right_id"); - assertEquals(new Integer(2), beans.get(1), "id"); - assertEquals(new Integer(5), beans.get(1), "left_id"); - assertEquals(new Integer(4), beans.get(1), "right_id"); - assertEquals(new Integer(3), beans.get(2), "id"); - assertEquals(new Integer(2), beans.get(2), "left_id"); - assertEquals(new Integer(4), beans.get(2), "right_id"); - assertEquals(new Integer(4), beans.get(3), "id"); - assertEquals(new Integer(6), beans.get(3), "left_id"); - assertEquals((Object)null, beans.get(3), "right_id"); - assertEquals(new Integer(5), beans.get(4), "id"); - assertEquals((Object)null, beans.get(4), "left_id"); - assertEquals(new Integer(6), beans.get(4), "right_id"); - assertEquals(new Integer(6), beans.get(5), "id"); - assertEquals((Object)null, beans.get(5), "left_id"); - assertEquals((Object)null, beans.get(5), "right_id"); - } - } - - /** - * Tests the backup and restore of several tables with complex relationships with an identity column and a foreign key to - * itself while identity override is off. - */ - public void testComplexTableModel() throws Exception - { - // A: self-reference (A1->A2) - // B: self- and foreign-reference (B1->B2|G1, B2->G2) - // C: circular reference involving more than one table (C1->D1,C2->D2) - // D: foreign-reference to F (D1->F1,D2) - // E: isolated table (E1) - // F: foreign-reference to C (F1->C2) - // G: no references (G1, G2) - - final String modelXml = - "\n"+ - "\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - " \n"+ - "
\n"+ - " \n"+ - " \n"+ - "
\n"+ - "
"; - - createDatabase(modelXml); - - getPlatform().setIdentityOverrideOn(true); - - // this is the optimal insertion order - insertRow("E", new Object[] { new Integer(1) }); - insertRow("G", new Object[] { new Integer(1) }); - insertRow("G", new Object[] { new Integer(2) }); - insertRow("A", new Object[] { new Integer(2), null }); - insertRow("A", new Object[] { new Integer(1), new Integer(2) }); - insertRow("B", new Object[] { new Integer(2), null, new Integer(2) }); - insertRow("B", new Object[] { new Integer(1), new Integer(2), new Integer(1) }); - insertRow("D", new Object[] { new Integer(2), null }); - insertRow("C", new Object[] { new Integer(2), new Integer(2) }); - insertRow("F", new Object[] { new Integer(1), new Integer(2) }); - insertRow("D", new Object[] { new Integer(1), new Integer(1) }); - insertRow("C", new Object[] { new Integer(1), new Integer(1) }); - - StringWriter stringWriter = new StringWriter(); - DatabaseDataIO dataIO = new DatabaseDataIO(); - - dataIO.writeDataToXML(getPlatform(), getModel(), stringWriter, "UTF-8"); - - String dataAsXml = stringWriter.toString(); - - // the somewhat optimized order that DdlUtils currently generates is: - // E1, G1, G2, A2, A1, B2, B1, C2, C1, D2, D1, F1 - // note that the order per table is the insertion order above - SAXReader reader = new SAXReader(); - Document testDoc = reader.read(new InputSource(new StringReader(dataAsXml))); - boolean uppercase = false; - List rows = testDoc.selectNodes("/*/*"); - String pkColumnName = "pk"; - - assertEquals(12, rows.size()); - if (!"e".equals(((Element)rows.get(0)).getName())) - { - assertEquals("E", ((Element)rows.get(0)).getName()); - uppercase = true; - } - if (!"pk".equals(((Element)rows.get(0)).attribute(0).getName())) - { - pkColumnName = pkColumnName.toUpperCase(); - } - assertEquals("1", ((Element)rows.get(0)).attributeValue(pkColumnName)); - - // we cannot be sure of the order in which the database returns the rows - // per table (some return them in pk order, some in insertion order) - // so we don't assume an order in this test - HashSet pkValues = new HashSet(); - HashSet expectedValues = new HashSet(Arrays.asList(new String[] { "1", "2" })); - - assertEquals(uppercase ? "G" : "g", ((Element)rows.get(1)).getName()); - assertEquals(uppercase ? "G" : "g", ((Element)rows.get(2)).getName()); - pkValues.add(((Element)rows.get(1)).attributeValue(pkColumnName)); - pkValues.add(((Element)rows.get(2)).attributeValue(pkColumnName)); - assertEquals(pkValues, expectedValues); - - pkValues.clear(); - - assertEquals(uppercase ? "A" : "a", ((Element)rows.get(3)).getName()); - assertEquals(uppercase ? "A" : "a", ((Element)rows.get(4)).getName()); - pkValues.add(((Element)rows.get(3)).attributeValue(pkColumnName)); - pkValues.add(((Element)rows.get(4)).attributeValue(pkColumnName)); - assertEquals(pkValues, expectedValues); - - pkValues.clear(); - - assertEquals(uppercase ? "B" : "b", ((Element)rows.get(5)).getName()); - assertEquals(uppercase ? "B" : "b", ((Element)rows.get(6)).getName()); - pkValues.add(((Element)rows.get(5)).attributeValue(pkColumnName)); - pkValues.add(((Element)rows.get(6)).attributeValue(pkColumnName)); - assertEquals(pkValues, expectedValues); - - pkValues.clear(); - - assertEquals(uppercase ? "C" : "c", ((Element)rows.get(7)).getName()); - assertEquals(uppercase ? "C" : "c", ((Element)rows.get(8)).getName()); - pkValues.add(((Element)rows.get(7)).attributeValue(pkColumnName)); - pkValues.add(((Element)rows.get(8)).attributeValue(pkColumnName)); - assertEquals(pkValues, expectedValues); - - pkValues.clear(); - - assertEquals(uppercase ? "D" : "d", ((Element)rows.get(9)).getName()); - assertEquals(uppercase ? "D" : "d", ((Element)rows.get(10)).getName()); - pkValues.add(((Element)rows.get(9)).attributeValue(pkColumnName)); - pkValues.add(((Element)rows.get(10)).attributeValue(pkColumnName)); - assertEquals(pkValues, expectedValues); - - pkValues.clear(); - - assertEquals(uppercase ? "F" : "f", ((Element)rows.get(11)).getName()); - assertEquals("1", ((Element)rows.get(11)).attributeValue(pkColumnName)); - - dropDatabase(); - createDatabase(modelXml); - - StringReader stringReader = new StringReader(dataAsXml); - - dataIO.writeDataToDatabase(getPlatform(), getModel(), new Reader[] { stringReader }); - - assertEquals(2, getRows("A").size()); - assertEquals(2, getRows("B").size()); - assertEquals(2, getRows("C").size()); - assertEquals(2, getRows("D").size()); - assertEquals(1, getRows("E").size()); - assertEquals(1, getRows("F").size()); - assertEquals(2, getRows("G").size()); - } - -} diff --git a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/platform/TestPlatformImplBase.java b/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/platform/TestPlatformImplBase.java index 054e4dfb8e..ecc991941f 100644 --- a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/platform/TestPlatformImplBase.java +++ b/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/platform/TestPlatformImplBase.java @@ -19,15 +19,7 @@ * under the License. */ -import java.util.Map; - -import org.apache.commons.beanutils.DynaBean; import org.jumpmind.symmetric.ddl.TestPlatformBase; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaBean; -import org.jumpmind.symmetric.ddl.dynabean.SqlDynaClass; -import org.jumpmind.symmetric.ddl.model.Database; -import org.jumpmind.symmetric.ddl.model.Table; -import org.jumpmind.symmetric.ddl.platform.PlatformImplBase; /** * Tests the {@link org.jumpmind.symmetric.ddl.PlatformImplBase} (abstract) class. @@ -61,23 +53,4 @@ protected String getDatabaseName() return null; } - /** - * Test the toColumnValues method. - */ - public void testToColumnValues() - { - Database database = parseDatabaseFromString(TESTED_MODEL); - PlatformImplBase platform = new TestPlatform(); - Table table = database.getTable(0); - SqlDynaClass clz = SqlDynaClass.newInstance(table); - DynaBean db = new SqlDynaBean(SqlDynaClass.newInstance(table)); - - db.set("name", "name"); - - Map map = platform.toColumnValues(clz.getSqlDynaProperties(), db); - - assertEquals("name", - map.get("name")); - assertTrue(map.containsKey("id")); - } } diff --git a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/util/DatabaseTestHelper.java b/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/util/DatabaseTestHelper.java deleted file mode 100644 index 9774b87752..0000000000 --- a/symmetric/symmetric-ddl/src/test/java/org/jumpmind/symmetric/ddl/util/DatabaseTestHelper.java +++ /dev/null @@ -1,212 +0,0 @@ -package org.jumpmind.symmetric.ddl.util; - -/* - * 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.Collection; -import java.util.Iterator; - -import junit.framework.Assert; -import junit.framework.AssertionFailedError; - -import org.apache.commons.beanutils.DynaBean; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.jumpmind.symmetric.ddl.Platform; -import org.jumpmind.symmetric.ddl.model.Column; -import org.jumpmind.symmetric.ddl.model.Database; -import org.jumpmind.symmetric.ddl.model.Table; - -/** - * Class that provides utility stuff for cpmaring data in databases. - * - * @version $Revision: 264616 $ - */ -public class DatabaseTestHelper extends Assert -{ - /** The log for this class. */ - private final Log _log = LogFactory.getLog(DatabaseTestHelper.class); - - /** - * Asserts that the data in the tables described by the given model is the same in the - * database accessed by the second platform as is in the database accessed by the first platform. - * Note that it is not tested whether the second database has more data.
- * All differences will be printed via logging in DEBUG level. - * - * @param model The database model to check - * @param origDbPlatform The first platform - * @param testedDbPlatform The second platform - */ - public void assertHasSameData(Database model, Platform origDbPlatform, Platform testedDbPlatform) - { - assertHasSameData(null, model, origDbPlatform, testedDbPlatform); - } - - /** - * Asserts that the data in the tables described by the given model is the same in the - * database accessed by the second platform as is in the database accessed by the first platform. - * Note that it is not tested whether the second database has more data.
- * All differences will be printed via logging in DEBUG level. - * - * @param failureMsg The failure message to issue if the data is not the same - * @param model The database model to check - * @param origDbPlatform The first platform - * @param testedDbPlatform The second platform - */ - public void assertHasSameData(String failureMsg, Database model, Platform origDbPlatform, Platform testedDbPlatform) - { - boolean hasError = false; - - for (int idx = 0; idx < model.getTableCount(); idx++) - { - Table table = model.getTable(idx); - Column[] pkCols = table.getPrimaryKeyColumns(); - - for (Iterator it = origDbPlatform.query(model, buildQueryString(origDbPlatform, table, null, null), new Table[] { table }); it.hasNext();) - { - DynaBean obj = (DynaBean)it.next(); - Collection result = testedDbPlatform.fetch(model, buildQueryString(origDbPlatform, table, pkCols, obj), new Table[] { table }); - - if (result.isEmpty()) - { - if (_log.isDebugEnabled()) - { - hasError = true; - _log.debug("Row "+obj.toString()+" is not present in second database"); - } - else - { - throw new AssertionFailedError(failureMsg); - } - } - else if (result.size() > 1) - { - if (_log.isDebugEnabled()) - { - hasError = true; - - StringBuffer debugMsg = new StringBuffer(); - - debugMsg.append("Row "); - debugMsg.append(obj.toString()); - debugMsg.append(" is present more than once in the second database:\n"); - for (Iterator resultIt = result.iterator(); resultIt.hasNext();) - { - debugMsg.append(" "); - debugMsg.append(resultIt.next().toString()); - } - _log.debug(debugMsg.toString()); - } - else - { - throw new AssertionFailedError(failureMsg); - } - } - else - { - DynaBean otherObj = (DynaBean)result.iterator().next(); - - if (!obj.equals(otherObj)) - { - if (_log.isDebugEnabled()) - { - hasError = true; - - _log.debug("Row "+obj.toString()+" is different in the second database: "+otherObj.toString()); - } - else - { - throw new AssertionFailedError(failureMsg); - } - } - } - } - } - if (hasError) - { - throw new AssertionFailedError(failureMsg); - } - } - - /** - * Helper method for build a SELECT statement. - * - * @param targetPlatform The platform for the queried database - * @param table The queried table - * @param whereCols The optional columns that make up the WHERE clause - * @param whereValues The optional column value that make up the WHERE clause - * @return The query string - */ - private String buildQueryString(Platform targetPlatform, Table table, Column[] whereCols, DynaBean whereValues) - { - StringBuffer result = new StringBuffer(); - - result.append("SELECT * FROM "); - if (targetPlatform.isDelimitedIdentifierModeOn()) - { - result.append(targetPlatform.getPlatformInfo().getDelimiterToken()); - } - result.append(table.getName()); - if (targetPlatform.isDelimitedIdentifierModeOn()) - { - result.append(targetPlatform.getPlatformInfo().getDelimiterToken()); - } - if ((whereCols != null) && (whereCols.length > 0)) - { - result.append(" WHERE "); - for (int idx = 0; idx < whereCols.length; idx++) - { - Object value = (whereValues == null ? null : whereValues.get(whereCols[idx].getName())); - - if (idx > 0) - { - result.append(" AND "); - } - if (targetPlatform.isDelimitedIdentifierModeOn()) - { - result.append(targetPlatform.getPlatformInfo().getDelimiterToken()); - } - result.append(whereCols[idx].getName()); - if (targetPlatform.isDelimitedIdentifierModeOn()) - { - result.append(targetPlatform.getPlatformInfo().getDelimiterToken()); - } - result.append(" = "); - if (value == null) - { - result.append("NULL"); - } - else - { - if (!whereCols[idx].isOfNumericType()) - { - result.append(targetPlatform.getPlatformInfo().getValueQuoteToken()); - } - result.append(value.toString()); - if (!whereCols[idx].isOfNumericType()) - { - result.append(targetPlatform.getPlatformInfo().getValueQuoteToken()); - } - } - } - } - - return result.toString(); - } -}