11package org .cloudfoundry .identity .uaa .db .beans ;
22
3+ import org .cloudfoundry .identity .uaa .db .beans .FlywayConfiguration .FlywayConfigurationWithMigration ;
34import org .cloudfoundry .identity .uaa .db .beans .FlywayConfiguration .FlywayConfigurationWithMigration .ConfiguredWithMigrations ;
45import org .cloudfoundry .identity .uaa .db .beans .FlywayConfiguration .FlywayConfigurationWithoutMigrations .ConfiguredWithoutMigrations ;
6+ import org .junit .jupiter .api .AfterEach ;
57import org .junit .jupiter .api .BeforeEach ;
68import org .junit .jupiter .api .Test ;
79import org .junit .jupiter .api .extension .ExtendWith ;
810import org .mockito .Mock ;
911import org .mockito .junit .jupiter .MockitoExtension ;
1012import org .springframework .context .annotation .ConditionContext ;
13+ import org .springframework .jdbc .datasource .DriverManagerDataSource ;
1114import org .springframework .mock .env .MockEnvironment ;
1215
16+ import javax .sql .DataSource ;
17+ import java .sql .Connection ;
18+ import java .sql .ResultSet ;
19+ import java .sql .SQLException ;
20+ import java .sql .Statement ;
21+ import java .util .UUID ;
22+
1323import static org .assertj .core .api .Assertions .assertThat ;
14- import static org .mockito .Mockito .when ;
24+ import static org .mockito .Mockito .lenient ;
1525
1626@ ExtendWith (MockitoExtension .class )
1727class FlywayConfigurationTest {
@@ -28,7 +38,7 @@ class FlywayConfigurationTest {
2838 @ BeforeEach
2939 void setUp () {
3040 mockEnvironment = new MockEnvironment ();
31- when (mockConditionContext .getEnvironment ()).thenReturn (mockEnvironment );
41+ lenient (). when (mockConditionContext .getEnvironment ()).thenReturn (mockEnvironment );
3242 configuredWithMigrations = new ConfiguredWithMigrations ();
3343 configuredWithoutMigrations = new ConfiguredWithoutMigrations ();
3444 }
@@ -62,4 +72,64 @@ void flywayConfiguration_RunsMigration_WhenInvalidConfiguration() {
6272 assertThat (configuredWithMigrations .matches (mockConditionContext , null )).isTrue ();
6373 assertThat (configuredWithoutMigrations .matches (mockConditionContext , null )).isFalse ();
6474 }
75+
76+ private DataSource dataSource ;
77+
78+ @ AfterEach
79+ void tearDown () throws SQLException {
80+ if (dataSource != null ) {
81+ try (Connection conn = dataSource .getConnection ();
82+ Statement stmt = conn .createStatement ()) {
83+ stmt .execute ("SHUTDOWN" );
84+ }
85+ }
86+ }
87+
88+ private DataSource inMemoryDataSource () {
89+ DriverManagerDataSource ds = new DriverManagerDataSource ();
90+ ds .setDriverClassName ("org.hsqldb.jdbc.JDBCDriver" );
91+ ds .setUrl ("jdbc:hsqldb:mem:" + UUID .randomUUID ());
92+ ds .setUsername ("sa" );
93+ ds .setPassword ("" );
94+ return ds ;
95+ }
96+
97+ private void createSchemaVersionTable (DataSource ds ) throws SQLException {
98+ try (Connection conn = ds .getConnection ();
99+ Statement stmt = conn .createStatement ()) {
100+ stmt .execute ("CREATE TABLE %s (installed_rank INT, type VARCHAR(20))" .formatted (FlywayConfiguration .VERSION_TABLE ));
101+ }
102+ }
103+
104+ private String typeForRank (DataSource ds , int rank ) throws SQLException {
105+ try (Connection conn = ds .getConnection ();
106+ Statement stmt = conn .createStatement ();
107+ ResultSet rs = stmt .executeQuery ("SELECT type FROM %s WHERE installed_rank = %d" .formatted (FlywayConfiguration .VERSION_TABLE , rank ))) {
108+ return rs .next () ? rs .getString ("type" ) : null ;
109+ }
110+ }
111+
112+ @ Test
113+ void updateSpringJdbcMigrationTypes_RewritesSpringJdbcToJdbc () throws SQLException {
114+ dataSource = inMemoryDataSource ();
115+ createSchemaVersionTable (dataSource );
116+ try (Connection conn = dataSource .getConnection ();
117+ Statement stmt = conn .createStatement ()) {
118+ stmt .execute ("INSERT INTO %s (installed_rank, type) VALUES (1, 'SPRING_JDBC')" .formatted (FlywayConfiguration .VERSION_TABLE ));
119+ stmt .execute ("INSERT INTO %s (installed_rank, type) VALUES (2, 'SQL')" .formatted (FlywayConfiguration .VERSION_TABLE ));
120+ }
121+
122+ FlywayConfigurationWithMigration .updateSpringJdbcMigrationTypes (dataSource );
123+
124+ assertThat (typeForRank (dataSource , 1 )).isEqualTo ("JDBC" );
125+ assertThat (typeForRank (dataSource , 2 )).isEqualTo ("SQL" );
126+ }
127+
128+ @ Test
129+ void updateSpringJdbcMigrationTypes_DoesNotThrow_WhenVersionTableMissing () {
130+ dataSource = inMemoryDataSource ();
131+
132+ org .assertj .core .api .Assertions .assertThatCode (() -> FlywayConfigurationWithMigration .updateSpringJdbcMigrationTypes (dataSource ))
133+ .doesNotThrowAnyException ();
134+ }
65135}
0 commit comments