Repository: nifi Updated Branches: refs/heads/master 2f3017638 -> 2ea8b4122
NIFI-3127: Fixing NPE for empty flow files in SplitText This closes #1358. Signed-off-by: Koji Kawamura <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/nifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/2ea8b412 Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/2ea8b412 Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/2ea8b412 Branch: refs/heads/master Commit: 2ea8b4122b342de39388f0f71769b77b59c57617 Parents: 2f30176 Author: Joe Gresock <[email protected]> Authored: Fri Dec 23 14:16:41 2016 +0000 Committer: Koji Kawamura <[email protected]> Committed: Wed Dec 28 11:21:44 2016 +0900 ---------------------------------------------------------------------- .../nifi/processors/standard/SplitText.java | 10 +++++--- .../nifi/processors/standard/TestSplitText.java | 26 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi/blob/2ea8b412/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitText.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitText.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitText.java index 2cbcd44..ddb770d 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitText.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitText.java @@ -71,7 +71,7 @@ import org.apache.nifi.stream.io.util.TextLineDemarcator.OffsetInfo; + "exceeds the configured maximum size limit. This component also allows one to specify that each split should include a header " + "lines. Header lines can be computed by either specifying the amount of lines that should constitute a header or by using header " + "marker to match against the read lines. If such match happens then the corresponding line will be treated as header. Keep in mind " - + "that upon the first failure of header marker match, no more marches will be performed and the rest of the data will be parsed as " + + "that upon the first failure of header marker match, no more matches will be performed and the rest of the data will be parsed as " + "regular lines for a given split. If after computation of the header there are no more data, the resulting split will consists " + "of only header lines.") @WritesAttributes({ @@ -215,7 +215,7 @@ public class SplitText extends AbstractProcessor { try { if (SplitText.this.headerLineCount > 0) { splitInfo = SplitText.this.computeHeader(demarcator, startOffset, SplitText.this.headerLineCount, null, null); - if (splitInfo.lineCount < SplitText.this.headerLineCount) { + if ((splitInfo != null) && (splitInfo.lineCount < SplitText.this.headerLineCount)) { error.set(true); getLogger().error("Unable to split " + sourceFlowFile + " due to insufficient amount of header lines. Required " + SplitText.this.headerLineCount + " but was " + splitInfo.lineCount + ". Routing to failure."); @@ -252,7 +252,9 @@ public class SplitText extends AbstractProcessor { } else { List<FlowFile> splitFlowFiles = this.generateSplitFlowFiles(sourceFlowFile, headerSplitInfoRef.get(), computedSplitsInfo, processSession); processSession.transfer(sourceFlowFile, REL_ORIGINAL); - processSession.transfer(splitFlowFiles, REL_SPLITS); + if (!splitFlowFiles.isEmpty()) { + processSession.transfer(splitFlowFiles, REL_SPLITS); + } } } @@ -288,7 +290,7 @@ public class SplitText extends AbstractProcessor { int fragmentIndex = 1; // set to 1 to preserve the existing behavior *only*. Perhaps should be deprecated to follow the 0,1,2... scheme String fragmentId = UUID.randomUUID().toString(); - if (computedSplitsInfo.size() == 0) { + if ((computedSplitsInfo.size() == 0) && (headerFlowFile != null)) { FlowFile splitFlowFile = processSession.clone(sourceFlowFile, 0, headerFlowFile.getSize() - headerCrlfLength); splitFlowFile = SplitText.this.updateAttributes(processSession, splitFlowFile, 0, splitFlowFile.getSize(), fragmentId, fragmentIndex++, 0, sourceFlowFile.getAttribute(CoreAttributes.FILENAME.key())); http://git-wip-us.apache.org/repos/asf/nifi/blob/2ea8b412/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitText.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitText.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitText.java index 1a2089c..3951e02 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitText.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitText.java @@ -358,6 +358,32 @@ public class TestSplitText { } @Test + public void testZeroByteInputWithoutHeader() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new SplitText()); + runner.setProperty(SplitText.HEADER_LINE_COUNT, "0"); + runner.setProperty(SplitText.LINE_SPLIT_COUNT, "1"); + + runner.enqueue("".getBytes()); + runner.run(); + runner.assertTransferCount(SplitText.REL_SPLITS, 0); + runner.assertTransferCount(SplitText.REL_ORIGINAL, 1); + runner.assertTransferCount(SplitText.REL_FAILURE, 0); + } + + @Test + public void testZeroByteInput() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new SplitText()); + runner.setProperty(SplitText.HEADER_LINE_COUNT, "1"); + runner.setProperty(SplitText.LINE_SPLIT_COUNT, "1"); + + runner.enqueue("".getBytes()); + runner.run(); + runner.assertTransferCount(SplitText.REL_SPLITS, 0); + runner.assertTransferCount(SplitText.REL_ORIGINAL, 1); + runner.assertTransferCount(SplitText.REL_FAILURE, 0); + } + + @Test public void testSplitWithoutHeader() throws IOException { final TestRunner runner = TestRunners.newTestRunner(new SplitText()); runner.setProperty(SplitText.HEADER_LINE_COUNT, "0");
