2525
2626import java .util .List ;
2727
28+ import static java .lang .String .format ;
29+ import static java .util .Objects .requireNonNull ;
2830import static org .assertj .core .api .Assertions .assertThat ;
2931import static org .junit .jupiter .api .TestInstance .Lifecycle .PER_CLASS ;
3032import static org .junit .jupiter .api .parallel .ExecutionMode .SAME_THREAD ;
3436@ Isolated
3537public abstract class BaseTestDatabaseMigrations
3638{
37- protected final JdbcDatabaseContainer <?> container = startContainer ();
38- protected final Jdbi jdbi = Jdbi .create (container .getJdbcUrl (), container .getUsername (), container .getPassword ());
39-
40- protected abstract JdbcDatabaseContainer <?> startContainer ();
39+ protected abstract void createGatewaySchema ();
4140
42- protected abstract String getDriver ();
41+ private final JdbcDatabaseContainer <?> container ;
42+ private final String schema ;
43+ protected final Jdbi jdbi ;
4344
44- protected abstract void createGatewaySchema ();
45+ public BaseTestDatabaseMigrations (JdbcDatabaseContainer <?> container , String schema )
46+ {
47+ this .container = requireNonNull (container , "container is null" );
48+ this .container .start ();
49+ this .schema = requireNonNull (schema , "schema is null" );
50+ jdbi = Jdbi .create (container .getJdbcUrl (), container .getUsername (), container .getPassword ());
51+ }
4552
4653 @ AfterAll
4754 public final void close ()
@@ -52,13 +59,7 @@ public final void close()
5259 @ Test
5360 public void testMigrationWithEmptyDatabase ()
5461 {
55- DataStoreConfiguration config = new DataStoreConfiguration (
56- container .getJdbcUrl (),
57- container .getUsername (),
58- container .getPassword (),
59- getDriver (),
60- 4 ,
61- true );
62+ DataStoreConfiguration config = dataStoreConfiguration ();
6263 FlywayMigration .migrate (config );
6364 verifyGatewaySchema (0 );
6465
@@ -68,13 +69,7 @@ public void testMigrationWithEmptyDatabase()
6869 @ Test
6970 public void testMigrationWithNonemptyDatabase ()
7071 {
71- DataStoreConfiguration config = new DataStoreConfiguration (
72- container .getJdbcUrl (),
73- container .getUsername (),
74- container .getPassword (),
75- getDriver (),
76- 4 ,
77- true );
72+ DataStoreConfiguration config = dataStoreConfiguration ();
7873 String t1Create = "CREATE TABLE t1 (id INT)" ;
7974 String t2Create = "CREATE TABLE t2 (id INT)" ;
8075 Handle jdbiHandle = jdbi .open ();
@@ -98,13 +93,7 @@ public void testMigrationWithExistingGatewaySchema()
9893 // add a row to one of the existing tables before migration
9994 jdbi .withHandle (handle ->
10095 handle .execute ("INSERT INTO resource_groups_global_properties VALUES ('a_name', 'a_value')" ));
101- DataStoreConfiguration config = new DataStoreConfiguration (
102- container .getJdbcUrl (),
103- container .getUsername (),
104- container .getPassword (),
105- getDriver (),
106- 4 ,
107- true );
96+ DataStoreConfiguration config = dataStoreConfiguration ();
10897 FlywayMigration .migrate (config );
10998 verifyGatewaySchema (1 );
11099 dropAllTables ();
@@ -137,7 +126,7 @@ protected void dropAllTables()
137126 String exactMatchTable = "DROP TABLE IF EXISTS exact_match_source_selectors" ;
138127 String flywayHistoryTable = "DROP TABLE IF EXISTS flyway_schema_history" ;
139128 Handle jdbiHandle = jdbi .open ();
140- String sql = String . format ("SELECT 1 FROM information_schema.tables WHERE table_schema = '%s'" , getTestSchema () );
129+ String sql = format ("SELECT 1 FROM information_schema.tables WHERE table_schema = '%s'" , schema );
141130 verifyResultSetCount (sql , 7 );
142131 jdbiHandle .execute (gatewayBackendTable );
143132 jdbiHandle .execute (queryHistoryTable );
@@ -150,8 +139,14 @@ protected void dropAllTables()
150139 jdbiHandle .close ();
151140 }
152141
153- protected String getTestSchema ()
142+ private DataStoreConfiguration dataStoreConfiguration ()
154143 {
155- return "public" ;
144+ return new DataStoreConfiguration (
145+ container .getJdbcUrl (),
146+ container .getUsername (),
147+ container .getPassword (),
148+ container .getDriverClassName (),
149+ 4 ,
150+ true );
156151 }
157152}
0 commit comments