feat: w06 类型推断增强辅助
All checks were successful
缓存序列化结构检查 / cache-schema-check (push) Has been skipped
All checks were successful
缓存序列化结构检查 / cache-schema-check (push) Has been skipped
This commit is contained in:
71
src/test/java/com/codechecker/cache/detector/CacheReadHintDetectorTest.java
vendored
Normal file
71
src/test/java/com/codechecker/cache/detector/CacheReadHintDetectorTest.java
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
package com.codechecker.cache.detector;
|
||||
|
||||
import com.codechecker.cache.schema.SourceIndex;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class CacheReadHintDetectorTest {
|
||||
|
||||
@Test
|
||||
void detectsParseObjectWithRedisGetPairing() {
|
||||
String vo = ""
|
||||
+ "package demo.model;\n"
|
||||
+ "public class FooVo { private String name; }\n";
|
||||
String source = ""
|
||||
+ "package demo;\n"
|
||||
+ "import demo.model.FooVo;\n"
|
||||
+ "public class CacheService {\n"
|
||||
+ " private RedisUtil redisUtil;\n"
|
||||
+ " private static final String KEY = \"demo:foo:\";\n"
|
||||
+ " public FooVo load(String id) {\n"
|
||||
+ " String raw = redisUtil.getString(KEY + id);\n"
|
||||
+ " return JSON.parseObject(raw, FooVo.class);\n"
|
||||
+ " }\n"
|
||||
+ " public void save(String id, FooVo vo) {\n"
|
||||
+ " redisUtil.insert(KEY + id, JSON.toJSONString(vo), 60);\n"
|
||||
+ " }\n"
|
||||
+ "}\n";
|
||||
|
||||
SourceIndex index = new SourceIndex();
|
||||
index.addSource(vo);
|
||||
index.addSource(source);
|
||||
|
||||
List<CacheReadHint> hints = new CacheReadHintDetector(index)
|
||||
.detect("CacheService.java", source);
|
||||
assertEquals(1, hints.size());
|
||||
CacheReadHint hint = hints.get(0);
|
||||
assertEquals("demo.model.FooVo", hint.getResolvedValueType());
|
||||
assertFalse(hint.isRootArray());
|
||||
assertEquals("demo:foo:*", hint.getResolvedKeyPattern());
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectsParseArray() {
|
||||
String vo = ""
|
||||
+ "package demo.model;\n"
|
||||
+ "public class ItemVo { private int rank; }\n";
|
||||
String source = ""
|
||||
+ "package demo;\n"
|
||||
+ "import demo.model.ItemVo;\n"
|
||||
+ "public class ListCache {\n"
|
||||
+ " public List<ItemVo> load(String raw) {\n"
|
||||
+ " return JSON.parseArray(raw, ItemVo.class);\n"
|
||||
+ " }\n"
|
||||
+ "}\n";
|
||||
|
||||
SourceIndex index = new SourceIndex();
|
||||
index.addSource(vo);
|
||||
index.addSource(source);
|
||||
|
||||
List<CacheReadHint> hints = new CacheReadHintDetector(index)
|
||||
.detect("ListCache.java", source);
|
||||
assertEquals(1, hints.size());
|
||||
assertTrue(hints.get(0).isRootArray());
|
||||
assertEquals("demo.model.ItemVo", hints.get(0).getResolvedValueType());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user