ReadOnlyDomainAutomaticTypeConverter extends AbstractToDomainTypeConverter implements Named, NeedsJTransfo { @Override public Object reverse(Object domainObject, SyntheticField toField, Object toObject, String... tags) throws JTransfoException { if (List.class.isAssignableFrom(toField.getType())) { return rodListTypeConverter.reverse((List<?>) domainObject, toField, toObject, tags); } if (Set.class.isAssignableFrom(toField.getType())) { return rodSetTypeConverter.reverse((Set<?>) domainObject, toField, toObject, tags); } return super.reverse(domainObject, toField, toObject, tags); }  @Override void setJTransfo(JTransfo jTransfo); @Override boolean canConvert(Type realToType, Type realDomainType); @Override Object convert(Object toObject, SyntheticField domainField, Object domainObject, String... tags); @Override Object reverse(Object domainObject, SyntheticField toField, Object toObject, String... tags); @Override Object doConvert(JTransfo jTransfo, Object toObject, SyntheticField domainField, String... tags); @Override String getName(); void setName(String name); void setKeepNullList(boolean keepNullList); void setKeepNullSet(boolean keepNullSet); void setAlwaysNewList(boolean alwaysNewList); void setAlwaysNewSet(boolean alwaysNewSet); void setSortList(boolean sortList);  }
@Test public void testReverseNullHandling() throws Exception { assertThat(typeConverter.reverse(null, toField, null)).isNull(); }
@Test public void testReverseInstantiationException() throws Exception { SimpleBaseDomain source = new SimpleBaseDomain(); when(reflectionHelper.newInstance(any(Class.class))).thenThrow(new InstantiationException()); exception.expect(JTransfoException.class); exception.expectMessage("Cannot create instance of transfer object class org.jtransfo.object.SimpleBaseTo."); typeConverter.reverse(source, toField, null); }
@Test public void testReverseIllegalAccessException() throws Exception { SimpleBaseDomain source = new SimpleBaseDomain(); when(reflectionHelper.newInstance(any(Class.class))).thenThrow(new IllegalAccessException()); exception.expect(JTransfoException.class); exception.expectMessage("Cannot create instance of transfer object class org.jtransfo.object.SimpleBaseTo."); typeConverter.reverse(source, toField, null); }
@Test public void testReverseList() throws Exception { AddressDomain ad1 = new AddressDomain(); ad1.setId(1L); AddressDomain ad2 = new AddressDomain(); ad2.setId(2L); List<AddressDomain> addresses = new ArrayList<>(); addresses.add(ad1); addresses.add(ad2); List<AddressTo> resList = new ArrayList<>(); Object to = new Object(); when(listField.get(to)).thenReturn(resList); List<AddressTo> res = (List) typeConverter.reverse(addresses, listField, to); assertThat(res).isNotNull(); assertThat(res).hasSize(2); verify(jTransfo).convertTo(ad1, AddressTo.class); verify(jTransfo).convertTo(ad2, AddressTo.class); assertThat(res).isEqualTo(resList); }
@Test public void testReverseSet() 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); Set<AddressTo> resList = new HashSet<>(); Object to = new Object(); when(setField.get(to)).thenReturn(resList); Set<AddressTo> res = (Set) typeConverter.reverse(addresses, setField, to); assertThat(res).isNotNull(); assertThat(res).hasSize(1); verify(jTransfo).convertTo(ad1, AddressTo.class); verify(jTransfo).convertTo(ad2, AddressTo.class); assertThat(res).isEqualTo(resList); }