22
22
23
23
import javax .sql .DataSource ;
24
24
25
+ import liquibase .integration .spring .SpringLiquibase ;
25
26
import org .junit .jupiter .api .Test ;
26
27
27
28
import org .springframework .boot .actuate .liquibase .LiquibaseEndpoint .LiquibaseBean ;
28
29
import org .springframework .boot .autoconfigure .AutoConfigurations ;
29
30
import org .springframework .boot .autoconfigure .jdbc .DataSourceAutoConfiguration ;
30
31
import org .springframework .boot .autoconfigure .liquibase .LiquibaseAutoConfiguration ;
32
+ import org .springframework .boot .jdbc .EmbeddedDatabaseConnection ;
31
33
import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
32
34
import org .springframework .context .ApplicationContext ;
33
35
import org .springframework .context .annotation .Bean ;
34
36
import org .springframework .context .annotation .Configuration ;
37
+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
35
38
36
39
import static org .assertj .core .api .Assertions .assertThat ;
37
40
41
44
* @author Eddú Meléndez
42
45
* @author Andy Wilkinson
43
46
* @author Stephane Nicoll
47
+ * @author Leo Li
44
48
*/
45
49
class LiquibaseEndpointTests {
46
50
@@ -92,6 +96,21 @@ void connectionAutoCommitPropertyIsReset() {
92
96
});
93
97
}
94
98
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
+
95
114
private boolean getAutoCommit (DataSource dataSource ) throws SQLException {
96
115
try (Connection connection = dataSource .getConnection ()) {
97
116
return connection .getAutoCommit ();
@@ -108,4 +127,42 @@ LiquibaseEndpoint endpoint(ApplicationContext context) {
108
127
109
128
}
110
129
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
+
111
168
}
0 commit comments