B2 JUEL 跨流回归:新 jakarta.el 与老 activiti JUEL 语义等价差分(真实语料)

B1 后带 conditionExpression 的 EL 写流从「抛 Can't find scripting engine」变「真求值」,
需证新引擎求值与老 activiti JUEL 语义等价(否则条件转移结果偏移会静默污染所有 EL 写流)。
- 真实语料:dev DB SYS_SERVICEFLOW 抽取 153 个去重 conditionExpression(XML 实体解码),
  落 test resource。特征:全 ${...}、无算术(数值提升风险 moot)、无 #{}、
  布尔/比较/null·空串/方法调用(.length/.booleanValue/.get_ID)/三元
- 差分:老 activiti JuelScriptEngineFactory(activiti-engine-5.14.jar 经独立 URLClassLoader
  隔离加载,绝不进 app 运行时)vs 新 JuelScriptEngine;153 表达式 × 9 binding 场景 = 1377 次比对
- 结果:完全等价 1376 / 真实偏移 0 / 已知良性差异 1——${isCover.booleanValue()==false} 当
  isCover=null 时老抛 NPE 中断、新对 null base 宽松返 false。真实流该变量恒由上游脚本赋值(非null)
  或被 !=null/||/三元短路守卫,不在执行路径;新行为亦更稳(不中断)。已白名单该差异类+单独记录,
  断言无其它偏移
- platform-app 全量 8 测试绿(差分+接缝+ArchUnit 共存,无 ServiceLoader 干扰)

activiti jar 缺失则整测 assumeTrue 跳过(本地差分门,不入 CI 强约束)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-13 23:41:16 +08:00
parent 9ee3ef2ace
commit c721bdebd6
2 changed files with 373 additions and 0 deletions

View File

@@ -0,0 +1,220 @@
package com.eip4.platform.app.script.juel;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.SimpleBindings;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
/**
* B2:JUEL 跨流回归——新 jakarta.el JuelScriptEngine 与 framework 原版 activiti JuelScriptEngineFactory
* 的<b>语义等价差分</b>。
*
* <p>B1 落地后,带 conditionExpression 的 EL 写流行为从「抛 Can't find scripting engine」变为「真实求值」。
* 本测试用<b>真实客户库语料</b>(dev DB SYS_SERVICEFLOW.CONTENTXML 抽取的 153 个去重 conditionExpression,
* 见 {@code src/test/resources/juel-condition-corpus.txt},XML 实体已解码)跨多种 binding 场景,
* 对同一表达式分别用<b>老引擎</b>(activiti-engine-5.14.jar,经独立 URLClassLoader 隔离加载,绝不进 app 运行时)
* 与<b>新引擎</b>求值,逐对比对:
* <ul>
* <li>两侧返回值相等 → 等价;</li>
* <li>两侧都抛错 → 等价(引擎行为一致,均无可用值);</li>
* <li>一侧求值成功、另一侧抛错,或两侧返回不同值 → <b>语义偏移</b>(回归风险,需三态三态triage)。</li>
* </ul>
*
* <p>真实语料特征(已盘点):全 {@code ${...}}、无算术(数值提升风险 moot)、无 {@code #{}}(延迟表达式 moot)、
* 主要是布尔/比较/null·空串/少量方法调用({@code .length()}/{@code .booleanValue()}/{@code .get_ID()})与三元。
*
* <p>activiti jar 缺失时整测跳过(本地差分门,不进 CI 强约束)。
*/
class JuelParityTest {
private static final String ACTIVITI_JAR =
"/Users/zhengwr/workspace/eip3/framework/com.dsasp.server.wf/lib/activiti-engine-5.14.jar";
private static final String OLD_FACTORY = "org.activiti.engine.impl.scripting.JuelScriptEngineFactory";
private static final Path CORPUS =
Path.of("src/test/resources/juel-condition-corpus.txt");
/** EL 关键字/操作符(非变量,不绑定)。 */
private static final Set<String> EL_KEYWORDS = Set.of(
"null", "true", "false", "empty", "and", "or", "not",
"eq", "ne", "lt", "gt", "le", "ge", "div", "mod", "instanceof");
/** 标识符:未被 '.' 或前一个词字符紧邻(排除方法名 .length()/.get_ID() 等 + 标识符内部片段)。 */
private static final Pattern IDENT = Pattern.compile("(?<![.\\w])([A-Za-z_][A-Za-z0-9_]*)");
/** 方法调用 {@code .method(} 模式,用于识别「对 null base 调方法」这一类已知良性差异。 */
private static final Pattern METHOD_CALL = Pattern.compile("\\.[A-Za-z_][A-Za-z0-9_]*\\s*\\(");
/** 探针 Bean:覆盖语料里出现的方法调用,使两引擎都能调到同一方法、比对调用语义。 */
public static class Probe {
public String get_ID() { return "PID"; }
public int length() { return 3; }
public boolean booleanValue() { return true; }
@Override public String toString() { return "Probe"; }
}
/** binding 场景值:每个场景把表达式里所有标识符统一绑成该值,跨类型扫语义。 */
private static final Object[] SCENARIOS = {
null, Boolean.TRUE, Boolean.FALSE, 0, 1, 2, "", "x", new Probe()
};
@Test
@DisplayName("153 真实 conditionExpression × 9 场景:新 jakarta.el 与老 activiti JUEL 求值等价")
void parity_realConditionCorpus() throws Exception {
File jar = new File(ACTIVITI_JAR);
Assumptions.assumeTrue(jar.exists(),
"activiti-engine-5.14.jar 缺失,跳过 JUEL 差分门:" + ACTIVITI_JAR);
Assumptions.assumeTrue(Files.exists(CORPUS),
"语料缺失,跳过:" + CORPUS.toAbsolutePath());
List<String> exprs = new ArrayList<>();
for (String line : Files.readAllLines(CORPUS, StandardCharsets.UTF_8)) {
String e = line.trim();
if (!e.isEmpty()) {
exprs.add(e);
}
}
// 老引擎:activiti JuelScriptEngineFactory,经独立子 classloader 加载(activiti 自带 shaded JUEL/javax.el,
// 不污染 app 运行时;javax.script.* 接口由父加载器共享,跨加载器可强转)。
ScriptEngine oldEngine;
try (URLClassLoader cl = new URLClassLoader(
new URL[]{jar.toURI().toURL()}, getClass().getClassLoader())) {
Class<?> factoryCls = Class.forName(OLD_FACTORY, true, cl);
ScriptEngineFactory oldFactory =
(ScriptEngineFactory) factoryCls.getDeclaredConstructor().newInstance();
oldEngine = oldFactory.getScriptEngine();
// 新引擎:直接实例化(app 运行时 classpath)。
ScriptEngine newEngine = new JuelScriptEngineFactory().getScriptEngine();
int total = 0;
int matched = 0;
List<String> divergences = new ArrayList<>();
List<String> acceptedNullMethod = new ArrayList<>(); // 已知良性差异:对 null base 调方法
for (String expr : exprs) {
Set<String> ids = identifiers(expr);
for (Object v : SCENARIOS) {
total++;
Object oldOut = evalSafe(oldEngine, expr, ids, v);
Object newOut = evalSafe(newEngine, expr, ids, v);
if (equivalent(oldOut, newOut)) {
matched++;
} else if (isNullMethodLeniency(expr, v, oldOut, newOut)) {
// 已知良性差异:对 null 变量调方法(如 ${isCover.booleanValue()==false} 当 isCover=null)——
// 老 activiti JUEL 抛 NPE(中断流),新 jakarta.el 对 null base 返 null(宽松,走 false 分支)。
// 真实流里该类方法调用的变量恒由上游脚本赋值(非 null),或被 !=null/||/三元短路守卫,
// 故此退化输入不在真实执行路径上;新引擎行为亦更稳(不中断)。归为可接受、单独记录。
acceptedNullMethod.add(String.format(
"expr=%s | 老=%s | 新=%s", expr, render(oldOut), render(newOut)));
} else {
divergences.add(String.format(
"expr=%s | 场景值=%s | 老=%s | 新=%s",
expr, render(v), render(oldOut), render(newOut)));
}
}
}
System.out.printf("[B2 JUEL 差分] 表达式 %d × 场景 %d = 比对 %d;完全等价 %d;"
+ "已知良性差异(null-base 方法调用) %d;真实偏移 %d%n",
exprs.size(), SCENARIOS.length, total, matched, acceptedNullMethod.size(),
divergences.size());
for (String a : acceptedNullMethod) {
System.out.println(" [可接受·null方法宽松] " + a);
}
if (!divergences.isEmpty()) {
int show = Math.min(divergences.size(), 60);
StringBuilder sb = new StringBuilder();
sb.append("JUEL 新旧引擎语义偏移 ").append(divergences.size())
.append(" 处(前 ").append(show).append(" 处):\n");
for (int i = 0; i < show; i++) {
sb.append(" - ").append(divergences.get(i)).append('\n');
}
fail(sb.toString());
}
}
}
/** 提取需绑定的标识符(排除 EL 关键字与方法名)。 */
private static Set<String> identifiers(String expr) {
Set<String> ids = new LinkedHashSet<>();
Matcher m = IDENT.matcher(expr);
while (m.find()) {
String t = m.group(1);
if (!EL_KEYWORDS.contains(t)) {
ids.add(t);
}
}
return ids;
}
/** 用「所有标识符 → v」的新鲜 bindings 求值,捕获异常为 ERR 标记(不让一侧异常中断比对)。 */
private static Object evalSafe(ScriptEngine engine, String expr, Set<String> ids, Object v) {
SimpleBindings b = new SimpleBindings();
for (String id : ids) {
b.put(id, v); // SimpleBindings 不允许 null key,但允许 null value
}
try {
return new Ok(engine.eval(expr, b));
} catch (Throwable t) {
return new Err(t.getClass().getSimpleName());
}
}
/** 等价判定:两侧都 ERR=等价;两侧都 Ok 且值相等=等价;其余=偏移。 */
private static boolean equivalent(Object a, Object b) {
if (a instanceof Err && b instanceof Err) {
return true; // 都求值失败 → 引擎行为一致
}
if (a instanceof Ok && b instanceof Ok) {
return Objects.equals(((Ok) a).value, ((Ok) b).value);
}
return false; // 一成功一失败 → 偏移
}
/**
* 已知良性差异:场景值为 null 且表达式含方法调用,老引擎抛错(对 null 调方法 NPE)、新引擎求值成功(宽松)。
* 仅此一类被接受;其余任何偏移(值不等、非 null 场景的差异、新抛错老成功等)均判真实偏移。
*/
private static boolean isNullMethodLeniency(String expr, Object v, Object oldOut, Object newOut) {
return v == null
&& oldOut instanceof Err
&& newOut instanceof Ok
&& METHOD_CALL.matcher(expr).find();
}
private static String render(Object o) {
if (o instanceof Ok) {
Object v = ((Ok) o).value;
return v == null ? "null" : (v.getClass().getSimpleName() + "(" + v + ")");
}
if (o instanceof Err) {
return "ERR:" + ((Err) o).type;
}
return String.valueOf(o);
}
private record Ok(Object value) { }
private record Err(String type) { }
}

View File

@@ -0,0 +1,153 @@
${!enableSave}
${!isSaveR}
${!isSave}
${!isprojectsave}
${!iszi}
${(SYS_ENTITYTYPE_ID!=null) ? SYS_ENTITYTYPE_ID.length()!=0 : false}
${(SYS_MENU_ID!=null) ? SYS_MENU_ID.length()!=0 : false}
${(po==null and MASTERPOSITION==null)||(po!= null and (po.get_ID()==MASTERPOSITION))}
${DM_FILE_ID != null }
${DM_FILE_ID == null }
${ISNEW == null || ISNEW == false}
${ISNEW == true}
${MASTERPOSITION != null }
${MASTERPOSITION == null }
${SYS_DEPART_ID != null }
${SYS_DEPART_ID == null }
${SYS_DIMENSIONDEPART_ID != null }
${SYS_DIMENSIONDEPART_ID == null }
${SYS_ENTITYTYPE_ID==null || SYS_ENTITYTYPE_ID.length()==0}
${SYS_MENU_ID==null || SYS_MENU_ID.length()==0}
${SYS_ORG_ID != null }
${SYS_ORG_ID == null }
${SYS_POSITION_ID != null }
${SYS_POSITION_ID == null }
${TEST_TREE_ID != null }
${TEST_TREE_ID == null }
${controltype==0}
${controltype==1}
${controltype==2}
${controltype==3}
${controltype==4}
${doUpdate==false}
${doUpdate}
${enableSave}
${hasInfo==false}
${hasInfo}
${is==false}
${isAdd==false}
${isAddAllProjectUserInfo==false}
${isAddAllProjectUserInfo}
${isAddStop==false}
${isAddStop}
${isAdd}
${isC==false}
${isCollect == false}
${isCollect}
${isCopy==false}
${isCopy}
${isCover.booleanValue()==false}
${isCoverPhoto==false}
${isCoverPhoto}
${isCover}
${isCr==false}
${isCr}
${isC}
${isDeleteStop==false}
${isDeleteStop}
${isExist==false}
${isExist}
${isLastPeriod==false}
${isLastPeriod}
${isNoQuota==false and isSave==false}
${isNoQuota==false and isSave}
${isNoQuota}
${isP==false}
${isP}
${isRecord==false}
${isRecord}
${isSave == false}
${isSave0==false}
${isSave0}
${isSave2==1}
${isSave2==2}
${isSave2==3}
${isSave2==false}
${isSave2}
${isSave==false}
${isSaveR}
${isSave}
${isSend == false}
${isSendSms==false}
${isSendSms}
${isSend}
${isStop1==false}
${isStop1}
${isStop2==false}
${isStop2}
${isStop3==false}
${isStop3}
${isStop4==false}
${isStop4}
${isStop5==false}
${isStop5}
${isStop6==false}
${isStop6}
${isStop7==false}
${isStop7}
${isStop==false and isManage==false}
${isStop==false and isManage}
${isStop==false and isSave==false}
${isStop==false and isSave}
${isStop==false}
${isStop==true}
${isStop}
${isThere==false}
${isThere}
${isTocompany==false}
${isTocompany}
${isTwo}
${isUpdate==false}
${isUpdateLast}
${isUpdateNext==false}
${isUpdateNext}
${isUpdateStop==false}
${isUpdateStop}
${isUpdate}
${ishavedata==false}
${ishavedata}
${isopen == false}
${isopen and tdId != null}
${isopen and tdId ==null}
${isprojectsave}
${ispsave==false}
${ispsave}
${isusersave==false}
${isusersave}
${iszi}
${is}
${nextNode==0}
${nextNode==1}
${nextNode==2}
${operateType == 0}
${operateType == 1}
${operateType == 2}
${operateType == 3}
${pagesize > 0 }
${po != null and (po.get_ID()!=MASTERPOSITION) }
${po == null}
${property!=2}
${property==2}
${sparePeriodState==0}
${sparePeriodState==1}
${sparePeriodState==2}
${tempDouble==false}
${tempDouble}
${tovoidid!=''}
${tovoidid==''}
${updateOrgId==false}
${updateOrgId}
${updateStatus==1}
${updateStatus==2}
${updateid!=''}
${updateid==''}