AbstractSetTypeConverter implements TypeConverter<Set, Set>, Named, NeedsJTransfo { @Override public Set convert(Set toObjects, SyntheticField domainField, Object domainObject, String... tags) throws JTransfoException { if (null == toObjects) { return getNullSet(domainField, domainObject); } Set<Object> res = newSet(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 res; } AbstractSetTypeConverter(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 Set convert(Set toObjects, SyntheticField domainField, Object domainObject, String... tags); Object doReverseOne(
            JTransfo jTransfo, Object domainObject, SyntheticField toField, Class<?> toType, String... tags); @Override Set reverse(Set domainObjects, SyntheticField toField, Object toObject, String... tags); void setKeepNullSet(boolean keepNullSet); void setAlwaysNewSet(boolean alwaysNewSet); void setEmptySetSupplier(Supplier<Set> emptySetSupplier);  }
@Test public void testConvert() throws Exception { AddressTo to1 = new AddressTo(); to1.setId(2L); AddressTo to2 = new AddressTo(); to2.setId(1L); Set<AddressTo> addresses = new HashSet<>(); addresses.add(to1); addresses.add(to2); Object domainObject = new Object(); Set resSet = spy(new HashSet()); when(field.get(domainObject)).thenReturn(resSet); Set<AddressDomain> res = setTypeConverter.convert(addresses, field, domainObject); assertThat(res).isNotNull(); assertThat(res).hasSize(2); assertThat(res).extracting("id").contains(1L, 2L); verify(field).get(domainObject); verify(resSet).clear(); }
@Test public void testConvertGetFails() throws Exception { AddressTo to1 = new AddressTo(); to1.setId(2L); AddressTo to2 = new AddressTo(); to2.setId(1L); Set<AddressTo> addresses = new HashSet<>(); addresses.add(to1); addresses.add(to2); Object domainObject = new Object(); Set resSet = spy(new HashSet()); when(field.get(domainObject)).thenThrow(new NullPointerException()); Set<AddressDomain> res = setTypeConverter.convert(addresses, field, domainObject); assertThat(res).isNotNull(); assertThat(res).hasSize(2); assertThat(res).extracting("id").contains(1L, 2L); verify(field).get(domainObject); }