Browse Source

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
pull/1/head
Mark Fisher 17 years ago
parent
commit
3796ab35ed
  1. 110
      org.springframework.context/src/test/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParserTests.java
  2. 21
      org.springframework.context/src/test/resources/org/springframework/scheduling/config/scheduledTasksContext.xml

110
org.springframework.context/src/test/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParserTests.java

@ -0,0 +1,110 @@ @@ -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<Runnable, Long> tasks = (Map<Runnable, Long>) 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<Runnable, Long> tasks = (Map<Runnable, Long>) new DirectFieldAccessor(
this.registrar).getPropertyValue("fixedRateTasks");
assertEquals(2, tasks.size());
Collection<Long> values = tasks.values();
assertTrue(values.contains(new Long(1000)));
assertTrue(values.contains(new Long(2000)));
}
@Test
public void fixedDelayTasks() {
Map<Runnable, Long> tasks = (Map<Runnable, Long>) 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<Runnable, String> tasks = (Map<Runnable, String>) 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() {
}
}
}

21
org.springframework.context/src/test/resources/org/springframework/scheduling/config/scheduledTasksContext.xml

@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<task:scheduled-tasks scheduler="testScheduler">
<task:task ref="testBean" method="test" fixed-rate="1000"/>
<task:task ref="testBean" method="test" fixed-rate="2000"/>
<task:task ref="testBean" method="test" fixed-delay="3000"/>
<task:task ref="testBean" method="test" cron="*/4 * 9-17 * * MON-FRI"/>
</task:scheduled-tasks>
<task:scheduler id="testScheduler"/>
<bean id="testBean" class="org.springframework.scheduling.config.ScheduledTasksBeanDefinitionParserTests$TestBean"/>
</beans>
Loading…
Cancel
Save