@Test public void addStartStopRemove() throws Exception { LifecycleManager underTest = new LifecycleManager(); LifecycleSupport foo = new LifecycleSupport(); LifecycleSupport bar = new LifecycleSupport(); assertState(underTest, State.NEW); assertState(foo, State.NEW); assertState(bar, State.NEW); underTest.add(foo); assertThat(underTest.size(), is(1)); underTest.add(bar); assertThat(underTest.size(), is(2)); underTest.start(); assertState(underTest, State.STARTED); assertState(foo, State.STARTED); assertState(bar, State.STARTED); underTest.stop(); assertState(underTest, State.STOPPED); assertState(foo, State.STOPPED); assertState(bar, State.STOPPED); underTest.remove(foo); assertThat(underTest.size(), is(1)); underTest.remove(bar); assertThat(underTest.size(), is(0)); }
@Test public void stopWithMultipleFailures() throws Exception { LifecycleManager underTest = new LifecycleManager(); LifecycleSupport foo = new LifecycleSupport() { @Override protected void doStop() throws Exception { throw new TestException(); } }; LifecycleSupport bar = new LifecycleSupport() { @Override protected void doStop() throws Exception { throw new TestError(); } }; underTest.add(foo, bar); underTest.start(); try { underTest.stop(); } catch (MultipleFailuresException e) { List<Throwable> failures = e.getFailures(); assertThat(failures.size(), is(2)); assertThat(failures.get(0), instanceOf(TestError.class)); assertThat(failures.get(1), instanceOf(TestException.class)); assertState(foo, State.FAILED); assertState(bar, State.FAILED); } }
@Test public void awareStartStopOrdering() throws Exception { final List<Lifecycle> started = new ArrayList<>(); final List<Lifecycle> stopped = new ArrayList<>(); LifecycleManager underTest = new LifecycleManager(); final Lifecycle foo = new LifecycleSupport() { @Override protected void doStart() throws Exception { started.add(this); } @Override protected void doStop() throws Exception { stopped.add(this); } }; final Lifecycle bar = new LifecycleSupport() { @Override protected void doStart() throws Exception { started.add(this); } @Override protected void doStop() throws Exception { stopped.add(this); } }; underTest.add( new LifecycleAware() { @Nonnull @Override public Lifecycle getLifecycle() { return foo; } }, new LifecycleAware() { @Nonnull @Override public Lifecycle getLifecycle() { return bar; } }); assertThat(started.size(), is(0)); assertThat(stopped.size(), is(0)); underTest.start(); assertThat(started.size(), is(2)); assertThat(started, contains(foo, bar)); underTest.stop(); assertThat(stopped.size(), is(2)); assertThat(stopped, contains(bar, foo)); }
@Test public void startStopOrdering() throws Exception { final List<LifecycleSupport> started = new ArrayList<>(); final List<LifecycleSupport> stopped = new ArrayList<>(); LifecycleManager underTest = new LifecycleManager(); LifecycleSupport foo = new LifecycleSupport() { @Override protected void doStart() throws Exception { started.add(this); } @Override protected void doStop() throws Exception { stopped.add(this); } }; Lifecycle bar = new LifecycleSupport() { @Override protected void doStart() throws Exception { started.add(this); } @Override protected void doStop() throws Exception { stopped.add(this); } }; underTest.add(foo, bar); assertThat(started.size(), is(0)); assertThat(stopped.size(), is(0)); underTest.start(); assertThat(started.size(), is(2)); assertThat(started, contains(foo, bar)); underTest.stop(); assertThat(stopped.size(), is(2)); assertThat(stopped, contains(bar, foo)); }