2222
2323import javax .sql .DataSource ;
2424
25+ import liquibase .integration .spring .SpringLiquibase ;
2526import org .junit .jupiter .api .Test ;
2627
2728import org .springframework .boot .actuate .liquibase .LiquibaseEndpoint .LiquibaseBean ;
2829import org .springframework .boot .autoconfigure .AutoConfigurations ;
2930import org .springframework .boot .autoconfigure .jdbc .DataSourceAutoConfiguration ;
3031import org .springframework .boot .autoconfigure .liquibase .LiquibaseAutoConfiguration ;
32+ import org .springframework .boot .jdbc .EmbeddedDatabaseConnection ;
3133import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
3234import org .springframework .context .ApplicationContext ;
3335import org .springframework .context .annotation .Bean ;
3436import org .springframework .context .annotation .Configuration ;
37+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
3538
3639import static org .assertj .core .api .Assertions .assertThat ;
3740
4144 * @author Eddú Meléndez
4245 * @author Andy Wilkinson
4346 * @author Stephane Nicoll
47+ * @author Leo Li
4448 */
4549class LiquibaseEndpointTests {
4650
@@ -92,6 +96,21 @@ void connectionAutoCommitPropertyIsReset() {
9296 });
9397 }
9498
99+ @ Test
100+ 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+ });
112+ }
113+
95114 private boolean getAutoCommit (DataSource dataSource ) throws SQLException {
96115 try (Connection connection = dataSource .getConnection ()) {
97116 return connection .getAutoCommit ();
@@ -108,4 +127,42 @@ LiquibaseEndpoint endpoint(ApplicationContext context) {
108127
109128 }
110129
130+ @ Configuration (proxyBeanMethods = false )
131+ static class MultipleDataSourceLiquibaseConfiguration {
132+
133+ @ Bean
134+ DataSource dataSource () {
135+ return createEmbeddedDatabase ();
136+ }
137+
138+ @ Bean
139+ DataSource dataSourceBackup () {
140+ return createEmbeddedDatabase ();
141+ }
142+
143+ @ Bean
144+ SpringLiquibase liquibase (DataSource dataSource ) {
145+ return createSpringLiquibase ("db.changelog-master.yaml" , dataSource );
146+ }
147+
148+ @ Bean
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+ SpringLiquibase liquibase = new SpringLiquibase ();
160+ liquibase .setChangeLog ("classpath:/db/changelog/" + changeLog );
161+ liquibase .setShouldRun (true );
162+ liquibase .setDataSource (dataSource );
163+ return liquibase ;
164+ }
165+
166+ }
167+
111168}
0 commit comments