|
|
@ -16,6 +16,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.autoconfigure.flyway;
|
|
|
|
package org.springframework.boot.autoconfigure.flyway;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
@ -25,13 +26,14 @@ import javax.sql.DataSource;
|
|
|
|
|
|
|
|
|
|
|
|
import org.flywaydb.core.Flyway;
|
|
|
|
import org.flywaydb.core.Flyway;
|
|
|
|
import org.flywaydb.core.api.MigrationVersion;
|
|
|
|
import org.flywaydb.core.api.MigrationVersion;
|
|
|
|
import org.flywaydb.core.api.callback.BaseFlywayCallback;
|
|
|
|
import org.flywaydb.core.api.callback.FlywayCallback;
|
|
|
|
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
|
|
|
|
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
|
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Rule;
|
|
|
|
import org.junit.Rule;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.rules.ExpectedException;
|
|
|
|
import org.junit.rules.ExpectedException;
|
|
|
|
|
|
|
|
import org.mockito.InOrder;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.BeanCreationException;
|
|
|
|
import org.springframework.beans.factory.BeanCreationException;
|
|
|
|
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
|
|
|
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
|
|
@ -44,12 +46,16 @@ import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
|
import org.springframework.core.Ordered;
|
|
|
|
import org.springframework.core.Ordered;
|
|
|
|
|
|
|
|
import org.springframework.core.annotation.Order;
|
|
|
|
import org.springframework.core.env.MapPropertySource;
|
|
|
|
import org.springframework.core.env.MapPropertySource;
|
|
|
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
|
|
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
|
|
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
|
|
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
|
|
|
import static org.mockito.Mockito.inOrder;
|
|
|
|
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Tests for {@link FlywayAutoConfiguration}.
|
|
|
|
* Tests for {@link FlywayAutoConfiguration}.
|
|
|
@ -204,16 +210,6 @@ public class FlywayAutoConfigurationTests {
|
|
|
|
assertThat(initializer.getOrder()).isEqualTo(Ordered.HIGHEST_PRECEDENCE);
|
|
|
|
assertThat(initializer.getOrder()).isEqualTo(Ordered.HIGHEST_PRECEDENCE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void customFlywayCallback() throws Exception {
|
|
|
|
|
|
|
|
registerAndRefresh(EmbeddedDataSourceConfiguration.class,
|
|
|
|
|
|
|
|
FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
|
|
|
|
|
|
|
|
MockOneFlywayCallback.class, MockTwoFlywayCallback.class);
|
|
|
|
|
|
|
|
assertThat(this.context.getBean(Flyway.class)).isNotNull();
|
|
|
|
|
|
|
|
this.context.getBean(MockOneFlywayCallback.class).assertCalled();
|
|
|
|
|
|
|
|
this.context.getBean(MockTwoFlywayCallback.class).assertCalled();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void customFlywayWithJpa() throws Exception {
|
|
|
|
public void customFlywayWithJpa() throws Exception {
|
|
|
|
registerAndRefresh(CustomFlywayWithJpaConfiguration.class,
|
|
|
|
registerAndRefresh(CustomFlywayWithJpaConfiguration.class,
|
|
|
@ -258,6 +254,23 @@ public class FlywayAutoConfigurationTests {
|
|
|
|
"classpath:db/vendors/h2", "classpath:db/changelog");
|
|
|
|
"classpath:db/vendors/h2", "classpath:db/changelog");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void callbacksAreConfiguredAndOrdered() throws Exception {
|
|
|
|
|
|
|
|
registerAndRefresh(EmbeddedDataSourceConfiguration.class,
|
|
|
|
|
|
|
|
FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
|
|
|
|
|
|
|
|
CallbackConfiguration.class);
|
|
|
|
|
|
|
|
assertThat(this.context.getBeansOfType(Flyway.class)).hasSize(1);
|
|
|
|
|
|
|
|
Flyway flyway = this.context.getBean(Flyway.class);
|
|
|
|
|
|
|
|
FlywayCallback callbackOne = this.context.getBean("callbackOne",
|
|
|
|
|
|
|
|
FlywayCallback.class);
|
|
|
|
|
|
|
|
FlywayCallback callbackTwo = this.context.getBean("callbackTwo",
|
|
|
|
|
|
|
|
FlywayCallback.class);
|
|
|
|
|
|
|
|
assertThat(flyway.getCallbacks()).containsExactly(callbackTwo, callbackOne);
|
|
|
|
|
|
|
|
InOrder orderedCallbacks = inOrder(callbackOne, callbackTwo);
|
|
|
|
|
|
|
|
orderedCallbacks.verify(callbackTwo).beforeMigrate(any(Connection.class));
|
|
|
|
|
|
|
|
orderedCallbacks.verify(callbackOne).beforeMigrate(any(Connection.class));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void registerAndRefresh(Class<?>... annotatedClasses) {
|
|
|
|
private void registerAndRefresh(Class<?>... annotatedClasses) {
|
|
|
|
this.context.register(annotatedClasses);
|
|
|
|
this.context.register(annotatedClasses);
|
|
|
|
this.context.refresh();
|
|
|
|
this.context.refresh();
|
|
|
@ -337,34 +350,21 @@ public class FlywayAutoConfigurationTests {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
@Configuration
|
|
|
|
protected static class MockOneFlywayCallback
|
|
|
|
static class CallbackConfiguration {
|
|
|
|
extends BaseFlywayCallback {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean called = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public MockOneFlywayCallback() {
|
|
|
|
|
|
|
|
this.called = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void assertCalled() {
|
|
|
|
@Bean
|
|
|
|
assertThat(this.called).isTrue();
|
|
|
|
@Order(1)
|
|
|
|
|
|
|
|
public FlywayCallback callbackOne() {
|
|
|
|
|
|
|
|
return mock(FlywayCallback.class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
|
|
|
protected static class MockTwoFlywayCallback
|
|
|
|
|
|
|
|
extends BaseFlywayCallback {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean called = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public MockTwoFlywayCallback() {
|
|
|
|
@Bean
|
|
|
|
this.called = true;
|
|
|
|
@Order(0)
|
|
|
|
|
|
|
|
public FlywayCallback callbackTwo() {
|
|
|
|
|
|
|
|
return mock(FlywayCallback.class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void assertCalled() {
|
|
|
|
|
|
|
|
assertThat(this.called).isTrue();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|