### Question:
GenericHibernateDao { public void saveOrUpdate(E e) { final Integer id = e.getId(); final boolean hasId = id != null; String createOrUpdatePrefix = hasId ? "Updating" : "Creating a new"; String idSuffix = hasId ? " with ID " + id : ""; logger.trace(createOrUpdatePrefix + " instance of " + entityClass.getSimpleName() + idSuffix); e.setModified(DateTime.now()); getSession().saveOrUpdate(e); } @SuppressWarnings("unchecked")  GenericHibernateDao(); protected  GenericHibernateDao(Class<E> clazz); E findById(ID id); @SuppressWarnings("unchecked") List<E> findAllWhereFieldEquals(String fieldName, Object fieldEntity,
                                           Criterion... criterion); @SuppressWarnings("unchecked") List<E> findAllWithCollectionContaining(String fieldName, PersistentObject subElement,
                                                   Criterion... criterion); E loadById(ID id); List<E> findAll(); void saveOrUpdate(E e); void delete(E e); @SuppressWarnings("unchecked") E unproxy(E e); void evict(E e); @SuppressWarnings("unchecked") List<E> findByCriteria(Criterion... criterion); @SuppressWarnings("unchecked") List<E> findByCriteriaRestricted(List<String> restrictFieldNames, Criterion... criterion); @SuppressWarnings("unchecked") E findByUniqueCriteria(Criterion... criterion); @SuppressWarnings("unchecked") PagingResult<E> findByCriteriaWithSortingAndPaging(Integer firstResult,
                                                              Integer maxResults, List<Order> sorters, Criterion... criterion); @SuppressWarnings({"unchecked"}) Map<PersistentObject, PermissionCollection> findAllUserPermissionsOfUser(User user); @SuppressWarnings({"unchecked"}) Map<PersistentObject, PermissionCollection> findAllUserGroupPermissionsOfUserGroup(UserGroup userGroup); Number getTotalCount(Criterion... criterion); Class<E> getEntityClass();  }### Answer:
@Test public void saveOrUpdate_shouldSetModified() { Application app = this.getRandomUnsavedMockApp(); ReadableDateTime before = app.getModified(); try { Thread.sleep(1); appDao.saveOrUpdate(app); ReadableDateTime after = app.getModified(); assertNotEquals("before and after should be different", before, after); assertTrue("after should be greater than before", after.isAfter(before)); } catch (InterruptedException e) { fail("Caught exception while attempting to wait"); return; } }
@Test public void saveOrUpdate_shouldAlwaysSetModified() { Application app = this.getRandomUnsavedMockApp(); try { Thread.sleep(1); DateTime before = DateTime.now(); app.setModified(before); Thread.sleep(1); appDao.saveOrUpdate(app); ReadableDateTime after = app.getModified(); assertNotEquals(before, after); boolean isLater = before.compareTo(after) == -1; assertTrue(isLater); } catch (InterruptedException e) { fail("Caught exception while attempting to wait"); return; } }
@Test public void saveOrUpdate_shouldUpdateModified() { Application app = new Application("Some Name", "Some description"); appDao.saveOrUpdate(app); ReadableDateTime before = app.getModified(); try { Thread.sleep(1); app.setName("Some other name"); app.setDescription("Changed description"); appDao.saveOrUpdate(app); ReadableDateTime after = app.getModified(); assertNotEquals(before, after); assertTrue(after.isAfter(before)); } catch (InterruptedException e) { fail("Caught exception while attempting to wait"); return; } }
@Test public void saveOrUpdate_shouldSaveOrUpdateApplication() { Application app = getRandomUnsavedMockApp(); assertNull(app.getId()); appDao.saveOrUpdate(app); Integer id = app.getId(); assertNotNull(id); assertTrue(id > 0); String changedNameOfApp = "Some other name"; String changedDescOfApp = "Changed description"; app.setName(changedNameOfApp); app.setDescription(changedDescOfApp); appDao.saveOrUpdate(app); assertEquals(id, app.getId()); assertEquals(changedNameOfApp, app.getName()); assertEquals(changedDescOfApp, app.getDescription()); }