AbstractSignalNotificationHandler { public boolean registerListener(final SignalListener listener) { assertNotNull(listener, "The SignalListener to register, listening for all signals cannot be null!"); boolean registered = false; for (Signal signal : Signal.values()) { registered |= registerListener(listener, signal); } return registered; } protected  AbstractSignalNotificationHandler(); boolean registerListener(final SignalListener listener); boolean unregisterListener(final SignalListener listener);  }
@Test public void registerListener() { AbstractSignalNotificationHandler signalHandler = createSignalNotificationHandler(); SignalListener mockListenerOne = mock(SignalListener.class, "SIGALL1"); SignalListener mockListenerTwo = mock(SignalListener.class, "SIGALL2"); assertThat(signalHandler.isListening(mockListenerOne)).isFalse(); assertThat(signalHandler.isListening(mockListenerTwo)).isFalse(); for (Signal signal : Signal.values()) { assertThat(signalHandler.hasListeners(signal)).isFalse(); assertThat(signalHandler.isListening(mockListenerOne, signal)).isFalse(); assertThat(signalHandler.isListening(mockListenerTwo, signal)).isFalse(); } assertThat(signalHandler.registerListener(mockListenerOne)).isTrue(); assertThat(signalHandler.registerListener(mockListenerTwo)).isTrue(); assertThat(signalHandler.registerListener(mockListenerTwo)).isFalse(); assertThat(signalHandler.isListening(mockListenerOne)).isTrue(); assertThat(signalHandler.isListening(mockListenerTwo)).isTrue(); for (Signal signal : Signal.values()) { assertThat(signalHandler.hasListeners(signal)).isTrue(); assertThat(signalHandler.isListening(mockListenerOne, signal)).isTrue(); assertThat(signalHandler.isListening(mockListenerTwo, signal)).isTrue(); } }
@Test public void registerListenerWithNullSignalListenerThrowsNullPointerException() { assertThatThrownBy(() -> createSignalNotificationHandler().registerListener(null)) .isInstanceOf(NullPointerException.class) .hasMessage("The SignalListener to register, listening for all signals cannot be null!"); }
@Test public void registerListenerWithNullSignalThrowsNullPointerException() { assertThatThrownBy(() -> createSignalNotificationHandler() .registerListener(mock(SignalListener.class, "SIGALL"), null)) .isInstanceOf(NullPointerException.class) .hasMessage("The signal to register the listener for cannot be null!"); }
@Test public void registerListenerWithSignalAndNullSignalListenerThrowsNullPointerException() { assertThatThrownBy( () -> createSignalNotificationHandler().registerListener(null, Signal.SIGQUIT)) .isInstanceOf(NullPointerException.class) .hasMessage(signalListenerForSignalCannotBeNullErrorMessage(Signal.SIGQUIT)); }