|
17 | 17 | package org.springframework.boot.autoconfigure.liquibase;
|
18 | 18 |
|
19 | 19 | import java.io.File;
|
| 20 | +import java.io.IOException; |
20 | 21 | import java.util.Map;
|
| 22 | +import java.util.function.Consumer; |
21 | 23 |
|
22 | 24 | import javax.sql.DataSource;
|
23 | 25 |
|
24 | 26 | import com.zaxxer.hikari.HikariDataSource;
|
25 | 27 | import liquibase.integration.spring.SpringLiquibase;
|
26 |
| -import org.junit.After; |
27 | 28 | import org.junit.Before;
|
28 | 29 | import org.junit.Rule;
|
29 | 30 | import org.junit.Test;
|
30 |
| -import org.junit.rules.ExpectedException; |
31 | 31 | import org.junit.rules.TemporaryFolder;
|
32 | 32 |
|
33 | 33 | import org.springframework.beans.factory.BeanCreationException;
|
34 |
| -import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; |
| 34 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
35 | 35 | import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
36 | 36 | import org.springframework.boot.jdbc.DataSourceBuilder;
|
37 | 37 | import org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger;
|
38 | 38 | import org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener;
|
| 39 | +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; |
| 40 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 41 | +import org.springframework.boot.test.context.runner.ContextConsumer; |
39 | 42 | import org.springframework.boot.test.rule.OutputCapture;
|
40 |
| -import org.springframework.boot.test.util.TestPropertyValues; |
41 |
| -import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
42 | 43 | import org.springframework.context.annotation.Bean;
|
43 | 44 | import org.springframework.context.annotation.Configuration;
|
44 | 45 | import org.springframework.context.annotation.Primary;
|
|
53 | 54 | * @author Marcel Overdijk
|
54 | 55 | * @author Eddú Meléndez
|
55 | 56 | * @author Andy Wilkinson
|
| 57 | + * @author Stephane Nicoll |
56 | 58 | */
|
57 | 59 | public class LiquibaseAutoConfigurationTests {
|
58 | 60 |
|
59 |
| - @Rule |
60 |
| - public ExpectedException expected = ExpectedException.none(); |
61 |
| - |
62 | 61 | @Rule
|
63 | 62 | public TemporaryFolder temp = new TemporaryFolder();
|
64 | 63 |
|
65 | 64 | @Rule
|
66 | 65 | public OutputCapture outputCapture = new OutputCapture();
|
67 | 66 |
|
68 |
| - private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
69 |
| - |
70 | 67 | @Before
|
71 | 68 | public void init() {
|
72 |
| - TestPropertyValues.of("spring.datasource.name:liquibasetest") |
73 |
| - .applyTo(this.context); |
74 | 69 | new LiquibaseServiceLocatorApplicationListener().onApplicationEvent(null);
|
75 | 70 | }
|
76 | 71 |
|
77 |
| - @After |
78 |
| - public void close() { |
79 |
| - if (this.context != null) { |
80 |
| - this.context.close(); |
81 |
| - } |
82 |
| - } |
| 72 | + private ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 73 | + .withConfiguration(AutoConfigurations.of(LiquibaseAutoConfiguration.class)) |
| 74 | + .withPropertyValues("spring.datasource.generate-unique-name=true"); |
83 | 75 |
|
84 | 76 | @Test
|
85 |
| - public void testNoDataSource() throws Exception { |
86 |
| - this.context.register(LiquibaseAutoConfiguration.class, |
87 |
| - PropertyPlaceholderAutoConfiguration.class); |
88 |
| - this.context.refresh(); |
89 |
| - assertThat(this.context.getBeanNamesForType(SpringLiquibase.class).length) |
90 |
| - .isEqualTo(0); |
| 77 | + public void noDataSource() { |
| 78 | + this.contextRunner.run((context) -> |
| 79 | + assertThat(context).doesNotHaveBean(SpringLiquibase.class)); |
91 | 80 | }
|
92 | 81 |
|
93 | 82 | @Test
|
94 |
| - public void testDefaultSpringLiquibase() throws Exception { |
95 |
| - this.context.register(EmbeddedDataSourceConfiguration.class, |
96 |
| - LiquibaseAutoConfiguration.class, |
97 |
| - PropertyPlaceholderAutoConfiguration.class); |
98 |
| - this.context.refresh(); |
99 |
| - SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); |
100 |
| - assertThat(liquibase.getChangeLog()) |
101 |
| - .isEqualTo("classpath:/db/changelog/db.changelog-master.yaml"); |
102 |
| - assertThat(liquibase.getContexts()).isNull(); |
103 |
| - assertThat(liquibase.getDefaultSchema()).isNull(); |
104 |
| - assertThat(liquibase.isDropFirst()).isFalse(); |
| 83 | + public void defaultSpringLiquibase() { |
| 84 | + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) |
| 85 | + .run(assertLiquibase((liquibase) -> { |
| 86 | + assertThat(liquibase.getChangeLog()).isEqualTo( |
| 87 | + "classpath:/db/changelog/db.changelog-master.yaml"); |
| 88 | + assertThat(liquibase.getContexts()).isNull(); |
| 89 | + assertThat(liquibase.getDefaultSchema()).isNull(); |
| 90 | + assertThat(liquibase.isDropFirst()).isFalse(); |
| 91 | + })); |
105 | 92 | }
|
106 | 93 |
|
107 | 94 | @Test
|
108 |
| - public void testXmlChangeLog() throws Exception { |
109 |
| - TestPropertyValues |
110 |
| - .of("spring.liquibase.change-log:classpath:/db/changelog/db.changelog-override.xml") |
111 |
| - .applyTo(this.context); |
112 |
| - this.context.register(EmbeddedDataSourceConfiguration.class, |
113 |
| - LiquibaseAutoConfiguration.class, |
114 |
| - PropertyPlaceholderAutoConfiguration.class); |
115 |
| - this.context.refresh(); |
116 |
| - SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); |
117 |
| - assertThat(liquibase.getChangeLog()) |
118 |
| - .isEqualTo("classpath:/db/changelog/db.changelog-override.xml"); |
| 95 | + public void changelogXml() { |
| 96 | + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) |
| 97 | + .withPropertyValues("spring.liquibase.change-log:classpath:/db/changelog/db.changelog-override.xml") |
| 98 | + .run(assertLiquibase((liquibase) -> |
| 99 | + assertThat(liquibase.getChangeLog()).isEqualTo( |
| 100 | + "classpath:/db/changelog/db.changelog-override.xml"))); |
119 | 101 | }
|
120 | 102 |
|
121 | 103 | @Test
|
122 |
| - public void testJsonChangeLog() throws Exception { |
123 |
| - TestPropertyValues |
124 |
| - .of("spring.liquibase.change-log:classpath:/db/changelog/db.changelog-override.json") |
125 |
| - .applyTo(this.context); |
126 |
| - this.context.register(EmbeddedDataSourceConfiguration.class, |
127 |
| - LiquibaseAutoConfiguration.class, |
128 |
| - PropertyPlaceholderAutoConfiguration.class); |
129 |
| - this.context.refresh(); |
130 |
| - SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); |
131 |
| - assertThat(liquibase.getChangeLog()) |
132 |
| - .isEqualTo("classpath:/db/changelog/db.changelog-override.json"); |
| 104 | + public void changelogJson() { |
| 105 | + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) |
| 106 | + .withPropertyValues("spring.liquibase.change-log:classpath:/db/changelog/db.changelog-override.json") |
| 107 | + .run(assertLiquibase((liquibase) -> |
| 108 | + assertThat(liquibase.getChangeLog()).isEqualTo( |
| 109 | + "classpath:/db/changelog/db.changelog-override.json"))); |
133 | 110 | }
|
134 | 111 |
|
135 | 112 | @Test
|
136 |
| - public void testSqlChangeLog() throws Exception { |
137 |
| - TestPropertyValues |
138 |
| - .of("spring.liquibase.change-log:classpath:/db/changelog/db.changelog-override.sql") |
139 |
| - .applyTo(this.context); |
140 |
| - this.context.register(EmbeddedDataSourceConfiguration.class, |
141 |
| - LiquibaseAutoConfiguration.class, |
142 |
| - PropertyPlaceholderAutoConfiguration.class); |
143 |
| - this.context.refresh(); |
144 |
| - SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); |
145 |
| - assertThat(liquibase.getChangeLog()) |
146 |
| - .isEqualTo("classpath:/db/changelog/db.changelog-override.sql"); |
| 113 | + public void changelogSql() { |
| 114 | + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) |
| 115 | + .withPropertyValues( |
| 116 | + "spring.liquibase.change-log:classpath:/db/changelog/db.changelog-override.sql") |
| 117 | + .run(assertLiquibase((liquibase) -> |
| 118 | + assertThat(liquibase.getChangeLog()).isEqualTo( |
| 119 | + "classpath:/db/changelog/db.changelog-override.sql"))); |
147 | 120 | }
|
148 | 121 |
|
149 | 122 | @Test
|
150 |
| - public void testOverrideContexts() throws Exception { |
151 |
| - TestPropertyValues.of("spring.liquibase.contexts:test, production") |
152 |
| - .applyTo(this.context); |
153 |
| - this.context.register(EmbeddedDataSourceConfiguration.class, |
154 |
| - LiquibaseAutoConfiguration.class, |
155 |
| - PropertyPlaceholderAutoConfiguration.class); |
156 |
| - this.context.refresh(); |
157 |
| - SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); |
158 |
| - assertThat(liquibase.getContexts()).isEqualTo("test, production"); |
| 123 | + public void overrideContexts() { |
| 124 | + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) |
| 125 | + .withPropertyValues("spring.liquibase.contexts:test, production") |
| 126 | + .run(assertLiquibase((liquibase) -> |
| 127 | + assertThat(liquibase.getContexts()).isEqualTo("test, production"))); |
159 | 128 | }
|
160 | 129 |
|
161 | 130 | @Test
|
162 |
| - public void testOverrideDefaultSchema() throws Exception { |
163 |
| - TestPropertyValues.of("spring.liquibase.default-schema:public") |
164 |
| - .applyTo(this.context); |
165 |
| - this.context.register(EmbeddedDataSourceConfiguration.class, |
166 |
| - LiquibaseAutoConfiguration.class, |
167 |
| - PropertyPlaceholderAutoConfiguration.class); |
168 |
| - this.context.refresh(); |
169 |
| - SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); |
170 |
| - assertThat(liquibase.getDefaultSchema()).isEqualTo("public"); |
| 131 | + public void overrideDefaultSchema() { |
| 132 | + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) |
| 133 | + .withPropertyValues("spring.liquibase.default-schema:public") |
| 134 | + .run(assertLiquibase((liquibase) -> |
| 135 | + assertThat(liquibase.getDefaultSchema()).isEqualTo("public"))); |
171 | 136 | }
|
172 | 137 |
|
173 | 138 | @Test
|
174 |
| - public void testOverrideDropFirst() throws Exception { |
175 |
| - TestPropertyValues.of("spring.liquibase.drop-first:true").applyTo(this.context); |
176 |
| - this.context.register(EmbeddedDataSourceConfiguration.class, |
177 |
| - LiquibaseAutoConfiguration.class, |
178 |
| - PropertyPlaceholderAutoConfiguration.class); |
179 |
| - this.context.refresh(); |
180 |
| - SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); |
181 |
| - assertThat(liquibase.isDropFirst()).isTrue(); |
| 139 | + public void overrideDropFirst() { |
| 140 | + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) |
| 141 | + .withPropertyValues("spring.liquibase.drop-first:true") |
| 142 | + .run(assertLiquibase((liquibase) -> |
| 143 | + assertThat(liquibase.isDropFirst()).isTrue())); |
182 | 144 | }
|
183 | 145 |
|
184 | 146 | @Test
|
185 |
| - public void testOverrideDataSource() throws Exception { |
186 |
| - TestPropertyValues.of("spring.liquibase.url:jdbc:hsqldb:mem:liquibase", |
187 |
| - "spring.liquibase.user:sa").applyTo(this.context); |
188 |
| - this.context.register(EmbeddedDataSourceConfiguration.class, |
189 |
| - LiquibaseAutoConfiguration.class, |
190 |
| - PropertyPlaceholderAutoConfiguration.class); |
191 |
| - this.context.refresh(); |
192 |
| - SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); |
193 |
| - DataSource dataSource = liquibase.getDataSource(); |
194 |
| - assertThat(((HikariDataSource) dataSource).isClosed()).isTrue(); |
195 |
| - assertThat(((HikariDataSource) dataSource).getJdbcUrl()) |
196 |
| - .isEqualTo("jdbc:hsqldb:mem:liquibase"); |
| 147 | + public void overrideDataSource() { |
| 148 | + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) |
| 149 | + .withPropertyValues("spring.liquibase.url:jdbc:hsqldb:mem:liquibase", |
| 150 | + "spring.liquibase.user:sa").run(assertLiquibase((liquibase) -> { |
| 151 | + DataSource dataSource = liquibase.getDataSource(); |
| 152 | + assertThat(((HikariDataSource) dataSource).isClosed()).isTrue(); |
| 153 | + assertThat(((HikariDataSource) dataSource).getJdbcUrl()) |
| 154 | + .isEqualTo("jdbc:hsqldb:mem:liquibase"); |
| 155 | + })); |
197 | 156 | }
|
198 | 157 |
|
199 |
| - @Test(expected = BeanCreationException.class) |
200 |
| - public void testChangeLogDoesNotExist() throws Exception { |
201 |
| - TestPropertyValues |
202 |
| - .of("spring.liquibase.change-log:classpath:/no-such-changelog.yaml") |
203 |
| - .applyTo(this.context); |
204 |
| - this.context.register(EmbeddedDataSourceConfiguration.class, |
205 |
| - LiquibaseAutoConfiguration.class, |
206 |
| - PropertyPlaceholderAutoConfiguration.class); |
207 |
| - this.context.refresh(); |
| 158 | + @Test |
| 159 | + public void changeLogDoesNotExist() { |
| 160 | + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) |
| 161 | + .withPropertyValues("spring.liquibase.change-log:classpath:/no-such-changelog.yaml") |
| 162 | + .run((context) -> { |
| 163 | + assertThat(context).hasFailed(); |
| 164 | + assertThat(context).getFailure().isInstanceOf( |
| 165 | + BeanCreationException.class); |
| 166 | + }); |
208 | 167 | }
|
209 | 168 |
|
210 | 169 | @Test
|
211 |
| - public void testLogger() throws Exception { |
212 |
| - this.context.register(EmbeddedDataSourceConfiguration.class, |
213 |
| - LiquibaseAutoConfiguration.class, |
214 |
| - PropertyPlaceholderAutoConfiguration.class); |
215 |
| - this.context.refresh(); |
216 |
| - SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); |
217 |
| - Object log = ReflectionTestUtils.getField(liquibase, "log"); |
218 |
| - assertThat(log).isInstanceOf(CommonsLoggingLiquibaseLogger.class); |
219 |
| - assertThat(this.outputCapture.toString()).doesNotContain(": liquibase:"); |
| 170 | + public void logging() { |
| 171 | + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) |
| 172 | + .run(assertLiquibase((liquibase) -> { |
| 173 | + Object log = ReflectionTestUtils.getField(liquibase, "log"); |
| 174 | + assertThat(log).isInstanceOf(CommonsLoggingLiquibaseLogger.class); |
| 175 | + assertThat(this.outputCapture.toString()).doesNotContain(": liquibase:"); |
| 176 | + })); |
220 | 177 | }
|
221 | 178 |
|
222 | 179 | @Test
|
223 |
| - public void testOverrideLabels() throws Exception { |
224 |
| - TestPropertyValues.of("spring.liquibase.labels:test, production") |
225 |
| - .applyTo(this.context); |
226 |
| - this.context.register(EmbeddedDataSourceConfiguration.class, |
227 |
| - LiquibaseAutoConfiguration.class, |
228 |
| - PropertyPlaceholderAutoConfiguration.class); |
229 |
| - this.context.refresh(); |
230 |
| - SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); |
231 |
| - assertThat(liquibase.getLabels()).isEqualTo("test, production"); |
| 180 | + public void overrideLabels() { |
| 181 | + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) |
| 182 | + .withPropertyValues("spring.liquibase.labels:test, production") |
| 183 | + .run(assertLiquibase((liquibase) -> |
| 184 | + assertThat(liquibase.getLabels()).isEqualTo("test, production"))); |
232 | 185 | }
|
233 | 186 |
|
234 | 187 | @Test
|
235 | 188 | @SuppressWarnings("unchecked")
|
236 |
| - public void testOverrideParameters() throws Exception { |
237 |
| - TestPropertyValues.of("spring.liquibase.parameters.foo:bar") |
238 |
| - .applyTo(this.context); |
239 |
| - this.context.register(EmbeddedDataSourceConfiguration.class, |
240 |
| - LiquibaseAutoConfiguration.class, |
241 |
| - PropertyPlaceholderAutoConfiguration.class); |
242 |
| - this.context.refresh(); |
243 |
| - SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); |
244 |
| - Map<String, String> parameters = (Map<String, String>) ReflectionTestUtils |
245 |
| - .getField(liquibase, "parameters"); |
246 |
| - assertThat(parameters.containsKey("foo")).isTrue(); |
247 |
| - assertThat(parameters.get("foo")).isEqualTo("bar"); |
| 189 | + public void testOverrideParameters() { |
| 190 | + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) |
| 191 | + .withPropertyValues("spring.liquibase.parameters.foo:bar") |
| 192 | + .run(assertLiquibase((liquibase) -> { |
| 193 | + Map<String, String> parameters = (Map<String, String>) ReflectionTestUtils |
| 194 | + .getField(liquibase, "parameters"); |
| 195 | + assertThat(parameters.containsKey("foo")).isTrue(); |
| 196 | + assertThat(parameters.get("foo")).isEqualTo("bar"); |
| 197 | + })); |
248 | 198 | }
|
249 | 199 |
|
250 | 200 | @Test
|
251 |
| - public void testRollbackFile() throws Exception { |
| 201 | + public void rollbackFile() throws IOException { |
252 | 202 | File file = this.temp.newFile("rollback-file.sql");
|
253 |
| - TestPropertyValues.of("spring.liquibase.rollbackFile:" + file.getAbsolutePath()) |
254 |
| - .applyTo(this.context); |
255 |
| - this.context.register(EmbeddedDataSourceConfiguration.class, |
256 |
| - LiquibaseAutoConfiguration.class, |
257 |
| - PropertyPlaceholderAutoConfiguration.class); |
258 |
| - this.context.refresh(); |
259 |
| - SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); |
260 |
| - File actualFile = (File) ReflectionTestUtils.getField(liquibase, "rollbackFile"); |
261 |
| - assertThat(actualFile).isEqualTo(file).exists(); |
262 |
| - String content = new String(FileCopyUtils.copyToByteArray(file)); |
263 |
| - assertThat(content).contains("DROP TABLE PUBLIC.customer;"); |
| 203 | + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) |
| 204 | + .withPropertyValues( |
| 205 | + "spring.liquibase.rollbackFile:" + file.getAbsolutePath()) |
| 206 | + .run((context) -> { |
| 207 | + SpringLiquibase liquibase = context.getBean(SpringLiquibase.class); |
| 208 | + File actualFile = (File) ReflectionTestUtils.getField(liquibase, "rollbackFile"); |
| 209 | + assertThat(actualFile).isEqualTo(file).exists(); |
| 210 | + String content = new String(FileCopyUtils.copyToByteArray(file)); |
| 211 | + assertThat(content).contains("DROP TABLE PUBLIC.customer;"); |
| 212 | + }); |
264 | 213 | }
|
265 | 214 |
|
266 | 215 | @Test
|
267 |
| - public void testLiquibaseDataSource() { |
268 |
| - this.context.register(LiquibaseDataSourceConfiguration.class, |
269 |
| - EmbeddedDataSourceConfiguration.class, LiquibaseAutoConfiguration.class, |
270 |
| - PropertyPlaceholderAutoConfiguration.class); |
271 |
| - this.context.refresh(); |
272 |
| - SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); |
273 |
| - assertThat(liquibase.getDataSource()) |
274 |
| - .isEqualTo(this.context.getBean("liquibaseDataSource")); |
| 216 | + public void liquibaseDataSource() { |
| 217 | + this.contextRunner.withUserConfiguration(LiquibaseDataSourceConfiguration.class, |
| 218 | + EmbeddedDataSourceConfiguration.class).run((context) -> { |
| 219 | + SpringLiquibase liquibase = context.getBean(SpringLiquibase.class); |
| 220 | + assertThat(liquibase.getDataSource()) |
| 221 | + .isEqualTo(context.getBean("liquibaseDataSource")); |
| 222 | + }); |
| 223 | + } |
| 224 | + |
| 225 | + private ContextConsumer<AssertableApplicationContext> assertLiquibase( |
| 226 | + Consumer<SpringLiquibase> consumer) { |
| 227 | + return context -> { |
| 228 | + assertThat(context).hasSingleBean(SpringLiquibase.class); |
| 229 | + SpringLiquibase liquibase = context.getBean(SpringLiquibase.class); |
| 230 | + consumer.accept(liquibase); |
| 231 | + }; |
275 | 232 | }
|
276 | 233 |
|
277 | 234 | @Configuration
|
|
0 commit comments