mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
add:Listener缺失测试
This commit is contained in:
parent
0e498c4ae2
commit
2388ceb4de
@ -0,0 +1,13 @@
|
||||
package com.mybatisflex.test.listener.missionListenerFix;
|
||||
|
||||
import com.mybatisflex.annotation.InsertListener;
|
||||
import com.mybatisflex.test.model.AccountMissingListenerTestModel;
|
||||
|
||||
public class AccountAgeInsertListener implements InsertListener {
|
||||
|
||||
@Override
|
||||
public void onInsert(Object entity) {
|
||||
AccountMissingListenerTestModel model = (AccountMissingListenerTestModel) entity;
|
||||
model.setAge(18);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
package com.mybatisflex.test.listener.missionListenerFix;
|
||||
|
||||
public interface AccountAgeInsertListenerFlag {
|
||||
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
package com.mybatisflex.test.listener.missionListenerFix;
|
||||
|
||||
import com.mybatisflex.annotation.InsertListener;
|
||||
import com.mybatisflex.test.model.AccountMissingListenerTestModel;
|
||||
|
||||
public class AccountTableAnnoInsertListener implements InsertListener {
|
||||
|
||||
@Override
|
||||
public void onInsert(Object entity) {
|
||||
AccountMissingListenerTestModel model = (AccountMissingListenerTestModel) entity;
|
||||
model.setUserName("测试缺失的监听器-userName");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package com.mybatisflex.test.listener.missionListenerFix;
|
||||
|
||||
import com.mybatisflex.annotation.Column;
|
||||
|
||||
public class BaseLogicDelete implements LogicDeleteInsertListenerFlag {
|
||||
|
||||
@Column(isLogicDelete = true)
|
||||
private Boolean isDelete;
|
||||
|
||||
public Boolean getDelete() {
|
||||
return isDelete;
|
||||
}
|
||||
|
||||
public void setDelete(Boolean delete) {
|
||||
isDelete = delete;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package com.mybatisflex.test.listener.missionListenerFix;
|
||||
|
||||
import com.mybatisflex.annotation.InsertListener;
|
||||
|
||||
public class LogicDeleteInsertListener implements InsertListener {
|
||||
|
||||
@Override
|
||||
public void onInsert(Object entity) {
|
||||
BaseLogicDelete logicDeleteEntity = (BaseLogicDelete) entity;
|
||||
logicDeleteEntity.setDelete(true);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
package com.mybatisflex.test.listener.missionListenerFix;
|
||||
|
||||
public interface LogicDeleteInsertListenerFlag {
|
||||
|
||||
}
|
||||
@ -0,0 +1,61 @@
|
||||
package com.mybatisflex.test.model;
|
||||
|
||||
import com.mybatisflex.annotation.*;
|
||||
import com.mybatisflex.test.listener.missionListenerFix.*;
|
||||
|
||||
/**
|
||||
* 缺失的监听器测试
|
||||
*
|
||||
* @author Ice 2023/10/23
|
||||
* @version 1.0
|
||||
*/
|
||||
@Table(value = "tb_account", onInsert = AccountTableAnnoInsertListener.class)
|
||||
public class AccountMissingListenerTestModel extends BaseLogicDelete implements AccountAgeInsertListenerFlag {
|
||||
|
||||
/**
|
||||
* 主键。
|
||||
*/
|
||||
@Id(keyType = KeyType.Auto)
|
||||
private Long id;
|
||||
|
||||
private String userName;
|
||||
|
||||
private Integer age;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,69 @@
|
||||
package com.mybatisflex.test.common;
|
||||
|
||||
import com.mybatisflex.annotation.InsertListener;
|
||||
import com.mybatisflex.core.FlexGlobalConfig;
|
||||
import com.mybatisflex.core.exception.FlexAssert;
|
||||
import com.mybatisflex.core.exception.FlexExceptions;
|
||||
import com.mybatisflex.core.exception.locale.LocalizedFormats;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.table.TableInfoFactory;
|
||||
import com.mybatisflex.core.util.CollectionUtil;
|
||||
import com.mybatisflex.test.listener.missionListenerFix.*;
|
||||
import com.mybatisflex.test.model.AccountMissingListenerTestModel;
|
||||
import org.apache.ibatis.util.MapUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 监听器测试
|
||||
*
|
||||
* @author Ice 2023/10/23
|
||||
* @version 1.0
|
||||
*/
|
||||
@SpringBootTest
|
||||
class ListenerTest {
|
||||
|
||||
@Test
|
||||
void missingListenerTest() {
|
||||
|
||||
AccountMissingListenerTestModel accountMissingListenerTestModel = new AccountMissingListenerTestModel();
|
||||
|
||||
//加入配置
|
||||
FlexGlobalConfig config = FlexGlobalConfig.getDefaultConfig();
|
||||
config.registerInsertListener(new LogicDeleteInsertListener(), LogicDeleteInsertListenerFlag.class);
|
||||
config.registerInsertListener(new AccountAgeInsertListener(), AccountAgeInsertListenerFlag.class);
|
||||
|
||||
//获取TableInfo
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(AccountMissingListenerTestModel.class);
|
||||
|
||||
//执行测试 ===> Listener列表比对
|
||||
|
||||
Map<Class<?>, List<InsertListener>> tempInsertListenerMap = new ConcurrentHashMap<>();//替代原本的缓存Map
|
||||
|
||||
List<InsertListener> insertListeners = MapUtil.computeIfAbsent(tempInsertListenerMap, AccountMissingListenerTestModel.class, aClass -> {
|
||||
List<InsertListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
||||
.getSupportedInsertListener(AccountMissingListenerTestModel.class, CollectionUtil.isNotEmpty(tableInfo.getOnInsertListeners()));
|
||||
List<InsertListener> allListeners = CollectionUtil.merge(tableInfo.getOnInsertListeners(), globalListeners);
|
||||
Collections.sort(allListeners);
|
||||
return allListeners;
|
||||
});
|
||||
|
||||
List<? extends Class<? extends InsertListener>> resolvedInsertListeners = insertListeners.stream().map(insertListener -> insertListener.getClass()).collect(Collectors.toList());
|
||||
for (Class<?> clazz : CollectionUtil.newArrayList(LogicDeleteInsertListener.class, AccountAgeInsertListener.class, AccountTableAnnoInsertListener.class)) {
|
||||
if (!resolvedInsertListeners.contains(clazz)) {
|
||||
throw FlexExceptions.wrap("缺失的InsertListener【%s】", clazz.getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
//执行测试 ===> 插入结果比对
|
||||
// BaseMapper baseMapper = Mappers.ofEntityClass(accountMissingListenerTestModel.getClass());
|
||||
// baseMapper.insert(accountMissingListenerTestModel);
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user