AbstractListTypeConverter implements TypeConverter<List, List>, Named, NeedsJTransfo { @Override public List convert(List toObjects, SyntheticField domainField, Object domainObject, String... tags) throws JTransfoException { if (null == toObjects) { return getNullList(domainField, domainObject); } List<Object> res = newList(domainField, domainObject); for (Object to : toObjects) { if (null == to) { res.add(null); } else { if (isPrimitiveOrString(to.getClass())) { res.add(to); } else { res.add(doConvertOne(jTransfo, to, jTransfo.getDomainClass(to.getClass()), tags)); } } } return sort(res); } AbstractListTypeConverter(String name, Class<?> toType); @Override String getName(); @Override void setJTransfo(JTransfo jTransfo); @Override boolean canConvert(Type realToType, Type realDomainType); abstract Object doConvertOne(JTransfo jTransfo, Object toObject, Class<?> domainObjectType, String... tags); @Override List convert(List toObjects, SyntheticField domainField, Object domainObject, String... tags); Object doReverseOne(
            JTransfo jTransfo, Object domainObject, SyntheticField toField, Class<?> toType, String... tags); @Override List reverse(List domainObjects, SyntheticField toField, Object toObject, String... tags); void setKeepNullList(boolean keepNullList); void setAlwaysNewList(boolean alwaysNewList); void setSortList(boolean sortList); void setEmptyListSupplier(Supplier<List> emptyListSupplier);  }
@Test public void testConvert() throws Exception { AddressTo to1 = new AddressTo(); to1.setId(2L); AddressTo to2 = new AddressTo(); to2.setId(1L); List<AddressTo> addresses = new ArrayList<>(); addresses.add(to1); addresses.add(to2); Object domainObject = new Object(); List resList = spy(new ArrayList()); when(field.get(domainObject)).thenReturn(resList); List<AddressDomain> res = listTypeConverter.convert(addresses, field, domainObject); assertThat(res).isNotNull(); assertThat(res).hasSize(2); assertThat(res.get(0).getId()).isEqualTo(2L); assertThat(res.get(1).getId()).isEqualTo(1L); verify(field).get(domainObject); verify(resList).clear(); }
@Test public void testConvertGetFails() throws Exception { AddressTo to1 = new AddressTo(); to1.setId(2L); AddressTo to2 = new AddressTo(); to2.setId(1L); List<AddressTo> addresses = new ArrayList<>(); addresses.add(to1); addresses.add(to2); Object domainObject = new Object(); List resList = spy(new ArrayList()); when(field.get(domainObject)).thenThrow(new NullPointerException()); List<AddressDomain> res = listTypeConverter.convert(addresses, field, domainObject); assertThat(res).isNotNull(); assertThat(res).hasSize(2); assertThat(res.get(0).getId()).isEqualTo(2L); assertThat(res.get(1).getId()).isEqualTo(1L); verify(field).get(domainObject); }