Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.support.SimpleThreadScope;
import org.springframework.core.annotation.Order;

/**
* Tests for {@link MybatisAutoConfiguration}
Expand Down Expand Up @@ -323,13 +324,34 @@ void testDefaultBootConfiguration() {
}

@Test
void testWithInterceptors() {
void testWithInterceptorsOrder1() {
this.context.register(EmbeddedDataSourceConfiguration.class, MybatisInterceptorConfiguration.class,
MybatisAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1);
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors()).hasSize(1);
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors()).hasSize(2);
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(0))
.isInstanceOf(MyInterceptor2.class);
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(1))
.isInstanceOf(MyInterceptor.class);

this.context.close();
}

@Test
void testWithInterceptorsOrder2() {
this.context.register(EmbeddedDataSourceConfiguration.class, MybatisInterceptorConfiguration2.class,
MybatisAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1);
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors()).hasSize(2);
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(0))
.isInstanceOf(MyInterceptor.class);
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(1))
.isInstanceOf(MyInterceptor2.class);

this.context.close();
}

Expand Down Expand Up @@ -365,8 +387,9 @@ void testMixedWithConfigurationFileAndInterceptor() {
assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(CityMapper.class)).hasSize(1);
assertThat(configuration.getDefaultFetchSize()).isEqualTo(1000);
assertThat(configuration.getInterceptors()).hasSize(1);
assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor.class);
assertThat(configuration.getInterceptors()).hasSize(2);
assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor2.class);
assertThat(configuration.getInterceptors().get(1)).isInstanceOf(MyInterceptor.class);
}

@Test
Expand Down Expand Up @@ -462,8 +485,9 @@ void testMixedWithFullConfigurations() {
assertThat(configuration.getMappedStatementNames())
.contains("org.mybatis.spring.boot.autoconfigure.mapper.CityMapper.findById");
assertThat(this.context.getBean(SqlSessionTemplate.class).getExecutorType()).isEqualTo(ExecutorType.REUSE);
assertThat(configuration.getInterceptors()).hasSize(1);
assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor.class);
assertThat(configuration.getInterceptors()).hasSize(2);
assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor2.class);
assertThat(configuration.getInterceptors().get(1)).isInstanceOf(MyInterceptor.class);
assertThat(configuration.getDatabaseId()).isEqualTo("h2");
}

Expand Down Expand Up @@ -803,10 +827,35 @@ public CityMapperImpl cityMapper() {
static class MybatisInterceptorConfiguration {

@Bean
@Order(2)
public MyInterceptor myInterceptor() {
return new MyInterceptor();
}

@Bean
@Order(1)
public MyInterceptor2 myInterceptor2() {
return new MyInterceptor2();
}

}

@Configuration
@EnableAutoConfiguration
static class MybatisInterceptorConfiguration2 {

@Bean
@Order(1)
public MyInterceptor myInterceptor() {
return new MyInterceptor();
}

@Bean
@Order(2)
public MyInterceptor2 myInterceptor2() {
return new MyInterceptor2();
}

}

@Configuration
Expand Down Expand Up @@ -870,6 +919,25 @@ public void setProperties(Properties properties) {
}
}

@Intercepts(@Signature(type = Map.class, method = "get", args = { Object.class }))
static class MyInterceptor2 implements Interceptor {

@Override
public Object intercept(Invocation invocation) {
return "Test2";
}

@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}

@Override
public void setProperties(Properties properties) {

}
}

@Configuration
static class DatabaseProvidersConfiguration {

Expand Down