optimize:GlobalListener缺失测试

update:GlobalListener缺失测试包名
This commit is contained in:
Ice 2023-10-24 11:33:51 +08:00
parent 9fa2d5df2f
commit bb4d821d64
10 changed files with 65 additions and 36 deletions

View File

@ -1,4 +1,4 @@
package com.mybatisflex.test.listener.missionListenerFix;
package com.mybatisflex.test.listener.missingListenerFix;
import com.mybatisflex.annotation.InsertListener;
import com.mybatisflex.test.model.AccountMissingListenerTestModel;

View File

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

View File

@ -1,4 +1,4 @@
package com.mybatisflex.test.listener.missionListenerFix;
package com.mybatisflex.test.listener.missingListenerFix;
import com.mybatisflex.annotation.InsertListener;
import com.mybatisflex.test.model.AccountMissingListenerTestModel;

View File

@ -1,4 +1,4 @@
package com.mybatisflex.test.listener.missionListenerFix;
package com.mybatisflex.test.listener.missingListenerFix;
import com.mybatisflex.annotation.Column;

View File

@ -1,4 +1,4 @@
package com.mybatisflex.test.listener.missionListenerFix;
package com.mybatisflex.test.listener.missingListenerFix;
import com.mybatisflex.annotation.InsertListener;

View File

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

View File

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

View File

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

View File

@ -1,7 +1,11 @@
package com.mybatisflex.test.model;
import com.mybatisflex.annotation.*;
import com.mybatisflex.test.listener.missionListenerFix.*;
import com.mybatisflex.test.listener.missingListenerFix.AccountAgeInsertListenerFlag;
import com.mybatisflex.test.listener.missingListenerFix.AccountTableAnnoInsertListener;
import com.mybatisflex.test.listener.missingListenerFix.BaseLogicDelete;
import java.util.Objects;
/**
* 缺失的监听器测试
@ -46,6 +50,28 @@ public class AccountMissingListenerTestModel extends BaseLogicDelete implements
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AccountMissingListenerTestModel that = (AccountMissingListenerTestModel) o;
return Objects.equals(id, that.id) && Objects.equals(userName, that.userName) && Objects.equals(age, that.age) && Objects.equals(getDelete(), that.getDelete());
}
@Override
public int hashCode() {
return Objects.hash(id, userName, age, getDelete());
}
@Override
public String toString() {
return "AccountMissingListenerTestModel{" +
"id=" + id +
", userName='" + userName + '\'' +
", age=" + age +
", isDelete=" + getDelete() +
'}';
}
}

View File

@ -3,16 +3,14 @@ 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.listener.missingListenerFix.*;
import com.mybatisflex.test.model.AccountMissingListenerTestModel;
import org.apache.ibatis.util.MapUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@ -45,7 +43,6 @@ class ListenerTest {
TableInfo tableInfo = TableInfoFactory.ofEntityClass(AccountMissingListenerTestModel.class);
//执行测试 ===> Listener列表比对
Map<Class<?>, List<InsertListener>> tempOnInsertListenerMap = new ConcurrentHashMap<>();//替代原本的缓存Map
List<InsertListener> insertListeners = MapUtil.computeIfAbsent(tempOnInsertListenerMap, AccountMissingListenerTestModel.class, aClass -> {
@ -56,29 +53,35 @@ class ListenerTest {
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)) {
List<Class<? extends InsertListener>> resolvedInsertListeners = insertListeners.stream().map(InsertListener::getClass).collect(Collectors.toList());
List<Class<? extends InsertListener>> expectedInsertListeners = CollectionUtil.newArrayList(LogicDeleteInsertListener.class, AccountAgeInsertListener.class, AccountTableAnnoInsertListener.class);
Assertions.assertTrue(
() -> {
for (Class<?> clazz : expectedInsertListeners) {
if (!resolvedInsertListeners.contains(clazz)) {
throw FlexExceptions.wrap("缺失的InsertListener【%s】", clazz.getSimpleName());
return false;
}
}
return true;
},
String.format("InsertListener与预期结果不一致\n预期Listener列表:%s\n实际Listener列表:%s", expectedInsertListeners, resolvedInsertListeners)
);
//执行测试 ===> 插入结果比对
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());
}
//预期数据
AccountMissingListenerTestModel expectedData = new AccountMissingListenerTestModel();
expectedData.setId(dbData.getId());
expectedData.setUserName("测试缺失的监听器-userName");
expectedData.setAge(18);
expectedData.setDelete(false);
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());
}
Assertions.assertEquals(expectedData, dbData);
}
}