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)
62934: Merged PLATFORM1 (Cloud33) to HEAD-BUG-FIX (Cloud33/4.3) 62832: ACE-403 Added tests for CronTriggerBean TODO: Add configured delayed test git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@62989 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -2,32 +2,28 @@ package org.alfresco.util;
|
|||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.quartz.CronTrigger;
|
|
||||||
import org.quartz.Job;
|
import org.quartz.Job;
|
||||||
import org.quartz.JobDataMap;
|
|
||||||
import org.quartz.JobDetail;
|
import org.quartz.JobDetail;
|
||||||
import org.quartz.JobExecutionContext;
|
import org.quartz.JobExecutionContext;
|
||||||
import org.quartz.JobExecutionException;
|
import org.quartz.JobExecutionException;
|
||||||
import org.quartz.Scheduler;
|
import org.quartz.Scheduler;
|
||||||
import org.quartz.SchedulerException;
|
|
||||||
import org.quartz.Trigger;
|
|
||||||
import org.quartz.impl.StdSchedulerFactory;
|
import org.quartz.impl.StdSchedulerFactory;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
public class CronTriggerBeanTest {
|
public class CronTriggerBeanTest {
|
||||||
|
|
||||||
private ClassPathXmlApplicationContext ctx;
|
private ClassPathXmlApplicationContext ctx;
|
||||||
private static int dummyJobRuns;
|
private Scheduler scheduler;
|
||||||
|
private static ArrayList<Long> dummyJobRuns;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setUpBeforeClass() throws Exception {
|
public static void setUpBeforeClass() throws Exception {
|
||||||
}
|
}
|
||||||
@@ -38,88 +34,112 @@ public class CronTriggerBeanTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
dummyJobRuns = 0;
|
dummyJobRuns = new ArrayList<>();
|
||||||
|
this.ctx = null;
|
||||||
|
this.scheduler = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() throws Exception {
|
public void tearDown() throws Exception {
|
||||||
|
try {
|
||||||
|
this.scheduler.shutdown();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ctx.close();
|
ctx.close();
|
||||||
} catch(Exception e) {
|
} catch (Exception e) {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCodedCronTriggerBean() throws Exception {
|
public void testCodedCronTriggerBean() throws Exception {
|
||||||
assertEquals(0, dummyJobRuns);
|
assertEquals(0, dummyJobRuns.size());
|
||||||
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
|
scheduler = StdSchedulerFactory.getDefaultScheduler();
|
||||||
scheduler.start();
|
scheduler.start();
|
||||||
CronTriggerBean ctBean = new CronTriggerBean();
|
CronTriggerBean ctBean = new CronTriggerBean();
|
||||||
ctBean.setBeanName("Dummy");
|
ctBean.setBeanName("Dummy");
|
||||||
ctBean.setCronExpression("0/1 * * * * ? *");
|
ctBean.setCronExpression("0/1 * * * * ? *");
|
||||||
ctBean.setEnabled(true);
|
ctBean.setEnabled(true);
|
||||||
JobDetail jobDetail = new JobDetail("DummyJob", "DefaultGroup", DummyJob.class);
|
JobDetail jobDetail = new JobDetail("DummyJob", "DefaultGroup", DummyJob.class);
|
||||||
ctBean.setJobDetail(jobDetail );
|
ctBean.setJobDetail(jobDetail);
|
||||||
ctBean.setScheduler(scheduler);
|
ctBean.setScheduler(scheduler);
|
||||||
ctBean.afterPropertiesSet();
|
ctBean.afterPropertiesSet();
|
||||||
|
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
int runs = dummyJobRuns;
|
int runs = dummyJobRuns.size();
|
||||||
assertTrue(runs > 0);
|
assertTrue(runs > 0);
|
||||||
|
|
||||||
scheduler.shutdown();
|
scheduler.shutdown();
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
assertEquals(runs, dummyJobRuns);
|
assertEquals(runs, dummyJobRuns.size());
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
assertEquals(runs, dummyJobRuns);
|
assertEquals(runs, dummyJobRuns.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Ignore
|
|
||||||
@Test
|
@Test
|
||||||
public void testCodedDelayedCronTriggerBean() throws Exception {
|
public void testCodedDelayedCronTriggerBean() throws Exception {
|
||||||
assertEquals(0, dummyJobRuns);
|
assertEquals(0, dummyJobRuns.size());
|
||||||
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
|
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
|
||||||
scheduler.start();
|
scheduler.start();
|
||||||
CronTriggerBean ctBean = new CronTriggerBean();
|
CronTriggerBean ctBean = new CronTriggerBean();
|
||||||
ctBean.setBeanName("Dummy");
|
ctBean.setBeanName("Dummy");
|
||||||
ctBean.setCronExpression("0/1 * * * * ? *");
|
ctBean.setCronExpression("0/1 * * * * ? *");
|
||||||
ctBean.setEnabled(true);
|
ctBean.setEnabled(true);
|
||||||
JobDetail jobDetail = new JobDetail("DummyJob", "DefaultGroup", DummyJob.class);
|
JobDetail jobDetail = new JobDetail("DummyJob", "DefaultGroup", DummyJob.class);
|
||||||
ctBean.setJobDetail(jobDetail );
|
ctBean.setJobDetail(jobDetail);
|
||||||
ctBean.setScheduler(scheduler);
|
ctBean.setScheduler(scheduler);
|
||||||
|
final long START_DELAY = 4000L;
|
||||||
|
ctBean.setStartDelay(START_DELAY);
|
||||||
ctBean.afterPropertiesSet();
|
ctBean.afterPropertiesSet();
|
||||||
|
|
||||||
// Validate delayed runs
|
// It should not have run during the delay. Give a second precision leeway
|
||||||
|
Thread.sleep(START_DELAY - 1000);
|
||||||
|
assertEquals(0, dummyJobRuns.size());
|
||||||
|
// It should have had a chance to run after the delay
|
||||||
|
Thread.sleep(1000);
|
||||||
|
int runs = dummyJobRuns.size();
|
||||||
|
assertTrue(runs > 0);
|
||||||
|
// It should not have run again after shutdown
|
||||||
|
scheduler.shutdown();
|
||||||
|
Thread.sleep(1000);
|
||||||
|
assertEquals(runs, dummyJobRuns.size());
|
||||||
|
// Indeed after another second, it should not have changed.
|
||||||
|
Thread.sleep(1000);
|
||||||
|
assertEquals(runs, dummyJobRuns.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testConfiguredCronTriggerBean() throws BeansException, Exception {
|
public void testConfiguredCronTriggerBean() throws BeansException,
|
||||||
assertEquals(0, dummyJobRuns);
|
Exception {
|
||||||
ctx = new ClassPathXmlApplicationContext("alfresco/scheduler-core-context.xml",
|
assertEquals(0, dummyJobRuns.size());
|
||||||
"org/alfresco/util/test-scheduled-jobs-context.xml");
|
ctx = new ClassPathXmlApplicationContext(
|
||||||
|
"alfresco/scheduler-core-context.xml",
|
||||||
CronTriggerBean ctBean = ctx.getBean(CronTriggerBean.class);
|
"org/alfresco/util/test-scheduled-jobs-context.xml");
|
||||||
Scheduler scheduler = ctBean.getScheduler();
|
CronTriggerBean ctBean = ctx.getBean(CronTriggerBean.class);
|
||||||
scheduler.start();
|
scheduler = ctBean.getScheduler();
|
||||||
|
scheduler.start();
|
||||||
|
|
||||||
Thread.sleep(1000);
|
// After the interval, there should be at least one run
|
||||||
int runs = dummyJobRuns;
|
final long INTERVAL = 2000L;
|
||||||
assertTrue(runs > 0);
|
Thread.sleep(INTERVAL);
|
||||||
|
int runs = dummyJobRuns.size();
|
||||||
|
assertTrue(runs > 0);
|
||||||
|
|
||||||
ctx.close();
|
// When the context closes, the scheduler should close, thereby stopping the job
|
||||||
Thread.sleep(1000);
|
ctx.close();
|
||||||
assertEquals(runs, dummyJobRuns);
|
Thread.sleep(INTERVAL);
|
||||||
Thread.sleep(1000);
|
assertEquals(runs, dummyJobRuns.size());
|
||||||
assertEquals(runs, dummyJobRuns);
|
Thread.sleep(INTERVAL);
|
||||||
|
assertEquals(runs, dummyJobRuns.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class DummyJob implements Job
|
public static class DummyJob implements Job {
|
||||||
{
|
public void execute(JobExecutionContext context) throws JobExecutionException {
|
||||||
public void execute(JobExecutionContext context) throws JobExecutionException
|
long now = System.currentTimeMillis();
|
||||||
{
|
dummyJobRuns.add(now);
|
||||||
dummyJobRuns++;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -45,10 +45,10 @@
|
|||||||
</property>
|
</property>
|
||||||
<!-- trigger at 3am each day -->
|
<!-- trigger at 3am each day -->
|
||||||
<property name="cronExpression">
|
<property name="cronExpression">
|
||||||
<value>0/1 * * * * ?</value>
|
<value>0/2 * * * * ?</value>
|
||||||
</property>
|
</property>
|
||||||
<property name="beanName">
|
<property name="beanName">
|
||||||
<value>dummy</value>
|
<value>dummyTriggerBean</value>
|
||||||
</property>
|
</property>
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user