JTransfoImpl implements JTransfo, ConfigurableJTransfo, ConvertSourceTarget { @Override public <T> T findTarget(Object source, Class<T> targetClass, String... tags) { if (null == source) { return null; } source = replaceObject(source); int i = objectFinders.size() - 1; T target = null; Class<T> realTargetClass = replaceClass(targetClass); if (isToClass(realTargetClass)) { realTargetClass = (Class<T>) getToSubType(realTargetClass, source); } while (null == target && i >= 0) { target = objectFinders.get(i--).getObject(realTargetClass, source, tags); } if (null == target) { throw new JTransfoException("Cannot create instance of target class " + realTargetClass.getName() + " for source object " + source + "."); } return target; } JTransfoImpl(); @Override List<TypeConverter> getTypeConverters(); @Override void updateTypeConverters(); @Override void updateTypeConverters(List<TypeConverter> newConverters); @Override List<PreConverter> getPreConverters(); @Override void updatePreConverters(); @Override void updatePreConverters(List<PreConverter> newConverters); @Override List<PostConverter> getPostConverters(); @Override void updatePostConverters(); @Override void updatePostConverters(List<PostConverter> newConverters); @Override List<ObjectFinder> getObjectFinders(); @Override void updateObjectFinders(); @Override void updateObjectFinders(List<ObjectFinder> newObjectFinders); @Override List<ObjectReplacer> getObjectReplacers(); @Override void updateObjectReplacers(); @Override void updateObjectReplacers(List<ObjectReplacer> newObjectReplacers); @Override List<ClassReplacer> getClassReplacers(); @Override void updateClassReplacers(); @Override void updateClassReplacers(List<ClassReplacer> newClassReplacers); List<ConvertInterceptor> getConvertInterceptors(); void updateConvertInterceptors(); void updateConvertInterceptors(List<ConvertInterceptor> newConvertInterceptors); @Override T convert(Object source, T target, String... tags); @Override T convert(Object source, T target, boolean targetIsTo, String... tags); @Override Object convert(Object source); @Override T convertTo(Object source, Class<T> targetClass, String... tags); @Override List<T> convertList(List<?> source, Class<T> targetClass, String... tags); @Override T findTarget(Object source, Class<T> targetClass, String... tags); @Override Class<?> getDomainClass(Class<?> toClass); @Override boolean isToClass(Class<?> toClass); @Override Class<?> getToSubType(Class<?> toType, Object domainObject); void clearCaches();  }
@Test public void testFindTargetNull() { assertThat(jTransfo.findTarget(null, this.getClass())).isNull(); }
@Test public void testFindTargetInstantiationException() throws Exception { when(reflectionHelper.newInstance(SimpleClassDomain.class)).thenThrow(new InstantiationException()); JTransfoException exc = Assertions.assertThrows(JTransfoException.class, () -> jTransfo.findTarget(new SimpleClassNameTo(), SimpleClassDomain.class)); assertThat(exc.getMessage()).isEqualTo("Cannot create instance for domain class org.jtransfo.object.SimpleClassDomain."); }
@Test public void testFindTargetIllegalAccessException() throws Exception { when(reflectionHelper.newInstance(SimpleClassDomain.class)).thenThrow(new IllegalAccessException()); JTransfoException exc = Assertions.assertThrows(JTransfoException.class, () -> jTransfo.findTarget(new SimpleClassNameTo(), SimpleClassDomain.class)); assertThat(exc.getMessage()).isEqualTo("Cannot create instance for domain class org.jtransfo.object.SimpleClassDomain."); }
@Test public void testFindTarget() throws Exception { SimpleClassNameTo to = new SimpleClassNameTo(); SimpleClassDomain target = mock(SimpleClassDomain.class); ObjectFinder objectFinder = mock(ObjectFinder.class); when(objectFinder.getObject(SimpleClassDomain.class, to)).thenReturn(target); jTransfo.updateObjectFinders(Collections.singletonList(objectFinder)); SimpleClassDomain res = jTransfo.findTarget(to, SimpleClassDomain.class); verify(objectFinder).getObject(SimpleClassDomain.class, to); assertThat(res).isEqualTo(target); }