01: package liquibase.test;
02:
03: import liquibase.database.Database;
04: import liquibase.database.sql.SqlStatement;
05: import liquibase.database.structure.DatabaseSnapshot;
06: import liquibase.database.template.JdbcTemplate;
07: import liquibase.exception.JDBCException;
08: import liquibase.exception.StatementNotSupportedOnDatabaseException;
09:
10: public abstract class SqlStatementDatabaseTest implements DatabaseTest {
11:
12: private String schema = null;
13: private SqlStatement statement;
14:
15: protected SqlStatementDatabaseTest(String schema,
16: SqlStatement statement) {
17: this .schema = schema;
18: this .statement = statement;
19: }
20:
21: protected void setup(Database database) throws JDBCException {
22: // try {
23: // database.getConnection().commit();
24: // } catch (SQLException e) {
25: // throw new JDBCException(e);
26: // }
27:
28: }
29:
30: protected boolean supportsTest(Database database) {
31: return true;
32: }
33:
34: protected boolean expectedException(Database database,
35: JDBCException exception) {
36: if (schema != null && !database.supportsSchemas()) {
37: if (exception instanceof StatementNotSupportedOnDatabaseException) {
38: return true;
39: }
40: }
41: return false;
42: }
43:
44: protected abstract void preExecuteAssert(DatabaseSnapshot snapshot)
45: throws Exception;
46:
47: protected abstract void postExecuteAssert(DatabaseSnapshot snapshot)
48: throws Exception;
49:
50: public final void performTest(Database database) throws Exception {
51: if (!supportsTest(database)) {
52: return;
53: }
54:
55: if (!statement.supportsDatabase(database)) {
56: try {
57: statement.getSqlStatement(database);
58: org.junit.Assert.fail("did not throw exception");
59: } catch (StatementNotSupportedOnDatabaseException e) {
60: return; //what we expected
61: }
62: }
63:
64: setup(database);
65: DatabaseSnapshot snapshot = new DatabaseSnapshot(database,
66: schema);
67: preExecuteAssert(snapshot);
68:
69: try {
70: new JdbcTemplate(database).execute(statement);
71: database.getConnection().commit();
72: } catch (JDBCException e) {
73: boolean expectsException = expectedException(database, e);
74: if (expectsException) {
75: return; //what we wanted
76: } else {
77: throw e;
78: }
79: }
80:
81: snapshot = new DatabaseSnapshot(database, schema);
82:
83: postExecuteAssert(snapshot);
84: }
85: }
|