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