### Question:
AuditEntityListener { @PrePersist public void persist(Object entity) { BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager(); Set<Bean<?>> beans = beanManager.getBeans(PrePersistAuditListener.class); for (Bean<?> bean : beans) { PrePersistAuditListener result = (PrePersistAuditListener) beanManager.getReference( bean, PrePersistAuditListener.class, beanManager.createCreationalContext(bean)); result.prePersist(entity); } }  @PrePersist void persist(Object entity); @PreUpdate void update(Object entity);  }### Answer:
@Test public void should_set_creation_date() throws Exception { AuditedEntity entity = new AuditedEntity(); getEntityManager().persist(entity); getEntityManager().flush(); assertNotNull(entity.getCreated()); assertNotNull(entity.getModified()); assertEquals(entity.getCreated().getTime(), entity.getModified()); }
@Test public void should_set_modification_date() throws Exception { AuditedEntity entity = new AuditedEntity(); getEntityManager().persist(entity); getEntityManager().flush(); entity = getEntityManager().find(AuditedEntity.class, entity.getId()); entity.setName("test"); getEntityManager().flush(); assertNotNull(entity.getGregorianModified()); assertNotNull(entity.getTimestamp()); }
@Test public void should_set_changing_principal() { AuditedEntity entity = new AuditedEntity(); getEntityManager().persist(entity); getEntityManager().flush(); entity = getEntityManager().find(AuditedEntity.class, entity.getId()); entity.setName("test"); getEntityManager().flush(); assertNotNull(entity.getChanger()); assertEquals(who, entity.getChanger()); assertNotNull(entity.getPrincipal()); assertEquals(who, entity.getPrincipal().getName()); assertNotNull(entity.getChangerOnly()); assertEquals(who, entity.getChangerOnly()); assertNotNull(entity.getChangerOnlyPrincipal()); assertEquals(who, entity.getChangerOnlyPrincipal().getName()); }
@Test public void should_set_creating_principal() { AuditedEntity entity = new AuditedEntity(); getEntityManager().persist(entity); getEntityManager().flush(); assertNotNull(entity.getCreator()); assertEquals(who, entity.getCreator()); assertNotNull(entity.getCreatorPrincipal()); assertEquals(who, entity.getCreatorPrincipal().getName()); assertNotNull(entity.getChanger()); assertEquals(who, entity.getChanger()); assertNotNull(entity.getPrincipal()); assertEquals(who, entity.getPrincipal().getName()); assertNull(entity.getChangerOnly()); assertNull(entity.getChangerOnlyPrincipal()); }