DomaEventEntityListener extends NullEntityListener<T> implements
		ApplicationEventPublisherAware { @Override public void preInsert(T t, PreInsertContext<T> context) { this.eventPublisher.publishEvent(new PreInsertEvent<>(t, context)); }  @Override void preInsert(T t, PreInsertContext<T> context); @Override void preUpdate(T t, PreUpdateContext<T> context); @Override void preDelete(T t, PreDeleteContext<T> context); @Override void postInsert(T t, PostInsertContext<T> context); @Override void postUpdate(T t, PostUpdateContext<T> context); @Override void postDelete(T t, PostDeleteContext<T> context); @Override void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher);  }
@Test public void handlePreInsert() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(DomaEventEntityListener.class); context.register(DomaEventListenerFactory.class); context.register(PreInsertHandler.class); context.refresh(); DomaEventEntityListener<Entity> entityListener = context .getBean(DomaEventEntityListener.class); PreInsertContext ctx = mock(PreInsertContext.class); Entity entity = new Entity(); entityListener.preInsert(entity, ctx); PreInsertHandler handler = context.getBean(PreInsertHandler.class); assertThat(handler.entity).isSameAs(entity); }
@Test public void handlePreInsertWithContext() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(DomaEventEntityListener.class); context.register(DomaEventListenerFactory.class); context.register(PreInsertHandlerWithContext.class); context.refresh(); DomaEventEntityListener<Entity> entityListener = context .getBean(DomaEventEntityListener.class); PreInsertContext ctx = mock(PreInsertContext.class); Entity entity = new Entity(); entityListener.preInsert(entity, ctx); PreInsertHandlerWithContext handler = context .getBean(PreInsertHandlerWithContext.class); assertThat(handler.entity).isSameAs(entity); assertThat(handler.ctx).isSameAs(ctx); }
@Test public void anotherEntity() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(DomaEventEntityListener.class); context.register(DomaEventListenerFactory.class); context.register(PreInsertHandler.class); context.register(PreInsertHandler2.class); context.refresh(); DomaEventEntityListener<Entity2> entityListener = context .getBean(DomaEventEntityListener.class); PreInsertContext ctx = mock(PreInsertContext.class); Entity2 entity = new Entity2(); entityListener.preInsert(entity, ctx); PreInsertHandler handler = context.getBean(PreInsertHandler.class); assertThat(handler.entity).isNull(); PreInsertHandler2 handler2 = context.getBean(PreInsertHandler2.class); assertThat(handler2.entity).isSameAs(entity); }
@Test public void springEventListener() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(DomaEventEntityListener.class); context.register(DomaEventListenerFactory.class); context.register(SpringListener.class); context.register(PreInsertHandler.class); context.refresh(); DomaEventEntityListener<Entity> entityListener = context .getBean(DomaEventEntityListener.class); SpringListener springListener = context.getBean(SpringListener.class); PreInsertContext ctx = mock(PreInsertContext.class); Entity entity = new Entity(); entityListener.preInsert(entity, ctx); DomaEvent event = springListener.event; assertThat(event).isNotNull(); assertThat(event).isInstanceOf(PreInsertEvent.class); assertThat(event.getSource()).isSameAs(entity); assertThat(event.getContext()).isSameAs(ctx); }
@Test public void springConditionalEventListener() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(DomaEventEntityListener.class); context.register(DomaEventListenerFactory.class); context.register(TodoListener.class); context.register(PreInsertHandler.class); context.refresh(); DomaEventEntityListener<Todo> entityListener = context .getBean(DomaEventEntityListener.class); TodoListener todoListener = context.getBean(TodoListener.class); PreInsertContext ctx = mock(PreInsertContext.class); Todo entity = new Todo(); entity.createdBy = "someone"; entityListener.preInsert(entity, ctx); Todo todo = todoListener.todo; assertThat(todo).isNull(); entity.createdBy = "making"; entityListener.preInsert(entity, ctx); todo = todoListener.todo; assertThat(todo).isNotNull(); assertThat(todo).isSameAs(entity); }