Skip to content

Commit

Permalink
MVEL.analyse is not using DataConverter coercions (apache#5297)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdproctor authored and rgdoliveira committed Mar 11, 2024
1 parent ccb71db commit d19d875
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public boolean canConvertFrom(Class cls) {

public static class StringCoercionCompatibilityEvaluator extends CompatibilityStrategy.DefaultCompatibilityEvaluator {

private static final CompatibilityStrategy.CompatibilityEvaluator INSTANCE = new StringCoercionCompatibilityEvaluator();
public static final CompatibilityStrategy.CompatibilityEvaluator INSTANCE = new StringCoercionCompatibilityEvaluator();

private StringCoercionCompatibilityEvaluator() { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

import org.drools.mvel.MVELConstraintBuilder.StringCoercionCompatibilityEvaluator;
import org.drools.util.DateUtils;
import org.drools.mvel.expr.MVELDateCoercion;
import org.junit.Test;
import org.mvel2.DataConversion;
import org.mvel2.MVEL;
import org.mvel2.ParserContext;
import org.mvel2.util.CompatibilityStrategy;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;
import static org.mvel2.MVEL.executeExpression;

public class MVELDateCoercionTest {

Expand All @@ -51,4 +61,66 @@ public void testString() throws Exception {
assertThat(co.convertFrom(dt)).isEqualTo(dt_);
}

@Test
public void testCoercionDuringAnalyze() {
CompatibilityStrategy.setCompatibilityEvaluator(StringCoercionCompatibilityEvaluator.INSTANCE);

DataConversion.addConversionHandler(Date.class,
new MVELDateCoercion());

String expr = "f.departureTime >= \"01-Jan-1970\" && f.departureTime <= \"01-Jan-2018\"";

ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.setStrictTypeEnforcement(true);
ctx.addInput("f", Flight.class);

Class cls = MVEL.analyze( expr,
ctx );

assertThat(cls).isNotNull();
}

public static class Flight {
private Date departureTime;

public Flight(Date departureTime) {
this.departureTime = departureTime;
}

public Date getDepartureTime() {
return departureTime;
}

public void setDepartureTime(Date departureTime) {
this.departureTime = departureTime;
}


@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

Flight flight = (Flight) o;

return Objects.equals(departureTime, flight.departureTime);
}

@Override
public int hashCode() {
return departureTime != null ? departureTime.hashCode() : 0;
}

@Override
public String toString() {
return "Flight{" +
"departureTime=" + departureTime +
'}';
}
}
}

0 comments on commit d19d875

Please sign in to comment.