### Question:
ReturnFields implements Iterable<String> { @Override public String toString() { return "[ReturnFieldsImpl: fields=" + this.fields + "]"; } ReturnFields();  ReturnFields(String spec); ReturnFields child(String field); boolean included(String... pathSegments); boolean excluded(String field); Iterator<String> iterator(); boolean isEmpty(); boolean isAll(); @Override String toString(); static ReturnFields ALL; static ReturnFields NONE; static ReturnFields ALL_RECURSIVELY; }### Answer:
@Test public void testBasic() { String spec = "field1,field2,field3"; ReturnFields fspec = new ReturnFields(spec); StringBuilder val = new StringBuilder(); for (String field : fspec) { if (val.length() > 0) val.append(','); val.append(field); } Assert.assertEquals(spec, val.toString()); String[] specs = { "", null, ",", "field1,", ",field2" }; for (String filter : specs) { try { fspec = new ReturnFields(filter); Assert.fail("Parsing of fields spec should have failed! : " + filter); } catch (Exception e) { } } }
@Test public void testNested() { String spec = "field1,field2(sub1,sub2(subsub1)),field3"; ReturnFields fspec = new ReturnFields(spec); String val = traverse(fspec); Assert.assertEquals(spec, val.toString()); String[] specs = { "(", ")", "field1,(", "field1,)", "field1,field2(", "field1,field2)", "field1,field2()", "field1,field2(sub1)(", "field1,field2(sub1))", "field1,field2(sub1)," }; for (String filter : specs) { try { fspec = new ReturnFields(filter); Assert.fail("Parsing of fields spec should have failed! : " + filter); } catch (Exception e) { } } }