22
22
23
23
import javax .sql .DataSource ;
24
24
25
- import com .zaxxer .hikari .HikariConfig ;
26
- import com .zaxxer .hikari .HikariDataSource ;
27
25
import liquibase .integration .spring .SpringLiquibase ;
28
26
import org .junit .Test ;
29
27
30
28
import org .springframework .boot .actuate .liquibase .LiquibaseEndpoint .LiquibaseBean ;
31
29
import org .springframework .boot .autoconfigure .AutoConfigurations ;
32
30
import org .springframework .boot .autoconfigure .jdbc .DataSourceAutoConfiguration ;
33
31
import org .springframework .boot .autoconfigure .liquibase .LiquibaseAutoConfiguration ;
32
+ import org .springframework .boot .jdbc .EmbeddedDatabaseConnection ;
34
33
import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
35
34
import org .springframework .context .ApplicationContext ;
36
35
import org .springframework .context .annotation .Bean ;
37
36
import org .springframework .context .annotation .Configuration ;
37
+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
38
38
39
39
import static org .assertj .core .api .Assertions .assertThat ;
40
40
@@ -97,17 +97,18 @@ public void connectionAutoCommitPropertyIsReset() {
97
97
}
98
98
99
99
@ Test
100
- public void multipleLiquibaseReportIsReturned () {
101
- this .contextRunner .withUserConfiguration (Config .class , LiquibaseConfiguration .class ).run ((context ) -> {
102
- Map <String , LiquibaseBean > liquibaseBeans = context .getBean (LiquibaseEndpoint .class ).liquibaseBeans ()
103
- .getContexts ().get (context .getId ()).getLiquibaseBeans ();
104
- assertThat (liquibaseBeans .get ("liquibase" ).getChangeSets ()).hasSize (1 );
105
- assertThat (liquibaseBeans .get ("liquibase" ).getChangeSets ().get (0 ).getChangeLog ())
106
- .isEqualTo ("classpath:/db/changelog/db.changelog-master.yaml" );
107
- assertThat (liquibaseBeans .get ("liquibaseBackup" ).getChangeSets ()).hasSize (1 );
108
- assertThat (liquibaseBeans .get ("liquibaseBackup" ).getChangeSets ().get (0 ).getChangeLog ())
109
- .isEqualTo ("classpath:/db/changelog/db.changelog-master-backup.yaml" );
110
- });
100
+ public void whenMultipleLiquibaseBeansArePresentChangeSetsAreCorrectlyReportedForEachBean () {
101
+ this .contextRunner .withUserConfiguration (Config .class , MultipleDataSourceLiquibaseConfiguration .class )
102
+ .run ((context ) -> {
103
+ Map <String , LiquibaseBean > liquibaseBeans = context .getBean (LiquibaseEndpoint .class )
104
+ .liquibaseBeans ().getContexts ().get (context .getId ()).getLiquibaseBeans ();
105
+ assertThat (liquibaseBeans .get ("liquibase" ).getChangeSets ()).hasSize (1 );
106
+ assertThat (liquibaseBeans .get ("liquibase" ).getChangeSets ().get (0 ).getChangeLog ())
107
+ .isEqualTo ("classpath:/db/changelog/db.changelog-master.yaml" );
108
+ assertThat (liquibaseBeans .get ("liquibaseBackup" ).getChangeSets ()).hasSize (1 );
109
+ assertThat (liquibaseBeans .get ("liquibaseBackup" ).getChangeSets ().get (0 ).getChangeLog ())
110
+ .isEqualTo ("classpath:/db/changelog/db.changelog-master-backup.yaml" );
111
+ });
111
112
}
112
113
113
114
private boolean getAutoCommit (DataSource dataSource ) throws SQLException {
@@ -127,39 +128,38 @@ public LiquibaseEndpoint endpoint(ApplicationContext context) {
127
128
}
128
129
129
130
@ Configuration
130
- static class LiquibaseConfiguration {
131
+ static class MultipleDataSourceLiquibaseConfiguration {
131
132
132
133
@ Bean
133
134
DataSource dataSource () {
134
- HikariConfig config = new HikariConfig ();
135
- config .setJdbcUrl ("jdbc:hsqldb:mem:test" );
136
- config .setUsername ("sa" );
137
- return new HikariDataSource (config );
135
+ return createEmbeddedDatabase ();
138
136
}
139
137
140
138
@ Bean
141
139
DataSource dataSourceBackup () {
142
- HikariConfig config = new HikariConfig ();
143
- config .setJdbcUrl ("jdbc:hsqldb:mem:testBackup" );
144
- config .setUsername ("sa" );
145
- return new HikariDataSource (config );
140
+ return createEmbeddedDatabase ();
146
141
}
147
142
148
143
@ Bean
149
144
SpringLiquibase liquibase (DataSource dataSource ) {
150
- SpringLiquibase liquibase = new SpringLiquibase ();
151
- liquibase .setChangeLog ("classpath:/db/changelog/db.changelog-master.yaml" );
152
- liquibase .setShouldRun (true );
153
- liquibase .setDataSource (dataSource );
154
- return liquibase ;
145
+ return createSpringLiquibase ("db.changelog-master.yaml" , dataSource );
155
146
}
156
147
157
148
@ Bean
158
149
SpringLiquibase liquibaseBackup (DataSource dataSourceBackup ) {
150
+ return createSpringLiquibase ("db.changelog-master-backup.yaml" , dataSourceBackup );
151
+ }
152
+
153
+ private DataSource createEmbeddedDatabase () {
154
+ return new EmbeddedDatabaseBuilder ().generateUniqueName (true )
155
+ .setType (EmbeddedDatabaseConnection .HSQL .getType ()).build ();
156
+ }
157
+
158
+ private SpringLiquibase createSpringLiquibase (String changeLog , DataSource dataSource ) {
159
159
SpringLiquibase liquibase = new SpringLiquibase ();
160
- liquibase .setChangeLog ("classpath:/db/changelog/db.changelog-master-backup.yaml" );
160
+ liquibase .setChangeLog ("classpath:/db/changelog/" + changeLog );
161
161
liquibase .setShouldRun (true );
162
- liquibase .setDataSource (dataSourceBackup );
162
+ liquibase .setDataSource (dataSource );
163
163
return liquibase ;
164
164
}
165
165
0 commit comments