Skip to content

Commit

Permalink
Merge pull request #615 from kazuki43zoo/gh-541.add-test-order-interc…
Browse files Browse the repository at this point in the history
…eptors

Add test for ordering interceptor
  • Loading branch information
kazuki43zoo committed Dec 31, 2021
2 parents b88f5a6 + 680170b commit 73b74dc
Showing 1 changed file with 74 additions and 6 deletions.
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

0 comments on commit 73b74dc

Please sign in to comment.