I think the code below will be self-explaining. I can make it work in two possible ways:
I am not able to change CollectionHolderB and CollectionHolderA classes. I am looking for a way around hinting all available collections within my model. Any suggestions?
public class CollectionHolderA {
private List<String> values = new ArrayList<String>();
public List<String> getValues() {
return values;
}
}
public class CollectionHolderB {
private List<Integer> values = new ArrayList<Integer>();
public List<Integer> getValues() {
return values;
}
}
@Test
public void publicTestCollectionMappingWithoutSetters() {
DozerBeanMapper mapper = new DozerBeanMapper();
// Works only if uncommented
// mapper.addMapping(new BeanMappingBuilder() {
//
// @Override
// protected void configure() {
// mapping(CollectionHolderB.class, CollectionHolderA.class)
// .fields("values", "values",
// FieldsMappingOptions.hintA(Integer.class),
// FieldsMappingOptions.hintB(String.class));
// }
CollectionHolderB objB = new CollectionHolderB();
objB.getValues().add(new Integer(0));
objB.getValues().add(new Integer(1));
CollectionHolderA objA = mapper.map(objB, CollectionHolderA.class);
Assert.assertArrayEquals(new String[] { "0", "1" }, (String[]) objA.getValues().toArray(new String[objA.getValues().size()]));
}