JTransfoImpl implements JTransfo, ConfigurableJTransfo, ConvertSourceTarget { @Override public <T> T convert(Object source, T target, String... tags) { if (null == source || null == target) { throw new JTransfoException("Source and target are required to be not-null."); } source = replaceObject(source); boolean targetIsTo = false; if (!toHelper.isTo(source)) { targetIsTo = true; if (!toHelper.isTo(target)) { throw new JTransfoException(String.format("Neither source nor target are annotated with DomainClass " + "on classes %s and %s.", source.getClass().getName(), target.getClass().getName())); } } if (0 == tags.length) { tags = DEFAULT_TAGS_WHEN_NO_TAGS; } return convertInterceptorChain.convert(source, target, targetIsTo, tags); } 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 testConvertNull() { assertThat(jTransfo.convert(null)).isNull(); }
@Test public void testConvert2NullLeft() { JTransfoException exc = Assertions.assertThrows(JTransfoException.class, () -> jTransfo.convert("bla", null)); assertThat(exc.getMessage()).isEqualTo("Source and target are required to be not-null."); }
@Test public void testConvert2NullRight() { JTransfoException exc = Assertions.assertThrows(JTransfoException.class, () -> jTransfo.convert(null, "bla")); assertThat(exc.getMessage()).isEqualTo("Source and target are required to be not-null."); }
@Test public void testConvert2DomainClass() { JTransfoException exc = Assertions.assertThrows(JTransfoException.class, () -> jTransfo.convert("alb", "bla")); assertThat(exc.getMessage()).isEqualTo("Neither source nor target are annotated with DomainClass on classes " + "java.lang.String and java.lang.String."); }
@Test public void testConvertInstantiationException() throws Exception { when(reflectionHelper.newInstance(SimpleClassDomain.class)).thenThrow(new InstantiationException()); JTransfoException exc = Assertions.assertThrows(JTransfoException.class, () -> jTransfo.convert(new SimpleClassNameTo())); assertThat(exc.getMessage()).isEqualTo("Cannot create instance for domain class org.jtransfo.object.SimpleClassDomain."); }
@Test public void testConvertIllegalAccessException() throws Exception { when(reflectionHelper.newInstance(SimpleClassDomain.class)).thenThrow(new IllegalAccessException()); JTransfoException exc = Assertions.assertThrows(JTransfoException.class, () -> jTransfo.convert(new SimpleClassNameTo())); assertThat(exc.getMessage()).isEqualTo("Cannot create instance for domain class org.jtransfo.object.SimpleClassDomain."); }
@Test public void testConvertNullTarget() throws Exception { when(reflectionHelper.newInstance(SimpleClassDomain.class)).thenReturn(null); JTransfoException exc = Assertions.assertThrows(JTransfoException.class, () -> jTransfo.convert(new SimpleClassNameTo())); assertThat(exc.getMessage()).startsWith("Cannot create instance of target class org.jtransfo.object.SimpleClassDomain " + "for source object org.jtransfo.object.SimpleClassNameTo"); }
@Test public void testConvert_passTags() throws Exception { ConvertSourceTarget cst = mock(ConvertSourceTarget.class); ReflectionTestUtils.setField(jTransfo, "convertInterceptorChain", cst); Object source = new PersonTo(); Object target = mock(Object.class); String[] tags = new String[] { "bla" }; jTransfo.convert(source, target, tags); verify(cst).convert(source, target, false, tags); }
@Test public void testConvert_noTags() throws Exception { ConvertSourceTarget cst = mock(ConvertSourceTarget.class); ReflectionTestUtils.setField(jTransfo, "convertInterceptorChain", cst); Object source = new PersonTo(); Object target = mock(Object.class); jTransfo.convert(source, target); verify(cst).convert(source, target, false, JTransfo.DEFAULT_TAG_WHEN_NO_TAGS); }