|
1 | 1 | /*
|
2 |
| - * Copyright 2010-2022 the original author or authors. |
| 2 | + * Copyright 2010-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
22 | 22 |
|
23 | 23 | import java.math.BigDecimal;
|
24 | 24 | import java.math.BigInteger;
|
| 25 | +import java.sql.CallableStatement; |
| 26 | +import java.sql.PreparedStatement; |
| 27 | +import java.sql.ResultSet; |
| 28 | +import java.sql.SQLException; |
25 | 29 | import java.util.Properties;
|
26 | 30 | import java.util.UUID;
|
27 | 31 | import java.util.concurrent.atomic.AtomicInteger;
|
28 | 32 | import java.util.concurrent.atomic.AtomicLong;
|
| 33 | +import java.util.stream.Collectors; |
29 | 34 |
|
30 | 35 | import org.apache.ibatis.cache.impl.PerpetualCache;
|
| 36 | +import org.apache.ibatis.executor.Executor; |
31 | 37 | import org.apache.ibatis.io.JBoss6VFS;
|
| 38 | +import org.apache.ibatis.mapping.MappedStatement; |
| 39 | +import org.apache.ibatis.plugin.Interceptor; |
| 40 | +import org.apache.ibatis.plugin.Intercepts; |
| 41 | +import org.apache.ibatis.plugin.Invocation; |
| 42 | +import org.apache.ibatis.plugin.Signature; |
32 | 43 | import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
|
33 | 44 | import org.apache.ibatis.reflection.factory.ObjectFactory;
|
34 | 45 | import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
|
|
41 | 52 | import org.apache.ibatis.session.SqlSessionFactory;
|
42 | 53 | import org.apache.ibatis.transaction.TransactionFactory;
|
43 | 54 | import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
|
| 55 | +import org.apache.ibatis.type.BaseTypeHandler; |
44 | 56 | import org.apache.ibatis.type.EnumOrdinalTypeHandler;
|
| 57 | +import org.apache.ibatis.type.JdbcType; |
45 | 58 | import org.apache.ibatis.type.TypeAliasRegistry;
|
46 | 59 | import org.apache.ibatis.type.TypeException;
|
| 60 | +import org.apache.ibatis.type.TypeHandler; |
47 | 61 | import org.apache.ibatis.type.TypeHandlerRegistry;
|
48 | 62 | import org.junit.jupiter.api.Test;
|
49 | 63 | import org.mybatis.core.jdk.type.AtomicNumberTypeHandler;
|
@@ -492,6 +506,88 @@ void testScriptingLanguageDriverWithDefault() throws Exception {
|
492 | 506 | assertThat(registry.getDriver(RawLanguageDriver.class)).isNotNull();
|
493 | 507 | }
|
494 | 508 |
|
| 509 | + @Test |
| 510 | + void testAppendableMethod() throws Exception { |
| 511 | + setupFactoryBean(); |
| 512 | + // add values |
| 513 | + this.factoryBean.addScriptingLanguageDrivers(new MyLanguageDriver1()); |
| 514 | + this.factoryBean.addScriptingLanguageDrivers(new MyLanguageDriver2()); |
| 515 | + this.factoryBean.addPlugins(new MyPlugin1(), new MyPlugin2()); |
| 516 | + this.factoryBean.addPlugins(new MyPlugin3()); |
| 517 | + this.factoryBean.addTypeHandlers(new MyTypeHandler1()); |
| 518 | + this.factoryBean.addTypeHandlers(new MyTypeHandler2(), new MyTypeHandler3()); |
| 519 | + this.factoryBean.addTypeAliases(MyTypeHandler1.class, MyTypeHandler2.class, MyTypeHandler3.class); |
| 520 | + this.factoryBean.addTypeAliases(MyPlugin1.class); |
| 521 | + this.factoryBean.addMapperLocations(new ClassPathResource("org/mybatis/spring/TestMapper.xml"), |
| 522 | + new ClassPathResource("org/mybatis/spring/TestMapper2.xml")); |
| 523 | + this.factoryBean.addMapperLocations(new ClassPathResource("org/mybatis/spring/TestMapper3.xml")); |
| 524 | + // ignore null value |
| 525 | + this.factoryBean.addScriptingLanguageDrivers(null); |
| 526 | + this.factoryBean.addPlugins(null); |
| 527 | + this.factoryBean.addTypeHandlers(null); |
| 528 | + this.factoryBean.addTypeAliases(null); |
| 529 | + this.factoryBean.addMapperLocations(null); |
| 530 | + SqlSessionFactory factory = this.factoryBean.getObject(); |
| 531 | + LanguageDriverRegistry languageDriverRegistry = factory.getConfiguration().getLanguageRegistry(); |
| 532 | + TypeHandlerRegistry typeHandlerRegistry = factory.getConfiguration().getTypeHandlerRegistry(); |
| 533 | + TypeAliasRegistry typeAliasRegistry = factory.getConfiguration().getTypeAliasRegistry(); |
| 534 | + assertThat(languageDriverRegistry.getDriver(MyLanguageDriver1.class)).isNotNull(); |
| 535 | + assertThat(languageDriverRegistry.getDriver(MyLanguageDriver2.class)).isNotNull(); |
| 536 | + assertThat(typeHandlerRegistry.getTypeHandlers().stream().map(TypeHandler::getClass).map(Class::getSimpleName) |
| 537 | + .collect(Collectors.toSet())).contains(MyTypeHandler1.class.getSimpleName(), |
| 538 | + MyTypeHandler2.class.getSimpleName(), MyTypeHandler3.class.getSimpleName()); |
| 539 | + assertThat(typeAliasRegistry.getTypeAliases()).containsKeys(MyTypeHandler1.class.getSimpleName().toLowerCase(), |
| 540 | + MyTypeHandler2.class.getSimpleName().toLowerCase(), MyTypeHandler3.class.getSimpleName().toLowerCase(), |
| 541 | + MyPlugin1.class.getSimpleName().toLowerCase()); |
| 542 | + assertThat(factory.getConfiguration().getMappedStatement("org.mybatis.spring.TestMapper.findFail")).isNotNull(); |
| 543 | + assertThat(factory.getConfiguration().getMappedStatement("org.mybatis.spring.TestMapper2.selectOne")).isNotNull(); |
| 544 | + assertThat(factory.getConfiguration().getMappedStatement("org.mybatis.spring.TestMapper3.selectOne")).isNotNull(); |
| 545 | + assertThat( |
| 546 | + factory.getConfiguration().getInterceptors().stream().map(Interceptor::getClass).map(Class::getSimpleName)) |
| 547 | + .contains(MyPlugin1.class.getSimpleName(), MyPlugin2.class.getSimpleName(), |
| 548 | + MyPlugin3.class.getSimpleName()); |
| 549 | + } |
| 550 | + |
| 551 | + @Test |
| 552 | + void testAppendableMethodWithEmpty() throws Exception { |
| 553 | + setupFactoryBean(); |
| 554 | + this.factoryBean.addScriptingLanguageDrivers(); |
| 555 | + this.factoryBean.addPlugins(); |
| 556 | + this.factoryBean.addTypeHandlers(); |
| 557 | + this.factoryBean.addTypeAliases(); |
| 558 | + this.factoryBean.addMapperLocations(); |
| 559 | + SqlSessionFactory factory = this.factoryBean.getObject(); |
| 560 | + LanguageDriverRegistry languageDriverRegistry = factory.getConfiguration().getLanguageRegistry(); |
| 561 | + TypeHandlerRegistry typeHandlerRegistry = factory.getConfiguration().getTypeHandlerRegistry(); |
| 562 | + TypeAliasRegistry typeAliasRegistry = factory.getConfiguration().getTypeAliasRegistry(); |
| 563 | + assertThat(languageDriverRegistry.getDriver(MyLanguageDriver1.class)).isNull(); |
| 564 | + assertThat(languageDriverRegistry.getDriver(MyLanguageDriver2.class)).isNull(); |
| 565 | + assertThat(typeHandlerRegistry.getTypeHandlers()).hasSize(40); |
| 566 | + assertThat(typeAliasRegistry.getTypeAliases()).hasSize(80); |
| 567 | + assertThat(factory.getConfiguration().getMappedStatementNames()).isEmpty(); |
| 568 | + assertThat(factory.getConfiguration().getInterceptors()).isEmpty(); |
| 569 | + } |
| 570 | + |
| 571 | + @Test |
| 572 | + void testAppendableMethodWithNull() throws Exception { |
| 573 | + setupFactoryBean(); |
| 574 | + this.factoryBean.addScriptingLanguageDrivers(null); |
| 575 | + this.factoryBean.addPlugins(null); |
| 576 | + this.factoryBean.addTypeHandlers(null); |
| 577 | + this.factoryBean.addTypeAliases(null); |
| 578 | + this.factoryBean.addMapperLocations(null); |
| 579 | + SqlSessionFactory factory = this.factoryBean.getObject(); |
| 580 | + LanguageDriverRegistry languageDriverRegistry = factory.getConfiguration().getLanguageRegistry(); |
| 581 | + TypeHandlerRegistry typeHandlerRegistry = factory.getConfiguration().getTypeHandlerRegistry(); |
| 582 | + TypeAliasRegistry typeAliasRegistry = factory.getConfiguration().getTypeAliasRegistry(); |
| 583 | + assertThat(languageDriverRegistry.getDriver(MyLanguageDriver1.class)).isNull(); |
| 584 | + assertThat(languageDriverRegistry.getDriver(MyLanguageDriver2.class)).isNull(); |
| 585 | + assertThat(typeHandlerRegistry.getTypeHandlers()).hasSize(40); |
| 586 | + assertThat(typeAliasRegistry.getTypeAliases()).hasSize(80); |
| 587 | + assertThat(factory.getConfiguration().getMappedStatementNames()).isEmpty(); |
| 588 | + assertThat(factory.getConfiguration().getInterceptors()).isEmpty(); |
| 589 | + } |
| 590 | + |
495 | 591 | private void assertDefaultConfig(SqlSessionFactory factory) {
|
496 | 592 | assertConfig(factory, SqlSessionFactoryBean.class.getSimpleName(),
|
497 | 593 | org.mybatis.spring.transaction.SpringManagedTransactionFactory.class);
|
@@ -522,6 +618,71 @@ private static class MyLanguageDriver1 extends RawLanguageDriver {
|
522 | 618 | private static class MyLanguageDriver2 extends RawLanguageDriver {
|
523 | 619 | }
|
524 | 620 |
|
| 621 | + private static class MyBasePlugin implements Interceptor { |
| 622 | + |
| 623 | + @Override |
| 624 | + public Object intercept(Invocation invocation) throws Throwable { |
| 625 | + return null; |
| 626 | + } |
| 627 | + |
| 628 | + @Override |
| 629 | + public Object plugin(Object target) { |
| 630 | + return Interceptor.super.plugin(target); |
| 631 | + } |
| 632 | + |
| 633 | + @Override |
| 634 | + public void setProperties(Properties properties) { |
| 635 | + Interceptor.super.setProperties(properties); |
| 636 | + } |
| 637 | + } |
| 638 | + |
| 639 | + @Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) }) |
| 640 | + private static class MyPlugin1 extends MyBasePlugin { |
| 641 | + |
| 642 | + } |
| 643 | + |
| 644 | + @Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) }) |
| 645 | + private static class MyPlugin2 extends MyBasePlugin { |
| 646 | + |
| 647 | + } |
| 648 | + |
| 649 | + @Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) }) |
| 650 | + private static class MyPlugin3 extends MyBasePlugin { |
| 651 | + |
| 652 | + } |
| 653 | + |
| 654 | + private static class MyBaseTypeHandler extends BaseTypeHandler<String> { |
| 655 | + |
| 656 | + @Override |
| 657 | + public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) |
| 658 | + throws SQLException { |
| 659 | + } |
| 660 | + |
| 661 | + @Override |
| 662 | + public String getNullableResult(ResultSet rs, String columnName) throws SQLException { |
| 663 | + return null; |
| 664 | + } |
| 665 | + |
| 666 | + @Override |
| 667 | + public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { |
| 668 | + return null; |
| 669 | + } |
| 670 | + |
| 671 | + @Override |
| 672 | + public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { |
| 673 | + return null; |
| 674 | + } |
| 675 | + } |
| 676 | + |
| 677 | + private static class MyTypeHandler1 extends MyBaseTypeHandler { |
| 678 | + } |
| 679 | + |
| 680 | + private static class MyTypeHandler2 extends MyBaseTypeHandler { |
| 681 | + } |
| 682 | + |
| 683 | + private static class MyTypeHandler3 extends MyBaseTypeHandler { |
| 684 | + } |
| 685 | + |
525 | 686 | private static enum MyEnum {
|
526 | 687 | }
|
527 | 688 |
|
|
0 commit comments