!374 GlobalListener缺失测试

Merge pull request !374 from Ice/main
This commit is contained in:
Michael Yang 2023-10-24 03:22:31 +00:00 committed by Gitee
commit 5f7403c663
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 220 additions and 3 deletions

View File

@ -221,13 +221,15 @@ public class FlexGlobalConfig {
private void doGetSupportedSetListener(Class<?> childClass, Map<Class<?>, SetListener> listeners) { private void doGetSupportedSetListener(Class<?> childClass, Map<Class<?>, SetListener> listeners) {
SetListener setListener = null; SetListener setListener = null;
Class<?> listenersMapKey = null;
while (setListener == null && childClass != null) { while (setListener == null && childClass != null) {
setListener = entitySetListeners.get(childClass); setListener = entitySetListeners.get(childClass);
listenersMapKey = childClass.getSuperclass() == null ? childClass : childClass.getSuperclass();
childClass = childClass.getSuperclass(); childClass = childClass.getSuperclass();
} }
if (setListener != null) { if (setListener != null) {
listeners.put(childClass, setListener); listeners.put(listenersMapKey, setListener);
} }
} }
@ -264,13 +266,15 @@ public class FlexGlobalConfig {
private void doGetSupportedUpdateListener(Class<?> childClass, Map<Class<?>, UpdateListener> listeners) { private void doGetSupportedUpdateListener(Class<?> childClass, Map<Class<?>, UpdateListener> listeners) {
UpdateListener updateListener = null; UpdateListener updateListener = null;
Class<?> listenersMapKey = null;
while (updateListener == null && childClass != null) { while (updateListener == null && childClass != null) {
updateListener = entityUpdateListeners.get(childClass); updateListener = entityUpdateListeners.get(childClass);
listenersMapKey = childClass.getSuperclass() == null ? childClass : childClass.getSuperclass();
childClass = childClass.getSuperclass(); childClass = childClass.getSuperclass();
} }
if (updateListener != null) { if (updateListener != null) {
listeners.put(childClass, updateListener); listeners.put(listenersMapKey, updateListener);
} }
} }
@ -307,13 +311,15 @@ public class FlexGlobalConfig {
private void doGetSupportedInsertListener(Class<?> childClass, Map<Class<?>, InsertListener> listeners) { private void doGetSupportedInsertListener(Class<?> childClass, Map<Class<?>, InsertListener> listeners) {
InsertListener insertListener = null; InsertListener insertListener = null;
Class<?> listenersMapKey = null;
while (insertListener == null && childClass != null) { while (insertListener == null && childClass != null) {
insertListener = entityInsertListeners.get(childClass); insertListener = entityInsertListeners.get(childClass);
listenersMapKey = childClass.getSuperclass() == null ? childClass : childClass.getSuperclass();
childClass = childClass.getSuperclass(); childClass = childClass.getSuperclass();
} }
if (insertListener != null) { if (insertListener != null) {
listeners.put(childClass, insertListener); listeners.put(listenersMapKey, insertListener);
} }
} }

View File

@ -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);
}
}

View File

@ -0,0 +1,5 @@
package com.mybatisflex.test.listener.missionListenerFix;
public interface AccountAgeInsertListenerFlag {
}

View File

@ -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");
}
}

View File

@ -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;
}
}

View File

@ -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(false);
}
}

View File

@ -0,0 +1,5 @@
package com.mybatisflex.test.listener.missionListenerFix;
public interface LogicDeleteInsertListenerFlag {
}

View File

@ -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;
}
}

View File

@ -0,0 +1,84 @@
package com.mybatisflex.test.common;
import com.mybatisflex.annotation.InsertListener;
import com.mybatisflex.core.BaseMapper;
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.mybatis.Mappers;
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>> tempOnInsertListenerMap = new ConcurrentHashMap<>();//替代原本的缓存Map
List<InsertListener> insertListeners = MapUtil.computeIfAbsent(tempOnInsertListenerMap, 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);
AccountMissingListenerTestModel dbData = (AccountMissingListenerTestModel) baseMapper.selectOneById(accountMissingListenerTestModel.getId());
if (dbData.getDelete() == null || dbData.getDelete() != false) {
throw FlexExceptions.wrap("缺失的InsertListener【%s】", LogicDeleteInsertListener.class.getSimpleName());
}
if (dbData.getAge() == null || dbData.getAge() != 18) {
throw FlexExceptions.wrap("缺失的InsertListener【%s】", AccountAgeInsertListener.class.getSimpleName());
}
if (dbData.getUserName() == null || !dbData.getUserName().equals("测试缺失的监听器-userName")) {
throw FlexExceptions.wrap("缺失的InsertListener【%s】", AccountTableAnnoInsertListener.class.getSimpleName());
}
}
}