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