CollectionUtils { public static String join(Collection<String> toJoin, String separator) { if (isNullOrEmpty(toJoin)) { return ""; } StringBuilder joinedString = new StringBuilder(); int currentIndex = 0; for (String s : toJoin) { if(s != null) { joinedString.append(s); } if (currentIndex++ != toJoin.size() - 1) { joinedString.append(separator); } } return joinedString.toString(); }  static boolean isNullOrEmpty(Collection<T> collection); static List<T> mergeLists(List<T> list1, List<T> list2); static String join(Collection<String> toJoin, String separator);  }
@Test public void join_NullCollection_ReturnsNull() { assertThat(CollectionUtils.join(null, ","), isEmptyString()); }
@Test public void join_EmptyCollection_ReturnsNull() { assertThat(CollectionUtils.join(Collections.<String>emptyList(), ","), isEmptyString()); }
@Test public void join_SingleItemCollection_ReturnsItemAsString() { assertEquals("foo", CollectionUtils.join(Arrays.asList("foo"), ",")); }
@Test public void join_SingleItemCollectionOfNullString_ReturnsEmptyString() { List<String> list = new ArrayList<String>(); list.add(null); assertEquals("", CollectionUtils.join(list, ",")); }
@Test public void join_MultiItemCollection_ReturnsItemsJoinedWithSeparator() { assertEquals("foo,bar,baz", CollectionUtils.join(Arrays.asList("foo", "bar", "baz"), ",")); }
@Test public void join_MultiItemCollectionWithNullItem_ReturnsItemsJoinedWithSeparator() { assertEquals("foo,,baz", CollectionUtils.join(Arrays.asList("foo", null, "baz"), ",")); }
@Test public void join_MultiItemCollectionWithAllNulls_ReturnsItemsJoinedWithSeparator() { assertEquals(",,", CollectionUtils.join(Arrays.<String>asList(null, null, null), ",")); }