AbstractSetTypeConverter implements TypeConverter<Set, Set>, Named, NeedsJTransfo { @Override public Set reverse(Set domainObjects, SyntheticField toField, Object toObject, String... tags) throws JTransfoException { if (null == domainObjects) { return getNullSet(toField, toObject); } Set<Object> res = newSet(toField, toObject); for (Object domain : domainObjects) { if (isPrimitiveOrString(domain.getClass())) { res.add(domain); } else { res.add(doReverseOne(jTransfo, domain, toField, toType, 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 testReverse() throws Exception { AddressDomain ad1 = new AddressDomain(); ad1.setId(1L); AddressDomain ad2 = new AddressDomain(); ad2.setId(2L); Set<AddressDomain> addresses = new HashSet<>(); addresses.add(ad1); addresses.add(ad2); Object toObject = new Object(); Set resSet = spy(new HashSet()); when(field.get(toObject)).thenReturn(resSet); Set<AddressTo> res = setTypeConverter.reverse(addresses, field, toObject); assertThat(res).isNotNull(); assertThat(res).hasSize(2); assertThat(res).extracting("id").contains(1L, 2L); verify(field).get(toObject); verify(resSet).clear(); }
@Test public void testReverseGetFails() throws Exception { AddressDomain ad1 = new AddressDomain(); ad1.setId(1L); AddressDomain ad2 = new AddressDomain(); ad2.setId(2L); Set<AddressDomain> addresses = new HashSet<>(); addresses.add(ad1); addresses.add(ad2); Object toObject = new Object(); when(field.get(toObject)).thenThrow(new NullPointerException()); Set<AddressTo> res = setTypeConverter.reverse(addresses, field, toObject); assertThat(res).isNotNull(); assertThat(res).hasSize(2); assertThat(res).extracting("id").contains(1L, 2L); verify(field).get(toObject); }