User user2 = newInstance(UserImpl.class);
user2.setFirstName("first name 2");
user2.setLastName("last name 2");
UserGroup userGroup = newInstance(UserGroupImpl.class);
userGroup.setName("usergroup name");
userGroup.addUser(user1);
userGroup.addUser(user2);
// assert recursion
assertEquals("Two users in usergroup expected.", 2, userGroup.getUsers().size());
Iterator<?> iterator = userGroup.getUsers().iterator();
while (iterator.hasNext()) {
User user = (User) iterator.next();
assertNotNull(user);
assertNotNull(user.getUserGroup());
assertTrue(user.getUserGroup() == userGroup); // same reference
}
// get mapper
List<String> mappingFiles = new ArrayList<String>();
mappingFiles.add("interface-recursion-mappings.xml");
Mapper mapper = new DozerBeanMapper(mappingFiles);
// do mapping
UserGroupPrime userGroupPrime = null;
try {
userGroupPrime = mapper.map(userGroup, UserGroupPrime.class);
} catch (StackOverflowError e) {
fail("Recursive mapping caused a stack overflow.");
}
// check mapped group
assertNotNull(userGroupPrime);
assertEquals(userGroup.getName(), userGroupPrime.getName());
// check mapped users and recursion
assertEquals("Two users in mapped usergroup expected.", 2, userGroupPrime.getUsers().size());
iterator = userGroupPrime.getUsers().iterator();
while (iterator.hasNext()) {