NIFI-1562 ExecuteStreamCommand and ExecuteProcess now support empty command line arguments

This closes #247

Signed-off-by: Matt Burgess <mattyb149@apache.org>
This commit is contained in:
Michal Klempa 2016-02-25 09:01:12 +01:00 committed by Matt Burgess
parent e7a254f78e
commit e12e7a55b7
2 changed files with 32 additions and 23 deletions

View File

@ -38,21 +38,18 @@ public class ArgumentUtils {
final List<String> args = new ArrayList<>();
final String trimmed = input.trim();
boolean inQuotes = false;
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < trimmed.length(); i++) {
final char c = trimmed.charAt(i);
for (int i = 0; i < input.length(); i++) {
final char c = input.charAt(i);
if (DELIMITING_CHARACTERS.contains(c) || c == definedDelimiter) {
if (inQuotes) {
sb.append(c);
} else {
final String arg = sb.toString().trim();
if (!arg.isEmpty()) {
args.add(arg);
}
final String arg = sb.toString();
args.add(arg);
sb.setLength(0);
}
continue;
@ -66,11 +63,7 @@ public class ArgumentUtils {
sb.append(c);
}
final String finalArg = sb.toString().trim();
if (!finalArg.isEmpty()) {
args.add(finalArg);
}
args.add(sb.toString());
return args;
}

View File

@ -16,6 +16,7 @@
*/
package org.apache.nifi.processors.standard;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@ -41,25 +42,40 @@ public class TestExecuteProcess {
final List<String> zeroArgs = ArgumentUtils.splitArgs(" ", ' ');
assertNotNull(zeroArgs);
assertTrue(zeroArgs.isEmpty());
assertEquals(3, zeroArgs.size());
String[] expectedArray = {"","",""};
assertArrayEquals(expectedArray, zeroArgs.toArray(new String[0]));
final List<String> singleArg = ArgumentUtils.splitArgs(" hello ", ' ');
final List<String> singleArg = ArgumentUtils.splitArgs(" hello ", ';');
assertEquals(1, singleArg.size());
assertEquals("hello", singleArg.get(0));
assertEquals(" hello ", singleArg.get(0));
final List<String> twoArg = ArgumentUtils.splitArgs(" hello good-bye ", ' ');
final List<String> twoArg = ArgumentUtils.splitArgs(" hello ; good-bye ", ';');
assertEquals(2, twoArg.size());
assertEquals("hello", twoArg.get(0));
assertEquals("good-bye", twoArg.get(1));
assertEquals(" hello ", twoArg.get(0));
assertEquals(" good-bye ", twoArg.get(1));
final List<String> singleQuotedArg = ArgumentUtils.splitArgs(" \"hello\" ", ' ');
assertEquals(1, singleQuotedArg.size());
assertEquals("hello", singleQuotedArg.get(0));
final List<String> oneUnnecessarilyQuotedArg = ArgumentUtils.splitArgs(" \"hello\" ", ';');
assertEquals(1, oneUnnecessarilyQuotedArg.size());
assertEquals(" hello ", oneUnnecessarilyQuotedArg.get(0));
final List<String> twoQuotedArg = ArgumentUtils.splitArgs(" hello \"good bye\"", ' ');
final List<String> twoQuotedArg = ArgumentUtils.splitArgs("\" hello\" \"good bye\"", ' ');
assertEquals(2, twoQuotedArg.size());
assertEquals("hello", twoQuotedArg.get(0));
assertEquals(" hello", twoQuotedArg.get(0));
assertEquals("good bye", twoQuotedArg.get(1));
final List<String> twoArgOneQuotedPerDelimiterArg = ArgumentUtils.splitArgs("one;two;three\";\"and\";\"half\"", ';');
assertEquals(3, twoArgOneQuotedPerDelimiterArg.size());
assertEquals("one", twoArgOneQuotedPerDelimiterArg.get(0));
assertEquals("two", twoArgOneQuotedPerDelimiterArg.get(1));
assertEquals("three;and;half", twoArgOneQuotedPerDelimiterArg.get(2));
final List<String> twoArgOneWholeQuotedArgOneEmptyArg = ArgumentUtils.splitArgs("one;two;\"three;and;half\";", ';');
assertEquals(4, twoArgOneWholeQuotedArgOneEmptyArg.size());
assertEquals("one", twoArgOneWholeQuotedArgOneEmptyArg.get(0));
assertEquals("two", twoArgOneWholeQuotedArgOneEmptyArg.get(1));
assertEquals("three;and;half", twoArgOneWholeQuotedArgOneEmptyArg.get(2));
assertEquals("", twoArgOneWholeQuotedArgOneEmptyArg.get(3));
}
@Ignore // won't run under Windows