Skip to content

Commit

Permalink
JEXL: add test on using regexp array (SO question)
Browse files Browse the repository at this point in the history
  • Loading branch information
henrib committed Sep 4, 2024
1 parent f31249b commit 8a52b3b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/test/java/org/apache/commons/jexl3/Issues400Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -37,6 +38,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Pattern;

import org.apache.commons.jexl3.introspection.JexlPermissions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -483,4 +485,28 @@ void testSortArray() {
assertEquals(9, m[1].get("type"));
}


public static class MatchingArithmetic extends JexlArithmetic {
public MatchingArithmetic(boolean astrict) {
super(astrict);
}

public boolean contains(Pattern[] container, String str) {
for(Pattern pattern : container) {
if (pattern.matcher(str).matches()) {
return true;
}
}
return false;
}
}

@Test
void testPatterns() {
final JexlEngine jexl = new JexlBuilder().arithmetic(new MatchingArithmetic(true)).create();
JexlScript script = jexl.createScript("str =~ [~/abc.*/, ~/def.*/]", "str");
assertTrue((boolean) script.execute(null, "abcdef"));
assertTrue((boolean) script.execute(null, "defghi"));
assertFalse((boolean) script.execute(null, "ghijkl"));
}
}

0 comments on commit 8a52b3b

Please sign in to comment.