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;
27  
28  import java.beans.PropertyChangeSupport;
29  import java.util.function.BiConsumer;
30  import java.util.function.Consumer;
31  import it.tidalwave.ui.core.role.impl.MutableListenerSupport;
32  import lombok.experimental.Delegate;
33  import org.testng.annotations.Test;
34  import static org.mockito.Mockito.*;
35  
36  /***************************************************************************************************************************************************************
37   *
38   * @author  Fabrizio Giudici
39   *
40   **************************************************************************************************************************************************************/
41  public class MutableTest
42    {
43      static class MutableHelper implements Mutable
44        {
45          @Delegate
46          private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
47  
48          @Delegate
49          private final MutableListenerSupport mls = new MutableListenerSupport(pcs);
50        }
51  
52      @FunctionalInterface
53      static interface TriConsumer<T, S, U>
54        {
55          public void accept (T t, S s, U u);
56        }
57  
58      @Test @SuppressWarnings("unchecked")
59      public void test_listeners()
60        {
61          // given
62          final var mutable = new MutableHelper();
63          final var consumer1 = mock(Consumer.class);
64          final var consumer2 = mock(BiConsumer.class);
65          final var consumer3 = mock(TriConsumer.class);
66          mutable.addListener((source, oldValue, newValue) -> consumer3.accept(source, oldValue, newValue));
67          mutable.addListener2((oldValue, newValue) -> consumer2.accept(oldValue, newValue));
68          mutable.addListener1(newValue -> consumer1.accept(newValue));
69          // when
70          mutable.firePropertyChange("property name", "old value", "new value");
71          // then
72          verify(consumer1).accept(eq("new value"));
73          verify(consumer2).accept(eq("old value"), eq("new value"));
74          verify(consumer3).accept(same(mutable), eq("old value"), eq("new value"));
75        }
76    }