01: package liquibase.test;
02:
03: import liquibase.database.Database;
04: import org.junit.ComparisonFailure;
05:
06: import java.util.Set;
07:
08: public class DatabaseTestTemplate {
09: public void testOnAvailableDatabases(DatabaseTest test)
10: throws Exception {
11: test(test, TestContext.getInstance().getAvailableDatabases());
12: }
13:
14: public void testOnAllDatabases(DatabaseTest test) throws Exception {
15: test(test, TestContext.getInstance().getAllDatabases());
16: }
17:
18: private void test(DatabaseTest test, Set<Database> databasesToTestOn)
19: throws Exception {
20: for (Database database : databasesToTestOn) {
21: try {
22: test.performTest(database);
23: } catch (ComparisonFailure e) {
24: String newMessage = "Database Test Failure on "
25: + database;
26: if (e.getMessage() != null) {
27: newMessage += ": " + e.getMessage();
28: }
29:
30: ComparisonFailure newError = new ComparisonFailure(
31: newMessage, e.getExpected(), e.getActual());
32: newError.setStackTrace(e.getStackTrace());
33: throw newError;
34: } catch (AssertionError e) {
35: e.printStackTrace();
36: String newMessage = "Database Test Failure on "
37: + database;
38: if (e.getMessage() != null) {
39: newMessage += ": " + e.getMessage();
40: }
41:
42: AssertionError newError = new AssertionError(newMessage);
43: newError.setStackTrace(e.getStackTrace());
44: throw newError;
45: } catch (Exception e) {
46: e.printStackTrace();
47: String newMessage = "Database Test Exception on "
48: + database;
49: if (e.getMessage() != null) {
50: newMessage += ": " + e.getMessage();
51: }
52:
53: Exception newError = e.getClass().getConstructor(
54: String.class).newInstance(newMessage);
55: if (e.getCause() == null) {
56: newError.setStackTrace(e.getStackTrace());
57: } else {
58: newError
59: .setStackTrace(e.getCause().getStackTrace());
60: }
61: throw newError;
62: } finally {
63: if (database.getConnection() != null
64: && !database.getAutoCommitMode()) {
65: database.rollback();
66: }
67: }
68: }
69: }
70: }
|