遗留① JUEL:platform-app 接入 jakarta.el 实现的 juel JSR-223 引擎
写路径提交前置:带 initExpression/条件转移的 EL 写流原本因运行时无名为 "juel" 的
引擎抛 "Can't find scripting engine for 'juel'" → status:1。
- 新增 com.eip4.platform.app.script.juel JSR-223 工厂+引擎(getNames=["juel"]),
EL 后端用已在 classpath 的 jakarta.el(tomcat-embed-el,经 spring-boot-starter-web 传递),
不引 activiti-engine / de.odysseus.juel,零改保真层 com.dsasp.*
- META-INF/services 注册 → ScriptingEnginesManager 既有惰性注册(getEngineByName("juel"))自动拾取
- BindingsELResolver 对 base==null 顶层标识符强制 setPropertyResolved,对齐 activiti JUEL
宽松语义(未绑定→null 不抛;jakarta.el 默认会抛 PropertyNotFoundException)
- 接缝单测 6 例(含端到端 ScriptingEnginesManager.evaluate(...,"juel",...))+ ArchUnit 全绿
注:B2 跨流回归(EL 求值语义差异:数值提升 Long/Double、真实 bean 缺 getter、#{} 延迟表达式)待专项。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
package com.eip4.platform.app.script.juel;
|
||||
|
||||
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.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.SimpleBindings;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.dsasp.server.core.service.script.ScriptingEnginesManager;
|
||||
|
||||
/**
|
||||
* JUEL JSR-223 接缝语义规格(TDD)。这些断言即「与 framework activiti JUEL 行为对齐」的规格。
|
||||
*
|
||||
* <p>(a)~(e) 直接验证新引擎语义;(f) 验证保真层
|
||||
* {@link ScriptingEnginesManager} 端到端发现并求值 "juel"(原本抛
|
||||
* "Can't find scripting engine for 'juel'" 的写路径前置缺口)。
|
||||
*/
|
||||
class JuelScriptEngineSeamTest {
|
||||
|
||||
/** 简单 POJO,验证属性导航 ${bean.name}。 */
|
||||
public static class Person {
|
||||
private final String name;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
private static Object evalJuel(String script, Bindings bindings) throws Exception {
|
||||
ScriptEngine engine = new ScriptEngineManager().getEngineByName("juel");
|
||||
assertNotNull(engine, "juel 引擎应可经 ServiceLoader 发现");
|
||||
return engine.eval(script, bindings);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("(a) ServiceLoader 可发现 juel 引擎")
|
||||
void juelEngineDiscoverable() {
|
||||
ScriptEngine engine = new ScriptEngineManager().getEngineByName("juel");
|
||||
assertNotNull(engine, "new ScriptEngineManager().getEngineByName(\"juel\") 不应为 null");
|
||||
assertEquals("juel", engine.getFactory().getNames().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("(b) 算术与比较:${1+2}=3, ${5>3}=true")
|
||||
void arithmeticAndComparison() throws Exception {
|
||||
Object sum = evalJuel("${1 + 2}", new SimpleBindings());
|
||||
assertTrue(sum instanceof Number);
|
||||
assertEquals(3, ((Number) sum).intValue());
|
||||
|
||||
Object gt = evalJuel("${5 > 3}", new SimpleBindings());
|
||||
assertEquals(Boolean.TRUE, gt);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("(c) 绑定变量:x=5 → ${x+1}=6, ${x>3}=true")
|
||||
void boundVariables() throws Exception {
|
||||
Bindings b = new SimpleBindings();
|
||||
b.put("x", 5);
|
||||
|
||||
Object inc = evalJuel("${x + 1}", b);
|
||||
assertTrue(inc instanceof Number);
|
||||
assertEquals(6, ((Number) inc).intValue());
|
||||
|
||||
Object gt = evalJuel("${x > 3}", b);
|
||||
assertEquals(Boolean.TRUE, gt);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("(d) 属性导航:${m.a}=7, ${bean.name}")
|
||||
void propertyNavigation() throws Exception {
|
||||
Bindings b = new SimpleBindings();
|
||||
Map<String, Object> m = new HashMap<>();
|
||||
m.put("a", 7);
|
||||
b.put("m", m);
|
||||
b.put("bean", new Person("Ada"));
|
||||
|
||||
Object mapVal = evalJuel("${m.a}", b);
|
||||
assertTrue(mapVal instanceof Number);
|
||||
assertEquals(7, ((Number) mapVal).intValue());
|
||||
|
||||
Object beanVal = evalJuel("${bean.name}", b);
|
||||
assertEquals("Ada", beanVal);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("(e) 未绑定标识符返回 null 不抛")
|
||||
void unboundIdentifierIsNull() throws Exception {
|
||||
Object r = evalJuel("${nope}", new SimpleBindings());
|
||||
assertNull(r, "未绑定标识符应按 EL 默认返回 null,不抛异常");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("(f) 端到端接缝:ScriptingEnginesManager.evaluate(\"${x+1}\",\"juel\",bindings)=6")
|
||||
void scriptingEnginesManagerSeam() {
|
||||
Bindings bindings = new SimpleBindings();
|
||||
bindings.put("x", 5);
|
||||
|
||||
Object result;
|
||||
try {
|
||||
result = ScriptingEnginesManager.getScriptingEnginesManager()
|
||||
.evaluate("${x + 1}", "juel", bindings);
|
||||
} catch (RuntimeException e) {
|
||||
// 若单例已被其它测试先行构造且当时 TCCL 未发现工厂,可能缓存了无 juel 的状态。
|
||||
// (a) 已独立验证发现链;此处明确暴露单例污染而非静默吞掉。
|
||||
assertFalse(String.valueOf(e.getMessage()).contains("Can't find scripting engine for 'juel'"),
|
||||
"保真层应能发现 juel 引擎,不应抛 'Can't find scripting engine for 'juel''(疑单例顺序污染): "
|
||||
+ e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
|
||||
assertTrue(result instanceof Number);
|
||||
assertEquals(6, ((Number) result).intValue());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user