diff --git a/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java b/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java index a7e9afd372..671a79bd9f 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java +++ b/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.scheduling.config; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.LinkedHashSet; import java.util.List; @@ -47,6 +48,7 @@ import org.springframework.util.CollectionUtils; * * @author Juergen Hoeller * @author Chris Beams + * @author Tobias Montagna-Hay * @since 3.0 * @see org.springframework.scheduling.annotation.EnableAsync * @see org.springframework.scheduling.annotation.SchedulingConfigurer @@ -123,6 +125,14 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean this.triggerTasks = triggerTasks; } + /** + * Return the trigger tasks as a list of {@link TriggerTask} + * @since 4.2 + */ + public List getTriggerTaskList() { + return Collections.unmodifiableList(this.triggerTasks); + } + /** * Specify triggered tasks as a Map of Runnables (the tasks) and cron expressions. * @see CronTrigger @@ -144,6 +154,14 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean this.cronTasks = cronTasks; } + /** + * Return the cron tasks as a list of {@link CronTask} + * @since 4.2 + */ + public List getCronTaskList() { + return Collections.unmodifiableList(this.cronTasks); + } + /** * Specify triggered tasks as a Map of Runnables (the tasks) and fixed-rate values. * @see TaskScheduler#scheduleAtFixedRate(Runnable, long) @@ -165,6 +183,14 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean this.fixedRateTasks = fixedRateTasks; } + /** + * Return the fixed-rate tasks as a list of {@link IntervalTask}. + * @since 4.2 + */ + public List getFixedRateTaskList() { + return Collections.unmodifiableList(this.fixedRateTasks); + } + /** * Specify triggered tasks as a Map of Runnables (the tasks) and fixed-delay values. * @see TaskScheduler#scheduleWithFixedDelay(Runnable, long) @@ -186,6 +212,14 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean this.fixedDelayTasks = fixedDelayTasks; } + /** + * Return the fixed-delay tasks as a list of {@link IntervalTask} + * @since 4.2 + */ + public List getFixedDelayTaskList() { + return Collections.unmodifiableList(this.fixedDelayTasks); + } + /** * Add a Runnable task to be triggered per the given {@link Trigger}. * @see TaskScheduler#scheduleAtFixedRate(Runnable, long) diff --git a/spring-context/src/test/java/org/springframework/scheduling/config/ScheduledTaskRegistrarTest.java b/spring-context/src/test/java/org/springframework/scheduling/config/ScheduledTaskRegistrarTest.java new file mode 100644 index 0000000000..e7c7eef94a --- /dev/null +++ b/spring-context/src/test/java/org/springframework/scheduling/config/ScheduledTaskRegistrarTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2002-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.scheduling.config; + +import java.util.Collections; +import java.util.List; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +/** + * @author Tobias Montagna-Hay + */ +public class ScheduledTaskRegistrarTest { + + private final ScheduledTaskRegistrar taskRegistrar = new ScheduledTaskRegistrar(); + + @Test + public void getTriggerTasks() { + TriggerTask mockTriggerTask = mock(TriggerTask.class); + List triggerTaskList = Collections.singletonList(mockTriggerTask); + this.taskRegistrar.setTriggerTasksList(triggerTaskList); + List retrievedList = this.taskRegistrar.getTriggerTaskList(); + assertEquals(1, retrievedList.size()); + assertEquals(mockTriggerTask, retrievedList.get(0)); + } + + @Test + public void getCronTasks() { + CronTask mockCronTask = mock(CronTask.class); + List cronTaskList = Collections.singletonList(mockCronTask); + this.taskRegistrar.setCronTasksList(cronTaskList); + List retrievedList = this.taskRegistrar.getCronTaskList(); + assertEquals(1, retrievedList.size()); + assertEquals(mockCronTask, retrievedList.get(0)); + } + + @Test + public void testGetFixedRateTasks() { + IntervalTask mockFixedRateTask = mock(IntervalTask.class); + List fixedRateTaskList = Collections.singletonList(mockFixedRateTask); + this.taskRegistrar.setFixedRateTasksList(fixedRateTaskList); + List retrievedList = this.taskRegistrar.getFixedRateTaskList(); + assertEquals(1, retrievedList.size()); + assertEquals(mockFixedRateTask, retrievedList.get(0)); + } + + @Test + public void testGetFixedDelayTasks() { + IntervalTask mockFixedDelayTask = mock(IntervalTask.class); + List fixedDelayTaskList = Collections.singletonList(mockFixedDelayTask); + this.taskRegistrar.setFixedDelayTasksList(fixedDelayTaskList); + List retrievedList = this.taskRegistrar.getFixedDelayTaskList(); + assertEquals(1, retrievedList.size()); + assertEquals(mockFixedDelayTask, retrievedList.get(0)); + } + +}