01: package liquibase.database.sql;
02:
03: import liquibase.database.Database;
04: import liquibase.database.HsqlDatabase;
05: import liquibase.database.OracleDatabase;
06: import liquibase.database.structure.DatabaseSnapshot;
07: import liquibase.database.structure.View;
08: import liquibase.exception.JDBCException;
09: import liquibase.test.DatabaseTestTemplate;
10: import liquibase.test.SqlStatementDatabaseTest;
11: import liquibase.test.TestContext;
12: import static org.junit.Assert.*;
13: import org.junit.Test;
14:
15: public class CreateViewStatementTest extends AbstractSqlStatementTest {
16:
17: private static final String VIEW_NAME = "CreateViewTest";
18: private static final String TABLE_NAME = "CreateViewTestTable";
19:
20: protected SqlStatement generateTestStatement() {
21: return new CreateViewStatement(null, null, null, false);
22: }
23:
24: protected void setupDatabase(Database database) throws Exception {
25:
26: dropViewIfExists(null, VIEW_NAME, database);
27:
28: dropViewIfExists(TestContext.ALT_SCHEMA, VIEW_NAME, database);
29:
30: dropAndCreateTable(new CreateTableStatement(null, TABLE_NAME)
31: .addPrimaryKeyColumn("id", "int").addColumn("name",
32: "varchar(50)"), database);
33:
34: dropAndCreateTable(new CreateTableStatement(
35: TestContext.ALT_SCHEMA, TABLE_NAME)
36: .addPrimaryKeyColumn("id", "int").addColumn("name",
37: "varchar(50)"), database);
38: }
39:
40: @Test
41: public void execute_defaultSchema() throws Exception {
42: final String definition = "SELECT * FROM " + TABLE_NAME;
43:
44: new DatabaseTestTemplate()
45: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
46: null, new CreateViewStatement(null, VIEW_NAME,
47: definition, false)) {
48: protected void preExecuteAssert(
49: DatabaseSnapshot snapshot) {
50: assertNull(snapshot.getView(VIEW_NAME));
51: }
52:
53: protected void postExecuteAssert(
54: DatabaseSnapshot snapshot) {
55: View view = snapshot.getView(VIEW_NAME);
56: assertNotNull(view);
57: assertEquals(2, view.getColumns().size());
58: }
59:
60: });
61: }
62:
63: @Test
64: public void execute_altSchema() throws Exception {
65: final String definition = "SELECT * FROM "
66: + TestContext.ALT_SCHEMA + "." + TABLE_NAME;
67: new DatabaseTestTemplate()
68: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
69: TestContext.ALT_SCHEMA,
70: new CreateViewStatement(TestContext.ALT_SCHEMA,
71: VIEW_NAME, definition, false)) {
72: protected boolean supportsTest(Database database) {
73: return !(database instanceof HsqlDatabase || database instanceof OracleDatabase); //don't know why oracle isn't working
74: }
75:
76: protected boolean expectedException(
77: Database database, JDBCException exception) {
78: return !database.supportsSchemas();
79: }
80:
81: protected void preExecuteAssert(
82: DatabaseSnapshot snapshot) {
83: assertNull(snapshot.getView(VIEW_NAME));
84: }
85:
86: protected void postExecuteAssert(
87: DatabaseSnapshot snapshot) {
88: View view = snapshot.getView(VIEW_NAME);
89: assertNotNull(view);
90: assertEquals(2, view.getColumns().size());
91: }
92:
93: });
94: }
95: }
|