mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Merged HEAD-BUG-FIX (Cloud33/4.3) to HEAD (Cloud33/4.3)
62933: Merged PLATFORM1 (Cloud33) to HEAD-BUG-FIX (Cloud33/4.3) 62763: ACE-403 Added tests for CronTriggerBean TODO: Add delayed test, Add delay capability git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@62988 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
125
source/test-java/org/alfresco/util/CronTriggerBeanTest.java
Normal file
125
source/test-java/org/alfresco/util/CronTriggerBeanTest.java
Normal file
@@ -0,0 +1,125 @@
|
||||
package org.alfresco.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.text.ParseException;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.quartz.CronTrigger;
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobDataMap;
|
||||
import org.quartz.JobDetail;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
import org.quartz.Scheduler;
|
||||
import org.quartz.SchedulerException;
|
||||
import org.quartz.Trigger;
|
||||
import org.quartz.impl.StdSchedulerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class CronTriggerBeanTest {
|
||||
|
||||
private ClassPathXmlApplicationContext ctx;
|
||||
private static int dummyJobRuns;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() throws Exception {
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownAfterClass() throws Exception {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
dummyJobRuns = 0;
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
try {
|
||||
ctx.close();
|
||||
} catch(Exception e) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCodedCronTriggerBean() throws Exception {
|
||||
assertEquals(0, dummyJobRuns);
|
||||
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
|
||||
scheduler.start();
|
||||
CronTriggerBean ctBean = new CronTriggerBean();
|
||||
ctBean.setBeanName("Dummy");
|
||||
ctBean.setCronExpression("0/1 * * * * ? *");
|
||||
ctBean.setEnabled(true);
|
||||
JobDetail jobDetail = new JobDetail("DummyJob", "DefaultGroup", DummyJob.class);
|
||||
ctBean.setJobDetail(jobDetail );
|
||||
ctBean.setScheduler(scheduler);
|
||||
ctBean.afterPropertiesSet();
|
||||
|
||||
Thread.sleep(1000);
|
||||
int runs = dummyJobRuns;
|
||||
assertTrue(runs > 0);
|
||||
|
||||
scheduler.shutdown();
|
||||
Thread.sleep(1000);
|
||||
assertEquals(runs, dummyJobRuns);
|
||||
Thread.sleep(1000);
|
||||
assertEquals(runs, dummyJobRuns);
|
||||
}
|
||||
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void testCodedDelayedCronTriggerBean() throws Exception {
|
||||
assertEquals(0, dummyJobRuns);
|
||||
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
|
||||
scheduler.start();
|
||||
CronTriggerBean ctBean = new CronTriggerBean();
|
||||
ctBean.setBeanName("Dummy");
|
||||
ctBean.setCronExpression("0/1 * * * * ? *");
|
||||
ctBean.setEnabled(true);
|
||||
JobDetail jobDetail = new JobDetail("DummyJob", "DefaultGroup", DummyJob.class);
|
||||
ctBean.setJobDetail(jobDetail );
|
||||
ctBean.setScheduler(scheduler);
|
||||
ctBean.afterPropertiesSet();
|
||||
|
||||
// Validate delayed runs
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfiguredCronTriggerBean() throws BeansException, Exception {
|
||||
assertEquals(0, dummyJobRuns);
|
||||
ctx = new ClassPathXmlApplicationContext("alfresco/scheduler-core-context.xml",
|
||||
"org/alfresco/util/test-scheduled-jobs-context.xml");
|
||||
|
||||
CronTriggerBean ctBean = ctx.getBean(CronTriggerBean.class);
|
||||
Scheduler scheduler = ctBean.getScheduler();
|
||||
scheduler.start();
|
||||
|
||||
Thread.sleep(1000);
|
||||
int runs = dummyJobRuns;
|
||||
assertTrue(runs > 0);
|
||||
|
||||
ctx.close();
|
||||
Thread.sleep(1000);
|
||||
assertEquals(runs, dummyJobRuns);
|
||||
Thread.sleep(1000);
|
||||
assertEquals(runs, dummyJobRuns);
|
||||
}
|
||||
|
||||
public static class DummyJob implements Job
|
||||
{
|
||||
public void execute(JobExecutionContext context) throws JobExecutionException
|
||||
{
|
||||
dummyJobRuns++;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
<?xml version='1.0' encoding='UTF-8'?>
|
||||
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
|
||||
|
||||
<beans>
|
||||
|
||||
|
||||
<bean id="dummyJobTest" class="org.springframework.scheduling.quartz.JobDetailBean">
|
||||
<property name="jobClass">
|
||||
<value>org.alfresco.util.CronTriggerBeanTest$DummyJob</value>
|
||||
</property>
|
||||
<property name="name">
|
||||
<value>dummy</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
|
||||
<property name="waitForJobsToCompleteOnShutdown">
|
||||
<value>true</value>
|
||||
</property>
|
||||
<property name="schedulerFactoryClass">
|
||||
<value>org.alfresco.repo.scheduler.AlfrescoSchedulerFactory</value>
|
||||
</property>
|
||||
<property name="jobFactory">
|
||||
<bean class="org.alfresco.repo.scheduler.AlfrescoJobFactory" />
|
||||
</property>
|
||||
<property name="quartzProperties">
|
||||
<ref bean="schedulerProperties" />
|
||||
</property>
|
||||
<property name="schedulerName">
|
||||
<value>DefaultScheduler</value>
|
||||
</property>
|
||||
<!-- Do not auto start the scheduler - this is done at the end of the bootstrap process -->
|
||||
<property name="autoStartup">
|
||||
<value>false</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
|
||||
<bean id="cronTriggerBean" class="org.alfresco.util.CronTriggerBean">
|
||||
<property name="jobDetail">
|
||||
<ref bean="dummyJobTest" />
|
||||
</property>
|
||||
<property name="scheduler">
|
||||
<ref bean="schedulerFactory" />
|
||||
</property>
|
||||
<!-- trigger at 3am each day -->
|
||||
<property name="cronExpression">
|
||||
<value>0/1 * * * * ?</value>
|
||||
</property>
|
||||
<property name="beanName">
|
||||
<value>dummy</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
|
||||
</beans>
|
Reference in New Issue
Block a user