|
| 1 | +package org.codefx.libfx.listener; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.mock; |
| 4 | +import static org.mockito.Mockito.times; |
| 5 | +import static org.mockito.Mockito.verify; |
| 6 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 7 | +import static org.mockito.Mockito.verifyZeroInteractions; |
| 8 | + |
| 9 | +import java.util.function.BiConsumer; |
| 10 | + |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +/** |
| 15 | + * Tests the class {@link GenericListenerHandle}. |
| 16 | + */ |
| 17 | +public class GenericListenerHandleTest { |
| 18 | + |
| 19 | + // #region INSTANCES |
| 20 | + |
| 21 | + /** |
| 22 | + * The tested handle. |
| 23 | + */ |
| 24 | + private GenericListenerHandle<Object, Object> handle; |
| 25 | + |
| 26 | + /** |
| 27 | + * The observable on which the {@link #handle} operates. |
| 28 | + */ |
| 29 | + private Object observable; |
| 30 | + |
| 31 | + /** |
| 32 | + * The listener on which the {@link #handle} operates. |
| 33 | + */ |
| 34 | + private Object listener; |
| 35 | + |
| 36 | + /** |
| 37 | + * The function which adds the listener to the observable. This is a mock so calls can be verified. |
| 38 | + */ |
| 39 | + private BiConsumer<Object, Object> add; |
| 40 | + |
| 41 | + /** |
| 42 | + * The function which adds the listener to the observable. This is a mock so calls can be verified. |
| 43 | + */ |
| 44 | + private BiConsumer<Object, Object> remove; |
| 45 | + |
| 46 | + // #end INSTANCES |
| 47 | + |
| 48 | + // #region SETUP |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates the tested instances. |
| 52 | + */ |
| 53 | + @Before |
| 54 | + @SuppressWarnings("unchecked") |
| 55 | + public void setUp() { |
| 56 | + add = mock(BiConsumer.class); |
| 57 | + remove = mock(BiConsumer.class); |
| 58 | + observable = "observable"; |
| 59 | + listener = "listner"; |
| 60 | + |
| 61 | + handle = new GenericListenerHandle<Object, Object>(observable, listener, add, remove); |
| 62 | + } |
| 63 | + |
| 64 | + // #end SETUP |
| 65 | + |
| 66 | + // #region TESTS |
| 67 | + |
| 68 | + /** |
| 69 | + * Tests whether the construction of the handle does not cause any calls to {@link #add} and {@link #remove}. |
| 70 | + */ |
| 71 | + @Test |
| 72 | + public void testNoCallsToAddAndRemoveOnConstruction() { |
| 73 | + verifyZeroInteractions(add, remove); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Tests whether the a call to {@link ListenerHandle#detach() detach()} after construction does not cause any calls |
| 78 | + * to {@link #add} and {@link #remove}. |
| 79 | + */ |
| 80 | + @Test |
| 81 | + public void testDetachAfterConstruction() { |
| 82 | + handle.detach(); |
| 83 | + |
| 84 | + verifyZeroInteractions(add, remove); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Tests whether the first call to {@link ListenerHandle#attach() attach()} correctly calls {@link #add}. |
| 89 | + */ |
| 90 | + @Test |
| 91 | + public void testAttachAfterConstruction() { |
| 92 | + handle.attach(); |
| 93 | + |
| 94 | + verify(add, times(1)).accept(observable, listener); |
| 95 | + verifyNoMoreInteractions(add); |
| 96 | + verifyZeroInteractions(remove); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Tests whether calling {@link ListenerHandle#attach() attach()} multiple times in a row calls {@link #add} only |
| 101 | + * once. |
| 102 | + */ |
| 103 | + @Test |
| 104 | + public void testMultipleAttach() { |
| 105 | + handle.attach(); |
| 106 | + handle.attach(); |
| 107 | + handle.attach(); |
| 108 | + |
| 109 | + verify(add, times(1)).accept(observable, listener); |
| 110 | + verifyNoMoreInteractions(add); |
| 111 | + verifyZeroInteractions(remove); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Tests whether calling {@link ListenerHandle#detach() detach()} correctly calls {@link #remove}. |
| 116 | + */ |
| 117 | + @Test |
| 118 | + public void testDetach() { |
| 119 | + handle.attach(); |
| 120 | + handle.detach(); |
| 121 | + |
| 122 | + // the order of those calls is not verified here; |
| 123 | + // but if it would not match the intuition (first 'add', then 'remove'), a more specific test above would fail |
| 124 | + verify(add, times(1)).accept(observable, listener); |
| 125 | + verify(remove, times(1)).accept(observable, listener); |
| 126 | + verifyNoMoreInteractions(add, remove); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Tests whether calling {@link ListenerHandle#detach() detach()} multiple times in a row calls {@link #remove} only |
| 131 | + * once. |
| 132 | + */ |
| 133 | + @Test |
| 134 | + public void testMultipleDetach() { |
| 135 | + handle.attach(); |
| 136 | + handle.detach(); |
| 137 | + handle.detach(); |
| 138 | + handle.detach(); |
| 139 | + |
| 140 | + verify(add, times(1)).accept(observable, listener); |
| 141 | + verify(remove, times(1)).accept(observable, listener); |
| 142 | + verifyZeroInteractions(remove); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Tests whether reattaching calls {@link #add} twice. |
| 147 | + */ |
| 148 | + @Test |
| 149 | + public void testReattach() { |
| 150 | + handle.attach(); |
| 151 | + handle.detach(); |
| 152 | + handle.attach(); |
| 153 | + |
| 154 | + // the order of those calls is not verified here; |
| 155 | + // but if it would not match the intuition ('add', 'remove', 'add'), a more specific test above would fail |
| 156 | + verify(add, times(2)).accept(observable, listener); |
| 157 | + verify(remove, times(1)).accept(observable, listener); |
| 158 | + verifyNoMoreInteractions(add, remove); |
| 159 | + } |
| 160 | + |
| 161 | + // #end TESTS |
| 162 | + |
| 163 | +} |
0 commit comments