[
https://issues.redhat.com/browse/DROOLS-5346?page=com.atlassian.jira.plug...
]
Toshiya Kobayashi commented on DROOLS-5346:
-------------------------------------------
For example, if you add the following code to BaseModelTest, you can easily check if unit
tests generate non-externalized lambda or not.
{code:java}
public abstract class BaseModelTest {
static {
//System.setProperty("drools.externaliseCanonicalModelLambda",
"true");
System.setProperty("drools.check.nonExternalisedLambda",
"true");
}
{code}
Enhance unit test to detect non-externalized lambda widely
----------------------------------------------------------
Key: DROOLS-5346
URL:
https://issues.redhat.com/browse/DROOLS-5346
Project: Drools
Issue Type: Sub-task
Components: executable model
Affects Versions: 7.37.0.Final
Reporter: Toshiya Kobayashi
Assignee: Toshiya Kobayashi
Priority: Major
Fix For: 7.38.0.Final
Currently "lambda is not externalized" issue is not widely detected in unit
tests. We need to write a specific assertion like this:
{code:java}
@Test
public void testExternalizeBindingVariableLambda() throws Exception {
String str =
"package defaultpkg;\n" +
"import " + Person.class.getCanonicalName() + ";" +
"global java.util.List list;\n" +
"rule R when\n" +
" $p : Person($n : name == \"Mario\")\n" +
"then\n" +
" list.add($n);\n" +
"end";
KieModuleModel kieModuleModel = KieServices.get().newKieModuleModel();
kieModuleModel.setConfigurationProperty("drools.externaliseCanonicalModelLambda",
Boolean.TRUE.toString());
KieSession ksession = getKieSession(kieModuleModel, str );
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
if (testRunType == RUN_TYPE.FLOW_DSL || testRunType == RUN_TYPE.PATTERN_DSL) {
RuleImpl rule =
(RuleImpl)ksession.getKieBase().getRule("defaultpkg", "R");
Pattern pattern = (Pattern)rule.getLhs().getChildren().get(0);
Declaration declaration = pattern.getDeclarations().get("$n");
LambdaReadAccessor lambdaReadAccessor =
(LambdaReadAccessor)declaration.getExtractor();
Field field = LambdaReadAccessor.class.getDeclaredField("lambda");
field.setAccessible(true);
Function1.Impl function1 = (Function1.Impl)field.get(lambdaReadAccessor);
Object lambda = function1.getLambda();
assertThat(lambda.getClass().getName(),
containsString("LambdaExtractor")); // materialized Lambda
}
Person me = new Person( "Mario", 40 );
ksession.insert( me );
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("Mario");
}
{code}
This JIRA is to enhance unit tests to assert it widely.
--
This message was sent by Atlassian Jira
(v7.13.8#713008)