NIFI-625: Fixed bug in expression language that caused EL not to get evaluated if enclosed within curly braces

This commit is contained in:
Mark Payne 2015-06-08 19:52:15 -04:00
parent b82d1428b2
commit 86cbfab14a
3 changed files with 125 additions and 86 deletions

View File

@ -264,7 +264,14 @@ public class Query {
} }
} }
// Keep track of the number of opening curly braces that we are embedded within,
// if we are within an Expression. If we are outside of an Expression, we can just ignore
// curly braces. This allows us to ignore the first character if the value is something
// like: { ${abc} }
// However, we will count the curly braces if we have something like: ${ $${abc} }
if (expressionStart > -1) {
embeddedCount++; embeddedCount++;
}
} else if (c == '}') { } else if (c == '}') {
if (embeddedCount <= 0) { if (embeddedCount <= 0) {
continue; continue;
@ -965,7 +972,7 @@ public class Query {
case NUMBER: case NUMBER:
return (NumberEvaluator) evaluator; return (NumberEvaluator) evaluator;
case STRING: case STRING:
return new NumberCastEvaluator((StringEvaluator) evaluator); return new NumberCastEvaluator(evaluator);
case DATE: case DATE:
return new DateToNumberEvaluator((DateEvaluator) evaluator); return new DateToNumberEvaluator((DateEvaluator) evaluator);
default: default:

View File

@ -361,6 +361,8 @@ public class TestQuery {
@Test @Test
public void testExtractExpressionRanges() { public void testExtractExpressionRanges() {
assertEquals(29, Query.extractExpressionRanges("${hello:equals( $${goodbye} )}").get(0).getEnd());
List<Range> ranges = Query.extractExpressionRanges("hello"); List<Range> ranges = Query.extractExpressionRanges("hello");
assertTrue(ranges.isEmpty()); assertTrue(ranges.isEmpty());
@ -1098,6 +1100,18 @@ public class TestQuery {
verifyEquals(query, attributes, true); verifyEquals(query, attributes, true);
} }
@Test
public void testEvaluateWithinCurlyBraces() {
final Map<String, String> attributes = new HashMap<>();
attributes.put("abc", "xyz");
final String query = "{ ${abc} }";
final List<String> expressions = Query.extractExpressions(query);
assertEquals(1, expressions.size());
assertEquals("${abc}", expressions.get(0));
assertEquals("{ xyz }", Query.evaluateExpressions(query, attributes));
}
private void verifyEquals(final String expression, final Map<String, String> attributes, final Object expectedResult) { private void verifyEquals(final String expression, final Map<String, String> attributes, final Object expectedResult) {
Query.validateExpression(expression, false); Query.validateExpression(expression, false);
assertEquals(String.valueOf(expectedResult), Query.evaluateExpressions(expression, attributes, null)); assertEquals(String.valueOf(expectedResult), Query.evaluateExpressions(expression, attributes, null));

View File

@ -368,4 +368,22 @@ public class TestReplaceText {
System.out.println(outContent); System.out.println(outContent);
} }
@Test
public void testReplaceWithinCurlyBraces() throws IOException {
final TestRunner runner = TestRunners.newTestRunner(new ReplaceText());
runner.setValidateExpressionUsage(false);
runner.setProperty(ReplaceText.REGEX, ".+");
runner.setProperty(ReplaceText.REPLACEMENT_VALUE, "{ ${filename} }");
final Map<String, String> attributes = new HashMap<>();
attributes.put("filename", "abc.txt");
runner.enqueue("Hello".getBytes(), attributes);
runner.run();
runner.assertAllFlowFilesTransferred(ReplaceText.REL_SUCCESS, 1);
final MockFlowFile out = runner.getFlowFilesForRelationship(ReplaceText.REL_SUCCESS).get(0);
out.assertContentEquals("{ abc.txt }");
}
} }