JaredDavis22 commented on code in PR #5837:
URL:
https://github.com/apache/incubator-kie-drools/pull/5837#discussion_r1561009321
##########
drools-model/drools-canonical-model/src/test/java/org/drools/model/operators/MatchesOperatorTest.java:
##########
@@ -0,0 +1,23 @@
+package org.drools.model.operators;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+
+public class MatchesOperatorTest {
+ @Test
+ public void testMatchesOperator() {
+ MatchesOperator instance = MatchesOperator.INSTANCE;
+ // Regular expression is second parameter
+ assertThat(instance.eval("a","a")).isTrue();
+ assertThat(instance.eval("a","b")).isFalse();
+ assertThat(instance.eval("a","a")).isTrue();
Review Comment:
I made these changes to improve the tests but did not commit them. Are they
better/uglier/? Test looks better, main class looks worse and no longer inlines
the size test in the maps removeEldestEntry.
```
public enum MatchesOperator implements Operator.SingleValue<String, String> {
INSTANCE;
private static final String CACHE_MATCHES_COMPILED_MAX_PROPERTY =
"drools.matches.compiled.cache.count";
// default to 0 for no cache, not final for unit tests
private static int MAX_SIZE_CACHE =
Integer.parseInt(System.getProperty(CACHE_MATCHES_COMPILED_MAX_PROPERTY, "0"));
// store Pattern for regular expressions using the regular expression as
the key up to MAX_SIZE_CACHE entries.
public final Map<String, Pattern> patternMap =
Collections.synchronizedMap(new LinkedHashMap<>() {
@Override
protected boolean removeEldestEntry( Map.Entry<String, Pattern>
eldest ) {
return size() > (MAX_SIZE_CACHE);
}
});
// for unit testing
public void forceCacheSize(int size) {
MAX_SIZE_CACHE = size;
patternMap.clear();
}
// for unit testing
public void reInitialize() {
forceCacheSize(Integer.parseInt(System.getProperty(CACHE_MATCHES_COMPILED_MAX_PROPERTY,
"0")));
}
```
```
public class MatchesOperatorTest {
@Test
public void testMatchesOperatorCache() {
MatchesOperator instance = MatchesOperator.INSTANCE;
instance.forceCacheSize(100);
// s1 maybe null
assertThat(instance.eval(null,"anything")).isFalse();
// cache enabled
// Regular expression is second parameter
assertThat(instance.eval("a","a")).isTrue();
assertThat(instance.eval("a","b")).isFalse();
assertThat(instance.eval("a","a")).isTrue(); // regular expression
"a" in map.
assertThat(instance.eval("b","b")).isTrue(); // regular expression
"b" in map.
assertThat(instance.eval("c","b")).isFalse(); // regular expression
"b" in map.
}
@Test
public void testMatchesOperatorNoCache() {
MatchesOperator instance = MatchesOperator.INSTANCE;
instance.forceCacheSize(0);
// s1 maybe null
assertThat(instance.eval(null,"anything")).isFalse();
assertThat(instance.eval("a","a")).isTrue();
assertThat(instance.eval("a","b")).isFalse();
assertThat(instance.eval("b","b")).isTrue();
}
@After
public void resetCache() {
MatchesOperator instance = MatchesOperator.INSTANCE;
instance.reInitialize();
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]