Skip to content

Commit

Permalink
DROOLS-5249: do not store DateTimeFormatter as free form line (#2859)
Browse files Browse the repository at this point in the history
We have two kinds of date boiler plate code snippets. For:
- java.util.Date: 'java.text.SimpleDateFormat sdf'
- java.time.LocalDate: 'java.time.format.DateTimeFormatter dtf'

If user set value either of 'Date' or 'LocalDate' field using 'Literal Value' guided editor action, the given date boiler plate is generated automatically behind the scene. This allows users to put in just value of date, e.g. "01-Jan-2000". User do not have to use parametric constructors, builders, parsers or factory methods. It is done behind the scene using 'sdf' or 'dtf' BoilerPlate.

If users set value either of 'Date' ot 'LocalDate' field using 'Formula' guided editor action, is their responsibility to construct 'Date' or 'LocalDate' instance.

For more details see https://issues.redhat.com/browse/DROOLS-5249
  • Loading branch information
Jozef Marko authored May 21, 2020
1 parent 7af62b3 commit fa6ccfe
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3100,7 +3100,9 @@ private void parseRhs(final RuleModel m,
if (eqPos > 0) {
String field = line.substring(0,
eqPos).trim();
if ("java.text.SimpleDateFormat sdf".equals(field) || "org.drools.core.process.instance.WorkItemManager wim".equals(field)) {
if ("java.text.SimpleDateFormat sdf".equals(field) ||
"java.time.format.DateTimeFormatter dtf".equals(field) ||
"org.drools.core.process.instance.WorkItemManager wim".equals(field)) {
addFreeFormLine = false;
}
String[] split = field.split(" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.StringReader;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -6909,6 +6910,7 @@ public void testNewKeywordVariableNamePrefix1() {
FreeFormLine ffl = (FreeFormLine) m.rhs[0];
assertEquals("DateTime newStartDate = new DateTime();",
ffl.getText());
assertDateBoilerPlateCodeIsNotUnmarshaledToFreeFormLine(m.rhs);

assertTrue(m.rhs[1] instanceof ActionUpdateField);
ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
Expand Down Expand Up @@ -6997,6 +6999,7 @@ public void testNewKeywordVariableNamePrefix2() {
FreeFormLine ffl = (FreeFormLine) m.rhs[0];
assertEquals("java.util.Date newStartDate = new java.util.Date();",
ffl.getText());
assertDateBoilerPlateCodeIsNotUnmarshaledToFreeFormLine(m.rhs);

assertTrue(m.rhs[1] instanceof ActionUpdateField);
ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
Expand Down Expand Up @@ -7084,6 +7087,7 @@ public void testNewKeywordVariableNamePrefix3() {
FreeFormLine ffl = (FreeFormLine) m.rhs[0];
assertEquals("java.util.Date newStartDate = new java.util.Date();",
ffl.getText());
assertDateBoilerPlateCodeIsNotUnmarshaledToFreeFormLine(m.rhs);

assertTrue(m.rhs[1] instanceof ActionUpdateField);
ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
Expand Down Expand Up @@ -7111,6 +7115,89 @@ public void testNewKeywordVariableNamePrefix3() {
}
}

@Test
public void testLocalDateBoilerPlateCodeUnmarshaling() {
String oldValue = System.getProperty("drools.dateformat");
try {

System.setProperty("drools.dateformat",
"dd-MMM-yyyy");

String drl = "package org.test;\n"
+ "rule \"rule1\"\n"
+ " dialect \"java\"\n"
+ " when\n"
+ " $a : Applicant()\n"
+ " then\n"
+ " java.time.format.DateTimeFormatter dtf = java.time.format.DateTimeFormatter.ofPattern(\"dd-MMM-yyyy\");\n"
+ " modify( $a ) {\n"
+ " setApplicantDate( java.time.LocalDate.parse(\"31-Jan-2000\", dtf) )"
+ " }\n"
+ "end\n";

addModelField("org.test.Applicant",
"this",
"org.test.Applicant",
DataType.TYPE_THIS);
addModelField("org.test.Applicant",
"applicantDate",
LocalDate.class.getName(),
DataType.TYPE_LOCAL_DATE);

when(dmo.getPackageName()).thenReturn("org.test");

RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl,
Collections.emptyList(),
dmo);

assertNotNull(m);
assertEquals("rule1",
m.name);

assertEquals(1,
m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);

FactPattern fp = (FactPattern) p;
assertEquals("Applicant",
fp.getFactType());
assertEquals("$a",
fp.getBoundName());

assertNull(fp.getConstraintList());

assertEquals(1,
m.rhs.length);

assertDateBoilerPlateCodeIsNotUnmarshaledToFreeFormLine(m.rhs);

assertTrue(m.rhs[0] instanceof ActionUpdateField);
ActionUpdateField auf = (ActionUpdateField) m.rhs[0];
assertEquals("$a",
auf.getVariable());
assertEquals(1,
auf.getFieldValues().length);
ActionFieldValue afv = auf.getFieldValues()[0];
assertEquals("applicantDate",
afv.getField());
assertEquals("31-Jan-2000",
afv.getValue());
assertEquals(FieldNatureType.TYPE_LITERAL,
afv.getNature());

assertEqualsIgnoreWhitespace(drl,
RuleModelDRLPersistenceImpl.getInstance().marshal(m));
} finally {
if (oldValue == null) {
System.clearProperty("drools.dateformat");
} else {
System.setProperty("drools.dateformat",
oldValue);
}
}
}

@Test
//https://bugzilla.redhat.com/show_bug.cgi?id=1158176
public void testRHSEntryPointInsertion() throws Exception {
Expand Down Expand Up @@ -9806,4 +9893,15 @@ public void methodInSetter() {
assertEqualsIgnoreWhitespace(expectedDRL,
RuleModelDRLPersistenceImpl.getInstance().marshal(model));
}

/**
* For more details see https://issues.redhat.com/browse/DROOLS-5249
*/
private void assertDateBoilerPlateCodeIsNotUnmarshaledToFreeFormLine(final IAction[] actions) {
Assertions.assertThat(actions)
.as("SimpleDateFormat and DateTimeFormatter boiler plates shouldn't be unmarshaled")
.filteredOn(action -> action instanceof FreeFormLine)
.noneMatch(action -> ((FreeFormLine)action).getText().contains("SimpleDateFormat"))
.noneMatch(action -> ((FreeFormLine)action).getText().contains("DateTimeFormatter"));
}
}

0 comments on commit fa6ccfe

Please sign in to comment.