Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
[7.67.x-blue] [DROOLS-7540] Coercion from Interger literal to BigDeci…
Browse files Browse the repository at this point in the history
…mal bind variable (#34)

* [DROOLS-7540] Coercion from Interger literal to BigDecimal bind variable (#32)

- Test cases

* remove wrong backport

---------

Co-authored-by: Toshiya Kobayashi <[email protected]>
  • Loading branch information
github-actions[bot] and tkobayas authored Dec 11, 2023
1 parent bf695db commit e716f71
Showing 1 changed file with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1730,4 +1730,123 @@ public void setter_deepNestedPropertyInModify() {

assertThat(person.getAddress().getVisitorCounter().getValue()).isEqualTo(1);
}

@Test
public void integerToBigDecimalBindVariableCoercion_shouldNotCoerceToInteger() {
// DROOLS-7540
String str = "package com.example.reproducer\n" +
"import " + Person.class.getCanonicalName() + ";\n" +
"global java.util.List results;\n" +
"rule R\n" +
" dialect \"mvel\"\n" +
" when\n" +
" Person($money : money)\n" +
" then\n" +
" $money = 5;\n" +
" results.add($money);\n" +
"end";

KieSession ksession = getKieSession(str);
List<Object> results = new ArrayList<>();
ksession.setGlobal("results", results);

Person person = new Person("John");
person.setMoney(new BigDecimal("0"));
ksession.insert(person);
ksession.fireAllRules();

assertThat(results.get(0)).isInstanceOf(BigDecimal.class);
assertThat(results).contains(new BigDecimal("5"));
}

@Test
public void integerToBigDecimalVariableCoercionTwice_shouldNotCoerceToInteger() {
// DROOLS-7540
String str = "package com.example.reproducer\n" +
"import " + Person.class.getCanonicalName() + ";\n" +
"import " + BigDecimal.class.getCanonicalName() + ";\n" +
"global java.util.List results;\n" +
"rule R\n" +
" dialect \"mvel\"\n" +
" when\n" +
" Person()\n" +
" then\n" +
" BigDecimal $var1 = 5;\n" +
" $var1 = 5;\n" + // This causes the issue
" results.add($var1);\n" +
"end";

KieSession ksession = getKieSession(str);
List<Object> results = new ArrayList<>();
ksession.setGlobal("results", results);

Person person = new Person("John");
person.setMoney(new BigDecimal("0"));
ksession.insert(person);
ksession.fireAllRules();

assertThat(results.get(0)).isInstanceOf(BigDecimal.class);
assertThat(results).contains(new BigDecimal("5"));
}

@Test
public void integerToBigDecimalBindVariableCoercionAndAddition_shouldNotThrowClassCastException() {
// DROOLS-7540
String str = "package com.example.reproducer\n" +
"import " + Person.class.getCanonicalName() + ";\n" +
"global java.util.List results;\n" +
"rule R\n" +
" dialect \"mvel\"\n" +
" when\n" +
" Person($money : money, $otherBigDecimalField : otherBigDecimalField)\n" +
" then\n" +
" $money = 5;\n" +
" $total = $money + $otherBigDecimalField;\n" +
" results.add($total);\n" +
"end";

KieSession ksession = getKieSession(str);
List<Object> results = new ArrayList<>();
ksession.setGlobal("results", results);

Person person = new Person("John");
person.setMoney(new BigDecimal("0"));
person.setOtherBigDecimalField(new BigDecimal("10"));
ksession.insert(person);
ksession.fireAllRules();

assertThat(results).contains(new BigDecimal("15"));
}

@Test
public void integerToBigDecimalVaribleSetFromBindVariableCoercionAndAddition_shouldNotThrowClassCastException() {
// DROOLS-7540
String str = "package com.example.reproducer\n" +
"import " + Person.class.getCanonicalName() + ";\n" +
"import " + BigDecimal.class.getCanonicalName() + ";\n" +
"global java.util.List results;\n" +
"rule R\n" +
" dialect \"mvel\"\n" +
" when\n" +
" Person($money : money, $otherBigDecimalField : otherBigDecimalField)\n" +
" then\n" +
" BigDecimal $var1 = $money;\n" +
" BigDecimal $var2 = $otherBigDecimalField;\n" +
" $var1 = 5;\n" +
" BigDecimal $total = $var1 + $var2;\n" +
" results.add($total);\n" +
"end";

KieSession ksession = getKieSession(str);
List<Object> results = new ArrayList<>();
ksession.setGlobal("results", results);

Person person = new Person("John");
person.setMoney(new BigDecimal("0"));
person.setOtherBigDecimalField(new BigDecimal("10"));
ksession.insert(person);
ksession.fireAllRules();

assertThat(results).contains(new BigDecimal("15"));
}
}

0 comments on commit e716f71

Please sign in to comment.