View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration2.builder;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertSame;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.Arrays;
24  
25  import org.apache.commons.configuration2.event.ConfigurationEvent;
26  import org.apache.commons.configuration2.event.EventListenerRegistrationData;
27  import org.apache.commons.configuration2.event.EventListenerTestImpl;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Test class for {@code EventListenerParameters}.
32   */
33  public class TestEventListenerParameters {
34      /**
35       * Tests whether an event listener with its type can be added.
36       */
37      @Test
38      public void testAddEventListener() {
39          final EventListenerTestImpl listener = new EventListenerTestImpl(null);
40          final EventListenerParameters parameters = new EventListenerParameters();
41          assertSame(parameters, parameters.addEventListener(ConfigurationEvent.ADD_PROPERTY, listener));
42          assertEquals(1, parameters.getListeners().getRegistrations().size());
43          final EventListenerRegistrationData<?> reg = parameters.getListeners().getRegistrations().get(0);
44          assertEquals(ConfigurationEvent.ADD_PROPERTY, reg.getEventType());
45          assertEquals(listener, reg.getListener());
46      }
47  
48      /**
49       * Tests whether an event listener registration can be added.
50       */
51      @Test
52      public void testAddEventListenerRegistration() {
53          final EventListenerRegistrationData<ConfigurationEvent> reg = new EventListenerRegistrationData<>(ConfigurationEvent.SET_PROPERTY,
54              new EventListenerTestImpl(null));
55          final EventListenerParameters parameters = new EventListenerParameters();
56          assertSame(parameters, parameters.addEventListener(reg));
57          assertEquals(Arrays.asList(reg), parameters.getListeners().getRegistrations());
58      }
59  
60      /**
61       * Tests the map with parameters.
62       */
63      @Test
64      public void testGetParameters() {
65          final EventListenerParameters parameters = new EventListenerParameters();
66          assertTrue(parameters.getParameters().isEmpty());
67      }
68  
69      /**
70       * Tests that the list of event listeners is empty for a newly created instance.
71       */
72      @Test
73      public void testRegistrationsAfterCreation() {
74          final EventListenerParameters parameters = new EventListenerParameters();
75          assertTrue(parameters.getListeners().getRegistrations().isEmpty());
76      }
77  }