Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test code for the MyBatis plugin. #559

Merged
merged 11 commits into from
Aug 8, 2024

Conversation

homejim
Copy link
Contributor

@homejim homejim commented Aug 8, 2024

1.Add unit tests for #558:: When using annotation-based SQL, verify the reload after the class has been modified.
2.Add MybatisSpringBootTest to verify reload behavior in Spring Boot applications

hejiajin and others added 11 commits July 31, 2024 23:32
…E) occurs during reload, and also differentiate between MyBatis and MyBatis-Spring modes
Fix the issue where using the insertAt method might fail due to changes in the processPropertyPlaceHolders method body. Add a list variable at the beginning of the method, include the factory in the list after factory initialization, and finally execute the removal logic at the end of the method.

Why not execute the removal right after initialization? I tried that, but it fails during reload.
Update MyBatisTransformers.java
1.Refactor MyBatisTransformers code to improve readability and maintainability.
3.Feature Added: Enable SQL reload when using annotation-based SQL.
2.Feature Added: Enable reload when there are changes in entity classes, such as result entity or parameter entity classes.
@homejim
Copy link
Contributor Author

homejim commented Aug 8, 2024

@skybber Added unit tests for the MyBatis plugin code that I previously submitted.

@skybber
Copy link
Contributor

skybber commented Aug 8, 2024

Great work , THANKS!!!

@skybber skybber merged commit 4ac7f5a into HotswapProjects:master Aug 8, 2024
@homejim homejim deleted the feat_mybatis_entity branch August 9, 2024 01:45
@skybber
Copy link
Contributor

skybber commented Aug 10, 2024

@homejim testUserAnnotationSQL is failing in mvn clean package, seems it is working from IDE.

@homejim
Copy link
Contributor Author

homejim commented Aug 10, 2024

@homejim testUserAnnotationSQL is failing in mvn clean package, seems it is working from IDE.

Hi, @skybber :

I downloaded the latest HotswapAgent code, then commented out all other unit test code for MyBatis, keeping only MyBatisPluginAnnoTest and removing @ignore. After compiling, I ran mvn test for the MyBatis plugin directly, and it worked fine. How can I reproduce the issue you mentioned?

image

@skybber
Copy link
Contributor

skybber commented Aug 10, 2024

I see next (mvn clean package):

Running org.hotswap.agent.plugin.mybatis.MyBatisPluginAnnoTest
HOTSWAP AGENT: 16:08:33.645 DEBUG (org.hotswap.agent.plugin.mybatis.MyBatisPlugin) - MyBatisPlugin - configuration file registered : /home/lada/workspaces/workspace-ha/HotswapAgent/plugin/hotswap-agent-mybatis-plugin/target/test-classes/org/hotswap/agent/plugin/mybatis/mybatis-config-anno.xml
HOTSWAP AGENT: 16:08:33.645 DEBUG (org.hotswap.agent.plugin.mybatis.proxy.ConfigurationProxy) - MyBatis runs in MyBatis-Spring mode, so there is no need to cache configuration-related data
2024-08-10 16:08:33.657 DEBUG 16648 --- [           main] o.a.i.transaction.jdbc.JdbcTransaction   : Opening JDBC Connection
2024-08-10 16:08:33.658 DEBUG 16648 --- [           main] o.a.i.transaction.jdbc.JdbcTransaction   : Setting autocommit to false on JDBC Connection [org.hsqldb.jdbc.JDBCConnection@2b0454d2]
2024-08-10 16:08:33.659 DEBUG 16648 --- [           main] o.a.i.transaction.jdbc.JdbcTransaction   : Resetting autocommit to true on JDBC Connection [org.hsqldb.jdbc.JDBCConnection@2b0454d2]
2024-08-10 16:08:33.659 DEBUG 16648 --- [           main] o.a.i.transaction.jdbc.JdbcTransaction   : Closing JDBC Connection [org.hsqldb.jdbc.JDBCConnection@2b0454d2]
HOTSWAP AGENT: 16:08:33.660 RELOAD (org.hotswap.agent.config.PluginManager) - Reloading classes [org.hotswap.agent.plugin.mybatis.testBeansHotswap.UserMapper] (autoHotswap)
2024-08-10 16:08:34.440  WARN 16648 --- [      Thread-26] o.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'mapper' and 'org.hotswap.agent.plugin.mybatis.Mapper' mapperInterface. Bean already defined with the same name!
2024-08-10 16:08:34.440  WARN 16648 --- [      Thread-26] o.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'bootUserMapper' and 'org.hotswap.agent.plugin.mybatis.springboot.BootUserMapper' mapperInterface. Bean already defined with the same name!
2024-08-10 16:08:34.441  WARN 16648 --- [      Thread-26] o.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'bootUserMapper2' and 'org.hotswap.agent.plugin.mybatis.springboot.BootUserMapper2' mapperInterface. Bean already defined with the same name!
2024-08-10 16:08:34.441  WARN 16648 --- [      Thread-26] o.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'userMapper' and 'org.hotswap.agent.plugin.mybatis.testBeansHotswap.UserMapper' mapperInterface. Bean already defined with the same name!
2024-08-10 16:08:34.441  WARN 16648 --- [      Thread-26] o.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'userMapper2' and 'org.hotswap.agent.plugin.mybatis.testBeansHotswap.UserMapper2' mapperInterface. Bean already defined with the same name!
2024-08-10 16:08:34.441  WARN 16648 --- [      Thread-26] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[org.hotswap.agent.plugin.mybatis]' package. Please check your configuration.
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.268 sec <<< FAILURE! - in org.hotswap.agent.plugin.mybatis.MyBatisPluginAnnoTest
testUserAnnotationSQL(org.hotswap.agent.plugin.mybatis.MyBatisPluginAnnoTest)  Time elapsed: 1.251 sec  <<< FAILURE!
java.lang.AssertionError: null
        at org.junit.Assert.fail(Assert.java:87)
        at org.junit.Assert.assertTrue(Assert.java:42)
        at org.junit.Assert.assertTrue(Assert.java:53)
        at org.hotswap.agent.plugin.mybatis.MyBatisPluginAnnoTest.swapClasses(MyBatisPluginAnnoTest.java:98)
        at org.hotswap.agent.plugin.mybatis.MyBatisPluginAnnoTest.testUserAnnotationSQL(MyBatisPluginAnnoTest.java:84)

2024-08-10 16:08:34.913  INFO 16648 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2024-08-10 16:08:34.918  INFO 16648 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Results :

Failed tests: 
  MyBatisPluginAnnoTest.testUserAnnotationSQL:84->swapClasses:98 null

@skybber
Copy link
Contributor

skybber commented Aug 10, 2024

With mvn clean package -X :

HOTSWAP AGENT: 16:35:49.610 DEBUG (org.hotswap.agent.plugin.mybatis.MyBatisRefreshCommands) - Refreshing MyBatis configuration.
java.lang.IllegalStateException: Error invoking method org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory([]) on org.mybatis.spring.SqlSessionFactoryBean@1cbc5693, params []
        at org.hotswap.agent.util.ReflectionHelper.invoke(ReflectionHelper.java:63)
        at org.hotswap.agent.util.ReflectionHelper.invoke(ReflectionHelper.java:120)
        at org.hotswap.agent.plugin.mybatis.proxy.SpringMybatisConfigurationProxy.refreshProxiedConfiguration(SpringMybatisConfigurationProxy.java:44)
        at org.hotswap.agent.plugin.mybatis.proxy.SpringMybatisConfigurationProxy.refreshProxiedConfigurations(SpringMybatisConfigurationProxy.java:36)
        at org.hotswap.agent.plugin.mybatis.MyBatisRefreshCommands.reloadConfiguration(MyBatisRefreshCommands.java:51)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.hotswap.agent.command.ReflectionCommand.doExecuteReflectionCommand(ReflectionCommand.java:207)
        at org.hotswap.agent.command.ReflectionCommand.executeCommand(ReflectionCommand.java:168)
        at org.hotswap.agent.command.impl.CommandExecutor.run(CommandExecutor.java:43)
Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.hotswap.agent.util.ReflectionHelper.invoke(ReflectionHelper.java:58)
        ... 11 more
Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [/home/lada/workspaces/workspace-ha/HotswapAgent/plugin/hotswap-agent-mybatis-plugin/target/test-classes/swapXML/BootUserMapper.xml]'; nested exception is java.io.FileNotFoundException: /home/lada/workspaces/workspace-ha/HotswapAgent/plugin/hotswap-agent-mybatis-plugin/target/test-classes/swapXML/BootUserMapper.xml
        at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:613)
        ... 16 more
Caused by: java.io.FileNotFoundException: /home/lada/workspaces/workspace-ha/HotswapAgent/plugin/hotswap-agent-mybatis-plugin/target/test-classes/swapXML/BootUserMapper.xml
        at org.springframework.core.io.FileSystemResource.getInputStream(FileSystemResource.java:192)
        at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:609)
        ... 16 more

seems that the file is missing in target:

lada@skybber ~/workspaces/workspace-ha/HotswapAgent $ ls /home/lada/workspaces/workspace-ha/HotswapAgent/plugin/hotswap-agent-mybatis-plugin/target/test-classes/swapXML/
BootUserMapper1.xml  BootUserMapper2.xml

@homejim
Copy link
Contributor Author

homejim commented Aug 10, 2024

According to the image, my system is Windows. I re-downloaded the latest code, commented out Ignore, and tried running mvn clean package multiple times, but I did not encounter the mybatis plugin unit test failure.

image

@homejim
Copy link
Contributor Author

homejim commented Aug 10, 2024

With mvn clean package -X :

HOTSWAP AGENT: 16:35:49.610 DEBUG (org.hotswap.agent.plugin.mybatis.MyBatisRefreshCommands) - Refreshing MyBatis configuration.
java.lang.IllegalStateException: Error invoking method org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory([]) on org.mybatis.spring.SqlSessionFactoryBean@1cbc5693, params []
        at org.hotswap.agent.util.ReflectionHelper.invoke(ReflectionHelper.java:63)
        at org.hotswap.agent.util.ReflectionHelper.invoke(ReflectionHelper.java:120)
        at org.hotswap.agent.plugin.mybatis.proxy.SpringMybatisConfigurationProxy.refreshProxiedConfiguration(SpringMybatisConfigurationProxy.java:44)
        at org.hotswap.agent.plugin.mybatis.proxy.SpringMybatisConfigurationProxy.refreshProxiedConfigurations(SpringMybatisConfigurationProxy.java:36)
        at org.hotswap.agent.plugin.mybatis.MyBatisRefreshCommands.reloadConfiguration(MyBatisRefreshCommands.java:51)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.hotswap.agent.command.ReflectionCommand.doExecuteReflectionCommand(ReflectionCommand.java:207)
        at org.hotswap.agent.command.ReflectionCommand.executeCommand(ReflectionCommand.java:168)
        at org.hotswap.agent.command.impl.CommandExecutor.run(CommandExecutor.java:43)
Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.hotswap.agent.util.ReflectionHelper.invoke(ReflectionHelper.java:58)
        ... 11 more
Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [/home/lada/workspaces/workspace-ha/HotswapAgent/plugin/hotswap-agent-mybatis-plugin/target/test-classes/swapXML/BootUserMapper.xml]'; nested exception is java.io.FileNotFoundException: /home/lada/workspaces/workspace-ha/HotswapAgent/plugin/hotswap-agent-mybatis-plugin/target/test-classes/swapXML/BootUserMapper.xml
        at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:613)
        ... 16 more
Caused by: java.io.FileNotFoundException: /home/lada/workspaces/workspace-ha/HotswapAgent/plugin/hotswap-agent-mybatis-plugin/target/test-classes/swapXML/BootUserMapper.xml
        at org.springframework.core.io.FileSystemResource.getInputStream(FileSystemResource.java:192)
        at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:609)
        ... 16 more

seems that the file is missing in target:

lada@skybber ~/workspaces/workspace-ha/HotswapAgent $ ls /home/lada/workspaces/workspace-ha/HotswapAgent/plugin/hotswap-agent-mybatis-plugin/target/test-classes/swapXML/
BootUserMapper1.xml  BootUserMapper2.xml

This is a different issue; it will ultimately not affect the unit test results。It sounds like there might be a conflict between org.hotswap.agent.plugin.mybatis.MybatisSpringBootTest and org.hotswap.agent.plugin.mybatis.MybatisSpringTest. I'll work on resolving this issue as soon as possible

But using mvn clean package actually runs successfully.
image

@skybber
Copy link
Contributor

skybber commented Aug 10, 2024

I cloned a fresh HA repository and ran mvn clean package, which completed without issues. git diff shows no differences on original repo, I'm puzzled about where the problem might be occurring. May be I'll start to use the new one.

@homejim
Copy link
Contributor Author

homejim commented Aug 10, 2024

I cloned a fresh HA repository and ran mvn clean package, which completed without issues. git diff shows no differences on original repo, I'm puzzled about where the problem might be occurring. May be I'll start to use the new one.

Okay. I wrote a HA plugin for MyBatis Plus. Should my code be a standalone MyBatis Plus plugin, or should it be combined with the MyBatis plugin?@skybber

https://github.com/baomidou/mybatis-plus

@skybber
Copy link
Contributor

skybber commented Aug 11, 2024

I'll leave it up to your decision. Generally:

  • If the MyBatis-Plus plugin shares most of its code with the MyBatis plugin, it would be better to combine the plugins into one.
  • The MyBatisPlus plugin can also use the MyBatis plugin as a dependency.
  • If the MyBatisPlus plugin is not very extensive and there are no plans for significant future expansion, it’s fine to keep it as a separate plugin.
  • Each additional new plugin slows down HA - fatjar, especially on architectures with a large number of classloaders, because all classes must be copied to all classloaders (unless delegation is used).

@homejim
Copy link
Contributor Author

homejim commented Aug 12, 2024

I'll leave it up to your decision. Generally:

  • If the MyBatis-Plus plugin shares most of its code with the MyBatis plugin, it would be better to combine the plugins into one.
  • The MyBatisPlus plugin can also use the MyBatis plugin as a dependency.
  • If the MyBatisPlus plugin is not very extensive and there are no plans for significant future expansion, it’s fine to keep it as a separate plugin.
  • Each additional new plugin slows down HA - fatjar, especially on architectures with a large number of classloaders, because all classes must be copied to all classloaders (unless delegation is used).

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants