AbstractSignalNotificationHandler { protected void notifyListeners(final SignalEvent event) { Set<SignalListener> listeners = signalListeners.get(event.getSignal()); Set<SignalListener> localListeners = Collections.emptySet(); if (listeners != null) { synchronized (listeners) { localListeners = new HashSet<>(listeners); } } for (SignalListener listener : localListeners) { listener.handle(event); } } protected  AbstractSignalNotificationHandler(); boolean registerListener(final SignalListener listener); boolean unregisterListener(final SignalListener listener);  }
@Test public void notifyListeners() { AbstractSignalNotificationHandler signalHandler = createSignalNotificationHandler(); SignalListener mockSigAllListener = mock(SignalListener.class, "SIGALL"); SignalListener mockSigIntListener = mock(SignalListener.class, "SIGINT"); SignalListener mockSigQuitListener = mock(SignalListener.class, "SIGQUIT"); SignalListener mockSigQuitTermListener = mock(SignalListener.class, "SIGQUIT + SIGTERM"); SignalEvent sigintEvent = new SignalEvent(this, Signal.SIGINT); SignalEvent sigioEvent = new SignalEvent(this, Signal.SIGIO); SignalEvent sigquitEvent = new SignalEvent(this, Signal.SIGQUIT); SignalEvent sigtermEvent = new SignalEvent(this, Signal.SIGTERM); assertThat(signalHandler.isListening(mockSigAllListener)).isFalse(); assertThat(signalHandler.isListening(mockSigIntListener)).isFalse(); assertThat(signalHandler.isListening(mockSigQuitListener)).isFalse(); assertThat(signalHandler.isListening(mockSigQuitTermListener)).isFalse(); for (Signal signal : Signal.values()) { assertThat(signalHandler.hasListeners(signal)).isFalse(); } assertThat(signalHandler.registerListener(mockSigAllListener)).isTrue(); assertThat(signalHandler.registerListener(mockSigIntListener, Signal.SIGINT)).isTrue(); assertThat(signalHandler.registerListener(mockSigQuitListener, Signal.SIGQUIT)).isTrue(); assertThat(signalHandler.registerListener(mockSigQuitTermListener, Signal.SIGQUIT)).isTrue(); assertThat(signalHandler.registerListener(mockSigQuitTermListener, Signal.SIGTERM)).isTrue(); assertThat(signalHandler.isListening(mockSigAllListener)).isTrue(); assertThat(signalHandler.isListening(mockSigIntListener)).isTrue(); assertThat(signalHandler.isListening(mockSigQuitListener)).isTrue(); assertThat(signalHandler.isListening(mockSigQuitTermListener)).isTrue(); for (Signal signal : Signal.values()) { assertThat(signalHandler.hasListeners(signal)).isTrue(); assertThat(signalHandler.isListening(mockSigAllListener, signal)).isTrue(); switch (signal) { case SIGINT: assertThat(signalHandler.isListening(mockSigIntListener, signal)).isTrue(); assertThat(signalHandler.isListening(mockSigQuitListener, signal)).isFalse(); assertThat(signalHandler.isListening(mockSigQuitTermListener, signal)).isFalse(); break; case SIGQUIT: assertThat(signalHandler.isListening(mockSigIntListener, signal)).isFalse(); assertThat(signalHandler.isListening(mockSigQuitListener, signal)).isTrue(); assertThat(signalHandler.isListening(mockSigQuitTermListener, signal)).isTrue(); break; case SIGTERM: assertThat(signalHandler.isListening(mockSigIntListener, signal)).isFalse(); assertThat(signalHandler.isListening(mockSigQuitListener, signal)).isFalse(); assertThat(signalHandler.isListening(mockSigQuitTermListener, signal)).isTrue(); break; default: assertThat(signalHandler.isListening(mockSigIntListener, signal)).isFalse(); assertThat(signalHandler.isListening(mockSigQuitListener, signal)).isFalse(); assertThat(signalHandler.isListening(mockSigQuitTermListener, signal)).isFalse(); } } signalHandler.notifyListeners(sigintEvent); signalHandler.notifyListeners(sigioEvent); signalHandler.notifyListeners(sigquitEvent); signalHandler.notifyListeners(sigtermEvent); verify(mockSigAllListener, times(1)).handle(eq(sigintEvent)); verify(mockSigAllListener, times(1)).handle(eq(sigioEvent)); verify(mockSigAllListener, times(1)).handle(eq(sigquitEvent)); verify(mockSigAllListener, times(1)).handle(eq(sigtermEvent)); verify(mockSigIntListener, times(1)).handle(eq(sigintEvent)); verify(mockSigQuitListener, times(1)).handle(eq(sigquitEvent)); verify(mockSigQuitTermListener, times(1)).handle(eq(sigquitEvent)); verify(mockSigQuitTermListener, times(1)).handle(eq(sigtermEvent)); }