Skip to content

Commit 73b74dc

Browse files
authored
Merge pull request #615 from kazuki43zoo/gh-541.add-test-order-interceptors
Add test for ordering interceptor
2 parents b88f5a6 + 680170b commit 73b74dc

File tree

1 file changed

+74
-6
lines changed

1 file changed

+74
-6
lines changed

mybatis-spring-boot-autoconfigure/src/test/java/org/mybatis/spring/boot/autoconfigure/MybatisAutoConfigurationTest.java

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import org.springframework.context.annotation.Configuration;
8585
import org.springframework.context.annotation.Primary;
8686
import org.springframework.context.support.SimpleThreadScope;
87+
import org.springframework.core.annotation.Order;
8788

8889
/**
8990
* Tests for {@link MybatisAutoConfiguration}
@@ -323,13 +324,34 @@ void testDefaultBootConfiguration() {
323324
}
324325

325326
@Test
326-
void testWithInterceptors() {
327+
void testWithInterceptorsOrder1() {
327328
this.context.register(EmbeddedDataSourceConfiguration.class, MybatisInterceptorConfiguration.class,
328329
MybatisAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
329330
this.context.refresh();
330331
assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1);
331332
assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1);
332-
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors()).hasSize(1);
333+
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors()).hasSize(2);
334+
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(0))
335+
.isInstanceOf(MyInterceptor2.class);
336+
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(1))
337+
.isInstanceOf(MyInterceptor.class);
338+
339+
this.context.close();
340+
}
341+
342+
@Test
343+
void testWithInterceptorsOrder2() {
344+
this.context.register(EmbeddedDataSourceConfiguration.class, MybatisInterceptorConfiguration2.class,
345+
MybatisAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
346+
this.context.refresh();
347+
assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1);
348+
assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1);
349+
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors()).hasSize(2);
350+
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(0))
351+
.isInstanceOf(MyInterceptor.class);
352+
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(1))
353+
.isInstanceOf(MyInterceptor2.class);
354+
333355
this.context.close();
334356
}
335357

@@ -365,8 +387,9 @@ void testMixedWithConfigurationFileAndInterceptor() {
365387
assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1);
366388
assertThat(this.context.getBeanNamesForType(CityMapper.class)).hasSize(1);
367389
assertThat(configuration.getDefaultFetchSize()).isEqualTo(1000);
368-
assertThat(configuration.getInterceptors()).hasSize(1);
369-
assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor.class);
390+
assertThat(configuration.getInterceptors()).hasSize(2);
391+
assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor2.class);
392+
assertThat(configuration.getInterceptors().get(1)).isInstanceOf(MyInterceptor.class);
370393
}
371394

372395
@Test
@@ -462,8 +485,9 @@ void testMixedWithFullConfigurations() {
462485
assertThat(configuration.getMappedStatementNames())
463486
.contains("org.mybatis.spring.boot.autoconfigure.mapper.CityMapper.findById");
464487
assertThat(this.context.getBean(SqlSessionTemplate.class).getExecutorType()).isEqualTo(ExecutorType.REUSE);
465-
assertThat(configuration.getInterceptors()).hasSize(1);
466-
assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor.class);
488+
assertThat(configuration.getInterceptors()).hasSize(2);
489+
assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor2.class);
490+
assertThat(configuration.getInterceptors().get(1)).isInstanceOf(MyInterceptor.class);
467491
assertThat(configuration.getDatabaseId()).isEqualTo("h2");
468492
}
469493

@@ -803,10 +827,35 @@ public CityMapperImpl cityMapper() {
803827
static class MybatisInterceptorConfiguration {
804828

805829
@Bean
830+
@Order(2)
806831
public MyInterceptor myInterceptor() {
807832
return new MyInterceptor();
808833
}
809834

835+
@Bean
836+
@Order(1)
837+
public MyInterceptor2 myInterceptor2() {
838+
return new MyInterceptor2();
839+
}
840+
841+
}
842+
843+
@Configuration
844+
@EnableAutoConfiguration
845+
static class MybatisInterceptorConfiguration2 {
846+
847+
@Bean
848+
@Order(1)
849+
public MyInterceptor myInterceptor() {
850+
return new MyInterceptor();
851+
}
852+
853+
@Bean
854+
@Order(2)
855+
public MyInterceptor2 myInterceptor2() {
856+
return new MyInterceptor2();
857+
}
858+
810859
}
811860

812861
@Configuration
@@ -870,6 +919,25 @@ public void setProperties(Properties properties) {
870919
}
871920
}
872921

922+
@Intercepts(@Signature(type = Map.class, method = "get", args = { Object.class }))
923+
static class MyInterceptor2 implements Interceptor {
924+
925+
@Override
926+
public Object intercept(Invocation invocation) {
927+
return "Test2";
928+
}
929+
930+
@Override
931+
public Object plugin(Object target) {
932+
return Plugin.wrap(target, this);
933+
}
934+
935+
@Override
936+
public void setProperties(Properties properties) {
937+
938+
}
939+
}
940+
873941
@Configuration
874942
static class DatabaseProvidersConfiguration {
875943

0 commit comments

Comments
 (0)