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.assertTrue;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.times;
26  import static org.mockito.Mockito.verify;
27  import static org.mockito.Mockito.verifyNoMoreInteractions;
28  import static org.mockito.Mockito.when;
29  
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  import org.apache.commons.configuration2.PropertiesConfiguration;
34  import org.apache.commons.configuration2.ex.ConfigurationException;
35  import org.apache.commons.configuration2.io.FileHandler;
36  import org.apache.commons.configuration2.reloading.FileHandlerReloadingDetector;
37  import org.apache.commons.configuration2.reloading.ReloadingDetector;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Test class for {@code ReloadingFileBasedConfigurationBuilder}.
42   */
43  public class TestReloadingFileBasedConfigurationBuilder {
44  
45      /**
46       * A test builder implementation which allows mocking the underlying reloading detector.
47       */
48      private static final class ReloadingFileBasedConfigurationBuilderTestImpl extends ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> {
49  
50          /** The mock for the reloading detector. */
51          private final ReloadingDetector mockDetector;
52  
53          /** Stores the file handler passed to createReloadingDetector(). */
54          private FileHandler handlerForDetector;
55  
56          /**
57           * Creates a new instance of {@code ReloadingFileBasedConfigurationBuilderTestImpl} and initializes it with a mock
58           * reloading detector.
59           *
60           * @param detector the mock detector
61           */
62          public ReloadingFileBasedConfigurationBuilderTestImpl(final ReloadingDetector detector) {
63              super(PropertiesConfiguration.class);
64              mockDetector = detector;
65          }
66  
67          /**
68           * Returns the mock file handler.
69           */
70          @Override
71          protected ReloadingDetector createReloadingDetector(final FileHandler handler, final FileBasedBuilderParametersImpl fbparams) {
72              handlerForDetector = handler;
73              return mockDetector;
74          }
75  
76          /**
77           * Returns the file handler that was passed to createReloadingDetector().
78           *
79           * @return the file handler
80           */
81          public FileHandler getHandlerForDetector() {
82              return handlerForDetector;
83          }
84      }
85  
86      /**
87       * Tests whether a correct reloading detector is created if no custom factory was set.
88       */
89      @Test
90      void testCreateReloadingDetectorDefaultFactory() throws ConfigurationException {
91          final ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder = new ReloadingFileBasedConfigurationBuilder<>(
92              PropertiesConfiguration.class);
93          final FileHandler handler = new FileHandler();
94          final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
95          final long refreshDelay = 60000L;
96          params.setReloadingRefreshDelay(refreshDelay);
97          final FileHandlerReloadingDetector detector = (FileHandlerReloadingDetector) builder.createReloadingDetector(handler, params);
98          assertSame(handler, detector.getFileHandler());
99          assertEquals(refreshDelay, detector.getRefreshDelay());
100     }
101 
102     /**
103      * Tests whether a custom reloading detector factory can be installed.
104      */
105     @Test
106     void testCreateReloadingDetectoryCustomFactory() throws ConfigurationException {
107         final ReloadingDetector detector = mock(ReloadingDetector.class);
108         final ReloadingDetectorFactory factory = mock(ReloadingDetectorFactory.class);
109         final FileHandler handler = new FileHandler();
110         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
111 
112         when(factory.createReloadingDetector(handler, params)).thenReturn(detector);
113 
114         params.setReloadingDetectorFactory(factory);
115         final ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder = new ReloadingFileBasedConfigurationBuilder<>(
116             PropertiesConfiguration.class);
117         assertSame(detector, builder.createReloadingDetector(handler, params));
118 
119         verify(factory).createReloadingDetector(handler, params);
120         verifyNoMoreInteractions(factory);
121     }
122 
123     /**
124      * Tests whether a configuration can be created if no location is set. This tests also ensures that the super
125      * constructor is called correctly.
126      */
127     @Test
128     void testGetConfigurationNoLocation() throws ConfigurationException {
129         final Map<String, Object> params = new HashMap<>();
130         params.put("throwExceptionOnMissing", Boolean.TRUE);
131         final ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder = new ReloadingFileBasedConfigurationBuilder<>(
132             PropertiesConfiguration.class, params);
133         final PropertiesConfiguration conf = builder.getConfiguration();
134         assertTrue(conf.isThrowExceptionOnMissing());
135         assertTrue(conf.isEmpty());
136     }
137 
138     /**
139      * Tests whether the allowFailOnInit flag is correctly initialized.
140      */
141     @Test
142     void testInitAllowFailOnInitFlag() {
143         final ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder = new ReloadingFileBasedConfigurationBuilder<>(
144             PropertiesConfiguration.class, null, true);
145         assertTrue(builder.isAllowFailOnInit());
146     }
147 
148     /**
149      * Tests whether this builder reacts on events fired by the reloading controller.
150      */
151     @Test
152     void testReloadingControllerEvents() throws ConfigurationException {
153         final ReloadingDetector detector = mock(ReloadingDetector.class);
154 
155         when(detector.isReloadingRequired()).thenReturn(Boolean.TRUE);
156 
157         final ReloadingFileBasedConfigurationBuilderTestImpl builder = new ReloadingFileBasedConfigurationBuilderTestImpl(detector);
158         final BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
159         builder.addEventListener(ConfigurationBuilderEvent.RESET, listener);
160         builder.getConfiguration();
161         builder.getReloadingController().checkForReloading(null);
162         listener.nextEvent(ConfigurationBuilderEvent.RESET);
163         listener.assertNoMoreEvents();
164 
165         verify(detector).isReloadingRequired();
166         verifyNoMoreInteractions(detector);
167     }
168 
169     /**
170      * Tests the isReloadingRequired() implementation of the detector associated with the reloading controller.
171      */
172     @Test
173     void testReloadingDetectorIsReloadingRequired() throws ConfigurationException {
174         final ReloadingDetector detector = mock(ReloadingDetector.class);
175 
176         when(detector.isReloadingRequired()).thenReturn(Boolean.TRUE, Boolean.FALSE);
177 
178         final ReloadingFileBasedConfigurationBuilderTestImpl builder = new ReloadingFileBasedConfigurationBuilderTestImpl(detector);
179         builder.getConfiguration();
180         final ReloadingDetector ctrlDetector = builder.getReloadingController().getDetector();
181         assertTrue(ctrlDetector.isReloadingRequired());
182         assertFalse(ctrlDetector.isReloadingRequired());
183         assertSame(builder.getFileHandler(), builder.getHandlerForDetector());
184 
185         verify(detector, times(2)).isReloadingRequired();
186         verifyNoMoreInteractions(detector);
187     }
188 
189     /**
190      * Tests the behavior of the reloading detector if no underlying detector is available.
191      */
192     @Test
193     void testReloadingDetectorNoFileHandler() {
194         final ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder = new ReloadingFileBasedConfigurationBuilder<>(
195             PropertiesConfiguration.class);
196         final ReloadingDetector ctrlDetector = builder.getReloadingController().getDetector();
197         ctrlDetector.reloadingPerformed();
198         assertFalse(ctrlDetector.isReloadingRequired());
199     }
200 
201     /**
202      * Tests the reloadingPerformed() implementation of the detector associated with the reloading controller.
203      */
204     @Test
205     void testReloadingDetectorReloadingPerformed() throws ConfigurationException {
206         final ReloadingDetector detector = mock(ReloadingDetector.class);
207         final ReloadingFileBasedConfigurationBuilderTestImpl builder = new ReloadingFileBasedConfigurationBuilderTestImpl(detector);
208         builder.getConfiguration();
209         final ReloadingDetector ctrlDetector = builder.getReloadingController().getDetector();
210         ctrlDetector.reloadingPerformed();
211 
212         verify(detector).reloadingPerformed();
213         verifyNoMoreInteractions(detector);
214     }
215 
216     /**
217      * Tests whether the controller's reloading state is reset when a new result configuration is created.
218      */
219     @Test
220     void testResetReloadingStateInGetConfiguration() throws ConfigurationException {
221         final ReloadingDetector detector = mock(ReloadingDetector.class);
222 
223         when(detector.isReloadingRequired()).thenReturn(Boolean.TRUE);
224 
225         final ReloadingFileBasedConfigurationBuilderTestImpl builder = new ReloadingFileBasedConfigurationBuilderTestImpl(detector);
226         final PropertiesConfiguration config1 = builder.getConfiguration();
227         builder.getReloadingController().checkForReloading(null);
228         final PropertiesConfiguration config2 = builder.getConfiguration();
229         assertNotSame(config1, config2);
230         assertFalse(builder.getReloadingController().isInReloadingState());
231 
232         verify(detector).isReloadingRequired();
233         verify(detector).reloadingPerformed();
234         verifyNoMoreInteractions(detector);
235     }
236 }