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