🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 模块说明 | | 说明 | | --- | --- | | | | ## repeater-aide 1. 比较器的定义是在repeater-aide中; 2. 结果的比对执行是在console的ReplayServiceImpl: ```java public RepeaterResult<String> saveRepeat(String body) { // 这里省略了一大段 Comparable comparable = ComparableFactory.instance().createDefault(); // simple compare CompareResult result = comparable.compare(actual, expect); replay.setSuccess(!result.hasDifference()); try { replay.setDiffResult(JacksonUtil.serialize(result.getDifferences() .stream() .map(differenceConvert::convert) .collect(Collectors.toList()), false)); } catch (SerializeException e) { log.error("error occurred serialize diff result", e); return RepeaterResult.builder().message("operate failed").build(); } Replay calllback = replayDao.saveAndFlush(replay); return RepeaterResult.builder().success(true).message("operate success").data("-/-").build(); } ``` 比较器执行主要代码:com.alibaba.jvm.sandbox.repeater.aide.compare.IntegratedComparator,每个比较器都会比较一遍结果。 ```java @Override public CompareResult compare(Object left, Object right) { // try clear last compare result tryClear(); // dispatch compare task List<Path> paths = new ArrayList<Path>(0); try { dispatch(left, right, paths); } catch (Exception e) { // LogUtil.error("error occurred when dispatch compare task", e); addDifference(left, right, Difference.Type.COMPARE_ERR, paths); } return new CompareResult(left, right, differences); } public void dispatch(Object left, Object right, List<Path> paths) { // cycle reference detect try { String nodeName = pathLocator.encode(paths); leftDetector.detect(left, nodeName); rightDetector.detect(right, nodeName); } catch (CycleReferenceException e) { // LogUtil.error("error occurred when dispatch compare task", e); return; } // do compare,每个比较器都会比较一遍 for (Comparator comparator : comparators) { if (comparator.accept(left, right)) { comparator.compare(left, right, paths, this); break; } } } ``` ### 自定义结果比较器 为什么要自定义结果比较器? 1. 重构等行为可能会造成包名的不一致、方法签名不一致; 2. 字段增加也会对对比较结果产生影响; 按理来讲,结果比较更加偏向于逻辑校验。比较结果应该忽略上述问题的影响。