Commandline implements Cloneable { public static List<String> translateCommandline(String toProcess) { if (toProcess == null || toProcess.length() == 0) { return Collections.emptyList(); } final int normal = 0; final int inQuote = 1; final int inDoubleQuote = 2; int state = normal; final StringTokenizer tok = new StringTokenizer(toProcess, "\"\' ", true); final ArrayList<String> result = new ArrayList<>(); final StringBuilder current = new StringBuilder(); boolean lastTokenHasBeenQuoted = false; while (tok.hasMoreTokens()) { String nextTok = tok.nextToken(); switch (state) { case inQuote: if ("\'".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = normal; } else { current.append(nextTok); } break; case inDoubleQuote: if ("\"".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = normal; } else { current.append(nextTok); } break; default: if ("\'".equals(nextTok)) { state = inQuote; } else if ("\"".equals(nextTok)) { state = inDoubleQuote; } else if (" ".equals(nextTok)) { if (lastTokenHasBeenQuoted || current.length() != 0) { result.add(current.toString()); current.setLength(0); } } else { current.append(nextTok); } lastTokenHasBeenQuoted = false; break; } } if (lastTokenHasBeenQuoted || current.length() != 0) { result.add(current.toString()); } if (state == inQuote || state == inDoubleQuote) { throw new IllegalArgumentException("unbalanced quotes in " + toProcess); } return result; }  static List<String> translateCommandline(String toProcess);  }
@Test public void simpleEmptyTest(){ result = commandline.translateCommandline(""); assertEquals(expected,result); }
@Test public void simpleNullTest(){ result = commandline.translateCommandline(null); assertEquals(expected,result); }
@Test public void simpleCommandTest(){ expected.clear(); expected.add("cd"); expected.add("/tmp"); result = commandline.translateCommandline("cd /tmp"); assertEquals(expected,result); }
@Test public void CommandWithDoubleQuoteTest(){ expected.clear(); expected.add("echo"); expected.add("Hello! World"); result = commandline.translateCommandline("echo \"Hello! World\""); assertEquals(expected,result); }
@Test public void commandWithBothTypeofQuotesTest(){ expected.clear(); expected.add("echo"); expected.add("Hello! World"); expected.add("Hello Java Folks"); result = commandline. translateCommandline("echo \"Hello! World\" \'Hello Java Folks\'"); assertEquals(expected,result); }
@Test public void commandWithNestedQuotesTest(){ expected.clear(); expected.add("echo"); expected.add("Hello! World \'Hello Java Folks\'"); result = commandline. translateCommandline("echo \"Hello! World \'Hello Java Folks\'\""); assertEquals(expected,result); }
@Test(expected = IllegalArgumentException.class) public void invalidDoubleQuoteCommandTest(){ result = commandline. translateCommandline("echo \"Hello! World\" \'Hello Java Folks"); }
@Test(expected = IllegalArgumentException.class) public void invalidSingleQuoteCommandTest(){ result = commandline. translateCommandline("echo \"Hello! World \'Hello Java Folks\'"); }