NIFI-604: Removed unused import, as it caused checkstyle violation
Project: http://git-wip-us.apache.org/repos/asf/nifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/24c08078 Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/24c08078 Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/24c08078 Branch: refs/heads/master Commit: 24c080788fe92af8a66fb8f2f2392273bd8f4553 Parents: 193dbe6 Author: Mark Payne <marka...@hotmail.com> Authored: Thu Sep 24 10:06:39 2015 -0400 Committer: Mark Payne <marka...@hotmail.com> Committed: Thu Sep 24 10:06:39 2015 -0400 ---------------------------------------------------------------------- .../standard/TestExecuteStreamCommand.java | 435 +++++++++---------- 1 file changed, 217 insertions(+), 218 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi/blob/24c08078/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestExecuteStreamCommand.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestExecuteStreamCommand.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestExecuteStreamCommand.java index f01f404..8f945ac 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestExecuteStreamCommand.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestExecuteStreamCommand.java @@ -16,14 +16,9 @@ */ package org.apache.nifi.processors.standard; -import org.apache.nifi.processor.Processor; -import org.apache.nifi.processors.standard.util.ArgumentUtils; -import org.apache.nifi.util.MockFlowFile; -import org.apache.nifi.util.TestRunner; -import org.apache.nifi.util.TestRunners; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import java.io.File; import java.io.FileInputStream; @@ -35,220 +30,224 @@ import java.util.List; import java.util.Set; import java.util.regex.Pattern; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import org.apache.nifi.processors.standard.util.ArgumentUtils; +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; /** * */ public class TestExecuteStreamCommand { - @BeforeClass - public static void init() { - System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info"); - System.setProperty("org.slf4j.simpleLogger.showDateTime", "true"); - System.setProperty("org.slf4j.simpleLogger.log.nifi.processors.standard.ExecuteStreamCommand", "debug"); - System.setProperty("org.slf4j.simpleLogger.log.nifi.processors.standard.TestExecuteStreamCommand", "debug"); - } - - @Test - public void testExecuteJar() throws Exception { - File exJar = new File("src/test/resources/ExecuteCommand/TestSuccess.jar"); - File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt"); - String jarPath = exJar.getAbsolutePath(); - exJar.setExecutable(true); - final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); - controller.setValidateExpressionUsage(false); - controller.enqueue(dummy.toPath()); - controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "java"); - controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "-jar;" + jarPath); - controller.run(1); - controller.assertTransferCount(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP, 1); - controller.assertTransferCount(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP, 1); - - List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP); - MockFlowFile outputFlowFile = flowFiles.get(0); - byte[] byteArray = outputFlowFile.toByteArray(); - String result = new String(byteArray); - assertTrue(Pattern.compile("Test was a success\r?\n").matcher(result).find()); - assertEquals("0", outputFlowFile.getAttribute("execution.status")); - assertEquals("java", outputFlowFile.getAttribute("execution.command")); - assertEquals("-jar;", outputFlowFile.getAttribute("execution.command.args").substring(0, 5)); - String attribute = outputFlowFile.getAttribute("execution.command.args"); - String expected = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "ExecuteCommand" + File.separator + "TestSuccess.jar"; - assertEquals(expected, attribute.substring(attribute.length() - expected.length())); - - MockFlowFile originalFlowFile = controller.getFlowFilesForRelationship(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP).get(0); - assertEquals(outputFlowFile.getAttribute("execution.status"), originalFlowFile.getAttribute("execution.status")); - assertEquals(outputFlowFile.getAttribute("execution.command"), originalFlowFile.getAttribute("execution.command")); - assertEquals(outputFlowFile.getAttribute("execution.command.args"), originalFlowFile.getAttribute("execution.command.args")); - } - - @Test - public void testExecuteJarWithBadPath() throws Exception { - File exJar = new File("src/test/resources/ExecuteCommand/noSuchFile.jar"); - File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt"); - String jarPath = exJar.getAbsolutePath(); - exJar.setExecutable(true); - final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); - controller.setValidateExpressionUsage(false); - controller.enqueue(dummy.toPath()); - controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "java"); - controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "-jar;" + jarPath); - controller.run(1); - controller.assertTransferCount(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP, 1); - controller.assertTransferCount(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP, 1); - List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP); - assertEquals(0, flowFiles.get(0).getSize()); - assertEquals("Error: Unable to access jarfile", flowFiles.get(0).getAttribute("execution.error").substring(0, 31)); - } - - @Test - public void testExecuteIngestAndUpdate() throws IOException { - File exJar = new File("src/test/resources/ExecuteCommand/TestIngestAndUpdate.jar"); - File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt"); - File dummy100MBytes = new File("target/100MB.txt"); - FileInputStream fis = new FileInputStream(dummy); - FileOutputStream fos = new FileOutputStream(dummy100MBytes); - byte[] bytes = new byte[1024]; - assertEquals(1000, fis.read(bytes)); - fis.close(); - for (int i = 0; i < 100000; i++) { - fos.write(bytes, 0, 1000); - } - fos.close(); - String jarPath = exJar.getAbsolutePath(); - exJar.setExecutable(true); - final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); - controller.setValidateExpressionUsage(false); - controller.enqueue(dummy100MBytes.toPath()); - controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "java"); - controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "-jar;" + jarPath); - controller.run(1); - controller.assertTransferCount(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP, 1); - controller.assertTransferCount(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP, 1); - List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP); - byte[] byteArray = flowFiles.get(0).toByteArray(); - String result = new String(byteArray); - - assertTrue(Pattern.compile("nifi-standard-processors:ModifiedResult\r?\n").matcher(result).find()); - } - - @Test - public void testExecuteIngestAndUpdateWithWorkingDir() throws IOException { - File exJar = new File("src/test/resources/ExecuteCommand/TestIngestAndUpdate.jar"); - File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt"); - String jarPath = exJar.getAbsolutePath(); - exJar.setExecutable(true); - final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); - controller.setValidateExpressionUsage(false); - controller.enqueue(dummy.toPath()); - controller.setProperty(ExecuteStreamCommand.WORKING_DIR, "target"); - controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "java"); - controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "-jar;" + jarPath); - controller.run(1); - controller.assertTransferCount(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP, 1); - controller.assertTransferCount(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP, 1); - List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP); - byte[] byteArray = flowFiles.get(0).toByteArray(); - String result = new String(byteArray); - - final String quotedSeparator = Pattern.quote(File.separator); - assertTrue(Pattern.compile(quotedSeparator + "nifi-standard-processors" + quotedSeparator + "target:ModifiedResult\r?\n").matcher(result).find()); - } - - @Test - public void testIgnoredStdin() throws IOException { - File exJar = new File("src/test/resources/ExecuteCommand/TestIngestAndUpdate.jar"); - File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt"); - String jarPath = exJar.getAbsolutePath(); - exJar.setExecutable(true); - final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); - controller.setValidateExpressionUsage(false); - controller.enqueue(dummy.toPath()); - controller.setProperty(ExecuteStreamCommand.WORKING_DIR, "target"); - controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "java"); - controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "-jar;" + jarPath); - controller.setProperty(ExecuteStreamCommand.IGNORE_STDIN, "true"); - controller.run(1); - controller.assertTransferCount(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP, 1); - controller.assertTransferCount(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP, 1); - List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP); - byte[] byteArray = flowFiles.get(0).toByteArray(); - String result = new String(byteArray); - assertTrue("TestIngestAndUpdate.jar should not have received anything to modify", - Pattern.compile("target:ModifiedResult\r?\n$").matcher(result).find()); - } - - // this is dependent on window with cygwin...so it's not enabled - @Ignore - @Test - public void testExecuteTouch() throws Exception { - File testFile = new File("target/test.txt"); - testFile.delete(); - File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt"); - final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); - controller.setValidateExpressionUsage(false); - controller.enqueue(dummy.toPath()); - controller.enqueue(dummy.toPath()); - controller.enqueue(dummy.toPath()); - controller.enqueue(dummy.toPath()); - controller.enqueue(dummy.toPath()); - controller.setProperty(ExecuteStreamCommand.WORKING_DIR, "target/xx1"); - controller.setThreadCount(6); - controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "c:\\cygwin\\bin\\touch"); - controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "test.txt"); - controller.assertValid(); - controller.run(6); - List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP); - assertEquals(5, flowFiles.size()); - assertEquals(0, flowFiles.get(0).getSize()); - - } - - @Test - public void testDynamicEnvironment() throws Exception { - File exJar = new File("src/test/resources/ExecuteCommand/TestDynamicEnvironment.jar"); - File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt"); - String jarPath = exJar.getAbsolutePath(); - exJar.setExecutable(true); - final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); - controller.setProperty("NIFI_TEST_1", "testvalue1"); - controller.setProperty("NIFI_TEST_2", "testvalue2"); - controller.setValidateExpressionUsage(false); - controller.enqueue(dummy.toPath()); - controller.setProperty(ExecuteStreamCommand.WORKING_DIR, "target"); - controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "java"); - controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "-jar;" + jarPath); - controller.run(1); - controller.assertTransferCount(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP, 1); - controller.assertTransferCount(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP, 1); - List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP); - byte[] byteArray = flowFiles.get(0).toByteArray(); - String result = new String(byteArray); - Set<String> dynamicEnvironmentVariables = new HashSet<>(Arrays.asList(result.split("\r?\n"))); - assertFalse("Should contain at least two environment variables starting with NIFI", dynamicEnvironmentVariables.size() < 2); - assertTrue("NIFI_TEST_1 environment variable is missing", dynamicEnvironmentVariables.contains("NIFI_TEST_1=testvalue1")); - assertTrue("NIFI_TEST_2 environment variable is missing", dynamicEnvironmentVariables.contains("NIFI_TEST_2=testvalue2")); - } - - @Test - public void testQuotedArguments() throws Exception { - List<String> args = ArgumentUtils.splitArgs("echo -n \"arg1 arg2 arg3\"", ' '); - assertEquals(3, args.size()); - args = ArgumentUtils.splitArgs("echo;-n;\"arg1 arg2 arg3\"", ';'); - assertEquals(3, args.size()); - } - - @Test - public void testInvalidDelimiter() throws Exception { - final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); - controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "echo"); - controller.assertValid(); - controller.setProperty(ExecuteStreamCommand.ARG_DELIMITER, "foo"); - controller.assertNotValid(); - controller.setProperty(ExecuteStreamCommand.ARG_DELIMITER, "f"); - controller.assertValid(); - } + @BeforeClass + public static void init() { + System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info"); + System.setProperty("org.slf4j.simpleLogger.showDateTime", "true"); + System.setProperty("org.slf4j.simpleLogger.log.nifi.processors.standard.ExecuteStreamCommand", "debug"); + System.setProperty("org.slf4j.simpleLogger.log.nifi.processors.standard.TestExecuteStreamCommand", "debug"); + } + + @Test + public void testExecuteJar() throws Exception { + File exJar = new File("src/test/resources/ExecuteCommand/TestSuccess.jar"); + File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt"); + String jarPath = exJar.getAbsolutePath(); + exJar.setExecutable(true); + final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); + controller.setValidateExpressionUsage(false); + controller.enqueue(dummy.toPath()); + controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "java"); + controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "-jar;" + jarPath); + controller.run(1); + controller.assertTransferCount(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP, 1); + controller.assertTransferCount(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP, 1); + + List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP); + MockFlowFile outputFlowFile = flowFiles.get(0); + byte[] byteArray = outputFlowFile.toByteArray(); + String result = new String(byteArray); + assertTrue(Pattern.compile("Test was a success\r?\n").matcher(result).find()); + assertEquals("0", outputFlowFile.getAttribute("execution.status")); + assertEquals("java", outputFlowFile.getAttribute("execution.command")); + assertEquals("-jar;", outputFlowFile.getAttribute("execution.command.args").substring(0, 5)); + String attribute = outputFlowFile.getAttribute("execution.command.args"); + String expected = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "ExecuteCommand" + File.separator + "TestSuccess.jar"; + assertEquals(expected, attribute.substring(attribute.length() - expected.length())); + + MockFlowFile originalFlowFile = controller.getFlowFilesForRelationship(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP).get(0); + assertEquals(outputFlowFile.getAttribute("execution.status"), originalFlowFile.getAttribute("execution.status")); + assertEquals(outputFlowFile.getAttribute("execution.command"), originalFlowFile.getAttribute("execution.command")); + assertEquals(outputFlowFile.getAttribute("execution.command.args"), originalFlowFile.getAttribute("execution.command.args")); + } + + @Test + public void testExecuteJarWithBadPath() throws Exception { + File exJar = new File("src/test/resources/ExecuteCommand/noSuchFile.jar"); + File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt"); + String jarPath = exJar.getAbsolutePath(); + exJar.setExecutable(true); + final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); + controller.setValidateExpressionUsage(false); + controller.enqueue(dummy.toPath()); + controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "java"); + controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "-jar;" + jarPath); + controller.run(1); + controller.assertTransferCount(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP, 1); + controller.assertTransferCount(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP, 1); + List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP); + assertEquals(0, flowFiles.get(0).getSize()); + assertEquals("Error: Unable to access jarfile", flowFiles.get(0).getAttribute("execution.error").substring(0, 31)); + } + + @Test + public void testExecuteIngestAndUpdate() throws IOException { + File exJar = new File("src/test/resources/ExecuteCommand/TestIngestAndUpdate.jar"); + File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt"); + File dummy100MBytes = new File("target/100MB.txt"); + FileInputStream fis = new FileInputStream(dummy); + FileOutputStream fos = new FileOutputStream(dummy100MBytes); + byte[] bytes = new byte[1024]; + assertEquals(1000, fis.read(bytes)); + fis.close(); + for (int i = 0; i < 100000; i++) { + fos.write(bytes, 0, 1000); + } + fos.close(); + String jarPath = exJar.getAbsolutePath(); + exJar.setExecutable(true); + final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); + controller.setValidateExpressionUsage(false); + controller.enqueue(dummy100MBytes.toPath()); + controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "java"); + controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "-jar;" + jarPath); + controller.run(1); + controller.assertTransferCount(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP, 1); + controller.assertTransferCount(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP, 1); + List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP); + byte[] byteArray = flowFiles.get(0).toByteArray(); + String result = new String(byteArray); + + assertTrue(Pattern.compile("nifi-standard-processors:ModifiedResult\r?\n").matcher(result).find()); + } + + @Test + public void testExecuteIngestAndUpdateWithWorkingDir() throws IOException { + File exJar = new File("src/test/resources/ExecuteCommand/TestIngestAndUpdate.jar"); + File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt"); + String jarPath = exJar.getAbsolutePath(); + exJar.setExecutable(true); + final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); + controller.setValidateExpressionUsage(false); + controller.enqueue(dummy.toPath()); + controller.setProperty(ExecuteStreamCommand.WORKING_DIR, "target"); + controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "java"); + controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "-jar;" + jarPath); + controller.run(1); + controller.assertTransferCount(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP, 1); + controller.assertTransferCount(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP, 1); + List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP); + byte[] byteArray = flowFiles.get(0).toByteArray(); + String result = new String(byteArray); + + final String quotedSeparator = Pattern.quote(File.separator); + assertTrue(Pattern.compile(quotedSeparator + "nifi-standard-processors" + quotedSeparator + "target:ModifiedResult\r?\n").matcher(result).find()); + } + + @Test + public void testIgnoredStdin() throws IOException { + File exJar = new File("src/test/resources/ExecuteCommand/TestIngestAndUpdate.jar"); + File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt"); + String jarPath = exJar.getAbsolutePath(); + exJar.setExecutable(true); + final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); + controller.setValidateExpressionUsage(false); + controller.enqueue(dummy.toPath()); + controller.setProperty(ExecuteStreamCommand.WORKING_DIR, "target"); + controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "java"); + controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "-jar;" + jarPath); + controller.setProperty(ExecuteStreamCommand.IGNORE_STDIN, "true"); + controller.run(1); + controller.assertTransferCount(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP, 1); + controller.assertTransferCount(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP, 1); + List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP); + byte[] byteArray = flowFiles.get(0).toByteArray(); + String result = new String(byteArray); + assertTrue("TestIngestAndUpdate.jar should not have received anything to modify", + Pattern.compile("target:ModifiedResult\r?\n$").matcher(result).find()); + } + + // this is dependent on window with cygwin...so it's not enabled + @Ignore + @Test + public void testExecuteTouch() throws Exception { + File testFile = new File("target/test.txt"); + testFile.delete(); + File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt"); + final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); + controller.setValidateExpressionUsage(false); + controller.enqueue(dummy.toPath()); + controller.enqueue(dummy.toPath()); + controller.enqueue(dummy.toPath()); + controller.enqueue(dummy.toPath()); + controller.enqueue(dummy.toPath()); + controller.setProperty(ExecuteStreamCommand.WORKING_DIR, "target/xx1"); + controller.setThreadCount(6); + controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "c:\\cygwin\\bin\\touch"); + controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "test.txt"); + controller.assertValid(); + controller.run(6); + List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP); + assertEquals(5, flowFiles.size()); + assertEquals(0, flowFiles.get(0).getSize()); + + } + + @Test + public void testDynamicEnvironment() throws Exception { + File exJar = new File("src/test/resources/ExecuteCommand/TestDynamicEnvironment.jar"); + File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt"); + String jarPath = exJar.getAbsolutePath(); + exJar.setExecutable(true); + final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); + controller.setProperty("NIFI_TEST_1", "testvalue1"); + controller.setProperty("NIFI_TEST_2", "testvalue2"); + controller.setValidateExpressionUsage(false); + controller.enqueue(dummy.toPath()); + controller.setProperty(ExecuteStreamCommand.WORKING_DIR, "target"); + controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "java"); + controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "-jar;" + jarPath); + controller.run(1); + controller.assertTransferCount(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP, 1); + controller.assertTransferCount(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP, 1); + List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP); + byte[] byteArray = flowFiles.get(0).toByteArray(); + String result = new String(byteArray); + Set<String> dynamicEnvironmentVariables = new HashSet<>(Arrays.asList(result.split("\r?\n"))); + assertFalse("Should contain at least two environment variables starting with NIFI", dynamicEnvironmentVariables.size() < 2); + assertTrue("NIFI_TEST_1 environment variable is missing", dynamicEnvironmentVariables.contains("NIFI_TEST_1=testvalue1")); + assertTrue("NIFI_TEST_2 environment variable is missing", dynamicEnvironmentVariables.contains("NIFI_TEST_2=testvalue2")); + } + + @Test + public void testQuotedArguments() throws Exception { + List<String> args = ArgumentUtils.splitArgs("echo -n \"arg1 arg2 arg3\"", ' '); + assertEquals(3, args.size()); + args = ArgumentUtils.splitArgs("echo;-n;\"arg1 arg2 arg3\"", ';'); + assertEquals(3, args.size()); + } + + @Test + public void testInvalidDelimiter() throws Exception { + final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class); + controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "echo"); + controller.assertValid(); + controller.setProperty(ExecuteStreamCommand.ARG_DELIMITER, "foo"); + controller.assertNotValid(); + controller.setProperty(ExecuteStreamCommand.ARG_DELIMITER, "f"); + controller.assertValid(); + } }