QueuingConnection implements Connection<I> { synchronized void setDelegate(Connection<I> delegate) { if (this.delegate != null) { throw new IllegalStateException("Attempt at setting delegate twice"); } this.delegate = checkNotNull(delegate); if (disposed) { return; } for (I item : queue) { delegate.accept(item); } queue.clear(); }  @Override synchronized void accept(I value); @Override synchronized void dispose();  }
@Test public void shouldNotAllowDuplicateDelegates() throws Exception { connection.setDelegate(delegate); assertThatThrownBy(() -> connection.setDelegate(new RecordingConnection<>())) .isInstanceOf(IllegalStateException.class); }