CalculateDDMFormRuleActionSerializer implements SPIDDMFormRuleActionSerializer { protected String buildExpression( String expression, Set<String> ddmFormFieldNames) { int start = Integer.MAX_VALUE; int end = Integer.MIN_VALUE; StringBuilder newExpressionSB = new StringBuilder(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < expression.length(); i++) { char token = expression.charAt(i); sb.append(token); String compareStr = sb.toString(); boolean match = matchAnyField(compareStr, ddmFormFieldNames); if (match) { newExpressionSB.append(token); if (start == Integer.MAX_VALUE) { start = i; } if (i == (expression.length() - 1)) { end = expression.length(); } } else { if (i > start) { replace(expression, newExpressionSB, start, i); } newExpressionSB.append(token); sb = new StringBuilder(); start = Integer.MAX_VALUE; end = Integer.MIN_VALUE; } } if (end > start) { replace(expression, newExpressionSB, start, end); } return newExpressionSB.toString(); } CalculateDDMFormRuleActionSerializer(
		CalculateDDMFormRuleAction calculateDDMFormRuleAction); @Override String serialize(
		SPIDDMFormRuleSerializerContext spiDDMFormRuleSerializerContext);  }
@Test public void testBuildExpression1() { CalculateDDMFormRuleActionSerializer calculateDDMFormRuleActionSerializer = new CalculateDDMFormRuleActionSerializer(null); Set<String> ddmFormFields = new HashSet<>(); ddmFormFields.add("Num1"); ddmFormFields.add("Num12"); ddmFormFields.add("Num123"); String expression = "(Num123+Num1+Num12) * 0.25 * Num123"; String result = calculateDDMFormRuleActionSerializer.buildExpression( expression, ddmFormFields); Assert.assertEquals( "(getValue('Num123')+getValue('Num1')+getValue('Num12')) * 0.25 " + "* getValue('Num123')", result); }
@Test public void testBuildExpression2() { CalculateDDMFormRuleActionSerializer calculateDDMFormRuleActionSerializer = new CalculateDDMFormRuleActionSerializer(null); Set<String> ddmFormFields = new HashSet<>(); ddmFormFields.add("Num1"); ddmFormFields.add("Num2"); ddmFormFields.add("Num3"); ddmFormFields.add("Num4"); String expression = "(Num1+Num2+Num3+Num4) * 0.25"; String result = calculateDDMFormRuleActionSerializer.buildExpression( expression, ddmFormFields); Assert.assertEquals( "(getValue('Num1')+getValue('Num2')+getValue('Num3')+" + "getValue('Num4')) * 0.25", result); }
@Test public void testBuildExpression3() { CalculateDDMFormRuleActionSerializer calculateDDMFormRuleActionSerializer = new CalculateDDMFormRuleActionSerializer(null); Set<String> ddmFormFields = new HashSet<>(); ddmFormFields.add("Num1"); ddmFormFields.add("Num2"); ddmFormFields.add("Num3"); String expression = "Num1+Num2+Num3*3"; String result = calculateDDMFormRuleActionSerializer.buildExpression( expression, ddmFormFields); Assert.assertEquals( "getValue('Num1')+getValue('Num2')+getValue('Num3')*3", result); }
@Test public void testBuildExpression4() { CalculateDDMFormRuleActionSerializer calculateDDMFormRuleActionSerializer = new CalculateDDMFormRuleActionSerializer(null); Set<String> ddmFormFields = new HashSet<>(); ddmFormFields.add("Num1"); ddmFormFields.add("Num2"); ddmFormFields.add("Num3"); String expression = "(Num1-Num2*2)-Num3"; String result = calculateDDMFormRuleActionSerializer.buildExpression( expression, ddmFormFields); Assert.assertEquals( "(getValue('Num1')-getValue('Num2')*2)-getValue('Num3')", result); }
@Test public void testBuildExpression5() { CalculateDDMFormRuleActionSerializer calculateDDMFormRuleActionSerializer = new CalculateDDMFormRuleActionSerializer(null); Set<String> ddmFormFields = new HashSet<>(); ddmFormFields.add("Num1"); ddmFormFields.add("Num2"); String expression = "(Num1 * Num2) + 0.25"; String result = calculateDDMFormRuleActionSerializer.buildExpression( expression, ddmFormFields); Assert.assertEquals( "(getValue('Num1') * getValue('Num2')) + 0.25", result); }