001: package liquibase.database.sql;
002:
003: import liquibase.database.Database;
004: import liquibase.database.PostgresDatabase;
005: import liquibase.database.template.JdbcTemplate;
006: import liquibase.exception.JDBCException;
007: import liquibase.test.DatabaseTest;
008: import liquibase.test.DatabaseTestTemplate;
009: import liquibase.test.TestContext;
010: import static org.junit.Assert.*;
011: import org.junit.Before;
012: import org.junit.Test;
013:
014: import java.sql.SQLException;
015:
016: public abstract class AbstractSqlStatementTest {
017:
018: @Before
019: public void setupAvailableDatabases() throws Exception {
020: for (Database database : TestContext.getInstance()
021: .getAvailableDatabases()) {
022: if (generateTestStatement().supportsDatabase(database)) {
023: setupDatabase(database);
024: }
025: }
026: }
027:
028: protected abstract void setupDatabase(Database database)
029: throws Exception;
030:
031: protected void dropAndCreateTable(CreateTableStatement statement,
032: Database database) throws SQLException, JDBCException {
033: if (statement.getSchemaName() != null
034: && !database.supportsSchemas()) {
035: return;
036: }
037:
038: dropTableIfExists(statement.getSchemaName(), statement
039: .getTableName(), database);
040:
041: new JdbcTemplate(database).execute(statement);
042:
043: if (!database.getAutoCommitMode()) {
044: database.getConnection().commit();
045: }
046:
047: }
048:
049: protected void dropTableIfExists(String schemaName,
050: String tableName, Database database) throws SQLException {
051: if (schemaName != null && !database.supportsSchemas()) {
052: return;
053: }
054:
055: if (!database.getAutoCommitMode()) {
056: database.getConnection().commit();
057: }
058:
059: try {
060: boolean cascade = false;
061: if (database instanceof PostgresDatabase) {
062: cascade = true;
063: }
064: new JdbcTemplate(database).execute(new DropTableStatement(
065: schemaName, tableName, cascade));
066:
067: if (!database.getAutoCommitMode()) {
068: database.getConnection().commit();
069: }
070:
071: } catch (JDBCException e) {
072: // System.out.println("Error dropping table "+database.escapeTableName(schemaName, tableName)+" on "+database);
073: // e.printStackTrace();
074: if (!database.getConnection().getAutoCommit()) {
075: database.getConnection().rollback();
076: }
077: }
078: }
079:
080: protected void dropAndCreateSequence(
081: CreateSequenceStatement statement, Database database)
082: throws SQLException, JDBCException {
083: if (statement.getSchemaName() != null
084: && !database.supportsSchemas()) {
085: return;
086: }
087:
088: dropSequenceIfExists(statement.getSchemaName(), statement
089: .getSequenceName(), database);
090:
091: new JdbcTemplate(database).execute(statement);
092:
093: if (!database.getAutoCommitMode()) {
094: database.getConnection().commit();
095: }
096:
097: }
098:
099: protected void dropSequenceIfExists(String schemaName,
100: String sequenceName, Database database) throws SQLException {
101: if (schemaName != null && !database.supportsSchemas()) {
102: return;
103: }
104:
105: if (!database.getAutoCommitMode()) {
106: database.getConnection().commit();
107: }
108:
109: try {
110: new JdbcTemplate(database)
111: .execute(new DropSequenceStatement(schemaName,
112: sequenceName));
113:
114: if (!database.getAutoCommitMode()) {
115: database.getConnection().commit();
116: }
117:
118: } catch (JDBCException e) {
119: // System.out.println("Error dropping sequence "+database.escapeSequenceName(schemaName, sequenceName));
120: // e.printStackTrace();
121: if (!database.getConnection().getAutoCommit()) {
122: database.getConnection().rollback();
123: }
124: }
125: if (!database.getAutoCommitMode()) {
126: database.getConnection().commit();
127: }
128:
129: }
130:
131: protected void dropAndCreateView(CreateViewStatement statement,
132: Database database) throws SQLException, JDBCException {
133: if (statement.getSchemaName() != null
134: && !database.supportsSchemas()) {
135: return;
136: }
137:
138: dropViewIfExists(statement.getSchemaName(), statement
139: .getViewName(), database);
140:
141: new JdbcTemplate(database).execute(statement);
142:
143: if (!database.getAutoCommitMode()) {
144: database.getConnection().commit();
145: }
146:
147: }
148:
149: protected void dropViewIfExists(String schemaName, String viewName,
150: Database database) throws SQLException {
151: if (schemaName != null && !database.supportsSchemas()) {
152: return;
153: }
154:
155: if (!database.getAutoCommitMode()) {
156: database.getConnection().commit();
157: }
158:
159: String schema = "";
160: if (schemaName != null) {
161: schema = schemaName + ".";
162: }
163:
164: try {
165: new JdbcTemplate(database).execute(new RawSqlStatement(
166: "drop view " + schema + viewName));
167:
168: if (!database.getAutoCommitMode()) {
169: database.getConnection().commit();
170: }
171:
172: } catch (JDBCException e) {
173: // System.out.println("Cannot drop view "+database.escapeViewName(schemaName, viewName)+" on "+database);
174: // e.printStackTrace();
175: if (!database.getConnection().getAutoCommit()) {
176: database.getConnection().rollback();
177: }
178: }
179:
180: if (!database.getAutoCommitMode()) {
181: database.getConnection().commit();
182: }
183:
184: }
185:
186: protected abstract SqlStatement generateTestStatement();
187:
188: @Test
189: public void supportsDatabase() throws Exception {
190: new DatabaseTestTemplate()
191: .testOnAllDatabases(new DatabaseTest() {
192: public void performTest(Database database)
193: throws Exception {
194: assertTrue(generateTestStatement()
195: .supportsDatabase(database));
196: }
197: });
198: }
199:
200: @Test
201: public void getEndDelimiter() throws Exception {
202: new DatabaseTestTemplate()
203: .testOnAllDatabases(new DatabaseTest() {
204: public void performTest(Database database)
205: throws Exception {
206: assertEquals(";", generateTestStatement()
207: .getEndDelimiter(database));
208: }
209: });
210: }
211: }
|