@Test public void testRemoveLifecycleListener() throws Exception { TestLifeCycle lifecycle = new TestLifeCycle(); TestListener listener = new TestListener(); lifecycle.addLifeCycleListener(listener); lifecycle.start(); assertTrue("The starting event didn't occur", listener.starting); lifecycle.removeLifeCycleListener(listener); lifecycle.stop(); assertFalse("The stopping event occurred", listener.stopping); }
protected void setUp() { if (_subject instanceof TestLifeCycle) { try { ((TestLifeCycle) _subject).setUp(); } catch (Exception e) { throw new SetupFailureException(e); } } }
protected void tearDown() { if (_subject instanceof TestLifeCycle) { try { ((TestLifeCycle) _subject).tearDown(); } catch (Exception e) { throw new TearDownFailureException(e); } } }
@Test public void testStop() throws Exception { TestLifeCycle lifecycle = new TestLifeCycle(); TestListener listener = new TestListener(); lifecycle.addLifeCycleListener(listener); // need to set the state to something other than stopped or stopping or // else // stop() will return without doing anything lifecycle.start(); lifecycle.setCause(cause); try { ((StdErrLog) Log.getLogger(AbstractLifeCycle.class)).setHideStacks(true); lifecycle.stop(); assertTrue(false); } catch (Exception e) { assertEquals(cause, e); assertEquals(cause, listener.getCause()); } finally { ((StdErrLog) Log.getLogger(AbstractLifeCycle.class)).setHideStacks(false); } lifecycle.setCause(null); lifecycle.stop(); // check that the stopping event has been thrown assertTrue("The stopping event didn't occur", listener.stopping); // check that the stopped event has been thrown assertTrue("The stopped event didn't occur", listener.stopped); // check that the stopping event occurs before the stopped event assertTrue( "The stopping event must occur before the stopped event", listener.stoppingTime <= listener.stoppedTime); // System.out.println("STOPING TIME : " + listener.stoppingTime + " : " + listener.stoppedTime); // check that the lifecycle's state is stopped assertTrue("The lifecycle state is not stooped", lifecycle.isStopped()); }
@Test public void testStart() throws Exception { TestLifeCycle lifecycle = new TestLifeCycle(); TestListener listener = new TestListener(); lifecycle.addLifeCycleListener(listener); lifecycle.setCause(cause); try { StdErrLog.getLogger(AbstractLifeCycle.class).setHideStacks(true); lifecycle.start(); assertTrue(false); } catch (Exception e) { assertEquals(cause, e); assertEquals(cause, listener.getCause()); } finally { StdErrLog.getLogger(AbstractLifeCycle.class).setHideStacks(false); } lifecycle.setCause(null); lifecycle.start(); // check that the starting event has been thrown assertTrue("The staring event didn't occur", listener.starting); // check that the started event has been thrown assertTrue("The started event didn't occur", listener.started); // check that the starting event occurs before the started event assertTrue( "The starting event must occur before the started event", listener.startingTime <= listener.startedTime); // check that the lifecycle's state is started assertTrue("The lifecycle state is not started", lifecycle.isStarted()); }