Skip to content

Commit

Permalink
Fix broken tests in QuartzSupportTests
Browse files Browse the repository at this point in the history
This commit ensures that QuartzSupportTests and its related
configuration are compatible with Quartz 2.1.7.

 - Test jobs are now durable where required.

 - Deleted legacy tests that attempted to use a Runnable instead of a
   Job as a jobClass for a JobDetail.

 - Replaced quartz-hsql.sql with current version for Quartz 2.1.7.

Issue: SPR-11630
  • Loading branch information
sbrannen committed Mar 30, 2014
1 parent 5d049e0 commit 906321d
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.junit.Test;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
Expand Down Expand Up @@ -56,7 +58,7 @@
public class QuartzSupportTests {

@Test
public void testSchedulerFactoryBeanWithApplicationContext() throws Exception {
public void schedulerFactoryBeanWithApplicationContext() throws Exception {
TestBean tb = new TestBean("tb", 99);
StaticApplicationContext ac = new StaticApplicationContext();

Expand Down Expand Up @@ -92,13 +94,14 @@ protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String sc
}

@Test
public void testSchedulerWithTaskExecutor() throws Exception {
public void schedulerWithTaskExecutor() throws Exception {
Assume.group(TestGroup.PERFORMANCE);

CountingTaskExecutor taskExecutor = new CountingTaskExecutor();
DummyJob.count = 0;

JobDetailImpl jobDetail = new JobDetailImpl();
jobDetail.setDurability(true);
jobDetail.setJobClass(DummyJob.class);
jobDetail.setName("myJob");

Expand All @@ -124,44 +127,22 @@ public void testSchedulerWithTaskExecutor() throws Exception {
bean.destroy();
}

@Test
public void testSchedulerWithRunnable() throws Exception {
Assume.group(TestGroup.PERFORMANCE);

DummyRunnable.count = 0;

@Test(expected = IllegalArgumentException.class)
@SuppressWarnings({ "unchecked", "rawtypes" })
public void jobDetailWithRunnableInsteadOfJob() {
JobDetailImpl jobDetail = new JobDetailImpl();
jobDetail.setJobClass((Class) DummyRunnable.class);
jobDetail.setName("myJob");

SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
trigger.setName("myTrigger");
trigger.setJobDetail(jobDetail);
trigger.setStartDelay(1);
trigger.setRepeatInterval(500);
trigger.setRepeatCount(1);
trigger.afterPropertiesSet();

SchedulerFactoryBean bean = new SchedulerFactoryBean();
bean.setTriggers(trigger.getObject());
bean.setJobDetails(jobDetail);
bean.afterPropertiesSet();
bean.start();

Thread.sleep(500);
assertTrue(DummyRunnable.count > 0);

bean.destroy();
}

@Test
public void testSchedulerWithQuartzJobBean() throws Exception {
public void schedulerWithQuartzJobBean() throws Exception {
Assume.group(TestGroup.PERFORMANCE);

DummyJob.param = 0;
DummyJob.count = 0;

JobDetailImpl jobDetail = new JobDetailImpl();
jobDetail.setDurability(true);
jobDetail.setJobClass(DummyJobBean.class);
jobDetail.setName("myJob");
jobDetail.getJobDataMap().put("param", "10");
Expand All @@ -188,13 +169,14 @@ public void testSchedulerWithQuartzJobBean() throws Exception {
}

@Test
public void testSchedulerWithSpringBeanJobFactory() throws Exception {
public void schedulerWithSpringBeanJobFactory() throws Exception {
Assume.group(TestGroup.PERFORMANCE);

DummyJob.param = 0;
DummyJob.count = 0;

JobDetailImpl jobDetail = new JobDetailImpl();
jobDetail.setDurability(true);
jobDetail.setJobClass(DummyJob.class);
jobDetail.setName("myJob");
jobDetail.getJobDataMap().put("param", "10");
Expand Down Expand Up @@ -223,13 +205,14 @@ public void testSchedulerWithSpringBeanJobFactory() throws Exception {
}

@Test
public void testSchedulerWithSpringBeanJobFactoryAndParamMismatchNotIgnored() throws Exception {
public void schedulerWithSpringBeanJobFactoryAndParamMismatchNotIgnored() throws Exception {
Assume.group(TestGroup.PERFORMANCE);

DummyJob.param = 0;
DummyJob.count = 0;

JobDetailImpl jobDetail = new JobDetailImpl();
jobDetail.setDurability(true);
jobDetail.setJobClass(DummyJob.class);
jobDetail.setName("myJob");
jobDetail.getJobDataMap().put("para", "10");
Expand Down Expand Up @@ -259,46 +242,13 @@ public void testSchedulerWithSpringBeanJobFactoryAndParamMismatchNotIgnored() th
}

@Test
public void testSchedulerWithSpringBeanJobFactoryAndRunnable() throws Exception {
Assume.group(TestGroup.PERFORMANCE);

DummyRunnable.param = 0;
DummyRunnable.count = 0;

JobDetailImpl jobDetail = new JobDetailImpl();
jobDetail.setJobClass((Class) DummyRunnable.class);
jobDetail.setName("myJob");
jobDetail.getJobDataMap().put("param", "10");

SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
trigger.setName("myTrigger");
trigger.setJobDetail(jobDetail);
trigger.setStartDelay(1);
trigger.setRepeatInterval(500);
trigger.setRepeatCount(1);
trigger.afterPropertiesSet();

SchedulerFactoryBean bean = new SchedulerFactoryBean();
bean.setJobFactory(new SpringBeanJobFactory());
bean.setTriggers(trigger.getObject());
bean.setJobDetails(jobDetail);
bean.afterPropertiesSet();
bean.start();

Thread.sleep(500);
assertEquals(10, DummyRunnable.param);
assertTrue(DummyRunnable.count > 0);

bean.destroy();
}

@Test
public void testSchedulerWithSpringBeanJobFactoryAndQuartzJobBean() throws Exception {
public void schedulerWithSpringBeanJobFactoryAndQuartzJobBean() throws Exception {
Assume.group(TestGroup.PERFORMANCE);
DummyJobBean.param = 0;
DummyJobBean.count = 0;

JobDetailImpl jobDetail = new JobDetailImpl();
jobDetail.setDurability(true);
jobDetail.setJobClass(DummyJobBean.class);
jobDetail.setName("myJob");
jobDetail.getJobDataMap().put("param", "10");
Expand Down Expand Up @@ -326,7 +276,7 @@ public void testSchedulerWithSpringBeanJobFactoryAndQuartzJobBean() throws Excep
}

@Test
public void testSchedulerWithSpringBeanJobFactoryAndJobSchedulingData() throws Exception {
public void schedulerWithSpringBeanJobFactoryAndJobSchedulingData() throws Exception {
Assume.group(TestGroup.PERFORMANCE);
DummyJob.param = 0;
DummyJob.count = 0;
Expand All @@ -348,9 +298,8 @@ public void testSchedulerWithSpringBeanJobFactoryAndJobSchedulingData() throws E
* Tests the creation of multiple schedulers (SPR-772)
*/
@Test
public void testMultipleSchedulers() throws Exception {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("/org/springframework/scheduling/quartz/multipleSchedulers.xml");
public void multipleSchedulers() throws Exception {
ClassPathXmlApplicationContext ctx = context("multipleSchedulers.xml");
try {
Scheduler scheduler1 = (Scheduler) ctx.getBean("scheduler1");
Scheduler scheduler2 = (Scheduler) ctx.getBean("scheduler2");
Expand All @@ -364,10 +313,9 @@ public void testMultipleSchedulers() throws Exception {
}

@Test
public void testWithTwoAnonymousMethodInvokingJobDetailFactoryBeans() throws InterruptedException {
public void twoAnonymousMethodInvokingJobDetailFactoryBeans() throws Exception {
Assume.group(TestGroup.PERFORMANCE);
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("/org/springframework/scheduling/quartz/multipleAnonymousMethodInvokingJobDetailFB.xml");
ClassPathXmlApplicationContext ctx = context("multipleAnonymousMethodInvokingJobDetailFB.xml");
Thread.sleep(3000);
try {
QuartzTestBean exportService = (QuartzTestBean) ctx.getBean("exportService");
Expand All @@ -384,10 +332,9 @@ public void testWithTwoAnonymousMethodInvokingJobDetailFactoryBeans() throws Int
}

@Test
public void testSchedulerAccessorBean() throws InterruptedException {
public void schedulerAccessorBean() throws Exception {
Assume.group(TestGroup.PERFORMANCE);
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("/org/springframework/scheduling/quartz/schedulerAccessorBean.xml");
ClassPathXmlApplicationContext ctx = context("schedulerAccessorBean.xml");
Thread.sleep(3000);
try {
QuartzTestBean exportService = (QuartzTestBean) ctx.getBean("exportService");
Expand All @@ -405,7 +352,7 @@ public void testSchedulerAccessorBean() throws InterruptedException {

@Test
@SuppressWarnings("resource")
public void testSchedulerAutoStartsOnContextRefreshedEventByDefault() throws Exception {
public void schedulerAutoStartsOnContextRefreshedEventByDefault() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
context.registerBeanDefinition("scheduler", new RootBeanDefinition(SchedulerFactoryBean.class));
Scheduler bean = context.getBean("scheduler", Scheduler.class);
Expand All @@ -416,7 +363,7 @@ public void testSchedulerAutoStartsOnContextRefreshedEventByDefault() throws Exc

@Test
@SuppressWarnings("resource")
public void testSchedulerAutoStartupFalse() throws Exception {
public void schedulerAutoStartupFalse() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(
SchedulerFactoryBean.class).addPropertyValue("autoStartup", false).getBeanDefinition();
Expand All @@ -428,35 +375,38 @@ public void testSchedulerAutoStartupFalse() throws Exception {
}

@Test
public void testSchedulerRepositoryExposure() throws InterruptedException {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("/org/springframework/scheduling/quartz/schedulerRepositoryExposure.xml");
public void schedulerRepositoryExposure() throws Exception {
ClassPathXmlApplicationContext ctx = context("schedulerRepositoryExposure.xml");
assertSame(SchedulerRepository.getInstance().lookup("myScheduler"), ctx.getBean("scheduler"));
ctx.close();
}

// SPR-6038: detect HSQL and stop illegal locks being taken
/**
* SPR-6038: detect HSQL and stop illegal locks being taken.
*/
@Test
public void testSchedulerWithHsqlDataSource() throws Exception {
public void schedulerWithHsqlDataSource() throws Exception {
Assume.group(TestGroup.PERFORMANCE);

DummyJob.param = 0;
DummyJob.count = 0;

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"/org/springframework/scheduling/quartz/databasePersistence.xml");
ClassPathXmlApplicationContext ctx = context("databasePersistence.xml");
JdbcTemplate jdbcTemplate = new JdbcTemplate(ctx.getBean(DataSource.class));
assertTrue("No triggers were persisted", jdbcTemplate.queryForList("SELECT * FROM qrtz_triggers").size()>0);
Thread.sleep(3000);
try {
// assertEquals(10, DummyJob.param);
assertTrue(DummyJob.count > 0);
}
finally {
ctx.close();
}
}

private ClassPathXmlApplicationContext context(String path) {
return new ClassPathXmlApplicationContext(path, getClass());
}


public static class CountingTaskExecutor implements TaskExecutor {

Expand Down Expand Up @@ -512,20 +462,9 @@ protected synchronized void executeInternal(JobExecutionContext jobExecutionCont

public static class DummyRunnable implements Runnable {

private static int param;

private static int count;

public void setParam(int value) {
if (param > 0) {
throw new IllegalStateException("Param already set");
}
param = value;
}

@Override
public void run() {
count++;
/* no-op */
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers" ref="trigger" />
<property name="dataSource" ref="dataSource"/>
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="trigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
Expand All @@ -20,8 +19,8 @@
<entry key="param" value="10" />
</map>
</property>
<property name="jobClass"
value="org.springframework.scheduling.quartz.QuartzSupportTests$DummyJob" />
<property name="jobClass" value="org.springframework.scheduling.quartz.QuartzSupportTests$DummyJob" />
<property name="durability" value="true" />
</bean>
</property>
</bean>
Expand Down
Loading

0 comments on commit 906321d

Please sign in to comment.