ALF-11370: ExportDbTest expects particular/PostgreSQL schema

* Refactored to allow easier addition of DBMS-specific test implementation.
* Added MySQL test implementation.



git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@32271 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Matt Ward
2011-11-24 12:38:24 +00:00
parent 73f27af3fc
commit f526d05094
6 changed files with 952 additions and 555 deletions

View File

@@ -37,7 +37,6 @@ import org.alfresco.util.schemacomp.model.Schema;
import org.alfresco.util.schemacomp.model.Sequence; import org.alfresco.util.schemacomp.model.Sequence;
import org.alfresco.util.schemacomp.model.Table; import org.alfresco.util.schemacomp.model.Table;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.dialect.TypeNames; import org.hibernate.dialect.TypeNames;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@@ -130,7 +129,7 @@ public class ExportDb
final Map<Integer, String> forwardMap2 = (Map<Integer, String>) defaultsField.get(typeNames); final Map<Integer, String> forwardMap2 = (Map<Integer, String>) defaultsField.get(typeNames);
for (final Map.Entry<Integer, String> e : forwardMap2.entrySet()) for (final Map.Entry<Integer, String> e : forwardMap2.entrySet())
{ {
this.reverseTypeMap.put(e.getValue(), e.getKey()); this.reverseTypeMap.put(e.getValue().toLowerCase(), e.getKey());
} }
final Field weightedField = TypeNames.class.getDeclaredField("weighted"); final Field weightedField = TypeNames.class.getDeclaredField("weighted");
@@ -141,7 +140,7 @@ public class ExportDb
{ {
for (final String type : e.getValue().values()) for (final String type : e.getValue().values())
{ {
this.reverseTypeMap.put(type, e.getKey()); this.reverseTypeMap.put(type.toLowerCase(), e.getKey());
} }
} }
} }
@@ -378,27 +377,26 @@ public class ExportDb
} }
protected String generateType(final String dbType, int size, final int digits, int sqlType) protected String generateType(final String dbTypeRaw, int size, final int digits, int sqlType)
throws IllegalArgumentException, IllegalAccessException throws IllegalArgumentException, IllegalAccessException
{ {
String dbName = dbType.toLowerCase() + "(" + size + ")"; final String dbType = dbTypeRaw.toLowerCase();
if (this.reverseTypeMap.containsKey(dbName))
final String sizeType = dbType + "(" + size + ")";
if (this.reverseTypeMap.containsKey(sizeType))
{ {
// the map may contain an exact match, e.g. "char(1)" // the map may contain an exact match, e.g. "char(1)"
return dbName; return sizeType;
} }
dbName = dbType.toLowerCase() + "(" + size + ", " + digits + ")"; final String precisionScaleType = dbType + "(" + size + ", " + digits + ")";
if (this.reverseTypeMap.containsKey(dbName)) if (this.reverseTypeMap.containsKey(precisionScaleType))
{ {
// the map may contain an exact match, e.g. "numeric(3, 1)" // the map may contain an exact match, e.g. "numeric(3, 1)"
return dbName; return precisionScaleType;
} }
else else
{ {
String precisionScaleType = dbType + "(" + size + ", " + digits + ")";
String sizeType = dbType + "(" + size + ")";
for (String key : reverseTypeMap.keySet()) for (String key : reverseTypeMap.keySet())
{ {
// Populate the placeholders, examples: // Populate the placeholders, examples:
@@ -451,27 +449,4 @@ public class ExportDb
{ {
this.namePrefix = namePrefix; this.namePrefix = namePrefix;
} }
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException
{
ExportDb exportDb = null;
try
{
exportDb = new ExportDb(null, new PostgreSQLDialect());
}
catch (Exception error)
{
throw new RuntimeException(error);
}
if (exportDb != null)
{
String varchar = exportDb.generateType("varchar", 20, 0, 12);
System.out.println("varchar: " + varchar);
String int4 = exportDb.generateType("int4", 20, 0, 4);
System.out.println("int4: " + int4);
}
}
} }

View File

@@ -19,38 +19,30 @@
package org.alfresco.util.schemacomp; package org.alfresco.util.schemacomp;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import java.util.Iterator;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.alfresco.util.ApplicationContextHelper; import org.alfresco.util.ApplicationContextHelper;
import org.alfresco.util.schemacomp.model.Column;
import org.alfresco.util.schemacomp.model.DbObject;
import org.alfresco.util.schemacomp.model.ForeignKey;
import org.alfresco.util.schemacomp.model.Index;
import org.alfresco.util.schemacomp.model.PrimaryKey;
import org.alfresco.util.schemacomp.model.Schema; import org.alfresco.util.schemacomp.model.Schema;
import org.alfresco.util.schemacomp.model.Sequence; import org.alfresco.util.schemacomp.test.exportdb.AbstractExportTester;
import org.alfresco.util.schemacomp.model.Table; import org.alfresco.util.schemacomp.test.exportdb.MySQLDialectExportTester;
import org.alfresco.util.schemacomp.test.exportdb.PostgreSQLDialectExportTester;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.PostgreSQLDialect; import org.hibernate.dialect.PostgreSQLDialect;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
/** /**
* Tests for the ExportDb class. * Tests for the ExportDb class. Loads the database into an in-memory {@link Schema} representation.
* <p>
* This test is DBMS specific, if the test is run on a system configured against MySQL for example,
* it will run MySQL specific tests. If there is no test available for the configured DBMS then
* the test will pass - this allows addition of new DBMS-specific tests when available.
* *
* @author Matt Ward * @author Matt Ward
*/ */
@@ -62,6 +54,9 @@ public class ExportDbTest
private DataSource dataSource; private DataSource dataSource;
private SimpleJdbcTemplate jdbcTemplate; private SimpleJdbcTemplate jdbcTemplate;
private PlatformTransactionManager tx; private PlatformTransactionManager tx;
private AbstractExportTester exportTester;
private static final Log logger = LogFactory.getLog(ExportDbTest.class);
@Before @Before
public void setUp() throws Exception public void setUp() throws Exception
@@ -79,275 +74,23 @@ public class ExportDbTest
@Test @Test
public void exportDb() throws Exception public void exportDb() throws Exception
{ {
if (dialect.getClass().equals(PostgreSQLDialect.class)) Class dialectClass = dialect.getClass();
if (logger.isInfoEnabled())
{ {
exportPostgreSQL(); logger.info("Using dialect class " + dialectClass.getName());
} }
}
if (PostgreSQLDialect.class.isAssignableFrom(dialectClass))
private void exportPostgreSQL() throws Exception
{
setupPostgreSQL();
exporter.execute();
Schema schema = exporter.getSchema();
assertNull("Schema shouldn't have a parent", schema.getParent());
System.out.println(schema);
Table exampleTable = null;
Table otherTable = null;
Sequence exampleSeq = null;
for (DbObject dbo : schema)
{
if (dbo.getName().equals("export_test_example"))
{
exampleTable = (Table) dbo;
}
if (dbo.getName().equals("export_test_other"))
{
otherTable = (Table) dbo;
}
if (dbo.getName().equals("export_test_example_seq"))
{
exampleSeq = (Sequence) dbo;
}
}
checkResultsFiltered(schema, "export_test_");
checkExampleTable(schema, exampleTable);
checkOtherTable(schema, otherTable);
checkExampleSequence(schema, exampleSeq);
}
private void setupPostgreSQL()
{
// Create database objects: this decouples test code from the actual schema which is
// free to change without breaking these tests.
final String[] createStatements = new String[]
{ {
"DROP TABLE IF EXISTS export_test_example CASCADE", exportTester = new PostgreSQLDialectExportTester(exporter, tx, jdbcTemplate);
"CREATE TABLE export_test_example" + }
" (" + else if (MySQLDialect.class.isAssignableFrom(dialectClass))
" id INT8 NOT NULL," + {
" description VARCHAR(1024)," + exportTester = new MySQLDialectExportTester(exporter, tx, jdbcTemplate);
" fixes_from_schema INT4," +
" fixes_to_schema INT4," +
" applied_to_schema INT4," +
" target_schema INT4," +
" applied_on_date TIMESTAMP," +
" applied_to_server VARCHAR(64)," +
" was_executed BOOL," +
" succeeded BOOL," +
" report VARCHAR(1024)," +
" PRIMARY KEY (id)" +
" )",
"DROP TABLE IF EXISTS export_test_other CASCADE",
"CREATE TABLE export_test_other" +
" (" +
" id INT8 NOT NULL," +
" version INT8 NOT NULL," +
" ex_id INT8 NOT NULL," +
" local_name VARCHAR(200) NOT NULL," +
" CONSTRAINT export_test_fk_example FOREIGN KEY (ex_id) REFERENCES export_test_example (id)," +
" PRIMARY KEY (id)" +
" )",
"DROP INDEX IF EXISTS export_test_idx_other_1",
"CREATE UNIQUE INDEX export_test_idx_other_1 ON export_test_other (ex_id, local_name)",
"DROP INDEX IF EXISTS export_test_idx_other_2",
"CREATE INDEX export_test_idx_other_2 ON export_test_other (ex_id)",
"DROP SEQUENCE IF EXISTS export_test_example_seq",
"CREATE SEQUENCE export_test_example_seq START WITH 1 INCREMENT BY 1"
};
TransactionTemplate tt = new TransactionTemplate(tx);
tt.execute(new TransactionCallbackWithoutResult()
{
@Override
protected void doInTransactionWithoutResult(TransactionStatus status)
{
for (String sql : createStatements)
{
jdbcTemplate.update(sql);
}
}
});
}
/**
* Check that all top level database objects are prefixed as expected
* (no other objects should have been retrieved)
*
* @param schema
* @param prefix
*/
private void checkResultsFiltered(Schema schema, String prefix)
{
for (DbObject dbo : schema)
{
if (!dbo.getName().startsWith(prefix))
{
fail("Database object's name does not start with '" + prefix + "': " + dbo);
}
} }
}
// Run the DBMS specific tests.
private void checkOtherTable(Schema schema, Table otherTable) exportTester.runExportTest();
{
assertNotNull("Couldn't find table export_test_other", otherTable);
assertSame("Incorrect parent or no parent set", schema, otherTable.getParent());
Iterator<Column> colIt = otherTable.getColumns().iterator();
Column col = colIt.next();
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertEquals("id", col.getName());
assertEquals("int8", col.getType());
assertEquals(false, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertEquals("version", col.getName());
assertEquals("int8", col.getType());
assertEquals(false, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertEquals("ex_id", col.getName());
assertEquals("int8", col.getType());
assertEquals(false, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertEquals("local_name", col.getName());
assertEquals("varchar(200)", col.getType());
assertEquals(false, col.isNullable());
assertEquals(2, otherTable.getIndexes().size());
Iterator<Index> indexIt = otherTable.getIndexes().iterator();
Index index = indexIt.next();
assertSame("Incorrect parent or no parent set", otherTable, index.getParent());
assertEquals("export_test_idx_other_1", index.getName());
assertEquals(true, index.isUnique());
assertEquals(2, index.getColumnNames().size());
assertEquals("ex_id", index.getColumnNames().get(0));
assertEquals("local_name", index.getColumnNames().get(1));
index = indexIt.next();
assertSame("Incorrect parent or no parent set", otherTable, index.getParent());
assertEquals("export_test_idx_other_2", index.getName());
assertEquals(1, index.getColumnNames().size());
assertEquals("ex_id", index.getColumnNames().get(0));
PrimaryKey pk = otherTable.getPrimaryKey();
assertSame("Incorrect parent or no parent set", otherTable, pk.getParent());
assertEquals("id", pk.getColumnNames().get(0));
assertEquals(1, otherTable.getForeignKeys().size());
ForeignKey fk = otherTable.getForeignKeys().get(0);
assertSame("Incorrect parent or no parent set", otherTable, fk.getParent());
assertEquals("export_test_fk_example", fk.getName());
assertEquals("ex_id", fk.getLocalColumn());
assertEquals("export_test_example", fk.getTargetTable());
assertEquals("id", fk.getTargetColumn());
}
private void checkExampleTable(Schema schema, Table exampleTable)
{
assertNotNull("Couldn't find export_test_example", exampleTable);
assertSame("Incorrect parent or no parent set", schema, exampleTable.getParent());
assertEquals("export_test_example", exampleTable.getName());
Iterator<Column> colIt = exampleTable.getColumns().iterator();
Column col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("id", col.getName());
assertEquals("int8", col.getType());
assertEquals(false, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("description", col.getName());
assertEquals("varchar(1024)", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("fixes_from_schema", col.getName());
assertEquals("int4", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("fixes_to_schema", col.getName());
assertEquals("int4", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("applied_to_schema", col.getName());
assertEquals("int4", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("target_schema", col.getName());
assertEquals("int4", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("applied_on_date", col.getName());
assertEquals("timestamp", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("applied_to_server", col.getName());
assertEquals("varchar(64)", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("was_executed", col.getName());
assertEquals("bool", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("succeeded", col.getName());
assertEquals("bool", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("report", col.getName());
assertEquals("varchar(1024)", col.getType());
assertEquals(true, col.isNullable());
PrimaryKey pk = exampleTable.getPrimaryKey();
assertSame("Incorrect parent or no parent set", exampleTable, pk.getParent());
assertEquals("id", pk.getColumnNames().get(0));
}
public void checkExampleSequence(Schema schema, Sequence seq)
{
assertNotNull("Couldn't find sequence", seq);
assertSame("Incorrect parent or no parent set", schema, seq.getParent());
assertEquals("export_test_example_seq", seq.getName());
} }
} }

View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.util.schemacomp.test.exportdb;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import org.alfresco.util.schemacomp.ExportDb;
import org.alfresco.util.schemacomp.model.DbObject;
import org.alfresco.util.schemacomp.model.Schema;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
/**
* Base class for DBMS-specific ExportDb tests.
*
* @author Matt Ward
*/
public abstract class AbstractExportTester
{
protected ExportDb exporter;
protected PlatformTransactionManager tx;
protected SimpleJdbcTemplate jdbcTemplate;
public AbstractExportTester(ExportDb exporter, PlatformTransactionManager tx, SimpleJdbcTemplate jdbcTemplate)
{
this.exporter = exporter;
this.tx = tx;
this.jdbcTemplate = jdbcTemplate;
}
protected abstract void doExportTest() throws Exception;
protected abstract void doDatabaseSetup();
public void runExportTest() throws Exception
{
doDatabaseSetup();
exporter.execute();
// Dump the schema for diagnostics
System.out.println(getSchema());
commonPostExportChecks();
doExportTest();
}
/**
* Common checks that do not need to be coded into every test implementation.
* May be overridden if required.
*/
protected void commonPostExportChecks()
{
Schema schema = getSchema();
assertNull("Schema shouldn't have a parent", getSchema().getParent());
checkResultsFiltered(schema, "export_test_");
}
public Schema getSchema()
{
return exporter.getSchema();
}
/**
* Check that all top level database objects are prefixed as expected
* (no other objects should have been retrieved)
*
* @param schema
* @param prefix
*/
protected void checkResultsFiltered(Schema schema, String prefix)
{
for (DbObject dbo : schema)
{
if (!dbo.getName().startsWith(prefix))
{
fail("Database object's name does not start with '" + prefix + "': " + dbo);
}
}
}
}

View File

@@ -0,0 +1,283 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.util.schemacomp.test.exportdb;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import java.util.Iterator;
import org.alfresco.util.schemacomp.ExportDb;
import org.alfresco.util.schemacomp.model.Column;
import org.alfresco.util.schemacomp.model.DbObject;
import org.alfresco.util.schemacomp.model.ForeignKey;
import org.alfresco.util.schemacomp.model.Index;
import org.alfresco.util.schemacomp.model.PrimaryKey;
import org.alfresco.util.schemacomp.model.Schema;
import org.alfresco.util.schemacomp.model.Table;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
/**
* MySQL specific test for the ExportDb class.
*
* @author Matt Ward
*/
public class MySQLDialectExportTester extends AbstractExportTester
{
/**
* Constructor.
*
* @param exporter
* @param tx
* @param jdbcTemplate
*/
public MySQLDialectExportTester(ExportDb exporter, PlatformTransactionManager tx,
SimpleJdbcTemplate jdbcTemplate)
{
super(exporter, tx, jdbcTemplate);
}
@Override
protected void doExportTest() throws Exception
{
Schema schema = getSchema();
Table exampleTable = null;
Table otherTable = null;
for (DbObject dbo : schema)
{
if (dbo.getName().equals("export_test_example"))
{
exampleTable = (Table) dbo;
}
if (dbo.getName().equals("export_test_other"))
{
otherTable = (Table) dbo;
}
}
checkExampleTable(schema, exampleTable);
checkOtherTable(schema, otherTable);
}
private void checkOtherTable(Schema schema, Table otherTable)
{
assertNotNull("Couldn't find table export_test_other", otherTable);
assertSame("Incorrect parent or no parent set", schema, otherTable.getParent());
Iterator<Column> colIt = otherTable.getColumns().iterator();
Column col = colIt.next();
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertEquals("id", col.getName());
assertEquals("bigint", col.getType());
assertEquals(false, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertEquals("version", col.getName());
assertEquals("bigint", col.getType());
assertEquals(false, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertEquals("ex_id", col.getName());
assertEquals("bigint", col.getType());
assertEquals(false, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertEquals("local_name", col.getName());
assertEquals("varchar(200)", col.getType());
assertEquals(false, col.isNullable());
assertEquals(2, otherTable.getIndexes().size());
Iterator<Index> indexIt = otherTable.getIndexes().iterator();
Index index = indexIt.next();
assertSame("Incorrect parent or no parent set", otherTable, index.getParent());
assertEquals("export_test_idx_other_1", index.getName());
assertEquals(true, index.isUnique());
assertEquals(2, index.getColumnNames().size());
assertEquals("ex_id", index.getColumnNames().get(0));
assertEquals("local_name", index.getColumnNames().get(1));
index = indexIt.next();
assertSame("Incorrect parent or no parent set", otherTable, index.getParent());
assertEquals("export_test_idx_other_2", index.getName());
assertEquals(1, index.getColumnNames().size());
assertEquals("ex_id", index.getColumnNames().get(0));
PrimaryKey pk = otherTable.getPrimaryKey();
assertSame("Incorrect parent or no parent set", otherTable, pk.getParent());
assertEquals("id", pk.getColumnNames().get(0));
assertEquals(1, otherTable.getForeignKeys().size());
ForeignKey fk = otherTable.getForeignKeys().get(0);
assertSame("Incorrect parent or no parent set", otherTable, fk.getParent());
assertEquals("export_test_fk_example", fk.getName());
assertEquals("ex_id", fk.getLocalColumn());
assertEquals("export_test_example", fk.getTargetTable());
assertEquals("id", fk.getTargetColumn());
}
private void checkExampleTable(Schema schema, Table exampleTable)
{
assertNotNull("Couldn't find export_test_example", exampleTable);
assertSame("Incorrect parent or no parent set", schema, exampleTable.getParent());
assertEquals("export_test_example", exampleTable.getName());
Iterator<Column> colIt = exampleTable.getColumns().iterator();
Column col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("id", col.getName());
assertEquals("bigint", col.getType());
assertEquals(false, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("description", col.getName());
assertEquals("text", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("fixes_from_schema", col.getName());
assertEquals("int", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("fixes_to_schema", col.getName());
assertEquals("int", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("applied_to_schema", col.getName());
assertEquals("int", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("target_schema", col.getName());
assertEquals("int", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("applied_on_date", col.getName());
assertEquals("datetime", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("applied_to_server", col.getName());
assertEquals("varchar(64)", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("was_executed", col.getName());
assertEquals("bit", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("succeeded", col.getName());
assertEquals("bit", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("report", col.getName());
assertEquals("text", col.getType());
assertEquals(true, col.isNullable());
PrimaryKey pk = exampleTable.getPrimaryKey();
assertSame("Incorrect parent or no parent set", exampleTable, pk.getParent());
assertEquals("id", pk.getColumnNames().get(0));
}
@Override
protected void doDatabaseSetup()
{
// Create database objects: this decouples test code from the actual schema which is
// free to change without breaking these tests.
final String[] createStatements = new String[]
{
"DROP TABLE IF EXISTS export_test_other CASCADE",
"DROP TABLE IF EXISTS export_test_example CASCADE",
"CREATE TABLE export_test_example" +
" (" +
" id BIGINT NOT NULL AUTO_INCREMENT," +
" description TEXT," +
" fixes_from_schema INTEGER," +
" fixes_to_schema INTEGER," +
" applied_to_schema INTEGER," +
" target_schema INTEGER," +
" applied_on_date DATETIME," +
" applied_to_server VARCHAR(64)," +
" was_executed BIT," +
" succeeded BIT," +
" report TEXT," +
" PRIMARY KEY (id)" +
" ) ENGINE=InnoDB",
"CREATE TABLE export_test_other" +
" (" +
" id BIGINT NOT NULL AUTO_INCREMENT," +
" version BIGINT NOT NULL," +
" ex_id BIGINT NOT NULL," +
" local_name VARCHAR(200) NOT NULL," +
" CONSTRAINT export_test_fk_example FOREIGN KEY (ex_id) REFERENCES export_test_example (id)," +
" PRIMARY KEY (id)" +
" ) ENGINE=InnoDB",
"CREATE UNIQUE INDEX export_test_idx_other_1 ON export_test_other (ex_id, local_name)",
"CREATE INDEX export_test_idx_other_2 ON export_test_other (ex_id)"
};
TransactionTemplate tt = new TransactionTemplate(tx);
tt.execute(new TransactionCallbackWithoutResult()
{
@Override
protected void doInTransactionWithoutResult(TransactionStatus status)
{
for (String sql : createStatements)
{
jdbcTemplate.update(sql);
}
}
});
}
}

View File

@@ -0,0 +1,299 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.util.schemacomp.test.exportdb;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import java.util.Iterator;
import org.alfresco.util.schemacomp.ExportDb;
import org.alfresco.util.schemacomp.model.Column;
import org.alfresco.util.schemacomp.model.DbObject;
import org.alfresco.util.schemacomp.model.ForeignKey;
import org.alfresco.util.schemacomp.model.Index;
import org.alfresco.util.schemacomp.model.PrimaryKey;
import org.alfresco.util.schemacomp.model.Schema;
import org.alfresco.util.schemacomp.model.Sequence;
import org.alfresco.util.schemacomp.model.Table;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
/**
* Test implementation for the PostgreSQL database dialect.
*
* @author Matt Ward
*/
public class PostgreSQLDialectExportTester extends AbstractExportTester
{
/**
* Constructor.
*
* @param exporter
* @param jdbcTemplate
* @param tx
*/
public PostgreSQLDialectExportTester(ExportDb exporter, PlatformTransactionManager tx, SimpleJdbcTemplate jdbcTemplate)
{
super(exporter, tx, jdbcTemplate);
}
@Override
protected void doExportTest() throws Exception
{
Schema schema = getSchema();
Table exampleTable = null;
Table otherTable = null;
Sequence exampleSeq = null;
for (DbObject dbo : schema)
{
if (dbo.getName().equals("export_test_example"))
{
exampleTable = (Table) dbo;
}
if (dbo.getName().equals("export_test_other"))
{
otherTable = (Table) dbo;
}
if (dbo.getName().equals("export_test_example_seq"))
{
exampleSeq = (Sequence) dbo;
}
}
checkExampleTable(schema, exampleTable);
checkOtherTable(schema, otherTable);
checkExampleSequence(schema, exampleSeq);
}
private void checkOtherTable(Schema schema, Table otherTable)
{
assertNotNull("Couldn't find table export_test_other", otherTable);
assertSame("Incorrect parent or no parent set", schema, otherTable.getParent());
Iterator<Column> colIt = otherTable.getColumns().iterator();
Column col = colIt.next();
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertEquals("id", col.getName());
assertEquals("int8", col.getType());
assertEquals(false, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertEquals("version", col.getName());
assertEquals("int8", col.getType());
assertEquals(false, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertEquals("ex_id", col.getName());
assertEquals("int8", col.getType());
assertEquals(false, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertSame("Incorrect parent or no parent set", otherTable, col.getParent());
assertEquals("local_name", col.getName());
assertEquals("varchar(200)", col.getType());
assertEquals(false, col.isNullable());
assertEquals(2, otherTable.getIndexes().size());
Iterator<Index> indexIt = otherTable.getIndexes().iterator();
Index index = indexIt.next();
assertSame("Incorrect parent or no parent set", otherTable, index.getParent());
assertEquals("export_test_idx_other_1", index.getName());
assertEquals(true, index.isUnique());
assertEquals(2, index.getColumnNames().size());
assertEquals("ex_id", index.getColumnNames().get(0));
assertEquals("local_name", index.getColumnNames().get(1));
index = indexIt.next();
assertSame("Incorrect parent or no parent set", otherTable, index.getParent());
assertEquals("export_test_idx_other_2", index.getName());
assertEquals(1, index.getColumnNames().size());
assertEquals("ex_id", index.getColumnNames().get(0));
PrimaryKey pk = otherTable.getPrimaryKey();
assertSame("Incorrect parent or no parent set", otherTable, pk.getParent());
assertEquals("id", pk.getColumnNames().get(0));
assertEquals(1, otherTable.getForeignKeys().size());
ForeignKey fk = otherTable.getForeignKeys().get(0);
assertSame("Incorrect parent or no parent set", otherTable, fk.getParent());
assertEquals("export_test_fk_example", fk.getName());
assertEquals("ex_id", fk.getLocalColumn());
assertEquals("export_test_example", fk.getTargetTable());
assertEquals("id", fk.getTargetColumn());
}
private void checkExampleTable(Schema schema, Table exampleTable)
{
assertNotNull("Couldn't find export_test_example", exampleTable);
assertSame("Incorrect parent or no parent set", schema, exampleTable.getParent());
assertEquals("export_test_example", exampleTable.getName());
Iterator<Column> colIt = exampleTable.getColumns().iterator();
Column col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("id", col.getName());
assertEquals("int8", col.getType());
assertEquals(false, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("description", col.getName());
assertEquals("varchar(1024)", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("fixes_from_schema", col.getName());
assertEquals("int4", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("fixes_to_schema", col.getName());
assertEquals("int4", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("applied_to_schema", col.getName());
assertEquals("int4", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("target_schema", col.getName());
assertEquals("int4", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("applied_on_date", col.getName());
assertEquals("timestamp", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("applied_to_server", col.getName());
assertEquals("varchar(64)", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("was_executed", col.getName());
assertEquals("bool", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("succeeded", col.getName());
assertEquals("bool", col.getType());
assertEquals(true, col.isNullable());
col = colIt.next();
assertSame("Incorrect parent or no parent set", exampleTable, col.getParent());
assertEquals("report", col.getName());
assertEquals("varchar(1024)", col.getType());
assertEquals(true, col.isNullable());
PrimaryKey pk = exampleTable.getPrimaryKey();
assertSame("Incorrect parent or no parent set", exampleTable, pk.getParent());
assertEquals("id", pk.getColumnNames().get(0));
}
public void checkExampleSequence(Schema schema, Sequence seq)
{
assertNotNull("Couldn't find sequence", seq);
assertSame("Incorrect parent or no parent set", schema, seq.getParent());
assertEquals("export_test_example_seq", seq.getName());
}
@Override
protected void doDatabaseSetup()
{
// Create database objects: this decouples test code from the actual schema which is
// free to change without breaking these tests.
final String[] createStatements = new String[]
{
"DROP TABLE IF EXISTS export_test_example CASCADE",
"CREATE TABLE export_test_example" +
" (" +
" id INT8 NOT NULL," +
" description VARCHAR(1024)," +
" fixes_from_schema INT4," +
" fixes_to_schema INT4," +
" applied_to_schema INT4," +
" target_schema INT4," +
" applied_on_date TIMESTAMP," +
" applied_to_server VARCHAR(64)," +
" was_executed BOOL," +
" succeeded BOOL," +
" report VARCHAR(1024)," +
" PRIMARY KEY (id)" +
" )",
"DROP TABLE IF EXISTS export_test_other CASCADE",
"CREATE TABLE export_test_other" +
" (" +
" id INT8 NOT NULL," +
" version INT8 NOT NULL," +
" ex_id INT8 NOT NULL," +
" local_name VARCHAR(200) NOT NULL," +
" CONSTRAINT export_test_fk_example FOREIGN KEY (ex_id) REFERENCES export_test_example (id)," +
" PRIMARY KEY (id)" +
" )",
"DROP INDEX IF EXISTS export_test_idx_other_1",
"CREATE UNIQUE INDEX export_test_idx_other_1 ON export_test_other (ex_id, local_name)",
"DROP INDEX IF EXISTS export_test_idx_other_2",
"CREATE INDEX export_test_idx_other_2 ON export_test_other (ex_id)",
"DROP SEQUENCE IF EXISTS export_test_example_seq",
"CREATE SEQUENCE export_test_example_seq START WITH 1 INCREMENT BY 1"
};
TransactionTemplate tt = new TransactionTemplate(tx);
tt.execute(new TransactionCallbackWithoutResult()
{
@Override
protected void doInTransactionWithoutResult(TransactionStatus status)
{
for (String sql : createStatements)
{
jdbcTemplate.update(sql);
}
}
});
}
}