From efcd7352f29410237e96090c4f49b0df1d3d5030 Mon Sep 17 00:00:00 2001 From: hengsin Date: Fri, 17 Mar 2023 18:36:27 +0800 Subject: [PATCH] IDEMPIERE-5632 Add unit test for regular expression match for contains (#1738) --- .../test/base/LogicExpressionTest.java | 84 ++++++++++++++++++- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/org.idempiere.test/src/org/idempiere/test/base/LogicExpressionTest.java b/org.idempiere.test/src/org/idempiere/test/base/LogicExpressionTest.java index 17229ded24..a6783e153e 100644 --- a/org.idempiere.test/src/org/idempiere/test/base/LogicExpressionTest.java +++ b/org.idempiere.test/src/org/idempiere/test/base/LogicExpressionTest.java @@ -53,9 +53,8 @@ import org.junit.jupiter.api.Test; @SuppressWarnings("deprecation") /** - * + * Unit test for logical expression using context variables * @author hengsin - * */ public class LogicExpressionTest extends AbstractTestCase { @@ -107,6 +106,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test and (&) + */ @Test public void testAnd() { String expr = "@$Element_BP@=Y & @AnyBPartner@=N"; @@ -135,6 +137,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertFalse(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test in operator (@...@=a,b) + */ @Test public void testIn() { String expr = "@LineType@=C&@CalculationType@=A,R,S"; @@ -198,6 +203,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertFalse(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test not equal (!) + */ @Test public void testNotEqual() { String expr = "@C_Bpartner_ID@!0"; @@ -237,6 +245,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test the combine use of or (|) plus and (&) operator + */ @Test public void testOrAnd() { String expr = "@IsSold@='Y' | @IsPurchased@='Y' & @IsSummary@='N'"; @@ -271,6 +282,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertFalse(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test the combine use of and (&) plus or (|) operator + */ @Test public void testAndOr() { String expr = "@IsSummary@='N' & @ProductType@=I | @ProductType@=S"; @@ -301,6 +315,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test the use of and (&), parenthesis and or (|) operator + */ @Test public void testAndOrGroup() { String expr = "@IsSummary@='N' & (@ProductType@=I | @ProductType@=S)"; @@ -318,6 +335,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertFalse(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test the use of or (|), parenthesis plus and (&) operator + */ @Test public void testOrAndGroup() { String expr = "@IsSold@='Y' | (@IsPurchased@='Y' & @IsSummary@='N')"; @@ -337,6 +357,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertFalse(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test greater than (>) + */ @Test public void testGT() { String expr = "@IsLot@=Y& @M_LotCtl_ID@ > 0"; @@ -365,6 +388,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertFalse(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test greater than or equal to (>=) + */ @Test public void testGE() { String expr = "@IsLot@=Y& @M_LotCtl_ID@ >= 100"; @@ -381,6 +407,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertFalse(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test less than (<) + */ @Test public void testLT() { String expr = "@A_Asset_ID@<1&@A_CreateAsset@='Y'"; @@ -405,6 +434,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test less than or equal to (<=) + */ @Test public void testLE() { String expr = "@A_Asset_ID@<=1&@A_CreateAsset@='Y'"; @@ -419,6 +451,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test not operator ($!) + */ @Test public void testNegate() { String expr = "$!(@IsLot@=Y & @M_LotCtl_ID@ > 0)"; @@ -433,6 +468,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test regular expression match (~) + */ @Test public void testRE() { String expr = "@Identifier@ ~ '^([a-zA-Z_$][a-zA-Z\\d_$]*)$'"; @@ -450,8 +488,29 @@ public class LogicExpressionTest extends AbstractTestCase { assertFalse(LogicEvaluator.evaluateLogic(evaluatee, expr)); Env.setContext(Env.getCtx(), "Identifier", "validIdentifier0"); assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); + + //test contains, useful for multiple selection choice + expr = "@Identifier@ ~ '.*\\b1\\b.*'"; + assertFalse(LogicEvaluator.evaluateLogic(evaluatee, expr)); + Env.setContext(Env.getCtx(), "Identifier", "0"); + assertFalse(LogicEvaluator.evaluateLogic(evaluatee, expr)); + Env.setContext(Env.getCtx(), "Identifier", "1"); + assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); + Env.setContext(Env.getCtx(), "Identifier", "1,2"); + assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); + Env.setContext(Env.getCtx(), "Identifier", "2,1"); + assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); + Env.setContext(Env.getCtx(), "Identifier", "1,2"); + assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); + Env.setContext(Env.getCtx(), "Identifier", "1,2,3"); + assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); + Env.setContext(Env.getCtx(), "Identifier", "2,3"); + assertFalse(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test syntax validation of expression + */ @Test public void testValidation() { Exception ex = null; @@ -472,6 +531,9 @@ public class LogicExpressionTest extends AbstractTestCase { System.out.println(ex.getMessage()); } + /** + * Test default operator (:) + */ @Test public void testConditionalVariable() { String expr = "@IsSOTrx:N@=N | @+IgnoreIsSOTrxInBPInfo:N@=Y"; @@ -492,6 +554,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Validate the syntax of all logical expression in AD + */ @Test public void testValidateAD() { String[] columns = {"MandatoryLogic","DocValueLogic","ReadOnlyLogic","DisplayLogic","ZoomLogic"}; @@ -533,6 +598,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertTrue(exceptions.isEmpty(), "Found " + exceptions.size() + " logic expression with invalid syntax in AD"); } + /** + * Test comparison of date and timestamp values + */ @Test public void testDateExpression() { String expr = "@DateAcct@<@DateOrdered@"; @@ -559,6 +627,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test comparison of numeric values + */ @Test public void testNumericExpression() { String expr = "@QtyReserved@=0"; @@ -582,6 +653,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test the use of empty string literal ('') + */ @Test public void testEmptyStringExpression() { String expr = "@ColumnSQL@=''"; @@ -604,6 +678,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertTrue(LogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test the use of $env to access environment variables + */ @Test public void testOSEnvVariable() { String username = System.getenv("USER"); @@ -613,6 +690,9 @@ public class LogicExpressionTest extends AbstractTestCase { assertTrue(LegacyLogicEvaluator.evaluateLogic(evaluatee, expr)); } + /** + * Test nested property operator (.) + */ @Test public void testNestedProperty() { String expr = "@Processed@=Y & @M_Product_ID.IsBOM@=Y";