BooleanExpressionParser { public static boolean evaluate(final String expression) { final Deque<String> operators = new ArrayDeque<>(); final Deque<String> values = new ArrayDeque<>(); final boolean result; char currentCharacter; int currentCharacterIndex = 0; try { while (currentCharacterIndex < expression.length()) { currentCharacter = expression.charAt(currentCharacterIndex); if (SeparatorToken.OPEN_PARENTHESIS.value == currentCharacter) { operators.push(SeparatorToken.OPEN_PARENTHESIS.toString()); currentCharacterIndex += moveCursor(SeparatorToken.OPEN_PARENTHESIS.toString()); } else if (SeparatorToken.SPACE.value == currentCharacter) { currentCharacterIndex += moveCursor(SeparatorToken.SPACE.toString()); } else if (SeparatorToken.CLOSE_PARENTHESIS.value == currentCharacter) { evaluateSubexpression(operators, values); currentCharacterIndex += moveCursor(SeparatorToken.CLOSE_PARENTHESIS.toString()); } else if (!Character.isDigit(currentCharacter)) { final String parsedNonDigit = parseNonDigits(expression, currentCharacterIndex); if (isBoolean(parsedNonDigit)) { values.push(replaceBooleanStringByIntegerRepresentation(parsedNonDigit)); } else { operators.push(validateOperator(parsedNonDigit)); } currentCharacterIndex += moveCursor(parsedNonDigit); } else if (Character.isDigit(currentCharacter)) { final String parsedDigits = parseDigits(expression, currentCharacterIndex); values.push(parsedDigits); currentCharacterIndex += moveCursor(parsedDigits); } } result = Boolean.valueOf(evaluateExpressionStack(operators, values)); if (log.isDebugEnabled()) { log.debug("Boolean expression {} evaluates to {}", expression, result); } } catch (final NoSuchElementException e) { throw new CitrusRuntimeException("Unable to parse boolean expression '" + expression + "'. Maybe expression is incomplete!", e); } return result; } private  BooleanExpressionParser(); static boolean evaluate(final String expression);  }
@Test public void testExpressionParser() { Assert.assertTrue(BooleanExpressionParser.evaluate("1 = 1")); Assert.assertFalse(BooleanExpressionParser.evaluate("1 = 2")); Assert.assertTrue(BooleanExpressionParser.evaluate("1 lt 2")); Assert.assertFalse(BooleanExpressionParser.evaluate("2 lt 1")); Assert.assertTrue(BooleanExpressionParser.evaluate("2 gt 1")); Assert.assertFalse(BooleanExpressionParser.evaluate("1 gt 2")); Assert.assertTrue(BooleanExpressionParser.evaluate("2 lt= 2")); Assert.assertTrue(BooleanExpressionParser.evaluate("2 gt= 2")); Assert.assertFalse(BooleanExpressionParser.evaluate("2 lt= 1")); Assert.assertFalse(BooleanExpressionParser.evaluate("2 gt= 3")); Assert.assertTrue(BooleanExpressionParser.evaluate("(1 = 1)")); Assert.assertTrue(BooleanExpressionParser.evaluate("(1 = 1) and (2 = 2)")); Assert.assertTrue(BooleanExpressionParser.evaluate("(1 lt= 1) and (2 gt= 2)")); Assert.assertTrue(BooleanExpressionParser.evaluate("(1 gt 2) or (2 = 2)")); Assert.assertTrue(BooleanExpressionParser.evaluate("((1 = 5) and (2 = 6)) or (2 gt 1)")); Assert.assertFalse(BooleanExpressionParser.evaluate("(1 = 2)")); Assert.assertFalse(BooleanExpressionParser.evaluate("(1 = 1) and (2 = 3)")); Assert.assertFalse(BooleanExpressionParser.evaluate("(1 lt 1) and (2 gt 2)")); Assert.assertFalse(BooleanExpressionParser.evaluate("(1 gt 2) or (2 = 3)")); Assert.assertFalse(BooleanExpressionParser.evaluate("((1 = 5) and (2 = 6)) or (2 lt 1)")); }
@Test public void testExpressionParserWithStringValues() { Assert.assertTrue(BooleanExpressionParser.evaluate("true")); Assert.assertTrue(BooleanExpressionParser.evaluate("true = true")); Assert.assertTrue(BooleanExpressionParser.evaluate("false = false")); Assert.assertFalse(BooleanExpressionParser.evaluate("false")); Assert.assertFalse(BooleanExpressionParser.evaluate("true = false")); Assert.assertFalse(BooleanExpressionParser.evaluate("false = true")); Assert.assertTrue(BooleanExpressionParser.evaluate("( false = false ) and ( true = true )")); Assert.assertFalse(BooleanExpressionParser.evaluate("( false = false ) and ( true = false )")); Assert.assertTrue(BooleanExpressionParser.evaluate("(false = false) and (true = true)")); Assert.assertFalse(BooleanExpressionParser.evaluate("(false = false) and (true = false)")); Assert.assertTrue(BooleanExpressionParser.evaluate("( false = false) and (true = true )")); Assert.assertFalse(BooleanExpressionParser.evaluate("(false = false ) and ( true = false)")); Assert.assertTrue(BooleanExpressionParser.evaluate("( true = false ) or ( false = false )")); Assert.assertTrue(BooleanExpressionParser.evaluate("(false = false) or (true = true)")); Assert.assertTrue(BooleanExpressionParser.evaluate("(false = false) or (true = false)")); Assert.assertTrue(BooleanExpressionParser.evaluate("(false = false ) or ( true = false)")); }
@Test public void testExpressionParserWithUnknownOperator() { try { BooleanExpressionParser.evaluate("wahr"); } catch(final CitrusRuntimeException e) { Assert.assertEquals(e.getLocalizedMessage(), "Unknown operator 'wahr'"); return; } Assert.fail("Missing " + CitrusRuntimeException.class + " because of unknown operator"); }
@Test public void testExpressionParserWithBrokenExpression() { try { BooleanExpressionParser.evaluate("1 = "); } catch(final CitrusRuntimeException e) { Assert.assertEquals(e.getLocalizedMessage(), "Unable to parse boolean expression '1 = '. Maybe expression is incomplete!"); return; } Assert.fail("Missing " + CitrusRuntimeException.class + " because of broken expression"); }