DozerConverter implements ConfigurableCustomConverter { public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) { Class<?> wrappedDestinationClass = ClassUtils.primitiveToWrapper(destinationClass); Class<?> wrappedSourceClass = ClassUtils.primitiveToWrapper(sourceClass); if (prototypeA.equals(wrappedDestinationClass)) { return convertFrom((B)sourceFieldValue, (A)existingDestinationFieldValue); } else if (prototypeB.equals(wrappedDestinationClass)) { return convertTo((A)sourceFieldValue, (B)existingDestinationFieldValue); } else if (prototypeA.equals(wrappedSourceClass)) { return convertTo((A)sourceFieldValue, (B)existingDestinationFieldValue); } else if (prototypeB.equals(wrappedSourceClass)) { return convertFrom((B)sourceFieldValue, (A)existingDestinationFieldValue); } else if (prototypeA.isAssignableFrom(wrappedDestinationClass)) { return convertFrom((B)sourceFieldValue, (A)existingDestinationFieldValue); } else if (prototypeB.isAssignableFrom(wrappedDestinationClass)) { return convertTo((A)sourceFieldValue, (B)existingDestinationFieldValue); } else if (prototypeA.isAssignableFrom(wrappedSourceClass)) { return convertTo((A)sourceFieldValue, (B)existingDestinationFieldValue); } else if (prototypeB.isAssignableFrom(wrappedSourceClass)) { return convertFrom((B)sourceFieldValue, (A)existingDestinationFieldValue); } else { throw new MappingException("Destination Type (" + wrappedDestinationClass.getName() + ") is not accepted by this Custom Converter (" + this.getClass().getName() + ")!"); } } DozerConverter(Class<A> prototypeA, Class<B> prototypeB); Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass); abstract B convertTo(A source, B destination); B convertTo(A source); abstract A convertFrom(B source, A destination); A convertFrom(B source); void setParameter(String parameter); String getParameter();  }
@Test(expected = MappingException.class) public void test_convert_exception() { converter.convert(Boolean.TRUE, new BigDecimal(1), Boolean.class, BigDecimal.class); fail(); }
@Test public void test_FullCycle() { assertEquals(1, converter.convert(null, "1", Integer.class, String.class)); assertEquals("1", converter.convert(null, new Integer(1), String.class, Integer.class)); }
@Test public void testObjectType() { assertEquals(1, converter .convert(null, "1", Object.class, String.class)); assertEquals("1", converter.convert(null, new Integer(1), Object.class, Integer.class)); }
@Test public void testAutoboxing() { assertEquals(1, converter.convert(null, "1", int.class, String.class)); }
@Test public void testPrimitiveToPrimitive() { DozerConverter<Integer, Double> converter = new DozerConverter<Integer, Double>( Integer.class, Double.class) { @Override public Double convertTo(Integer source, Double destination) { return new Double(Integer.toString(source)); } @Override public Integer convertFrom(Double source, Integer destination) { return new Integer(Double.toString(source)); } }; converter.convert(1d, 2, double.class, int.class); }
@Test public void test_hierarchy() { DozerConverter<Number, Integer> converter = new DozerConverter<Number, Integer>( Number.class, Integer.class) { public Integer convertTo(Number source, Integer destination) { return source.intValue(); } public Number convertFrom(Integer source, Number destination) { return source; } }; assertEquals(new Integer(1), converter.convert(null, new Integer(1), Number.class, Integer.class)); assertEquals(new Integer(1), converter.convert(null, new Double(1), Integer.class, Number.class)); }
@Test public void testAssignments() { DozerConverter<Number, Number> converter = new DozerConverter<Number, Number>( Number.class, Number.class) { @Override public Number convertFrom(Number source, Number destination) { return source; } @Override public Number convertTo(Number source, Number destination) { return source; } }; assertEquals(new Integer(1), converter.convert(null, new Integer(1), Long.class, Integer.class)); assertEquals(new Integer(11), converter.convert(null, new Integer(11), Object.class, Integer.class)); }
@Test public void testAssignments2() { DozerConverter<String, Number> converter = new DozerConverter<String, Number>( String.class, Number.class) { @Override public String convertFrom(Number source, String destination) { return source.toString(); } @Override public Number convertTo(String source, Number destination) { return Long.parseLong(source); } }; assertEquals(new Long(1L), converter.convert(null, new String("1"), Long.class, Object.class)); assertEquals(new String("1"), converter.convert(null, new Integer(1), Object.class, Integer.class)); }