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.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  import static org.mockito.ArgumentMatchers.any;
24  import static org.mockito.ArgumentMatchers.eq;
25  import static org.mockito.Mockito.mock;
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.concurrent.ScheduledExecutorService;
31  import java.util.concurrent.ScheduledFuture;
32  import java.util.concurrent.TimeUnit;
33  
34  import org.apache.commons.lang3.mutable.MutableObject;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  import org.mockito.stubbing.OngoingStubbing;
38  
39  /**
40   * Test class for {@code PeriodicReloadingTrigger}.
41   */
42  public class TestPeriodicReloadingTrigger {
43  
44      /** Constant for a parameter to be passed to the controller. */
45      private static final Object CTRL_PARAM = "Test controller parameter";
46  
47      /** Constant for the period. */
48      private static final long PERIOD = 60;
49  
50      /** Constant for the period's time unit. */
51      private static final TimeUnit UNIT = TimeUnit.SECONDS;
52  
53      /**
54       * Creates a mock object for a scheduled future.
55       *
56       * @return the mock
57       */
58      @SuppressWarnings("unchecked")
59      private static ScheduledFuture<Void> createFutureMock() {
60          return mock(ScheduledFuture.class);
61      }
62  
63      /** A mock for the executor service. */
64      private ScheduledExecutorService executor;
65  
66      /** A mock for the reloading controller. */
67      private ReloadingController controller;
68  
69      /**
70       * Creates a test instance with default parameters.
71       *
72       * @return the test instance
73       */
74      private PeriodicReloadingTrigger createTrigger() {
75          return new PeriodicReloadingTrigger(controller, CTRL_PARAM, PERIOD, UNIT, executor);
76      }
77  
78      @BeforeEach
79      public void setUp() throws Exception {
80          executor = mock(ScheduledExecutorService.class);
81          controller = mock(ReloadingController.class);
82      }
83  
84      /**
85       * Tests whether a default executor service is created if necessary.
86       */
87      @Test
88      void testDefaultExecutor() {
89          final PeriodicReloadingTrigger trigger = new PeriodicReloadingTrigger(controller, CTRL_PARAM, PERIOD, UNIT);
90          assertNotNull(trigger.getExecutorService());
91      }
92  
93      /**
94       * Tries to create an instance without a controller.
95       */
96      @Test
97      void testInitNoController() {
98          assertThrows(IllegalArgumentException.class, () -> new PeriodicReloadingTrigger(null, CTRL_PARAM, PERIOD, UNIT));
99      }
100 
101     /**
102      * Tests that a newly created trigger is not running.
103      */
104     @Test
105     void testIsRunningAfterInit() {
106         assertFalse(createTrigger().isRunning());
107     }
108 
109     /**
110      * Tests a shutdown operation.
111      */
112     @Test
113     void testShutdown() {
114         final ScheduledFuture<Void> future = createFutureMock();
115 
116         whenScheduled().thenReturn(future);
117         when(future.cancel(false)).thenReturn(Boolean.TRUE);
118 
119         final PeriodicReloadingTrigger trigger = createTrigger();
120         trigger.start();
121         trigger.shutdown();
122 
123         verifyScheduled();
124         verify(future).cancel(false);
125         verify(executor).shutdown();
126         verifyNoMoreInteractions(future, controller, executor);
127     }
128 
129     /**
130      * Tests a shutdown operation which excludes the executor service.
131      */
132     @Test
133     void testShutdownNoExecutor() {
134         createTrigger().shutdown(false);
135     }
136 
137     /**
138      * Tests whether the trigger can be started.
139      */
140     @Test
141     void testStart() {
142         final ScheduledFuture<Void> future = createFutureMock();
143         final MutableObject<Runnable> refTask = new MutableObject<>();
144 
145         whenScheduled().thenAnswer(invocation -> {
146             refTask.setValue(invocation.getArgument(0, Runnable.class));
147             return future;
148         });
149         when(controller.checkForReloading(CTRL_PARAM)).thenReturn(Boolean.FALSE);
150 
151         final PeriodicReloadingTrigger trigger = createTrigger();
152         trigger.start();
153         assertTrue(trigger.isRunning());
154         refTask.getValue().run();
155 
156         verifyScheduled();
157         verify(controller).checkForReloading(CTRL_PARAM);
158         verifyNoMoreInteractions(future, controller, executor);
159     }
160 
161     /**
162      * Tests whether start() is a noop if the trigger is already running.
163      */
164     @Test
165     void testStartTwice() {
166         final ScheduledFuture<Void> future = createFutureMock();
167 
168         whenScheduled().thenReturn(future);
169 
170         final PeriodicReloadingTrigger trigger = createTrigger();
171         trigger.start();
172         trigger.start();
173 
174         verifyScheduled();
175         verifyNoMoreInteractions(future, controller, executor);
176     }
177 
178     /**
179      * Tests whether a running trigger can be stopped.
180      */
181     @Test
182     void testStop() {
183         final ScheduledFuture<Void> future = createFutureMock();
184 
185         whenScheduled().thenReturn(future);
186         when(future.cancel(false)).thenReturn(Boolean.TRUE);
187 
188         final PeriodicReloadingTrigger trigger = createTrigger();
189         trigger.start();
190         trigger.stop();
191         assertFalse(trigger.isRunning());
192 
193         verifyScheduled();
194         verify(future).cancel(false);
195         verifyNoMoreInteractions(future, controller, executor);
196     }
197 
198     /**
199      * Tests stop() if the trigger is not running.
200      */
201     @Test
202     void testStopNotRunning() {
203         createTrigger().stop();
204     }
205 
206     /**
207      * Verifies that an invocation has occurred on the executor mock which schedules the trigger task.
208      */
209     private void verifyScheduled() {
210         verify(executor).scheduleAtFixedRate(any(), eq(PERIOD), eq(PERIOD), eq(UNIT));
211     }
212 
213     /**
214      * Prepares the executor mock to for any invocation which schedules the trigger task.
215      * This method should be used to call one of the {@code thenReturn}, {@code thenAnswer} or {@code thenThrow} methods.
216      *
217      * @return An ongoing stubbing for the future
218      */
219     private OngoingStubbing<ScheduledFuture<?>> whenScheduled() {
220         return when(executor.scheduleAtFixedRate(any(), eq(PERIOD), eq(PERIOD), eq(UNIT)));
221     }
222 }