View Javadoc
1   /*
2    * *************************************************************************************************************************************************************
3    *
4    * SteelBlue: DCI User Interfaces
5    * http://tidalwave.it/projects/steelblue
6    *
7    * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8    *
9    * *************************************************************************************************************************************************************
10   *
11   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17   * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
18   *
19   * *************************************************************************************************************************************************************
20   *
21   * git clone https://bitbucket.org/tidalwave/steelblue-src
22   * git clone https://github.com/tidalwave-it/steelblue-src
23   *
24   * *************************************************************************************************************************************************************
25   */
26  package it.tidalwave.ui.core.role.impl;
27  
28  import java.beans.PropertyChangeSupport;
29  import it.tidalwave.ui.core.MutableListener;
30  import org.testng.annotations.Test;
31  import static org.mockito.Mockito.*;
32  import static org.hamcrest.MatcherAssert.assertThat;
33  import static org.hamcrest.CoreMatchers.is;
34  
35  /***************************************************************************************************************************************************************
36   *
37   * @author      Fabrizio Giudici
38   *
39   **************************************************************************************************************************************************************/
40  public class MutableListenerSupportTest
41    {
42      @Test @SuppressWarnings("unchecked")
43      public void test_named_property_listener()
44        {
45          // given
46          final var source = new Object();
47          final var pcs = new PropertyChangeSupport(source);
48          final var underTest = new MutableListenerSupport(pcs);
49          final var propertyName = "theProperty";
50          final var listener = mock(MutableListener.class);
51          final var anotherListener = mock(MutableListener.class);
52          underTest.addListener(propertyName, listener);
53          underTest.addListener("anotherProperty", anotherListener);
54          // when
55          underTest.fireChange(propertyName, "old", "new");
56          // then
57          verify(listener).changed(same(source), eq("old"),  eq("new"));
58          verifyNoInteractions(anotherListener);
59        }
60  
61      /**********************************************************************************************************************************************************/
62      @Test @SuppressWarnings("unchecked")
63      public void test_generic_listener()
64        {
65          // given
66          final var source = new Object();
67          final var pcs = new PropertyChangeSupport(source);
68          final var underTest = new MutableListenerSupport(pcs);
69          final var listener = mock(MutableListener.class);
70          underTest.addListener(listener);
71          // when
72          underTest.fireChange("old", "new");
73          // then
74          verify(listener).changed(same(source), eq("old"),  eq("new"));
75          assertThat(underTest.adapterMap.containsKey(listener), is(true));
76        }
77  
78      /**********************************************************************************************************************************************************/
79      @Test @SuppressWarnings("unchecked")
80      public void test_remove_listener()
81        {
82          // given
83          final var source = new Object();
84          final var pcs = new PropertyChangeSupport(source);
85          final var underTest = new MutableListenerSupport(pcs);
86          final var mutableListener = mock(MutableListener.class);
87          underTest.addListener(mutableListener);
88          // when
89          underTest.removeListener(mutableListener);
90          underTest.fireChange("old", "new");
91          // then
92          verifyNoInteractions(mutableListener);
93          assertThat(underTest.adapterMap.containsKey(mutableListener), is(false));
94        }
95    }