ConverterHelper { TypeConverter getDeclaredTypeConverter(String typeConverterClassName1, String typeConverterClassName2) { String typeConverterClass = typeConverterClassName1; if (MappedBy.DefaultTypeConverter.class.getName().equals(typeConverterClass)) { typeConverterClass = typeConverterClassName2; } if (!MappedBy.DEFAULT_TYPE_CONVERTER.equals(typeConverterClass)) { TypeConverter typeConverter = typeConverterInstances.get(typeConverterClass); if (null == typeConverter) { try { typeConverter = reflectionHelper.newInstance(typeConverterClass); typeConverterInstances.put(typeConverterClass, typeConverter); } catch (ClassNotFoundException cnfe) { throw new JTransfoException(DECLARED_TYPE_CONVERTER_CLASS + typeConverterClass + " cannot be found.", cnfe); } catch (InstantiationException ie) { throw new JTransfoException(DECLARED_TYPE_CONVERTER_CLASS + typeConverterClass + " cannot be instantiated.", ie); } catch (IllegalAccessException iae) { throw new JTransfoException(DECLARED_TYPE_CONVERTER_CLASS + typeConverterClass + " cannot be accessed.", iae); } catch (ClassCastException cce) { throw new JTransfoException(DECLARED_TYPE_CONVERTER_CLASS + typeConverterClass + " cannot be cast to a TypeConverter.", cce); } } return typeConverter; } return null; }  ToConverter getToConverter(Class toClass, Class domainClass);  }
@Test public void testGetDeclaredTypeConverterNull() throws Exception { assertThat(converterHelper.getDeclaredTypeConverter(null)).isNull(); }
@Test public void testGetDeclaredTypeConverterDefaults() throws Exception { MappedBy mappedBy = mock(MappedBy.class); when(mappedBy.typeConverterClass()).thenReturn(MappedBy.DefaultTypeConverter.class); when(mappedBy.typeConverter()).thenReturn(MappedBy.DEFAULT_TYPE_CONVERTER); when(mappedBy.field()).thenReturn(MappedBy.DEFAULT_FIELD); assertThat(converterHelper.getDeclaredTypeConverter(mappedBy)).isNull(); }
@Test public void testGetDeclaredTypeConverterAsClass() throws Exception { ReflectionHelper reflectionHelper = mock(ReflectionHelper.class); ReflectionTestUtils.setField(converterHelper, "reflectionHelper", reflectionHelper); MappedBy mappedBy = mock(MappedBy.class); when(mappedBy.typeConverterClass()).thenReturn(NoConversionTypeConverter.class); when(mappedBy.typeConverter()).thenReturn(MappedBy.DEFAULT_TYPE_CONVERTER); when(mappedBy.field()).thenReturn(MappedBy.DEFAULT_FIELD); when(reflectionHelper.newInstance(NoConversionTypeConverter.class.getName())). thenReturn(new NoConversionTypeConverter()); TypeConverter res = converterHelper.getDeclaredTypeConverter(mappedBy); assertThat(res).isInstanceOf(NoConversionTypeConverter.class); TypeConverter res2 = converterHelper.getDeclaredTypeConverter(mappedBy); assertThat(res2 == res).isTrue(); }
@Test public void testGetDeclaredTypeConverterAsName() throws Exception { ReflectionHelper reflectionHelper = mock(ReflectionHelper.class); ReflectionTestUtils.setField(converterHelper, "reflectionHelper", reflectionHelper); MappedBy mappedBy = mock(MappedBy.class); when(mappedBy.typeConverterClass()).thenReturn(MappedBy.DefaultTypeConverter.class); when(mappedBy.typeConverter()).thenReturn(NoConversionTypeConverter.class.getName()); when(mappedBy.field()).thenReturn(MappedBy.DEFAULT_FIELD); when(reflectionHelper.newInstance(NoConversionTypeConverter.class.getName())). thenReturn(new NoConversionTypeConverter()); TypeConverter res = converterHelper.getDeclaredTypeConverter(mappedBy); assertThat(res).isInstanceOf(NoConversionTypeConverter.class); }
@Test public void testGetDeclaredTypeConverterCnfe() throws Exception { ReflectionHelper reflectionHelper = mock(ReflectionHelper.class); ReflectionTestUtils.setField(converterHelper, "reflectionHelper", reflectionHelper); MappedBy mappedBy = mock(MappedBy.class); when(mappedBy.typeConverterClass()).thenReturn(MappedBy.DefaultTypeConverter.class); when(mappedBy.typeConverter()).thenReturn(NoConversionTypeConverter.class.getName()); when(mappedBy.field()).thenReturn(MappedBy.DEFAULT_FIELD); when(reflectionHelper.newInstance(NoConversionTypeConverter.class.getName())). thenThrow(new ClassNotFoundException()); exception.expect(JTransfoException.class); exception.expectMessage("Declared TypeConverter class org.jtransfo.NoConversionTypeConverter cannot be found."); converterHelper.getDeclaredTypeConverter(mappedBy); }
@Test public void testGetDeclaredTypeConverterIe() throws Exception { ReflectionHelper reflectionHelper = mock(ReflectionHelper.class); ReflectionTestUtils.setField(converterHelper, "reflectionHelper", reflectionHelper); MappedBy mappedBy = mock(MappedBy.class); when(mappedBy.typeConverterClass()).thenReturn(MappedBy.DefaultTypeConverter.class); when(mappedBy.typeConverter()).thenReturn(NoConversionTypeConverter.class.getName()); when(mappedBy.field()).thenReturn(MappedBy.DEFAULT_FIELD); when(reflectionHelper.newInstance(NoConversionTypeConverter.class.getName())). thenThrow(new InstantiationException()); exception.expect(JTransfoException.class); exception.expectMessage( "Declared TypeConverter class org.jtransfo.NoConversionTypeConverter cannot be instantiated."); converterHelper.getDeclaredTypeConverter(mappedBy); }
@Test public void testGetDeclaredTypeConverterIae() throws Exception { ReflectionHelper reflectionHelper = mock(ReflectionHelper.class); ReflectionTestUtils.setField(converterHelper, "reflectionHelper", reflectionHelper); MappedBy mappedBy = mock(MappedBy.class); when(mappedBy.typeConverterClass()).thenReturn(MappedBy.DefaultTypeConverter.class); when(mappedBy.typeConverter()).thenReturn(NoConversionTypeConverter.class.getName()); when(mappedBy.field()).thenReturn(MappedBy.DEFAULT_FIELD); when(reflectionHelper.newInstance(NoConversionTypeConverter.class.getName())). thenThrow(new IllegalAccessException()); exception.expect(JTransfoException.class); exception.expectMessage( "Declared TypeConverter class org.jtransfo.NoConversionTypeConverter cannot be accessed."); converterHelper.getDeclaredTypeConverter(mappedBy); }
@Test public void testGetDeclaredTypeConverterCce() throws Exception { ReflectionHelper reflectionHelper = mock(ReflectionHelper.class); ReflectionTestUtils.setField(converterHelper, "reflectionHelper", reflectionHelper); MappedBy mappedBy = mock(MappedBy.class); when(mappedBy.typeConverterClass()).thenReturn(MappedBy.DefaultTypeConverter.class); when(mappedBy.typeConverter()).thenReturn(NoConversionTypeConverter.class.getName()); when(mappedBy.field()).thenReturn(MappedBy.DEFAULT_FIELD); when(reflectionHelper.newInstance(NoConversionTypeConverter.class.getName())). thenThrow(new ClassCastException()); exception.expect(JTransfoException.class); exception.expectMessage("Declared TypeConverter class org.jtransfo.NoConversionTypeConverter " + "cannot be cast to a TypeConverter."); converterHelper.getDeclaredTypeConverter(mappedBy); }
@Test public void testGetDeclaredTypeConverter() throws Exception { MapOnly mapOnly = mock(MapOnly.class); when(mapOnly.typeConverterClass()).thenReturn(MappedBy.DefaultTypeConverter.class); when(mapOnly.typeConverter()).thenReturn(MappedBy.DEFAULT_TYPE_CONVERTER); TypeConverter tc = mock(TypeConverter.class); TypeConverter res = converterHelper.getDeclaredTypeConverter(mapOnly, tc); assertThat(res).isEqualTo(tc); }
@Test public void testGetDeclaredTypeConverter_withTypeConverter() throws Exception { ReflectionHelper reflectionHelper = mock(ReflectionHelper.class); ReflectionTestUtils.setField(converterHelper, "reflectionHelper", reflectionHelper); MapOnly mapOnly = mock(MapOnly.class); when(reflectionHelper.newInstance("org.jtransfo.internal.ConverterHelperTest$DefaultTypeConverter")). thenReturn(new DefaultTypeConverter()); when(mapOnly.typeConverterClass()).thenReturn(DefaultTypeConverter.class); when(mapOnly.typeConverter()).thenReturn(MappedBy.DEFAULT_TYPE_CONVERTER); TypeConverter tc = mock(TypeConverter.class); TypeConverter res = converterHelper.getDeclaredTypeConverter(mapOnly, tc); assertThat(res).isInstanceOf(DefaultTypeConverter.class); }
@Test public void testGetDeclaredTypeConverter_null() throws Exception { TypeConverter tc = mock(TypeConverter.class); TypeConverter res = converterHelper.getDeclaredTypeConverter(null, tc); assertThat(res).isEqualTo(tc); }