entityCodeAdditional.setCode("CODE-DT1");
entityCodeAdditional.setId("ID-235");
entity.setType(entityCodePrime);
entity.setCodes(new ArrayList<EntityCatalogCode>(Arrays.asList(entityCodePrime, entityCodeAdditional)));
final Assembler assembler = DTOAssembler.newAssembler(dto.getClass(), entity.getClass());
final Map<String, Object> adapters = new HashMap<String, Object>();
adapters.put("CatalogCodeMatcher", new CatalogCodeMatcher());
assembler.assembleDto(dto, entity, adapters, new BeanFactory() {
public Class getClazz(final String entityBeanKey) {
if ("DtoCatalogCode".equals(entityBeanKey)) {
return DtoCatalogCodeClass.class;
} else if ("DtoCode".equals(entityBeanKey)) {
return DtoCodeClass.class;
} else if ("DtoCatalog".equals(entityBeanKey)) {
return DtoCatalogClass.class;
}
fail("Unknown DTO key: " + entityBeanKey);
return null;
}
public Object get(final String entityBeanKey) {
if ("DtoCatalogCode".equals(entityBeanKey)) {
return new DtoCatalogCodeClass();
} else if ("DtoCode".equals(entityBeanKey)) {
return new DtoCodeClass();
} else if ("DtoCatalog".equals(entityBeanKey)) {
return new DtoCatalogClass();
}
fail("Unknown DTO key: " + entityBeanKey);
return null;
}
});
assertEquals("ID-ABC", dto.getId());
final DtoCodeClass dtoType = dto.getType();
assertNotNull(dtoType);
assertEquals("ID-123", dtoType.getId());
assertEquals("CODE-AG1", dtoType.getCode());
final Collection<DtoCodeClass> dtoCodes = dto.getCodes();
assertNotNull(dtoCodes);
assertFalse(dtoCodes.isEmpty());
// The collection is actually an ArrayList, so we will cheat a little
final List<DtoCodeClass> dtoCodesAsList = (List) dtoCodes;
assertEquals(2, dtoCodesAsList.size());
final DtoCodeClass dtoCode1 = dtoCodesAsList.get(0);
assertNotNull(dtoCode1);
assertEquals("ID-123", dtoCode1.getId());
assertEquals("CODE-AG1", dtoCode1.getCode());
final DtoCodeClass dtoCode2 = dtoCodesAsList.get(1);
assertNotNull(dtoCode2);
assertEquals("ID-235", dtoCode2.getId());
assertEquals("CODE-DT1", dtoCode2.getCode());
final EntityCatalog<EntityCatalogCode> entityCopy = new EntityCatalogClass<EntityCatalogCode>();
assembler.assembleEntity(dto, entityCopy, adapters, new BeanFactory() {
public Class getClazz(final String entityBeanKey) {
if ("CatalogCode".equals(entityBeanKey)) {
return EntityCatalogCodeClass.class;
} else if ("Catalog".equals(entityBeanKey)) {
return EntityCatalogClass.class;