ScheduledTasksBeanDefinitionParserTests.java 4.0 KB
Newer Older
1
/*
2
 * Copyright 2002-2012 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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;

J
Juergen Hoeller 已提交
19
import java.lang.reflect.Method;
20
import java.util.Collection;
21
import java.util.Date;
22 23 24 25 26 27 28 29
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;
30 31
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
J
Juergen Hoeller 已提交
32
import org.springframework.scheduling.support.ScheduledMethodRunnable;
33

34 35
import static org.junit.Assert.*;

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/**
 * @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();
J
Juergen Hoeller 已提交
70 71 72
		assertEquals(ScheduledMethodRunnable.class, runnable.getClass());
		Object targetObject = ((ScheduledMethodRunnable) runnable).getTarget();
		Method targetMethod = ((ScheduledMethodRunnable) runnable).getMethod();
73
		assertEquals(this.testBean, targetObject);
J
Juergen Hoeller 已提交
74
		assertEquals("test", targetMethod.getName());
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	}

	@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);		
	}

105 106 107 108 109 110 111 112 113
	@Test
	public void triggerTasks() {
		Map<Runnable, Trigger> tasks = (Map<Runnable, Trigger>) new DirectFieldAccessor(
				this.registrar).getPropertyValue("triggerTasks");
		assertEquals(1, tasks.size());
		Trigger trigger = tasks.values().iterator().next();
		assertEquals(TestTrigger.class, trigger.getClass());		
	}

114 115 116 117 118

	static class TestBean {

		public void test() {
		}
119 120
	}

121

122 123 124 125 126
	static class TestTrigger implements Trigger {

		public Date nextExecutionTime(TriggerContext triggerContext) {
			return null;
		}
127 128 129
	}

}