use of org.springframework.beans.DirectFieldAccessor in project spring-framework by spring-projects.
the class ScheduledAnnotationBeanPostProcessorTests method cronTask.
@Test
public void cronTask() throws InterruptedException {
Assume.group(TestGroup.LONG_RUNNING);
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
BeanDefinition targetDefinition = new RootBeanDefinition(CronTestBean.class);
context.registerBeanDefinition("postProcessor", processorDefinition);
context.registerBeanDefinition("target", targetDefinition);
context.refresh();
Object postProcessor = context.getBean("postProcessor");
Object target = context.getBean("target");
ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar) new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
@SuppressWarnings("unchecked") List<CronTask> cronTasks = (List<CronTask>) new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
assertEquals(1, cronTasks.size());
CronTask task = cronTasks.get(0);
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
Object targetObject = runnable.getTarget();
Method targetMethod = runnable.getMethod();
assertEquals(target, targetObject);
assertEquals("cron", targetMethod.getName());
assertEquals("*/7 * * * * ?", task.getExpression());
Thread.sleep(10000);
}
use of org.springframework.beans.DirectFieldAccessor in project spring-framework by spring-projects.
the class ScheduledAnnotationBeanPostProcessorTests method propertyPlaceholderWithCron.
@Test
public void propertyPlaceholderWithCron() {
String businessHoursCronExpression = "0 0 9-17 * * MON-FRI";
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
BeanDefinition placeholderDefinition = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
Properties properties = new Properties();
properties.setProperty("schedules.businessHours", businessHoursCronExpression);
placeholderDefinition.getPropertyValues().addPropertyValue("properties", properties);
BeanDefinition targetDefinition = new RootBeanDefinition(PropertyPlaceholderWithCronTestBean.class);
context.registerBeanDefinition("postProcessor", processorDefinition);
context.registerBeanDefinition("placeholder", placeholderDefinition);
context.registerBeanDefinition("target", targetDefinition);
context.refresh();
Object postProcessor = context.getBean("postProcessor");
Object target = context.getBean("target");
ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar) new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
@SuppressWarnings("unchecked") List<CronTask> cronTasks = (List<CronTask>) new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
assertEquals(1, cronTasks.size());
CronTask task = cronTasks.get(0);
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
Object targetObject = runnable.getTarget();
Method targetMethod = runnable.getMethod();
assertEquals(target, targetObject);
assertEquals("x", targetMethod.getName());
assertEquals(businessHoursCronExpression, task.getExpression());
}
use of org.springframework.beans.DirectFieldAccessor in project spring-framework by spring-projects.
the class ScheduledAnnotationBeanPostProcessorTests method severalFixedRates.
private void severalFixedRates(StaticApplicationContext context, BeanDefinition processorDefinition, BeanDefinition targetDefinition) {
context.registerBeanDefinition("postProcessor", processorDefinition);
context.registerBeanDefinition("target", targetDefinition);
context.refresh();
Object postProcessor = context.getBean("postProcessor");
Object target = context.getBean("target");
ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar) new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
@SuppressWarnings("unchecked") List<IntervalTask> fixedRateTasks = (List<IntervalTask>) new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
assertEquals(2, fixedRateTasks.size());
IntervalTask task1 = fixedRateTasks.get(0);
ScheduledMethodRunnable runnable1 = (ScheduledMethodRunnable) task1.getRunnable();
Object targetObject = runnable1.getTarget();
Method targetMethod = runnable1.getMethod();
assertEquals(target, targetObject);
assertEquals("fixedRate", targetMethod.getName());
assertEquals(0, task1.getInitialDelay());
assertEquals(4000L, task1.getInterval());
IntervalTask task2 = fixedRateTasks.get(1);
ScheduledMethodRunnable runnable2 = (ScheduledMethodRunnable) task2.getRunnable();
targetObject = runnable2.getTarget();
targetMethod = runnable2.getMethod();
assertEquals(target, targetObject);
assertEquals("fixedRate", targetMethod.getName());
assertEquals(2000L, task2.getInitialDelay());
assertEquals(4000L, task2.getInterval());
}
use of org.springframework.beans.DirectFieldAccessor in project spring-framework by spring-projects.
the class SchedulerBeanDefinitionParserTests method customScheduler.
@Test
public void customScheduler() {
ThreadPoolTaskScheduler scheduler = (ThreadPoolTaskScheduler) this.context.getBean("customScheduler");
Integer size = (Integer) new DirectFieldAccessor(scheduler).getPropertyValue("poolSize");
assertEquals(new Integer(42), size);
}
use of org.springframework.beans.DirectFieldAccessor in project spring-boot by spring-projects.
the class RabbitAutoConfigurationTests method testConnectionFactoryPublisherSettings.
@Test
public void testConnectionFactoryPublisherSettings() {
load(TestConfiguration.class, "spring.rabbitmq.publisher-confirms=true", "spring.rabbitmq.publisher-returns=true");
CachingConnectionFactory connectionFactory = this.context.getBean(CachingConnectionFactory.class);
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
assertThat(dfa.getPropertyValue("publisherConfirms")).isEqualTo(true);
assertThat(dfa.getPropertyValue("publisherReturns")).isEqualTo(true);
assertThat(getMandatory(rabbitTemplate)).isTrue();
}
Aggregations