@Test(expected = EntityNotFoundException.class) public void insertRemovCycle() throws Exception { Person p = new Person("etester", "Tester", "Ehrenfried", "12345678"); try (DbContext dbContext = contextProvider.getDbContext()) { PersonDAO personDAO = dbContext.getPersonDAO(); personDAO.insert(p); dbContext.commit(); assertNotEquals(0, p.getPersonId()); } try (DbContext dbContext = contextProvider.getDbContext()) { PersonDAO personDAO = dbContext.getPersonDAO(); Person x = personDAO.getById(p.getPersonId()); personDAO.remove(x); dbContext.commit(); } try (DbContext dbContext = contextProvider.getDbContext()) { PersonDAO personDAO = dbContext.getPersonDAO(); Person x = personDAO.getById(p.getPersonId()); assertEquals(x, new Person()); } }
@Test public void insert() throws Exception, ConstraintViolationException { Person p = new Person("etester", "Tester", "Ehrenfried", "1234567"); try (DbContext dbContext = contextProvider.getDbContext()) { PersonDAO personDAO = dbContext.getPersonDAO(); personDAO.insert(p); dbContext.commit(); assertNotEquals(0, p.getPersonId()); } catch (Exception e) { e.printStackTrace(); fail("Exception " + e.getMessage()); } finally { if (p.getPersonId() != 0) { try (DbContext dbContext = contextProvider.getDbContext()) { PersonDAO personDAO = dbContext.getPersonDAO(); personDAO.remove(p.getPersonId()); dbContext.commit(); } } } }
@Test(expected = ConstraintViolationException.class) public void insertDuplicate() throws Exception { try (DbContext dbContext = contextProvider.getDbContext()) { PersonDAO personDAO = dbContext.getPersonDAO(); Person p1 = new Person("etester", "Tester", "Ehrenfried", "12345678"); Person p2 = new Person("etester", "Tester", "Ehrenfried", "12345678"); personDAO.insert(p1); personDAO.insert(p2); dbContext.commit(); } catch (Exception e) { throw e; } }