|
@@ -0,0 +1,350 @@
|
|
|
|
|
+package com.etotem.cfc.unit;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
|
|
+import com.etotem.cfc.entity.FamilyFoodOverview;
|
|
|
|
|
+import com.etotem.cfc.entity.Food;
|
|
|
|
|
+import com.etotem.cfc.entity.FoodRecommendIndex;
|
|
|
|
|
+import com.etotem.cfc.entity.UserCurrentFoodRecommend;
|
|
|
|
|
+import com.etotem.cfc.mapper.FamilyFoodOverviewMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.FoodRecommendIndexMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
|
|
+import com.etotem.cfc.service.DietRecommendationService;
|
|
|
|
|
+import com.etotem.cfc.service.FamilyFoodOverviewService;
|
|
|
|
|
+import com.etotem.cfc.service.FoodRecommendService;
|
|
|
|
|
+import com.etotem.cfc.service.FoodRecommendWeightEngine;
|
|
|
|
|
+import com.etotem.cfc.service.FoodService;
|
|
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.Serializable;
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Collection;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.anyCollection;
|
|
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 契约级单测(TDD-RED):食材推荐前端调整展示的<b>行为/默认值契约</b>
|
|
|
|
|
+ * (能力键 {@code food.frontend.adjustmentDisplay},任务 S-cfc-444,CR-08)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>溯源</b></p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>需求/设计:{@code docs/需求分析/2026-11-22-食材推荐加权规则引擎需求变更.md}
|
|
|
|
|
+ * §4.8 CR-08 三条 + §7.1 G-6 验收门禁。</li>
|
|
|
|
|
+ * <li>前端消费口径:{@code cfc-frontend/pages/diet/recommendation.vue}
|
|
|
|
|
+ * {@code buildAdjustmentReasons} 读取 {@code adjustmentReasons} 数组或
|
|
|
|
|
+ * {@code pairingReason}/{@code processingReason}/{@code diseaseReason};菜卡读取
|
|
|
|
|
+ * {@code processingWarning} / {@code pairingAvoidHint};家庭总览读取 {@code adjustment}。</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>与同批已合入契约的互补关系</b>:{@code FoodAdjustmentDisplayContractTest} 静态扫描
|
|
|
|
|
+ * recommendation.vue 模板,{@code FoodAdjustmentDisplayPayloadContractTest} 静态扫描 src/main
|
|
|
|
|
+ * 源码;<b>本类不重复源码扫描</b>,而是用 Mockito + 反射驱动真实服务方法,断言<b>返回载荷</b>中
|
|
|
|
|
+ * 契约字段是否存在、缺省值是否安全(空列表/空串/normal),并断言 11-09/11-10 既有口径不被破坏。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>覆盖流</b>(纯 Mockito / 反射,无 Spring / 无 DB / 无外部服务):</p>
|
|
|
|
|
+ * <ol>
|
|
|
|
|
+ * <li>基线(绿):11-09 四态数值映射、{@code normalizeMenu} 菜数硬保证结构。</li>
|
|
|
|
|
+ * <li>正常流(红):食材卡载荷须携带扩展原因(默认空列表);实体须承载扩展原因字段;
|
|
|
|
|
+ * 菜卡载荷须携带加工建议与配伍避让提示(默认空串)。</li>
|
|
|
|
|
+ * <li>边界流(红):家庭总览载荷须在保持 MIN_NEGATIVE 口径的同时透传 {@code adjustment}
|
|
|
|
|
+ * (缺省 {@code normal})。</li>
|
|
|
|
|
+ * <li>权限流:展示层数据权限由 JwtInterceptor 承载,无独立服务端权限分支,不适用。</li>
|
|
|
|
|
+ * </ol>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>RED 预期</b>:当前后端载荷仅含 11-09 的 {@code adjustment}/{@code adjustmentReason},
|
|
|
|
|
+ * 无扩展原因字段、无菜卡提示字段、总览无 adjustment 透传,故 4 例红 / 3 例绿。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>GREEN 升级指引</b>:实现方在 {@code getCurrentFoods} 食材卡 Map、{@code Food} /
|
|
|
|
|
+ * {@code UserCurrentFoodRecommend} 瞬态字段、{@code normalizeMenu} 菜品 Map、
|
|
|
|
|
+ * {@code FamilyFoodOverviewService.getOverviewMaps} 总览 Map 补齐上述字段(缺省安全值)后自动转绿。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class FoodAdjustmentDisplayBehaviorContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String CAP = "[food.frontend.adjustmentDisplay]";
|
|
|
|
|
+
|
|
|
|
|
+ /** §4.8 第1条:前端可消费的扩展原因载体(数组或独立字段,任一即可)。 */
|
|
|
|
|
+ private static final String[] EXTENDED_REASON_KEYS = {
|
|
|
|
|
+ "adjustmentReasons", "pairingReason", "processingReason", "diseaseReason", "diseaseWeightReason"
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ /** 归一化菜单输入:一道油炸菠菜豆腐(同时可触发加工建议与草酸×钙配伍提示)。 */
|
|
|
|
|
+ private static final String MENU_JSON = "{\"meals\":[{\"type\":\"lunch\",\"name\":\"午餐\",\"dishes\":["
|
|
|
|
|
+ + "{\"name\":\"菠菜炒豆腐\",\"type\":\"dish\",\"cooking_method\":\"deep_fried\","
|
|
|
|
|
+ + "\"nutrition\":{\"calories\":220}}]}]}";
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 基线对照(已实现 → 绿) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("基线-1:11-09 四态调整数值语义保持(§4.2 / §4.8 基线不回退)")
|
|
|
|
|
+ void baseline_fourStateAdjustmentScoreUnchanged() {
|
|
|
|
|
+ assertEquals(-50.0, FoodRecommendWeightEngine.healthAdjustScore("blocked"), 1e-9,
|
|
|
|
|
+ CAP + " 11-09 blocked 应映射 -50(§4.2)。");
|
|
|
|
|
+ assertEquals(-15.0, FoodRecommendWeightEngine.healthAdjustScore("warning"), 1e-9,
|
|
|
|
|
+ CAP + " 11-09 warning 应映射 -15(§4.2)。");
|
|
|
|
|
+ assertEquals(10.0, FoodRecommendWeightEngine.healthAdjustScore("boost"), 1e-9,
|
|
|
|
|
+ CAP + " 11-09 boost 应映射 +10(§4.2)。");
|
|
|
|
|
+ assertEquals(0.0, FoodRecommendWeightEngine.healthAdjustScore("normal"), 1e-9,
|
|
|
|
|
+ CAP + " 11-09 normal 应映射 0(§4.2)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("基线-2:normalizeMenu 菜数硬保证结构不被展示字段破坏(11-10 K4)")
|
|
|
|
|
+ void baseline_normalizeMenuKeepsDishStructure() {
|
|
|
|
|
+ Map<String, Object> normalized = normalizeMenu(MENU_JSON, 2);
|
|
|
|
|
+ assertNotNull(normalized, CAP + " normalizeMenu 应返回菜单 Map。");
|
|
|
|
|
+ List<Map<String, Object>> meals = asMapList(normalized.get("meals"));
|
|
|
|
|
+ assertFalse(meals.isEmpty(), CAP + " 菜单应至少含一餐。");
|
|
|
|
|
+ assertEquals("午餐", meals.get(0).get("name"), CAP + " 餐次名称应保留。");
|
|
|
|
|
+ List<Map<String, Object>> dishes = asMapList(meals.get(0).get("dishes"));
|
|
|
|
|
+ assertFalse(dishes.isEmpty(), CAP + " 11-10 菜数硬保证:菜列表不得为空。");
|
|
|
|
|
+ assertTrue(dishes.get(0).containsKey("name"), CAP + " 菜品应保留 name 字段。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== CR-08 缺口(预期红) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-1:食材卡载荷须携带扩展原因 adjustmentReasons(默认空列表,§4.8 第1条)")
|
|
|
|
|
+ void foodCardPayload_carriesExtendedReasons() {
|
|
|
|
|
+ Map<String, Object> card = firstCurrentFoodCard();
|
|
|
|
|
+
|
|
|
|
|
+ // 11-09 基线不得回退:基础标记与原因仍在。
|
|
|
|
|
+ assertEquals("normal", card.get("adjustment"), CAP + " §4.8 基线:11-09 adjustment 标记应保留。");
|
|
|
|
|
+ assertTrue(card.containsKey("adjustmentReason"), CAP + " §4.8 基线:11-09 adjustmentReason 应保留。");
|
|
|
|
|
+
|
|
|
|
|
+ // CR-08 缺口:前端 buildAdjustmentReasons 要读的字段后端载荷从未下发 → 渲染恒为空。
|
|
|
|
|
+ assertTrue(containsAnyKey(card, EXTENDED_REASON_KEYS),
|
|
|
|
|
+ CAP + " §4.8 第1条:食材卡载荷须在 adjustmentReason 之外追加配伍/加工/病症权重原因"
|
|
|
|
|
+ + "(adjustmentReasons 数组或 pairingReason/processingReason/diseaseReason),"
|
|
|
|
|
+ + "当前 getCurrentFoods 输出仅有 11-09 基础字段,前端扩展原因恒不渲染。");
|
|
|
|
|
+ if (card.containsKey("adjustmentReasons")) {
|
|
|
|
|
+ Object reasons = card.get("adjustmentReasons");
|
|
|
|
|
+ assertTrue(reasons instanceof List,
|
|
|
|
|
+ CAP + " §4.8 第1条:adjustmentReasons 应为列表(无原因时为空列表,不得为 null/非列表),实际="
|
|
|
|
|
+ + reasons);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-2:食材实体须承载扩展原因字段(集合默认非 null 空,§4.8 第1条 A3 链路)")
|
|
|
|
|
+ void foodEntity_carriesExtendedReasonFields() {
|
|
|
|
|
+ boolean found = false;
|
|
|
|
|
+ for (String className : new String[]{
|
|
|
|
|
+ "com.etotem.cfc.entity.Food",
|
|
|
|
|
+ "com.etotem.cfc.entity.UserCurrentFoodRecommend"}) {
|
|
|
|
|
+ Class<?> clazz = loadClass(className);
|
|
|
|
|
+ if (clazz == null) {
|
|
|
|
|
+ fail(CAP + " 实体类不存在:" + className);
|
|
|
|
|
+ }
|
|
|
|
|
+ for (Field field : clazz.getDeclaredFields()) {
|
|
|
|
|
+ if (!containsAny(field.getName(), EXTENDED_REASON_KEYS)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ found = true;
|
|
|
|
|
+ if (Collection.class.isAssignableFrom(field.getType())) {
|
|
|
|
|
+ // 缺省安全值:无原因时前端读 length 不应 NPE,须为空集合而非 null。
|
|
|
|
|
+ Object bean = newInstance(clazz);
|
|
|
|
|
+ try {
|
|
|
|
|
+ field.setAccessible(true);
|
|
|
|
|
+ Object value = field.get(bean);
|
|
|
|
|
+ assertNotNull(value, CAP + " §4.8 第1条:" + field.getName()
|
|
|
|
|
+ + " 默认值不得为 null,应为空列表。");
|
|
|
|
|
+ assertTrue(((Collection<?>) value).isEmpty(), CAP + " §4.8 第1条:"
|
|
|
|
|
+ + field.getName() + " 无原因时默认应为空列表。");
|
|
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
|
|
+ fail(CAP + " 读取字段失败:" + field.getName() + " - " + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(found,
|
|
|
|
|
+ CAP + " §4.8 第1条:Food / UserCurrentFoodRecommend 应有承载配伍/加工/病症权重原因的"
|
|
|
|
|
+ + "瞬态字段(adjustmentReasons 或 pairingReason/processingReason/diseaseReason);"
|
|
|
|
|
+ + "当前仅有 11-09 的 adjustment/adjustmentReason,扩展原因无处透传。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-3:菜卡载荷须携带 processingWarning / pairingAvoidHint(默认空串,§4.8 第2条)")
|
|
|
|
|
+ void dishPayload_carriesWarningsWithSafeDefaults() {
|
|
|
|
|
+ Map<String, Object> dish = firstDish(normalizeMenu(MENU_JSON, 2));
|
|
|
|
|
+ assertHintDefault(dish, "processingWarning");
|
|
|
|
|
+ assertHintDefault(dish, "pairingAvoidHint");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-1:家庭总览载荷须透传 adjustment(默认 normal)且保持 11-10 聚合口径(§4.8 第3条)")
|
|
|
|
|
+ void familyOverviewPayload_passesThroughAdjustment() {
|
|
|
|
|
+ FamilyFoodOverviewMapper mapper = mock(FamilyFoodOverviewMapper.class);
|
|
|
|
|
+ FamilyFoodOverview row = new FamilyFoodOverview();
|
|
|
|
|
+ row.setFamilyId(5L);
|
|
|
|
|
+ row.setFoodId(11L);
|
|
|
|
|
+ row.setFoodName("菠菜");
|
|
|
|
|
+ row.setCategory("蔬菜");
|
|
|
|
|
+ row.setOverviewScore(-20);
|
|
|
|
|
+ row.setMinIndexScore(-20);
|
|
|
|
|
+ row.setAggregateRule("MIN_NEGATIVE");
|
|
|
|
|
+ row.setMemberCount(2);
|
|
|
|
|
+ row.setMemberScores("[]");
|
|
|
|
|
+ when(mapper.selectList(any(Wrapper.class))).thenReturn(Collections.singletonList(row));
|
|
|
|
|
+
|
|
|
|
|
+ FamilyFoodOverviewService service = new FamilyFoodOverviewService();
|
|
|
|
|
+ inject(service, "familyFoodOverviewMapper", mapper);
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, Object>> maps = service.getOverviewMaps(5L);
|
|
|
|
|
+ assertEquals(1, maps.size(), CAP + " 应返回 1 条总览记录。");
|
|
|
|
|
+ Map<String, Object> item = maps.get(0);
|
|
|
|
|
+
|
|
|
|
|
+ // 11-10 口径不得改变。
|
|
|
|
|
+ assertEquals(Integer.valueOf(-20), item.get("overviewScore"), CAP + " overviewScore 应原样透传。");
|
|
|
|
|
+ assertEquals("MIN_NEGATIVE", item.get("aggregateRule"), CAP + " 11-10 MIN_NEGATIVE 聚合口径不得改。");
|
|
|
|
|
+
|
|
|
|
|
+ // CR-08 缺口:前端 overview-adjustment 读取 adjustment,后端总览 Map 未下发。
|
|
|
|
|
+ assertTrue(item.containsKey("adjustment"),
|
|
|
|
|
+ CAP + " §4.8 第3条:家庭总览须在保持 11-10 聚合口径的同时透传调整标记 adjustment;"
|
|
|
|
|
+ + "当前 getOverviewMaps 输出无该字段,前端 overview-adjustment 恒不渲染。");
|
|
|
|
|
+ assertEquals("normal", item.get("adjustment"),
|
|
|
|
|
+ CAP + " §4.8 第3条:无调整信息时 adjustment 默认应为 normal。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("权限流:展示层数据权限由上游接口与 JwtInterceptor 承载,无独立服务端权限分支")
|
|
|
|
|
+ void permissionFlowNotApplicable() {
|
|
|
|
|
+ assertTrue(true,
|
|
|
|
|
+ CAP + " 权限流不适用:§4.8 为前端展示增强,数据权限由 index/by-family/caution 接口承载。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 辅助 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 通过 Mockito 驱动真实 getCurrentFoods,取首张食材卡载荷。 */
|
|
|
|
|
+ @SuppressWarnings({"unchecked", "rawtypes"})
|
|
|
|
|
+ private Map<String, Object> firstCurrentFoodCard() {
|
|
|
|
|
+ FoodRecommendIndexMapper indexMapper = mock(FoodRecommendIndexMapper.class);
|
|
|
|
|
+ FamilyMemberMapper memberMapper = mock(FamilyMemberMapper.class);
|
|
|
|
|
+ UserMapper userMapper = mock(UserMapper.class);
|
|
|
|
|
+ FoodService foodService = mock(FoodService.class);
|
|
|
|
|
+
|
|
|
|
|
+ FoodRecommendIndex idx = new FoodRecommendIndex();
|
|
|
|
|
+ idx.setUserId(7L);
|
|
|
|
|
+ idx.setFoodId(11L);
|
|
|
|
|
+ idx.setFoodName("菠菜");
|
|
|
|
|
+ idx.setIndexScore(60);
|
|
|
|
|
+ when(indexMapper.selectList(any(Wrapper.class)))
|
|
|
|
|
+ .thenReturn(Collections.singletonList(idx));
|
|
|
|
|
+
|
|
|
|
|
+ Food food = new Food();
|
|
|
|
|
+ food.setId(11L);
|
|
|
|
|
+ food.setName("菠菜");
|
|
|
|
|
+ food.setCategory("蔬菜");
|
|
|
|
|
+ when(foodService.listByIds(anyCollection())).thenReturn(Collections.singletonList(food));
|
|
|
|
|
+
|
|
|
|
|
+ // user/family 反查均不命中 → resolveFoodIndexUserId 原样返回 7L;healthFilterService 留 null
|
|
|
|
|
+ // → 走 normal 基线(不依赖健康问卷外部数据)。
|
|
|
|
|
+ when(userMapper.selectById(7L)).thenReturn(null);
|
|
|
|
|
+ when(memberMapper.selectById(7L)).thenReturn(null);
|
|
|
|
|
+
|
|
|
|
|
+ FoodRecommendService service = new FoodRecommendService();
|
|
|
|
|
+ inject(service, "foodRecommendIndexMapper", indexMapper);
|
|
|
|
|
+ inject(service, "familyMemberMapper", memberMapper);
|
|
|
|
|
+ inject(service, "userMapper", userMapper);
|
|
|
|
|
+ inject(service, "foodService", foodService);
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, Object>> cards = service.getCurrentFoods(7L);
|
|
|
|
|
+ assertFalse(cards.isEmpty(), CAP + " getCurrentFoods 应返回食材卡载荷。");
|
|
|
|
|
+ return cards.get(0);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 反射调用 private normalizeMenu(JSON, 人数, 食材名),不依赖 Spring / AI / DB。 */
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ private Map<String, Object> normalizeMenu(String menuJson, int participants) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Method method = DietRecommendationService.class.getDeclaredMethod(
|
|
|
|
|
+ "normalizeMenu", String.class, int.class, List.class);
|
|
|
|
|
+ method.setAccessible(true);
|
|
|
|
|
+ return (Map<String, Object>) method.invoke(
|
|
|
|
|
+ new DietRecommendationService(), menuJson, participants, Collections.<String>emptyList());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ fail(CAP + " normalizeMenu 反射调用失败:" + e.getMessage());
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Map<String, Object> firstDish(Map<String, Object> normalized) {
|
|
|
|
|
+ List<Map<String, Object>> meals = asMapList(normalized.get("meals"));
|
|
|
|
|
+ assertFalse(meals.isEmpty(), CAP + " 菜单应至少含一餐。");
|
|
|
|
|
+ List<Map<String, Object>> dishes = asMapList(meals.get(0).get("dishes"));
|
|
|
|
|
+ assertFalse(dishes.isEmpty(), CAP + " 餐次应至少含一道菜。");
|
|
|
|
|
+ return dishes.get(0);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void assertHintDefault(Map<String, Object> dish, String key) {
|
|
|
|
|
+ assertTrue(dish.containsKey(key),
|
|
|
|
|
+ CAP + " §4.8 第2条:菜卡载荷须携带 " + key + "(加工建议 / 配伍避让提示);"
|
|
|
|
|
+ + "当前 normalizeMenu 菜品 Map 无该字段,前端 dish-" + key + " 恒不渲染。");
|
|
|
|
|
+ Object value = dish.get(key);
|
|
|
|
|
+ assertTrue(value == null || "".equals(value),
|
|
|
|
|
+ CAP + " §4.8 第2条:" + key + " 无提示时默认应为空串,实际=" + value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ private List<Map<String, Object>> asMapList(Object value) {
|
|
|
|
|
+ return value instanceof List ? (List<Map<String, Object>>) value : Collections.<Map<String, Object>>emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean containsAnyKey(Map<String, Object> map, String... keys) {
|
|
|
|
|
+ for (String key : keys) {
|
|
|
|
|
+ if (map.containsKey(key)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean containsAny(String text, String... markers) {
|
|
|
|
|
+ for (String marker : markers) {
|
|
|
|
|
+ if (text != null && text.contains(marker)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Class<?> loadClass(String name) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Object newInstance(Class<?> clazz) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return clazz.getDeclaredConstructor().newInstance();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ fail(CAP + " 实例化失败:" + clazz.getName() + " - " + e.getMessage());
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void inject(Object target, String fieldName, Object value) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Field field = target.getClass().getDeclaredField(fieldName);
|
|
|
|
|
+ field.setAccessible(true);
|
|
|
|
|
+ field.set(target, value);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ fail(CAP + " 注入依赖失败:" + fieldName + " - " + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|