001: package liquibase.database.sql;
002:
003: import liquibase.database.Database;
004: import liquibase.database.structure.DatabaseSnapshot;
005: import liquibase.exception.JDBCException;
006: import liquibase.test.DatabaseTestTemplate;
007: import liquibase.test.SqlStatementDatabaseTest;
008: import liquibase.test.TestContext;
009: import static org.junit.Assert.*;
010: import org.junit.Test;
011:
012: public class DropForeignKeyConstraintStatementTest extends
013: AbstractSqlStatementTest {
014:
015: private static final String TABLE_NAME = "DropFKTest";
016: private static final String FK_TABLE_NAME = "DropFKTestFK";
017: private static final String CONSTRAINT_NAME = "fk_droptest";
018: private static final String ALT_SCHEMA_NAME = "ALT"
019: + CONSTRAINT_NAME;
020:
021: protected void setupDatabase(Database database) throws Exception {
022: dropTableIfExists(null, TABLE_NAME, database);
023:
024: dropAndCreateTable(
025: new CreateTableStatement(null, FK_TABLE_NAME)
026: .addPrimaryKeyColumn("id", "int").addColumn(
027: "name", "varchar(50)"), database);
028:
029: dropAndCreateTable(new CreateTableStatement(null, TABLE_NAME)
030: .addPrimaryKeyColumn("id", "int").addColumn(
031: "test_id",
032: "int",
033: new ForeignKeyConstraint(CONSTRAINT_NAME,
034: FK_TABLE_NAME + "(id)")).addColumn(
035: "otherCol", "varchar(50)"), database);
036:
037: dropTableIfExists(TestContext.ALT_SCHEMA, TABLE_NAME, database);
038:
039: dropAndCreateTable(new CreateTableStatement(
040: TestContext.ALT_SCHEMA, FK_TABLE_NAME)
041: .addPrimaryKeyColumn("id", "int").addColumn("name",
042: "varchar(50)"), database);
043:
044: dropAndCreateTable(new CreateTableStatement(
045: TestContext.ALT_SCHEMA, TABLE_NAME)
046: .addPrimaryKeyColumn("id", "int").addColumn(
047: "test_id",
048: "int",
049: new ForeignKeyConstraint(ALT_SCHEMA_NAME,
050: TestContext.ALT_SCHEMA + "."
051: + FK_TABLE_NAME + "(id)"))
052: .addColumn("otherCol", "varchar(50)"), database);
053: }
054:
055: protected DropForeignKeyConstraintStatement generateTestStatement() {
056: return new DropForeignKeyConstraintStatement(null, null, null);
057: }
058:
059: @Test
060: public void execute_defaultSchema() throws Exception {
061: new DatabaseTestTemplate()
062: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
063: null, new DropForeignKeyConstraintStatement(
064: null, TABLE_NAME, CONSTRAINT_NAME)) {
065:
066: protected void preExecuteAssert(
067: DatabaseSnapshot snapshot) {
068: assertNotNull(snapshot
069: .getForeignKey(CONSTRAINT_NAME));
070: }
071:
072: protected void postExecuteAssert(
073: DatabaseSnapshot snapshot) {
074: assertNull(snapshot
075: .getForeignKey(CONSTRAINT_NAME));
076: }
077:
078: });
079: }
080:
081: @Test
082: public void execute_altSchema() throws Exception {
083: new DatabaseTestTemplate().testOnAvailableDatabases(
084:
085: new SqlStatementDatabaseTest(TestContext.ALT_SCHEMA,
086: new DropForeignKeyConstraintStatement(
087: TestContext.ALT_SCHEMA, TABLE_NAME,
088: ALT_SCHEMA_NAME)) {
089:
090: protected boolean expectedException(Database database,
091: JDBCException exception) {
092: return !database.supportsSchemas();
093: }
094:
095: protected void preExecuteAssert(DatabaseSnapshot snapshot) {
096: //fk constraint is not stored in the alt schema, how can we best test it?
097: // assertNotNull(snapshot.getForeignKey(ALT_SCHEMA_NAME));
098: }
099:
100: protected void postExecuteAssert(DatabaseSnapshot snapshot) {
101: //fk constraint is not stored in the alt schema, how can we best test it?
102: // assertNull(snapshot.getForeignKey(ALT_SCHEMA_NAME));
103: }
104:
105: });
106: }
107: }
|