@Test
@DatabaseSetup(value = CREATE_DATASET)
@DatabaseTearDown(value = UPDATE_DATASET, type = DatabaseOperation.DELETE_ALL)
public void testSave2() throws Exception {
try {
DatabaseConnection connection = new DatabaseConnection(dataSource.getConnection());
IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(this.getClass().getResource("/dbunit/dao/admin/uzer-update_aftersave.xml")
.openStream());
Uzer uzer = new Uzer();
Uzer nuzer;
Program program = new Program();
Profile profile = new Profile();
Set<Program> programsWithConsultationRights = new HashSet<Program>();
Set<Program> programsWithEditionRights = new HashSet<Program>();
program.setId(1L);
program.setName(PROGRAM_NAME);
program.setVersion(1);
profile.setId(1L);
profile.setAdministrator(false);
profile.setLicensesConsultation(false);
profile.setLicensesEdition(false);
profile.setProgramsConsultation(false);
profile.setProgramsEdition(false);
profile.setName(PROFILE_NAME);
profile.setVersion(1);
uzer.setId(1L);
uzer.setEmail(UPDATED_USER_EMAIL);
uzer.setName(USER_NAME);
uzer.setFirstname(USER_FIRSTNAME);
uzer.setProfile(profile);
programsWithConsultationRights.add(program);
uzer.getProgramsWithConsultationRights().addAll(programsWithConsultationRights);
programsWithEditionRights.add(program);
uzer.getProgramsWithEditionRights().addAll(programsWithEditionRights);
uzer.setVersion(1);
nuzer = uzerDAO.save(uzer);
assertNotNull(nuzer);
assertEquals(nuzer.getEmail(), uzer.getEmail());
entityManager.getTransaction().commit();
// programsWithConsultationRights and programsWithEditionRights are many-to-many relationships
// whenever they are set, both users and programs are updated
program.setVersion(2);
DatabaseAssertionMode.NON_STRICT.getDatabaseAssertion().assertEquals(expectedDataSet,
connection.createDataSet(expectedDataSet.getTableNames()));
connection.close();
} catch (DatabaseUnitException e) {
e.printStackTrace();
fail(e.getLocalizedMessage());
} catch (SQLException e) {
e.printStackTrace();