Skip to content

Commit

Permalink
add 优化代码,增加测试覆盖率
Browse files Browse the repository at this point in the history
  • Loading branch information
Robot committed Nov 27, 2023
1 parent 90337ce commit 4353826
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
15 changes: 7 additions & 8 deletions easy-enum/src/main/java/com/robot/dict/DictPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
*
* @author R
*/
class DictPool {
interface DictPool {
/**
* 存储所有字典
*/
private static final Map<Dict<?>, DictBean> DICT_MAP = new ConcurrentHashMap<>();
Map<Dict<?>, DictBean> DICT_MAP = new ConcurrentHashMap<>();
/**
* 枚举类和对应的字典集合
*/
private static final Map<Class<?>, List<DictBean>> DICT_CLASS_ITEMS_MAP = new ConcurrentHashMap<>();
Map<Class<?>, List<DictBean>> DICT_CLASS_ITEMS_MAP = new ConcurrentHashMap<>();

/**
* 放入字典和对应的code、text
Expand All @@ -29,9 +29,9 @@ static <T> void putDict(Dict<T> dict, T code, String text) {
Class<?> dictClass = dict.getClass();
boolean isEnumDeprecated = false;
try {
isEnumDeprecated = dictClass.isEnum() && dictClass.getDeclaredField(((Enum<?>) dict).name()).isAnnotationPresent(Deprecated.class);
} catch (NoSuchFieldException ignore) {
// impossible
isEnumDeprecated = dictClass.getDeclaredField(((Enum<?>) dict).name()).isAnnotationPresent(Deprecated.class);
} catch (ClassCastException | NoSuchFieldException e) {
throw new RuntimeException(dictClass + "不是枚举类", e);
}
DictBean dictBean = new DictBean(code, text, isEnumDeprecated);
DICT_MAP.put(dict, dictBean);
Expand Down Expand Up @@ -61,6 +61,5 @@ static List<DictBean> getAll(Class<? extends Dict<?>> clazz, boolean isExcludeDe
return dictBeans;
}

private DictPool() {
}

}
22 changes: 22 additions & 0 deletions easy-enum/src/test/java/com/robot/dict/DictPoolTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.robot.dict;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

class DictPoolTest {

@Test
void putDictException() {
// 不允许非枚举类使用字典池
try {
DictPool.putDict(new Dict<Integer>() {
}, 1, "1");
fail("No exception thrown.");
} catch (Exception e) {
assertTrue(e.getMessage().contains("不是枚举类"));
}
}

}

0 comments on commit 4353826

Please sign in to comment.