From 3796ab35ed8a66122c2a1fef30e4e9f981810279 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Mon, 1 Jun 2009 21:40:45 +0000 Subject: [PATCH] Added tests for the 'scheduled-tasks' element parsing within the 'task' namespace. git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1290 50f2f4bb-b051-0410-bef5-90022cba6387 --- ...heduledTasksBeanDefinitionParserTests.java | 110 ++++++++++++++++++ .../config/scheduledTasksContext.xml | 21 ++++ 2 files changed, 131 insertions(+) create mode 100644 org.springframework.context/src/test/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParserTests.java create mode 100644 org.springframework.context/src/test/resources/org/springframework/scheduling/config/scheduledTasksContext.xml diff --git a/org.springframework.context/src/test/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParserTests.java b/org.springframework.context/src/test/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParserTests.java new file mode 100644 index 00000000000..52112929038 --- /dev/null +++ b/org.springframework.context/src/test/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParserTests.java @@ -0,0 +1,110 @@ +/* + * Copyright 2002-2009 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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Collection; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +import org.springframework.beans.DirectFieldAccessor; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.scheduling.support.MethodInvokingRunnable; + +/** + * @author Mark Fisher + */ +@SuppressWarnings("unchecked") +public class ScheduledTasksBeanDefinitionParserTests { + + private ApplicationContext context; + + private ScheduledTaskRegistrar registrar; + + private Object testBean; + + + @Before + public void setup() { + this.context = new ClassPathXmlApplicationContext( + "scheduledTasksContext.xml", ScheduledTasksBeanDefinitionParserTests.class); + this.registrar = (ScheduledTaskRegistrar) this.context.getBeansOfType( + ScheduledTaskRegistrar.class).values().iterator().next(); + this.testBean = this.context.getBean("testBean"); + } + + @Test + public void checkScheduler() { + Object schedulerBean = this.context.getBean("testScheduler"); + Object schedulerRef = new DirectFieldAccessor(this.registrar).getPropertyValue("taskScheduler"); + assertEquals(schedulerBean, schedulerRef); + } + + @Test + public void checkTarget() { + Map tasks = (Map) new DirectFieldAccessor( + this.registrar).getPropertyValue("fixedRateTasks"); + Runnable runnable = tasks.keySet().iterator().next(); + assertEquals(MethodInvokingRunnable.class, runnable.getClass()); + Object targetObject = ((MethodInvokingRunnable) runnable).getTargetObject(); + String targetMethod = ((MethodInvokingRunnable) runnable).getTargetMethod(); + assertEquals(this.testBean, targetObject); + assertEquals("test", targetMethod); + } + + @Test + public void fixedRateTasks() { + Map tasks = (Map) new DirectFieldAccessor( + this.registrar).getPropertyValue("fixedRateTasks"); + assertEquals(2, tasks.size()); + Collection values = tasks.values(); + assertTrue(values.contains(new Long(1000))); + assertTrue(values.contains(new Long(2000))); + } + + @Test + public void fixedDelayTasks() { + Map tasks = (Map) new DirectFieldAccessor( + this.registrar).getPropertyValue("fixedDelayTasks"); + assertEquals(1, tasks.size()); + Long value = tasks.values().iterator().next(); + assertEquals(new Long(3000), value); + } + + @Test + public void cronTasks() { + Map tasks = (Map) new DirectFieldAccessor( + this.registrar).getPropertyValue("cronTasks"); + assertEquals(1, tasks.size()); + String expression = tasks.values().iterator().next(); + assertEquals("*/4 * 9-17 * * MON-FRI", expression); + } + + + static class TestBean { + + public void test() { + } + + } + +} diff --git a/org.springframework.context/src/test/resources/org/springframework/scheduling/config/scheduledTasksContext.xml b/org.springframework.context/src/test/resources/org/springframework/scheduling/config/scheduledTasksContext.xml new file mode 100644 index 00000000000..f7970c63747 --- /dev/null +++ b/org.springframework.context/src/test/resources/org/springframework/scheduling/config/scheduledTasksContext.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + +