From d4b4d1c015fe3c5f5017f4da2726ffd1165a0c7c Mon Sep 17 00:00:00 2001 From: Alan Davis Date: Thu, 20 Feb 2014 15:00:54 +0000 Subject: [PATCH] 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 --- .../alfresco/util/CronTriggerBeanTest.java | 125 ++++++++++++++++++ .../util/test-scheduled-jobs-context.xml | 56 ++++++++ 2 files changed, 181 insertions(+) create mode 100644 source/test-java/org/alfresco/util/CronTriggerBeanTest.java create mode 100644 source/test-resources/org/alfresco/util/test-scheduled-jobs-context.xml diff --git a/source/test-java/org/alfresco/util/CronTriggerBeanTest.java b/source/test-java/org/alfresco/util/CronTriggerBeanTest.java new file mode 100644 index 0000000000..fc82288fb0 --- /dev/null +++ b/source/test-java/org/alfresco/util/CronTriggerBeanTest.java @@ -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++; + } + } +} diff --git a/source/test-resources/org/alfresco/util/test-scheduled-jobs-context.xml b/source/test-resources/org/alfresco/util/test-scheduled-jobs-context.xml new file mode 100644 index 0000000000..c8d3fb7967 --- /dev/null +++ b/source/test-resources/org/alfresco/util/test-scheduled-jobs-context.xml @@ -0,0 +1,56 @@ + + + + + + + + + org.alfresco.util.CronTriggerBeanTest$DummyJob + + + dummy + + + + + + true + + + org.alfresco.repo.scheduler.AlfrescoSchedulerFactory + + + + + + + + + DefaultScheduler + + + + false + + + + + + + + + + + + + + 0/1 * * * * ? + + + dummy + + + + +