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