ContextJdbcEventListenerFactory implements JdbcEventListenerFactory { @Override public JdbcEventListener createJdbcEventListener() { return compoundJdbcEventListener; } ContextJdbcEventListenerFactory(JdbcEventListenerFactory delegate, List<JdbcEventListener> listeners); @Override JdbcEventListener createJdbcEventListener();  }
@Test void shouldUseDelegateToCreateListener() { SimpleJdbcEventListener listener1 = new SimpleJdbcEventListener() {}; SimpleJdbcEventListener listener2 = new SimpleJdbcEventListener() {}; Mockito.when(delegate.createJdbcEventListener()).thenReturn(listener1); ContextJdbcEventListenerFactory contextJdbcEventListenerFactory = new ContextJdbcEventListenerFactory(delegate, Collections.singletonList(listener2)); CompoundJdbcEventListener jdbcEventListener = (CompoundJdbcEventListener) contextJdbcEventListenerFactory.createJdbcEventListener(); assertThat(jdbcEventListener.getEventListeners()).hasSize(2); assertThat(jdbcEventListener.getEventListeners()).contains(listener1, listener2); }
@Test void shouldReuseCompoundListenerFromFactory() { SimpleJdbcEventListener listener1 = new SimpleJdbcEventListener() {}; Mockito.when(delegate.createJdbcEventListener()).thenReturn(new CompoundJdbcEventListener()); ContextJdbcEventListenerFactory contextJdbcEventListenerFactory = new ContextJdbcEventListenerFactory(delegate, Collections.singletonList(listener1)); CompoundJdbcEventListener jdbcEventListener = (CompoundJdbcEventListener) contextJdbcEventListenerFactory.createJdbcEventListener(); assertThat(jdbcEventListener.getEventListeners()).hasSize(1); assertThat(jdbcEventListener.getEventListeners()).contains(listener1); }