You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by mo...@apache.org on 2017/05/02 19:26:51 UTC
nifi git commit: NIFI-2752 Correct ReplaceText default pattern and
unit tests (0.x)
Repository: nifi
Updated Branches:
refs/heads/0.x d38a324b3 -> 2328d1b0f
NIFI-2752 Correct ReplaceText default pattern and unit tests (0.x)
Signed-off-by: Mike Moser <mo...@apache.org>
This closes #1732.
Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/2328d1b0
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/2328d1b0
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/2328d1b0
Branch: refs/heads/0.x
Commit: 2328d1b0f39cff30eadfa97408c6f1d8d995456f
Parents: d38a324
Author: Joe Skora <js...@apache.org>
Authored: Tue May 2 13:14:28 2017 -0400
Committer: Mike Moser <mo...@apache.org>
Committed: Tue May 2 18:53:59 2017 +0000
----------------------------------------------------------------------
.../nifi/processors/standard/ReplaceText.java | 9 +--
.../processors/standard/TestReplaceText.java | 60 +++++++++++++++++++-
2 files changed, 60 insertions(+), 9 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/nifi/blob/2328d1b0/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 1df7b91..61c5f1f 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
@@ -84,7 +84,7 @@ public class ReplaceText extends AbstractProcessor {
public static final String literalReplaceValue = "Literal Replace";
public static final String alwaysReplace = "Always Replace";
private static final Pattern backReferencePattern = Pattern.compile("\\$(\\d+)");
- private static final String DEFAULT_REGEX = "(?s:^.*$)";
+ private static final String DEFAULT_REGEX = "(?s)(^.*$)";
private static final String DEFAULT_REPLACEMENT_VALUE = "$1";
// Prepend and Append will just insert the replacement value at the beginning or end
@@ -214,13 +214,6 @@ public class ReplaceText extends AbstractProcessor {
String unsubstitutedReplacement = context.getProperty(REPLACEMENT_VALUE).getValue();
final String replacementStrategy = context.getProperty(REPLACEMENT_STRATEGY).getValue();
- if (replacementStrategy.equalsIgnoreCase(regexReplaceValue) && 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);
- return;
- }
-
final Charset charset = Charset.forName(context.getProperty(CHARACTER_SET).getValue());
final int maxBufferSize = context.getProperty(MAX_BUFFER_SIZE).asDataSize(DataUnit.B).intValue();
http://git-wip-us.apache.org/repos/asf/nifi/blob/2328d1b0/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 a6e0971..b65bff1 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
@@ -30,10 +30,15 @@ import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.Assert;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
public class TestReplaceText {
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
+
@Test
public void testConfigurationCornerCase() throws IOException {
final TestRunner runner = TestRunners.newTestRunner(new ReplaceText());
@@ -64,7 +69,7 @@ public class TestReplaceText {
}
@Test
- public void testWithEscaped$InReplacemenmt() throws IOException {
+ public void testWithEscaped$InReplacement() throws IOException {
final TestRunner runner = TestRunners.newTestRunner(new ReplaceText());
runner.setValidateExpressionUsage(false);
runner.setProperty(ReplaceText.SEARCH_VALUE, "(?s:^.*$)");
@@ -1106,7 +1111,60 @@ public class TestReplaceText {
out.assertContentEquals("abc.txt\nabc.txt\r\nabc.txt\n");
}
+ @Test
+ public void testRegexWithBadCaptureGroup() throws IOException {
+ // Test the old Default Regex and with a custom Replacement Value that should fail because the
+ // Perl regex "(?s:^.*$)" must be written "(?s)(^.*$)" in Java for there to be a capture group.
+ // private static final String DEFAULT_REGEX = "(?s:^.*$)";
+ final TestRunner runner = TestRunners.newTestRunner(new ReplaceText());
+ runner.setValidateExpressionUsage(false);
+ runner.setProperty(ReplaceText.SEARCH_VALUE, "(?s:^.*$)");
+ runner.setProperty(ReplaceText.REPLACEMENT_VALUE, "${'$1':toUpper()}"); // should uppercase group but there is none
+ runner.setProperty(ReplaceText.REPLACEMENT_STRATEGY, ReplaceText.REGEX_REPLACE);
+ runner.setProperty(ReplaceText.EVALUATION_MODE, ReplaceText.ENTIRE_TEXT);
+
+ runner.enqueue("testing\n123".getBytes());
+ runner.run();
+
+ runner.assertAllFlowFilesTransferred(ReplaceText.REL_SUCCESS, 1);
+ final MockFlowFile out = runner.getFlowFilesForRelationship(ReplaceText.REL_SUCCESS).get(0);
+ out.assertContentEquals("");
+ }
+ @Test
+ public void testRegexWithGoodCaptureGroup() throws IOException {
+ // Test the new Default Regex and with a custom Replacement Values that should succeed.
+ final TestRunner runner = TestRunners.newTestRunner(new ReplaceText());
+ runner.setValidateExpressionUsage(false);
+ runner.setProperty(ReplaceText.SEARCH_VALUE, "(?s)(^.*$)");
+ runner.setProperty(ReplaceText.REPLACEMENT_VALUE, "${'$1':toUpper()}"); // will uppercase group with good Java regex
+ runner.setProperty(ReplaceText.REPLACEMENT_STRATEGY, ReplaceText.REGEX_REPLACE);
+ runner.setProperty(ReplaceText.EVALUATION_MODE, ReplaceText.ENTIRE_TEXT);
+
+ runner.enqueue("testing\n123".getBytes());
+ runner.run();
+
+ runner.assertAllFlowFilesTransferred(ReplaceText.REL_SUCCESS, 1);
+ final MockFlowFile out = runner.getFlowFilesForRelationship(ReplaceText.REL_SUCCESS).get(0);
+ out.assertContentEquals("TESTING\n123");
+ }
+
+ @Test
+ public void testRegexNoCaptureDefaultReplacement() throws IOException {
+ // Test the old Default Regex and new Default Regex with the default replacement. This should fail
+ // because the regex does not create a capture group.
+ final TestRunner runner = TestRunners.newTestRunner(new ReplaceText());
+ runner.setValidateExpressionUsage(false);
+ runner.setProperty(ReplaceText.SEARCH_VALUE, "(?s:^.*$)");
+ runner.setProperty(ReplaceText.REPLACEMENT_VALUE, "$1");
+ runner.setProperty(ReplaceText.REPLACEMENT_STRATEGY, ReplaceText.REGEX_REPLACE);
+ runner.setProperty(ReplaceText.EVALUATION_MODE, ReplaceText.ENTIRE_TEXT);
+
+ exception.expect(AssertionError.class);
+ exception.expectMessage("java.lang.IndexOutOfBoundsException: No group 1");
+ runner.enqueue("testing\n123".getBytes());
+ runner.run();
+ }
private String translateNewLines(final File file) throws IOException {
return translateNewLines(file.toPath());