Repository: nifi Updated Branches: refs/heads/master 2bb785300 -> cd2e1424c
NIFI-911: - Updating default value for Regex so it matches once (?s:^.*$) instead of twice (.*). Matching on .* results in matching for every character and then again for 0 characters. Project: http://git-wip-us.apache.org/repos/asf/nifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/cd2e1424 Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/cd2e1424 Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/cd2e1424 Branch: refs/heads/master Commit: cd2e1424cb3ccd975d87193603669269899aac2a Parents: 2bb7853 Author: Matt Gilman <[email protected]> Authored: Mon Aug 31 14:58:38 2015 -0400 Committer: Matt Gilman <[email protected]> Committed: Mon Aug 31 14:58:38 2015 -0400 ---------------------------------------------------------------------- .../nifi/processors/standard/ReplaceText.java | 9 +++-- .../processors/standard/TestReplaceText.java | 38 +++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi/blob/cd2e1424/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ReplaceText.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ReplaceText.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ReplaceText.java index e47d58c..68155d1 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ReplaceText.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ReplaceText.java @@ -71,6 +71,9 @@ public class ReplaceText extends AbstractProcessor { public static final String ENTIRE_TEXT = "Entire text"; private final Pattern backReferencePattern = Pattern.compile("\\$(\\d+)"); private static final byte[] ZERO_BYTE_BUFFER = new byte[0]; + private static final String DEFAULT_REGEX = "(?s:^.*$)"; + private static final String DEFAULT_REPLACEMENT_VALUE = "$1"; + // Properties public static final PropertyDescriptor REGEX = new PropertyDescriptor.Builder() .name("Regular Expression") @@ -78,14 +81,14 @@ public class ReplaceText extends AbstractProcessor { .required(true) .addValidator(StandardValidators.createRegexValidator(0, Integer.MAX_VALUE, true)) .expressionLanguageSupported(true) - .defaultValue("(.*)") + .defaultValue(DEFAULT_REGEX) .build(); public static final PropertyDescriptor REPLACEMENT_VALUE = new PropertyDescriptor.Builder() .name("Replacement Value") .description("The value to replace the regular expression with. Back-references to Regular Expression capturing groups are supported, but " + "back-references that reference capturing groups that do not exist in the regular expression will be treated as literal value.") .required(true) - .defaultValue("$1") + .defaultValue(DEFAULT_REPLACEMENT_VALUE) .addValidator(Validator.VALID) .expressionLanguageSupported(true) .build(); @@ -166,7 +169,7 @@ public class ReplaceText extends AbstractProcessor { final ProcessorLog logger = getLogger(); final String unsubstitutedRegex = context.getProperty(REGEX).getValue(); String unsubstitutedReplacement = context.getProperty(REPLACEMENT_VALUE).getValue(); - if (unsubstitutedRegex.equals("(.*)") && unsubstitutedReplacement.equals("$1")) { + if (unsubstitutedRegex.equals(DEFAULT_REGEX) && unsubstitutedReplacement.equals(DEFAULT_REPLACEMENT_VALUE)) { // This pattern says replace content with itself. We can highly optimize this process by simply transferring // all FlowFiles to the 'success' relationship session.transfer(flowFiles, REL_SUCCESS); http://git-wip-us.apache.org/repos/asf/nifi/blob/cd2e1424/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestReplaceText.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestReplaceText.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestReplaceText.java index 203f77a..ab5f6be 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestReplaceText.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestReplaceText.java @@ -16,7 +16,6 @@ */ package org.apache.nifi.processors.standard; -import org.apache.nifi.processors.standard.ReplaceText; import org.apache.nifi.util.MockFlowFile; import org.apache.nifi.util.TestRunner; import org.apache.nifi.util.TestRunners; @@ -386,4 +385,41 @@ public class TestReplaceText { out.assertContentEquals("{ abc.txt }"); } + @Test + public void testDefaultReplacement() throws Exception { + final String defaultValue = "default-replacement-value"; + + // leave the default regex settings + final TestRunner runner = TestRunners.newTestRunner(new ReplaceText()); + runner.setValidateExpressionUsage(false); + runner.setProperty(ReplaceText.REPLACEMENT_VALUE, defaultValue); + + final Map<String, String> attributes = new HashMap<>(); + runner.enqueue("original-text".getBytes(StandardCharsets.UTF_8), attributes); + + runner.run(); + + runner.assertAllFlowFilesTransferred(ReplaceText.REL_SUCCESS, 1); + final MockFlowFile out = runner.getFlowFilesForRelationship(ReplaceText.REL_SUCCESS).get(0); + out.assertContentEquals(defaultValue); + } + + @Test + public void testDefaultMultilineReplacement() throws Exception { + final String defaultValue = "default-replacement-value"; + + // leave the default regex settings + final TestRunner runner = TestRunners.newTestRunner(new ReplaceText()); + runner.setValidateExpressionUsage(false); + runner.setProperty(ReplaceText.REPLACEMENT_VALUE, defaultValue); + + final Map<String, String> attributes = new HashMap<>(); + runner.enqueue(("original-text-line-1" + System.lineSeparator() + "original-text-line-2").getBytes(StandardCharsets.UTF_8), attributes); + + runner.run(); + + runner.assertAllFlowFilesTransferred(ReplaceText.REL_SUCCESS, 1); + final MockFlowFile out = runner.getFlowFilesForRelationship(ReplaceText.REL_SUCCESS).get(0); + out.assertContentEquals(defaultValue); + } }
