### Question:
StringUtil { public static String[] split(String text) { if (text == null || text.isEmpty()) { return NO_STRINGS; } final Matcher matcher = ITEM_PATTERN.matcher(text); String item = null; StringBuilder b = null; ArrayList<String> list = new ArrayList<>(4); while (matcher.find()) { if (matcher.group(1) != null) { if (item != null) { list.add(item); item = null; } } else if (matcher.group(2) != null) { assert item == null : "Regular expression matching malfunctioned"; item = matcher.group(2); } else if (matcher.group(3) != null) { if (b == null) { b = new StringBuilder(); } if (item != null) { b.append(item); item = null; } b.append(matcher.group(3)); while (matcher.find()) { if (matcher.group(1) != null) { break; } else if (matcher.group(2) != null) { b.append(matcher.group(2)); } else if (matcher.group(3) != null) { b.append(matcher.group(3)); } else { throw new IllegalStateException(); } } list.add(b.toString()); b.setLength(0); } else { throw new IllegalStateException(); } } if (item != null) { list.add(item); } return list.toArray(NO_STRINGS); } private  StringUtil(); static String[] split(String text);  }### Answer:
@Test public void testSplit() { String text = "large:cheese\\,mushroom,medium:chicken,small:pepperoni"; String[] split = StringUtil.split(text); assertEquals(3, split.length); assertEquals("large:cheese,mushroom", split[0]); assertEquals("medium:chicken", split[1]); assertEquals("small:pepperoni", split[2]); }
@Test public void testTrailingSegmentsIgnored() { String text = "foo,bar,baz,,,,,"; final String[] split = StringUtil.split(text); assertEquals(3, split.length); assertEquals("foo", split[0]); assertEquals("bar", split[1]); assertEquals("baz", split[2]); }
@Test public void testLeadingSegmentsIgnored() { String text = ",,,,,,,,foo,bar,baz"; final String[] split = StringUtil.split(text); assertEquals(3, split.length); assertEquals("foo", split[0]); assertEquals("bar", split[1]); assertEquals("baz", split[2]); }
@Test public void testMidSegmentsIgnored() { String text = "foo,,,,bar,,,baz"; final String[] split = StringUtil.split(text); assertEquals(3, split.length); assertEquals("foo", split[0]); assertEquals("bar", split[1]); assertEquals("baz", split[2]); }
@Test public void testAllEmptySegments() { String text = ",,,,,"; final String[] split = StringUtil.split(text); assertEquals(0, split.length); }
@Test public void testTwoEmptySegments() { String text = ","; final String[] split = StringUtil.split(text); assertEquals(0, split.length); }
@Test public void testEmptyString() { assertEquals(0, StringUtil.split("").length); }
@Test public void testIffyEscapingSituations() { String text = "foo\\\\,bar\\x,,,baz"; final String[] split = StringUtil.split(text); assertEquals(3, split.length); assertEquals("foo\\", split[0]); assertEquals("barx", split[1]); assertEquals("baz", split[2]); }