|
| 1 | +/* |
| 2 | + * Copyright 2002-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.security.scheduling; |
| 17 | + |
| 18 | +import org.junit.After; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | +import org.mockito.Mock; |
| 22 | +import org.mockito.MockitoAnnotations; |
| 23 | +import org.springframework.scheduling.TaskScheduler; |
| 24 | +import org.springframework.scheduling.Trigger; |
| 25 | + |
| 26 | +import java.time.Duration; |
| 27 | +import java.time.Instant; |
| 28 | +import java.util.Date; |
| 29 | + |
| 30 | +import static org.mockito.Mockito.*; |
| 31 | + |
| 32 | +/** |
| 33 | + * Test An implementation of {@link TaskScheduler} invoking it whenever the trigger |
| 34 | + * indicates a next execution time. |
| 35 | + * |
| 36 | + * @author Richard Valdivieso |
| 37 | + * @since 5.1 |
| 38 | + */ |
| 39 | +public class DelegatingSecurityContextTaskSchedulerTests { |
| 40 | + |
| 41 | + @Mock |
| 42 | + private TaskScheduler scheduler; |
| 43 | + @Mock |
| 44 | + private Runnable runnable; |
| 45 | + @Mock |
| 46 | + private Trigger trigger; |
| 47 | + |
| 48 | + private DelegatingSecurityContextTaskScheduler delegatingSecurityContextTaskScheduler; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setup() { |
| 52 | + MockitoAnnotations.initMocks(this); |
| 53 | + delegatingSecurityContextTaskScheduler = new DelegatingSecurityContextTaskScheduler(scheduler); |
| 54 | + } |
| 55 | + |
| 56 | + @After |
| 57 | + public void cleanup() { |
| 58 | + delegatingSecurityContextTaskScheduler = null; |
| 59 | + } |
| 60 | + |
| 61 | + @Test(expected = IllegalArgumentException.class) |
| 62 | + public void testSchedulerIsNotNull() { |
| 63 | + delegatingSecurityContextTaskScheduler = new DelegatingSecurityContextTaskScheduler(null); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testSchedulerWithRunnableAndTrigger() { |
| 68 | + delegatingSecurityContextTaskScheduler.schedule(runnable, trigger); |
| 69 | + verify(scheduler).schedule(any(Runnable.class), any(Trigger.class)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testSchedulerWithRunnableAndInstant() { |
| 74 | + Instant date = Instant.now(); |
| 75 | + delegatingSecurityContextTaskScheduler.schedule(runnable, date); |
| 76 | + verify(scheduler).schedule(any(Runnable.class), any(Date.class)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testScheduleAtFixedRateWithRunnableAndDate() { |
| 81 | + Date date = new Date(1544751374L); |
| 82 | + Duration duration = Duration.ofSeconds(4L); |
| 83 | + delegatingSecurityContextTaskScheduler.scheduleAtFixedRate(runnable, date, 1000L); |
| 84 | + verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), isA(Date.class), eq(1000L)); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testScheduleAtFixedRateWithRunnableAndLong() { |
| 89 | + delegatingSecurityContextTaskScheduler.scheduleAtFixedRate(runnable, 1000L); |
| 90 | + verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), eq(1000L)); |
| 91 | + } |
| 92 | +} |
0 commit comments