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.reloading;
18  
19  import static org.junit.jupiter.api.Assertions.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertIterableEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.verify;
25  import static org.mockito.Mockito.verifyNoMoreInteractions;
26  import static org.mockito.Mockito.when;
27  
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.Collection;
31  import java.util.List;
32  
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Test class for {@code CombinedReloadingController}.
37   */
38  public class TestCombinedReloadingController {
39  
40      /** An array with mock objects for the sub controllers. */
41      private ReloadingController[] subControllers;
42  
43      /**
44       * Creates an array with mock objects for sub controllers.
45       */
46      private void initSubControllers() {
47          subControllers = new ReloadingController[3];
48          for (int i = 0; i < subControllers.length; i++) {
49              subControllers[i] = mock(ReloadingController.class);
50          }
51      }
52  
53      /**
54       * Creates a test instance with default settings.
55       *
56       * @return the test instance
57       */
58      private CombinedReloadingController setUpController() {
59          initSubControllers();
60          final List<ReloadingController> lstCtrls = new ArrayList<>(Arrays.asList(subControllers));
61          final CombinedReloadingController result = new CombinedReloadingController(lstCtrls);
62          // check whether a defensive copy is created
63          lstCtrls.clear();
64          return result;
65      }
66  
67      /**
68       * Tests a check for a reloading operation which results in false.
69       */
70      @Test
71      void testCheckForReloadingFalse() {
72          final CombinedReloadingController ctrl = setUpController();
73  
74          for (final ReloadingController rc : subControllers) {
75              when(rc.checkForReloading(null)).thenReturn(Boolean.FALSE);
76          }
77  
78          assertFalse(ctrl.checkForReloading("someParam"));
79  
80          for (final ReloadingController rc : subControllers) {
81              verify(rc).checkForReloading(null);
82              verifyNoMoreInteractions(rc);
83          }
84      }
85  
86      /**
87       * Tests a check for a reloading operation which results in true.
88       */
89      @Test
90      void testCheckForReloadingTrue() {
91          final CombinedReloadingController ctrl = setUpController();
92  
93          when(subControllers[0].checkForReloading(null)).thenReturn(Boolean.FALSE);
94          when(subControllers[1].checkForReloading(null)).thenReturn(Boolean.TRUE);
95          when(subControllers[2].checkForReloading(null)).thenReturn(Boolean.FALSE);
96  
97          assertTrue(ctrl.checkForReloading("someData"));
98  
99          for (final ReloadingController rc : subControllers) {
100             verify(rc).checkForReloading(null);
101             verifyNoMoreInteractions(rc);
102         }
103     }
104 
105     /**
106      * Tests whether the sub controllers can be accessed.
107      */
108     @Test
109     void testGetSubControllers() {
110         final CombinedReloadingController ctrl = setUpController();
111         final Collection<ReloadingController> subs = ctrl.getSubControllers();
112         assertIterableEquals(Arrays.asList(subControllers), subs);
113     }
114 
115     /**
116      * Tests that the list of sub controllers cannot be manipulated.
117      */
118     @Test
119     void testGetSubControllersModify() {
120         final Collection<ReloadingController> subs = setUpController().getSubControllers();
121         assertThrows(UnsupportedOperationException.class, subs::clear);
122     }
123 
124     /**
125      * Tries to create an instance without a collection.
126      */
127     @Test
128     void testInitNull() {
129         assertThrows(IllegalArgumentException.class, () -> new CombinedReloadingController(null));
130     }
131 
132     /**
133      * Tries to create an instance with a collection containing a null entry.
134      */
135     @Test
136     void testInitNullEntries() {
137         initSubControllers();
138         final Collection<ReloadingController> ctrls = new ArrayList<>(Arrays.asList(subControllers));
139         ctrls.add(null);
140         assertThrows(IllegalArgumentException.class, () -> new CombinedReloadingController(ctrls));
141     }
142 
143     /**
144      * Tests whether the sub controller's reloading state can be reset unconditionally.
145      */
146     @Test
147     void testResetInitialReloadingState() {
148         final CombinedReloadingController ctrl = setUpController();
149         ctrl.resetInitialReloadingState();
150 
151         for (final ReloadingController rc : subControllers) {
152             verify(rc).resetReloadingState();
153             verifyNoMoreInteractions(rc);
154         }
155     }
156 
157     /**
158      * Tests whether the reloading state can be reset.
159      */
160     @Test
161     void testResetReloadingState() {
162         final CombinedReloadingController ctrl = setUpController();
163 
164         when(subControllers[0].checkForReloading(null)).thenReturn(Boolean.TRUE);
165         when(subControllers[1].checkForReloading(null)).thenReturn(Boolean.FALSE);
166         when(subControllers[2].checkForReloading(null)).thenReturn(Boolean.FALSE);
167 
168         ctrl.checkForReloading(null);
169         ctrl.resetReloadingState();
170 
171         for (final ReloadingController rc : subControllers) {
172             verify(rc).checkForReloading(null);
173             verify(rc).resetReloadingState();
174             verifyNoMoreInteractions(rc);
175         }
176     }
177 }