001: package liquibase.database.sql;
002:
003: import liquibase.database.CacheDatabase;
004: import liquibase.database.DB2Database;
005: import liquibase.database.Database;
006: import liquibase.database.DerbyDatabase;
007: import liquibase.database.structure.DatabaseSnapshot;
008: import liquibase.test.DatabaseTest;
009: import liquibase.test.DatabaseTestTemplate;
010: import liquibase.test.SqlStatementDatabaseTest;
011: import liquibase.test.TestContext;
012: import static org.junit.Assert.*;
013: import org.junit.Test;
014:
015: public class RenameColumnStatementTest extends AbstractSqlStatementTest {
016: private static final String TABLE_NAME = "RenameColumnTest";
017: private static final String COL_NAME = "testCol";
018: private static final String NEW_COL_NAME = "newColName";
019: private static final String DATA_TYPE = "varchar(50)";
020:
021: protected void setupDatabase(Database database) throws Exception {
022:
023: dropAndCreateTable(new CreateTableStatement(null, TABLE_NAME)
024: .addPrimaryKeyColumn("id", "int").addColumn(COL_NAME,
025: DATA_TYPE), database);
026:
027: dropAndCreateTable(new CreateTableStatement(
028: TestContext.ALT_SCHEMA, TABLE_NAME)
029: .addPrimaryKeyColumn("id", "int").addColumn(COL_NAME,
030: DATA_TYPE), database);
031: }
032:
033: protected SqlStatement generateTestStatement() {
034: return new RenameColumnStatement(null, null, null, null, null);
035: }
036:
037: @Test
038: public void supportsDatabase() throws Exception {
039: new DatabaseTestTemplate()
040: .testOnAllDatabases(new DatabaseTest() {
041: public void performTest(Database database)
042: throws Exception {
043:
044: if (database instanceof DB2Database
045: || database instanceof CacheDatabase
046: || database instanceof DerbyDatabase) {
047: assertFalse(generateTestStatement()
048: .supportsDatabase(database));
049: } else {
050: assertTrue(generateTestStatement()
051: .supportsDatabase(database));
052: }
053: }
054: });
055: }
056:
057: @Test
058: public void execute_defaultSchema() throws Exception {
059: new DatabaseTestTemplate()
060: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
061: null, new RenameColumnStatement(null,
062: TABLE_NAME, COL_NAME, NEW_COL_NAME,
063: DATA_TYPE)) {
064:
065: protected void preExecuteAssert(
066: DatabaseSnapshot snapshot) {
067: assertNotNull(snapshot.getTable(TABLE_NAME)
068: .getColumn(COL_NAME));
069: assertNull(snapshot.getTable(TABLE_NAME)
070: .getColumn(NEW_COL_NAME));
071: }
072:
073: protected void postExecuteAssert(
074: DatabaseSnapshot snapshot) {
075: assertNull(snapshot.getTable(TABLE_NAME)
076: .getColumn(COL_NAME));
077: assertNotNull(snapshot.getTable(TABLE_NAME)
078: .getColumn(NEW_COL_NAME));
079: }
080:
081: });
082: }
083:
084: @Test
085: public void execute_altSchema() throws Exception {
086: new DatabaseTestTemplate()
087: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
088: TestContext.ALT_SCHEMA,
089: new RenameColumnStatement(
090: TestContext.ALT_SCHEMA, TABLE_NAME,
091: COL_NAME, NEW_COL_NAME, DATA_TYPE)) {
092:
093: protected void preExecuteAssert(
094: DatabaseSnapshot snapshot) {
095: assertNotNull(snapshot.getTable(TABLE_NAME)
096: .getColumn(COL_NAME));
097: assertNull(snapshot.getTable(TABLE_NAME)
098: .getColumn(NEW_COL_NAME));
099: }
100:
101: protected void postExecuteAssert(
102: DatabaseSnapshot snapshot) {
103: assertNull(snapshot.getTable(TABLE_NAME)
104: .getColumn(COL_NAME));
105: assertNotNull(snapshot.getTable(TABLE_NAME)
106: .getColumn(NEW_COL_NAME));
107: }
108:
109: });
110: }
111:
112: }
|