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    *     https://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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotSame;
22  import static org.junit.jupiter.api.Assertions.assertSame;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import org.apache.commons.configuration2.Configuration;
27  import org.apache.commons.configuration2.PropertiesConfiguration;
28  import org.apache.commons.configuration2.event.Event;
29  import org.apache.commons.configuration2.event.EventType;
30  import org.apache.commons.configuration2.ex.ConfigurationException;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * A test class for {@code BasicConfigurationBuilder} with tests related to events fired by the builder.
35   */
36  public class TestBasicConfigurationBuilderEvents {
37  
38      /**
39       * Tests whether the base type for builder events is correctly configured.
40       */
41      @Test
42      void testBuilderEventType() {
43          final EventType<ConfigurationBuilderEvent> builderEventType = ConfigurationBuilderEvent.ANY;
44          assertEquals(Event.ANY, builderEventType.getSuperType());
45      }
46  
47      /**
48       * Tests whether builder reset events are correctly distributed.
49       */
50      @Test
51      void testBuilderResetEvent() {
52          final BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
53          final BasicConfigurationBuilder<PropertiesConfiguration> builder = new BasicConfigurationBuilder<>(PropertiesConfiguration.class);
54          builder.addEventListener(ConfigurationBuilderEvent.RESET, listener);
55  
56          builder.reset();
57          builder.resetResult();
58          ConfigurationBuilderEvent event = listener.nextEvent(ConfigurationBuilderEvent.RESET);
59          assertSame(builder, event.getSource());
60          event = listener.nextEvent(ConfigurationBuilderEvent.RESET);
61          assertSame(builder, event.getSource());
62          listener.assertNoMoreEvents();
63      }
64  
65      /**
66       * Tests whether the reset builder event type is correctly configured.
67       */
68      @Test
69      void testBuilderResetEventType() {
70          final EventType<ConfigurationBuilderEvent> builderResetType = ConfigurationBuilderEvent.RESET;
71          assertEquals(ConfigurationBuilderEvent.ANY, builderResetType.getSuperType());
72      }
73  
74      /**
75       * Tests whether a configuration request event is generated.
76       */
77      @Test
78      void testConfigurationRequestEvent() throws ConfigurationException {
79          final BasicConfigurationBuilder<PropertiesConfiguration> builder = new BasicConfigurationBuilder<>(PropertiesConfiguration.class);
80          builder.getConfiguration();
81          final BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
82          builder.addEventListener(ConfigurationBuilderEvent.ANY, listener);
83  
84          builder.getConfiguration();
85          final ConfigurationBuilderEvent event = listener.nextEvent(ConfigurationBuilderEvent.CONFIGURATION_REQUEST);
86          assertSame(builder, event.getSource());
87          listener.assertNoMoreEvents();
88      }
89  
90      /**
91       * Tests whether the configuration request event type is correctly configured.
92       */
93      @Test
94      void testConfigurationRequestEventType() {
95          final EventType<ConfigurationBuilderEvent> eventType = ConfigurationBuilderEvent.CONFIGURATION_REQUEST;
96          assertEquals(ConfigurationBuilderEvent.ANY, eventType.getSuperType());
97      }
98  
99      /**
100      * Tests whether an event listener can be removed again.
101      */
102     @Test
103     void testRemoveEventListener() {
104         final BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
105         final BasicConfigurationBuilder<PropertiesConfiguration> builder = new BasicConfigurationBuilder<>(PropertiesConfiguration.class);
106         builder.addEventListener(ConfigurationBuilderEvent.RESET, listener);
107 
108         builder.reset();
109         assertTrue(builder.removeEventListener(ConfigurationBuilderEvent.RESET, listener));
110         builder.resetResult();
111         listener.nextEvent(ConfigurationBuilderEvent.RESET);
112         listener.assertNoMoreEvents();
113     }
114 
115     /**
116      * Tests removeEventListener() for a non-existing listener.
117      */
118     @Test
119     void testRemoveEventListenerNotExisting() {
120         final BasicConfigurationBuilder<PropertiesConfiguration> builder = new BasicConfigurationBuilder<>(PropertiesConfiguration.class);
121         final BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
122         builder.addEventListener(ConfigurationBuilderEvent.RESET, listener);
123         assertFalse(builder.removeEventListener(ConfigurationBuilderEvent.CONFIGURATION_REQUEST, listener));
124     }
125 
126     /**
127      * Tests the use case that a listener on the request event triggers a reset of the builder.
128      */
129     @Test
130     void testResetOnConfigurationRequestEvent() throws ConfigurationException {
131         final BasicConfigurationBuilder<PropertiesConfiguration> builder = new BasicConfigurationBuilder<>(PropertiesConfiguration.class);
132         final PropertiesConfiguration configuration = builder.getConfiguration();
133         final BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
134         builder.addEventListener(ConfigurationBuilderEvent.RESET, listener);
135         builder.addEventListener(ConfigurationBuilderEvent.CONFIGURATION_REQUEST, event -> builder.resetResult());
136 
137         final PropertiesConfiguration configuration2 = builder.getConfiguration();
138         assertNotSame(configuration, configuration2);
139         listener.nextEvent(ConfigurationBuilderEvent.RESET);
140         listener.assertNoMoreEvents();
141     }
142 
143     /**
144      * Tests whether a result created event is correctly generated.
145      */
146     @Test
147     void testResultCreatedEvent() throws ConfigurationException {
148         final BasicConfigurationBuilder<PropertiesConfiguration> builder = new BasicConfigurationBuilder<>(PropertiesConfiguration.class);
149         final BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
150         builder.addEventListener(ConfigurationBuilderEvent.ANY, listener);
151 
152         final PropertiesConfiguration configuration = builder.getConfiguration();
153         listener.nextEvent(ConfigurationBuilderEvent.CONFIGURATION_REQUEST);
154         final ConfigurationBuilderResultCreatedEvent event = listener.nextEvent(ConfigurationBuilderResultCreatedEvent.RESULT_CREATED);
155         assertSame(builder, event.getSource());
156         assertSame(configuration, event.getConfiguration());
157     }
158 
159     /**
160      * Tries to create an event about a newly created configuration without a configuration instance.
161      */
162     @Test
163     void testResultCreatedEventNoConfiguration() {
164         final BasicConfigurationBuilder<Configuration> builder = new BasicConfigurationBuilder<>(Configuration.class);
165         assertThrows(IllegalArgumentException.class,
166                 () -> new ConfigurationBuilderResultCreatedEvent(builder, ConfigurationBuilderResultCreatedEvent.RESULT_CREATED, null));
167     }
168 
169     /**
170      * Tests whether the type of a result created event is correctly configured.
171      */
172     @Test
173     void testResultCreatedEventType() {
174         assertEquals(ConfigurationBuilderEvent.ANY, ConfigurationBuilderResultCreatedEvent.RESULT_CREATED.getSuperType());
175     }
176 }