Skip to content

Commit 906321d

Browse files
committed
Fix broken tests in QuartzSupportTests
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
1 parent 5d049e0 commit 906321d

File tree

3 files changed

+110
-160
lines changed

3 files changed

+110
-160
lines changed

spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSupportTests.java

Lines changed: 37 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
import java.util.HashMap;
2020
import java.util.Map;
21+
2122
import javax.sql.DataSource;
2223

2324
import org.junit.Test;
25+
2426
import org.quartz.Job;
2527
import org.quartz.JobExecutionContext;
2628
import org.quartz.JobExecutionException;
@@ -56,7 +58,7 @@
5658
public class QuartzSupportTests {
5759

5860
@Test
59-
public void testSchedulerFactoryBeanWithApplicationContext() throws Exception {
61+
public void schedulerFactoryBeanWithApplicationContext() throws Exception {
6062
TestBean tb = new TestBean("tb", 99);
6163
StaticApplicationContext ac = new StaticApplicationContext();
6264

@@ -92,13 +94,14 @@ protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String sc
9294
}
9395

9496
@Test
95-
public void testSchedulerWithTaskExecutor() throws Exception {
97+
public void schedulerWithTaskExecutor() throws Exception {
9698
Assume.group(TestGroup.PERFORMANCE);
9799

98100
CountingTaskExecutor taskExecutor = new CountingTaskExecutor();
99101
DummyJob.count = 0;
100102

101103
JobDetailImpl jobDetail = new JobDetailImpl();
104+
jobDetail.setDurability(true);
102105
jobDetail.setJobClass(DummyJob.class);
103106
jobDetail.setName("myJob");
104107

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

127-
@Test
128-
public void testSchedulerWithRunnable() throws Exception {
129-
Assume.group(TestGroup.PERFORMANCE);
130-
131-
DummyRunnable.count = 0;
132-
130+
@Test(expected = IllegalArgumentException.class)
131+
@SuppressWarnings({ "unchecked", "rawtypes" })
132+
public void jobDetailWithRunnableInsteadOfJob() {
133133
JobDetailImpl jobDetail = new JobDetailImpl();
134134
jobDetail.setJobClass((Class) DummyRunnable.class);
135-
jobDetail.setName("myJob");
136-
137-
SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
138-
trigger.setName("myTrigger");
139-
trigger.setJobDetail(jobDetail);
140-
trigger.setStartDelay(1);
141-
trigger.setRepeatInterval(500);
142-
trigger.setRepeatCount(1);
143-
trigger.afterPropertiesSet();
144-
145-
SchedulerFactoryBean bean = new SchedulerFactoryBean();
146-
bean.setTriggers(trigger.getObject());
147-
bean.setJobDetails(jobDetail);
148-
bean.afterPropertiesSet();
149-
bean.start();
150-
151-
Thread.sleep(500);
152-
assertTrue(DummyRunnable.count > 0);
153-
154-
bean.destroy();
155135
}
156136

157137
@Test
158-
public void testSchedulerWithQuartzJobBean() throws Exception {
138+
public void schedulerWithQuartzJobBean() throws Exception {
159139
Assume.group(TestGroup.PERFORMANCE);
160140

161141
DummyJob.param = 0;
162142
DummyJob.count = 0;
163143

164144
JobDetailImpl jobDetail = new JobDetailImpl();
145+
jobDetail.setDurability(true);
165146
jobDetail.setJobClass(DummyJobBean.class);
166147
jobDetail.setName("myJob");
167148
jobDetail.getJobDataMap().put("param", "10");
@@ -188,13 +169,14 @@ public void testSchedulerWithQuartzJobBean() throws Exception {
188169
}
189170

190171
@Test
191-
public void testSchedulerWithSpringBeanJobFactory() throws Exception {
172+
public void schedulerWithSpringBeanJobFactory() throws Exception {
192173
Assume.group(TestGroup.PERFORMANCE);
193174

194175
DummyJob.param = 0;
195176
DummyJob.count = 0;
196177

197178
JobDetailImpl jobDetail = new JobDetailImpl();
179+
jobDetail.setDurability(true);
198180
jobDetail.setJobClass(DummyJob.class);
199181
jobDetail.setName("myJob");
200182
jobDetail.getJobDataMap().put("param", "10");
@@ -223,13 +205,14 @@ public void testSchedulerWithSpringBeanJobFactory() throws Exception {
223205
}
224206

225207
@Test
226-
public void testSchedulerWithSpringBeanJobFactoryAndParamMismatchNotIgnored() throws Exception {
208+
public void schedulerWithSpringBeanJobFactoryAndParamMismatchNotIgnored() throws Exception {
227209
Assume.group(TestGroup.PERFORMANCE);
228210

229211
DummyJob.param = 0;
230212
DummyJob.count = 0;
231213

232214
JobDetailImpl jobDetail = new JobDetailImpl();
215+
jobDetail.setDurability(true);
233216
jobDetail.setJobClass(DummyJob.class);
234217
jobDetail.setName("myJob");
235218
jobDetail.getJobDataMap().put("para", "10");
@@ -259,46 +242,13 @@ public void testSchedulerWithSpringBeanJobFactoryAndParamMismatchNotIgnored() th
259242
}
260243

261244
@Test
262-
public void testSchedulerWithSpringBeanJobFactoryAndRunnable() throws Exception {
263-
Assume.group(TestGroup.PERFORMANCE);
264-
265-
DummyRunnable.param = 0;
266-
DummyRunnable.count = 0;
267-
268-
JobDetailImpl jobDetail = new JobDetailImpl();
269-
jobDetail.setJobClass((Class) DummyRunnable.class);
270-
jobDetail.setName("myJob");
271-
jobDetail.getJobDataMap().put("param", "10");
272-
273-
SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
274-
trigger.setName("myTrigger");
275-
trigger.setJobDetail(jobDetail);
276-
trigger.setStartDelay(1);
277-
trigger.setRepeatInterval(500);
278-
trigger.setRepeatCount(1);
279-
trigger.afterPropertiesSet();
280-
281-
SchedulerFactoryBean bean = new SchedulerFactoryBean();
282-
bean.setJobFactory(new SpringBeanJobFactory());
283-
bean.setTriggers(trigger.getObject());
284-
bean.setJobDetails(jobDetail);
285-
bean.afterPropertiesSet();
286-
bean.start();
287-
288-
Thread.sleep(500);
289-
assertEquals(10, DummyRunnable.param);
290-
assertTrue(DummyRunnable.count > 0);
291-
292-
bean.destroy();
293-
}
294-
295-
@Test
296-
public void testSchedulerWithSpringBeanJobFactoryAndQuartzJobBean() throws Exception {
245+
public void schedulerWithSpringBeanJobFactoryAndQuartzJobBean() throws Exception {
297246
Assume.group(TestGroup.PERFORMANCE);
298247
DummyJobBean.param = 0;
299248
DummyJobBean.count = 0;
300249

301250
JobDetailImpl jobDetail = new JobDetailImpl();
251+
jobDetail.setDurability(true);
302252
jobDetail.setJobClass(DummyJobBean.class);
303253
jobDetail.setName("myJob");
304254
jobDetail.getJobDataMap().put("param", "10");
@@ -326,7 +276,7 @@ public void testSchedulerWithSpringBeanJobFactoryAndQuartzJobBean() throws Excep
326276
}
327277

328278
@Test
329-
public void testSchedulerWithSpringBeanJobFactoryAndJobSchedulingData() throws Exception {
279+
public void schedulerWithSpringBeanJobFactoryAndJobSchedulingData() throws Exception {
330280
Assume.group(TestGroup.PERFORMANCE);
331281
DummyJob.param = 0;
332282
DummyJob.count = 0;
@@ -348,9 +298,8 @@ public void testSchedulerWithSpringBeanJobFactoryAndJobSchedulingData() throws E
348298
* Tests the creation of multiple schedulers (SPR-772)
349299
*/
350300
@Test
351-
public void testMultipleSchedulers() throws Exception {
352-
ClassPathXmlApplicationContext ctx =
353-
new ClassPathXmlApplicationContext("/org/springframework/scheduling/quartz/multipleSchedulers.xml");
301+
public void multipleSchedulers() throws Exception {
302+
ClassPathXmlApplicationContext ctx = context("multipleSchedulers.xml");
354303
try {
355304
Scheduler scheduler1 = (Scheduler) ctx.getBean("scheduler1");
356305
Scheduler scheduler2 = (Scheduler) ctx.getBean("scheduler2");
@@ -364,10 +313,9 @@ public void testMultipleSchedulers() throws Exception {
364313
}
365314

366315
@Test
367-
public void testWithTwoAnonymousMethodInvokingJobDetailFactoryBeans() throws InterruptedException {
316+
public void twoAnonymousMethodInvokingJobDetailFactoryBeans() throws Exception {
368317
Assume.group(TestGroup.PERFORMANCE);
369-
ClassPathXmlApplicationContext ctx =
370-
new ClassPathXmlApplicationContext("/org/springframework/scheduling/quartz/multipleAnonymousMethodInvokingJobDetailFB.xml");
318+
ClassPathXmlApplicationContext ctx = context("multipleAnonymousMethodInvokingJobDetailFB.xml");
371319
Thread.sleep(3000);
372320
try {
373321
QuartzTestBean exportService = (QuartzTestBean) ctx.getBean("exportService");
@@ -384,10 +332,9 @@ public void testWithTwoAnonymousMethodInvokingJobDetailFactoryBeans() throws Int
384332
}
385333

386334
@Test
387-
public void testSchedulerAccessorBean() throws InterruptedException {
335+
public void schedulerAccessorBean() throws Exception {
388336
Assume.group(TestGroup.PERFORMANCE);
389-
ClassPathXmlApplicationContext ctx =
390-
new ClassPathXmlApplicationContext("/org/springframework/scheduling/quartz/schedulerAccessorBean.xml");
337+
ClassPathXmlApplicationContext ctx = context("schedulerAccessorBean.xml");
391338
Thread.sleep(3000);
392339
try {
393340
QuartzTestBean exportService = (QuartzTestBean) ctx.getBean("exportService");
@@ -405,7 +352,7 @@ public void testSchedulerAccessorBean() throws InterruptedException {
405352

406353
@Test
407354
@SuppressWarnings("resource")
408-
public void testSchedulerAutoStartsOnContextRefreshedEventByDefault() throws Exception {
355+
public void schedulerAutoStartsOnContextRefreshedEventByDefault() throws Exception {
409356
StaticApplicationContext context = new StaticApplicationContext();
410357
context.registerBeanDefinition("scheduler", new RootBeanDefinition(SchedulerFactoryBean.class));
411358
Scheduler bean = context.getBean("scheduler", Scheduler.class);
@@ -416,7 +363,7 @@ public void testSchedulerAutoStartsOnContextRefreshedEventByDefault() throws Exc
416363

417364
@Test
418365
@SuppressWarnings("resource")
419-
public void testSchedulerAutoStartupFalse() throws Exception {
366+
public void schedulerAutoStartupFalse() throws Exception {
420367
StaticApplicationContext context = new StaticApplicationContext();
421368
BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(
422369
SchedulerFactoryBean.class).addPropertyValue("autoStartup", false).getBeanDefinition();
@@ -428,35 +375,38 @@ public void testSchedulerAutoStartupFalse() throws Exception {
428375
}
429376

430377
@Test
431-
public void testSchedulerRepositoryExposure() throws InterruptedException {
432-
ClassPathXmlApplicationContext ctx =
433-
new ClassPathXmlApplicationContext("/org/springframework/scheduling/quartz/schedulerRepositoryExposure.xml");
378+
public void schedulerRepositoryExposure() throws Exception {
379+
ClassPathXmlApplicationContext ctx = context("schedulerRepositoryExposure.xml");
434380
assertSame(SchedulerRepository.getInstance().lookup("myScheduler"), ctx.getBean("scheduler"));
435381
ctx.close();
436382
}
437383

438-
// SPR-6038: detect HSQL and stop illegal locks being taken
384+
/**
385+
* SPR-6038: detect HSQL and stop illegal locks being taken.
386+
*/
439387
@Test
440-
public void testSchedulerWithHsqlDataSource() throws Exception {
388+
public void schedulerWithHsqlDataSource() throws Exception {
441389
Assume.group(TestGroup.PERFORMANCE);
442390

443391
DummyJob.param = 0;
444392
DummyJob.count = 0;
445393

446-
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
447-
"/org/springframework/scheduling/quartz/databasePersistence.xml");
394+
ClassPathXmlApplicationContext ctx = context("databasePersistence.xml");
448395
JdbcTemplate jdbcTemplate = new JdbcTemplate(ctx.getBean(DataSource.class));
449396
assertTrue("No triggers were persisted", jdbcTemplate.queryForList("SELECT * FROM qrtz_triggers").size()>0);
450397
Thread.sleep(3000);
451398
try {
452-
// assertEquals(10, DummyJob.param);
453399
assertTrue(DummyJob.count > 0);
454400
}
455401
finally {
456402
ctx.close();
457403
}
458404
}
459405

406+
private ClassPathXmlApplicationContext context(String path) {
407+
return new ClassPathXmlApplicationContext(path, getClass());
408+
}
409+
460410

461411
public static class CountingTaskExecutor implements TaskExecutor {
462412

@@ -512,20 +462,9 @@ protected synchronized void executeInternal(JobExecutionContext jobExecutionCont
512462

513463
public static class DummyRunnable implements Runnable {
514464

515-
private static int param;
516-
517-
private static int count;
518-
519-
public void setParam(int value) {
520-
if (param > 0) {
521-
throw new IllegalStateException("Param already set");
522-
}
523-
param = value;
524-
}
525-
526465
@Override
527466
public void run() {
528-
count++;
467+
/* no-op */
529468
}
530469
}
531470

spring-context-support/src/test/resources/org/springframework/scheduling/quartz/databasePersistence.xml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
4-
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
2+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
4+
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
55
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
66

7-
<bean id="scheduler"
8-
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
7+
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
98
<property name="triggers" ref="trigger" />
10-
<property name="dataSource" ref="dataSource"/>
9+
<property name="dataSource" ref="dataSource" />
1110
</bean>
1211

1312
<bean id="trigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
@@ -20,8 +19,8 @@
2019
<entry key="param" value="10" />
2120
</map>
2221
</property>
23-
<property name="jobClass"
24-
value="org.springframework.scheduling.quartz.QuartzSupportTests$DummyJob" />
22+
<property name="jobClass" value="org.springframework.scheduling.quartz.QuartzSupportTests$DummyJob" />
23+
<property name="durability" value="true" />
2524
</bean>
2625
</property>
2726
</bean>

0 commit comments

Comments
 (0)