AbstractSignalNotificationHandler { boolean unregisterListeners(final Signal signal) { assertNotNull(signal, "The signal from which to unregister all listeners cannot be null!"); Set<SignalListener> listeners = signalListeners.get(signal); synchronized (listeners) { listeners.clear(); return listeners.isEmpty(); } } protected  AbstractSignalNotificationHandler(); boolean registerListener(final SignalListener listener); boolean unregisterListener(final SignalListener listener);  }
@Test public void unregisterListeners() { AbstractSignalNotificationHandler signalHandler = createSignalNotificationHandler(); SignalListener mockSigQuitListener = mock(SignalListener.class, "SIGQUIT"); SignalListener mockSigTermListener = mock(SignalListener.class, "SIGTERM"); SignalListener mockSigTermQuitListener = mock(SignalListener.class, "SIGTERM + SIGQUIT"); assertThat(signalHandler.isListening(mockSigQuitListener)).isFalse(); assertThat(signalHandler.isListening(mockSigTermListener)).isFalse(); assertThat(signalHandler.isListening(mockSigTermQuitListener)).isFalse(); for (Signal signal : Signal.values()) { assertThat(signalHandler.hasListeners(signal)).isFalse(); } assertThat(signalHandler.registerListener(mockSigQuitListener, Signal.SIGQUIT)).isTrue(); assertThat(signalHandler.registerListener(mockSigTermListener, Signal.SIGTERM)).isTrue(); assertThat(signalHandler.registerListener(mockSigTermQuitListener, Signal.SIGQUIT)).isTrue(); assertThat(signalHandler.registerListener(mockSigTermQuitListener, Signal.SIGTERM)).isTrue(); assertThat(signalHandler.isListening(mockSigQuitListener)).isTrue(); assertThat(signalHandler.isListening(mockSigQuitListener, Signal.SIGINT)).isFalse(); assertThat(signalHandler.isListening(mockSigQuitListener, Signal.SIGQUIT)).isTrue(); assertThat(signalHandler.isListening(mockSigQuitListener, Signal.SIGTERM)).isFalse(); assertThat(signalHandler.isListening(mockSigTermListener)).isTrue(); assertThat(signalHandler.isListening(mockSigTermListener, Signal.SIGINT)).isFalse(); assertThat(signalHandler.isListening(mockSigTermListener, Signal.SIGQUIT)).isFalse(); assertThat(signalHandler.isListening(mockSigTermListener, Signal.SIGTERM)).isTrue(); assertThat(signalHandler.isListening(mockSigTermQuitListener)).isTrue(); assertThat(signalHandler.isListening(mockSigTermQuitListener, Signal.SIGINT)).isFalse(); assertThat(signalHandler.isListening(mockSigTermQuitListener, Signal.SIGQUIT)).isTrue(); assertThat(signalHandler.isListening(mockSigTermQuitListener, Signal.SIGTERM)).isTrue(); assertThat(signalHandler.hasListeners(Signal.SIGINT)).isFalse(); assertThat(signalHandler.hasListeners(Signal.SIGQUIT)).isTrue(); assertThat(signalHandler.hasListeners(Signal.SIGTERM)).isTrue(); assertThat(signalHandler.unregisterListeners(Signal.SIGTERM)).isTrue(); assertThat(signalHandler.isListening(mockSigQuitListener)).isTrue(); assertThat(signalHandler.isListening(mockSigQuitListener, Signal.SIGINT)).isFalse(); assertThat(signalHandler.isListening(mockSigQuitListener, Signal.SIGQUIT)).isTrue(); assertThat(signalHandler.isListening(mockSigQuitListener, Signal.SIGTERM)).isFalse(); assertThat(signalHandler.isListening(mockSigTermListener)).isFalse(); assertThat(signalHandler.isListening(mockSigTermListener, Signal.SIGINT)).isFalse(); assertThat(signalHandler.isListening(mockSigTermListener, Signal.SIGQUIT)).isFalse(); assertThat(signalHandler.isListening(mockSigTermListener, Signal.SIGTERM)).isFalse(); assertThat(signalHandler.isListening(mockSigTermQuitListener)).isTrue(); assertThat(signalHandler.isListening(mockSigTermQuitListener, Signal.SIGINT)).isFalse(); assertThat(signalHandler.isListening(mockSigTermQuitListener, Signal.SIGQUIT)).isTrue(); assertThat(signalHandler.isListening(mockSigTermQuitListener, Signal.SIGTERM)).isFalse(); assertThat(signalHandler.hasListeners(Signal.SIGINT)).isFalse(); assertThat(signalHandler.hasListeners(Signal.SIGQUIT)).isTrue(); assertThat(signalHandler.hasListeners(Signal.SIGTERM)).isFalse(); }
@Test public void unregisterListenersWithNullSignalThrowsNullPointerException() { assertThatThrownBy(() -> createSignalNotificationHandler().unregisterListeners(null)) .isInstanceOf(NullPointerException.class) .hasMessage("The signal from which to unregister all listeners cannot be null!"); }