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