QuotedStringSplitter { public static List<String> split(String str) { Matcher m = pattern.matcher(str); List<String> result = new ArrayList<>(); while (m.find()) { String r; if (m.group(1) != null) { r = m.group(1); } else if (m.group(3) != null) { r = m.group(3); } else { r = m.group(); } result.add(StringEscapeUtils.unescapeJava(r)); } return result; }  static List<String> split(String str);  }
@Test public void singleSingleQuote() { assertEquals(Arrays.asList("that's", "cool"), QuotedStringSplitter.split("that's cool")); }
@Test public void mixed() { assertEquals(Arrays.asList("'s g'", "s' ", "\"s", "s\""), QuotedStringSplitter.split("\"'s g'\" \"s' \" '\"s' 's\"'")); }
@Test public void escaped() { assertEquals(Arrays.asList("a'b", "a\"b", "\n"), QuotedStringSplitter.split("'a\\'b' \"a\\\"b\" \\n")); }
@Test public void noQuotes() { assertEquals(Arrays.asList("hello", "world"), QuotedStringSplitter.split("hello world")); }
@Test public void manySpaces() { assertEquals(Arrays.asList("hello", "world"), QuotedStringSplitter.split(" hello world ")); }
@Test public void doubleQuotes() { assertEquals(Arrays.asList("hello world", "test"), QuotedStringSplitter.split("\"hello world\" test")); }
@Test public void doubleQuotesWithSpaces() { assertEquals(Arrays.asList(" hello world", "test"), QuotedStringSplitter.split("\" hello world\" test")); }
@Test public void singleQuotes() { assertEquals(Arrays.asList("hello world", "test"), QuotedStringSplitter.split("'hello world' test")); }
@Test public void singleQuotesWithSpaces() { assertEquals(Arrays.asList(" hello world", "test"), QuotedStringSplitter.split("' hello world' test")); }