private void assertEventCount(BreakableStateChangeListener listener, int expected) { assertEquals(listener.toString(), expected, listener.getEventCount()); }
/** * Assert that the last state of the listener is that the test expected. * @param breakable a breakable listener * @param state the expected state */ public void assertListenerState(BreakableStateChangeListener breakable, Service.STATE state) { assertEquals("Wrong state in " + breakable, state, breakable.getLastState()); }
@Override public synchronized void stateChanged(Service service) { super.stateChanged(service); service.unregisterServiceListener(this); } }
@Test public void testServiceFailingNotifications() throws Throwable { BreakableService svc = new BreakableService(false, false, false); BreakableStateChangeListener listener = new BreakableStateChangeListener(); listener.setFailingState(Service.STATE.STARTED); svc.registerServiceListener(listener); svc.init(new Configuration()); assertEventCount(listener, 1); //start this; the listener failed but this won't show svc.start(); //counter went up assertEventCount(listener, 2); assertEquals(1, listener.getFailureCount()); //stop the service -this doesn't fail svc.stop(); assertEventCount(listener, 3); assertEquals(1, listener.getFailureCount()); svc.stop(); }
/** * This test triggers a failure in the listener - the expectation is that the * service has already reached it's desired state, purely because the * notifications take place afterwards. * */ @Test public void testListenerFailure() { listener.setFailingState(Service.STATE.INITED); register(); BreakableStateChangeListener l2 = new BreakableStateChangeListener(); register(l2); BreakableService service = new BreakableService(); service.init(new Configuration()); //expected notifications to fail //still should record its invocation assertListenerState(listener, Service.STATE.INITED); assertListenerEventCount(listener, 1); //and second listener didn't get notified of anything assertListenerEventCount(l2, 0); //service should still consider itself started assertServiceStateInited(service); service.start(); service.stop(); }
@Test public void testStartInInitService() throws Throwable { Service service = new StartInInitService(); BreakableStateChangeListener listener = new BreakableStateChangeListener(); service.registerServiceListener(listener); service.init(new Configuration()); assertServiceInState(service, Service.STATE.STARTED); assertEventCount(listener, 1); }
/** * Test that the {@link BreakableStateChangeListener} is picking up * the state changes and that its last event field is as expected. */ @Test public void testEventHistory() { register(); BreakableService service = new BreakableService(); assertListenerState(listener, Service.STATE.NOTINITED); assertEquals(0, listener.getEventCount()); service.init(new Configuration()); assertListenerState(listener, Service.STATE.INITED); assertSame(service, listener.getLastService()); assertListenerEventCount(listener, 1); service.start(); assertListenerState(listener, Service.STATE.STARTED); assertListenerEventCount(listener, 2); service.stop(); assertListenerState(listener, Service.STATE.STOPPED); assertListenerEventCount(listener, 3); }
/** * Assert that the number of state change notifications matches expectations. * @param breakable the listener * @param count the expected count. */ public void assertListenerEventCount(BreakableStateChangeListener breakable, int count) { assertEquals("Wrong event count in " + breakable, count, breakable.getEventCount()); }
@Test public void testServiceFailingNotifications() throws Throwable { BreakableService svc = new BreakableService(false, false, false); BreakableStateChangeListener listener = new BreakableStateChangeListener(); listener.setFailingState(Service.STATE.STARTED); svc.registerServiceListener(listener); svc.init(new Configuration()); assertEventCount(listener, 1); //start this; the listener failed but this won't show svc.start(); //counter went up assertEventCount(listener, 2); assertEquals(1, listener.getFailureCount()); //stop the service -this doesn't fail svc.stop(); assertEventCount(listener, 3); assertEquals(1, listener.getFailureCount()); svc.stop(); }
/** * This test triggers a failure in the listener - the expectation is that the * service has already reached it's desired state, purely because the * notifications take place afterwards. * */ @Test public void testListenerFailure() { listener.setFailingState(Service.STATE.INITED); register(); BreakableStateChangeListener l2 = new BreakableStateChangeListener(); register(l2); BreakableService service = new BreakableService(); service.init(new Configuration()); //expected notifications to fail //still should record its invocation assertListenerState(listener, Service.STATE.INITED); assertListenerEventCount(listener, 1); //and second listener didn't get notified of anything assertListenerEventCount(l2, 0); //service should still consider itself started assertServiceStateInited(service); service.start(); service.stop(); }
@Test public void testStopInInitService() throws Throwable { Service service = new StopInInitService(); BreakableStateChangeListener listener = new BreakableStateChangeListener(); service.registerServiceListener(listener); service.init(new Configuration()); assertServiceInState(service, Service.STATE.STOPPED); assertEventCount(listener, 1); }
/** * Test that the {@link BreakableStateChangeListener} is picking up * the state changes and that its last event field is as expected. */ @Test public void testEventHistory() { register(); BreakableService service = new BreakableService(); assertListenerState(listener, Service.STATE.NOTINITED); assertEquals(0, listener.getEventCount()); service.init(new Configuration()); assertListenerState(listener, Service.STATE.INITED); assertSame(service, listener.getLastService()); assertListenerEventCount(listener, 1); service.start(); assertListenerState(listener, Service.STATE.STARTED); assertListenerEventCount(listener, 2); service.stop(); assertListenerState(listener, Service.STATE.STOPPED); assertListenerEventCount(listener, 3); }
/** * Assert that the number of state change notifications matches expectations. * @param breakable the listener * @param count the expected count. */ public void assertListenerEventCount(BreakableStateChangeListener breakable, int count) { assertEquals("Wrong event count in " + breakable, count, breakable.getEventCount()); }
BreakableStateChangeListener l0 = new BreakableStateChangeListener("l0"); register(l0); listener.setFailingState(Service.STATE.STARTED); register(); BreakableStateChangeListener l3 = new BreakableStateChangeListener("l3"); register(l3);
@Test public void testStartInInitService() throws Throwable { Service service = new StartInInitService(); BreakableStateChangeListener listener = new BreakableStateChangeListener(); service.registerServiceListener(listener); service.init(new Configuration()); assertServiceInState(service, Service.STATE.STARTED); assertEventCount(listener, 1); }
private void assertEventCount(BreakableStateChangeListener listener, int expected) { assertEquals(listener.toString(), expected, listener.getEventCount()); }
@Override public synchronized void stateChanged(Service service) { super.stateChanged(service); service.unregisterServiceListener(this); } }
/** * Assert that the last state of the listener is that the test expected. * @param breakable a breakable listener * @param state the expected state */ public void assertListenerState(BreakableStateChangeListener breakable, Service.STATE state) { assertEquals("Wrong state in " + breakable, state, breakable.getLastState()); }
BreakableStateChangeListener l0 = new BreakableStateChangeListener("l0"); register(l0); listener.setFailingState(Service.STATE.STARTED); register(); BreakableStateChangeListener l3 = new BreakableStateChangeListener("l3"); register(l3);
@Test public void testStopInInitService() throws Throwable { Service service = new StopInInitService(); BreakableStateChangeListener listener = new BreakableStateChangeListener(); service.registerServiceListener(listener); service.init(new Configuration()); assertServiceInState(service, Service.STATE.STOPPED); assertEventCount(listener, 1); }