Skip to content

Commit edba535

Browse files
committed
Stop using Constants utility in CronTriggerFactoryBean
See gh-30851
1 parent 79cf532 commit edba535

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

spring-context-support/src/main/java/org/springframework/scheduling/quartz/CronTriggerFactoryBean.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.springframework.beans.factory.BeanNameAware;
3131
import org.springframework.beans.factory.FactoryBean;
3232
import org.springframework.beans.factory.InitializingBean;
33-
import org.springframework.core.Constants;
3433
import org.springframework.lang.Nullable;
3534
import org.springframework.util.Assert;
3635

@@ -48,6 +47,7 @@
4847
* instead of registering the JobDetail separately.
4948
*
5049
* @author Juergen Hoeller
50+
* @author Sam Brannen
5151
* @since 3.1
5252
* @see #setName
5353
* @see #setGroup
@@ -58,8 +58,16 @@
5858
*/
5959
public class CronTriggerFactoryBean implements FactoryBean<CronTrigger>, BeanNameAware, InitializingBean {
6060

61-
/** Constants for the CronTrigger class. */
62-
private static final Constants constants = new Constants(CronTrigger.class);
61+
/**
62+
* Map of constant names to constant values for the misfire instruction constants
63+
* defined in {@link org.quartz.Trigger} and {@link org.quartz.CronTrigger}.
64+
*/
65+
private static final Map<String, Integer> constants = Map.of(
66+
"MISFIRE_INSTRUCTION_SMART_POLICY", CronTrigger.MISFIRE_INSTRUCTION_SMART_POLICY,
67+
"MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY", CronTrigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY,
68+
"MISFIRE_INSTRUCTION_FIRE_ONCE_NOW", CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW,
69+
"MISFIRE_INSTRUCTION_DO_NOTHING", CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING
70+
);
6371

6472

6573
@Nullable
@@ -213,7 +221,10 @@ public void setMisfireInstruction(int misfireInstruction) {
213221
* @see org.quartz.CronTrigger#MISFIRE_INSTRUCTION_DO_NOTHING
214222
*/
215223
public void setMisfireInstructionName(String constantName) {
216-
this.misfireInstruction = constants.asNumber(constantName).intValue();
224+
Assert.hasText(constantName, "'constantName' must not be null or blank");
225+
Integer misfireInstruction = constants.get(constantName);
226+
Assert.notNull(misfireInstruction, "Only misfire instruction constants allowed");
227+
this.misfireInstruction = misfireInstruction;
217228
}
218229

219230
/**

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,26 +16,62 @@
1616

1717
package org.springframework.scheduling.quartz;
1818

19+
import java.lang.reflect.Field;
1920
import java.text.ParseException;
21+
import java.util.Arrays;
22+
import java.util.stream.Stream;
2023

2124
import org.junit.jupiter.api.Test;
2225
import org.quartz.CronTrigger;
2326

27+
import org.springframework.util.ReflectionUtils;
28+
2429
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
31+
import static org.assertj.core.api.Assertions.assertThatNoException;
2532

2633
/**
34+
* Tests for {@link CronTriggerFactoryBean}.
35+
*
2736
* @author Stephane Nicoll
37+
* @author Sam Brannen
2838
*/
29-
public class CronTriggerFactoryBeanTests {
39+
class CronTriggerFactoryBeanTests {
40+
41+
private final CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
42+
3043

3144
@Test
32-
public void createWithoutJobDetail() throws ParseException {
33-
CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
45+
void createWithoutJobDetail() throws ParseException {
3446
factory.setName("myTrigger");
3547
factory.setCronExpression("0 15 10 ? * *");
3648
factory.afterPropertiesSet();
3749
CronTrigger trigger = factory.getObject();
3850
assertThat(trigger.getCronExpression()).isEqualTo("0 15 10 ? * *");
3951
}
4052

53+
@Test
54+
void setMisfireInstructionNameToUnsupportedValues() {
55+
assertThatIllegalArgumentException().isThrownBy(() -> factory.setMisfireInstructionName(null));
56+
assertThatIllegalArgumentException().isThrownBy(() -> factory.setMisfireInstructionName(" "));
57+
assertThatIllegalArgumentException().isThrownBy(() -> factory.setMisfireInstructionName("bogus"));
58+
}
59+
60+
/**
61+
* This test effectively verifies that the internal 'constants' map is properly
62+
* configured for all MISFIRE_INSTRUCTION_ constants defined in {@link CronTrigger}.
63+
*/
64+
@Test
65+
void setMisfireInstructionNameToAllSupportedValues() {
66+
streamMisfireInstructionConstants()
67+
.map(Field::getName)
68+
.forEach(name -> assertThatNoException().as(name).isThrownBy(() -> factory.setMisfireInstructionName(name)));
69+
}
70+
71+
private static Stream<Field> streamMisfireInstructionConstants() {
72+
return Arrays.stream(CronTrigger.class.getFields())
73+
.filter(ReflectionUtils::isPublicStaticFinal)
74+
.filter(field -> field.getName().startsWith("MISFIRE_INSTRUCTION_"));
75+
}
76+
4177
}

0 commit comments

Comments
 (0)