Skip to content

Commit d623082

Browse files
committed
Stop using Constants utility in SimpleTriggerFactoryBean
See gh-30851
1 parent edba535 commit d623082

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.beans.factory.BeanNameAware;
2929
import org.springframework.beans.factory.FactoryBean;
3030
import org.springframework.beans.factory.InitializingBean;
31-
import org.springframework.core.Constants;
3231
import org.springframework.lang.Nullable;
3332
import org.springframework.util.Assert;
3433

@@ -46,6 +45,7 @@
4645
* instead of registering the JobDetail separately.
4746
*
4847
* @author Juergen Hoeller
48+
* @author Sam Brannen
4949
* @since 3.1
5050
* @see #setName
5151
* @see #setGroup
@@ -56,9 +56,26 @@
5656
*/
5757
public class SimpleTriggerFactoryBean implements FactoryBean<SimpleTrigger>, BeanNameAware, InitializingBean {
5858

59-
/** Constants for the SimpleTrigger class. */
60-
private static final Constants constants = new Constants(SimpleTrigger.class);
61-
59+
/**
60+
* Map of constant names to constant values for the misfire instruction constants
61+
* defined in {@link org.quartz.Trigger} and {@link org.quartz.SimpleTrigger}.
62+
*/
63+
private static final Map<String, Integer> constants = Map.of(
64+
"MISFIRE_INSTRUCTION_SMART_POLICY",
65+
SimpleTrigger.MISFIRE_INSTRUCTION_SMART_POLICY,
66+
"MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY",
67+
SimpleTrigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY,
68+
"MISFIRE_INSTRUCTION_FIRE_NOW",
69+
SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW,
70+
"MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT",
71+
SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT,
72+
"MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT",
73+
SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT,
74+
"MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT",
75+
SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT,
76+
"MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT",
77+
SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT
78+
);
6279

6380
@Nullable
6481
private String name;
@@ -204,7 +221,10 @@ public void setMisfireInstruction(int misfireInstruction) {
204221
* @see org.quartz.SimpleTrigger#MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT
205222
*/
206223
public void setMisfireInstructionName(String constantName) {
207-
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;
208228
}
209229

210230
/**

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

Lines changed: 40 additions & 5 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,21 +16,32 @@
1616

1717
package org.springframework.scheduling.quartz;
1818

19-
import java.text.ParseException;
19+
import java.lang.reflect.Field;
20+
import java.util.Arrays;
21+
import java.util.stream.Stream;
2022

2123
import org.junit.jupiter.api.Test;
2224
import org.quartz.SimpleTrigger;
2325

26+
import org.springframework.util.ReflectionUtils;
27+
2428
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
30+
import static org.assertj.core.api.Assertions.assertThatNoException;
2531

2632
/**
33+
* Tests for {@link SimpleTriggerFactoryBean}.
34+
*
2735
* @author Stephane Nicoll
36+
* @author Sam Brannen
2837
*/
29-
public class SimpleTriggerFactoryBeanTests {
38+
class SimpleTriggerFactoryBeanTests {
39+
40+
private final SimpleTriggerFactoryBean factory = new SimpleTriggerFactoryBean();
41+
3042

3143
@Test
32-
public void createWithoutJobDetail() throws ParseException {
33-
SimpleTriggerFactoryBean factory = new SimpleTriggerFactoryBean();
44+
void createWithoutJobDetail() {
3445
factory.setName("myTrigger");
3546
factory.setRepeatCount(5);
3647
factory.setRepeatInterval(1000L);
@@ -40,4 +51,28 @@ public void createWithoutJobDetail() throws ParseException {
4051
assertThat(trigger.getRepeatInterval()).isEqualTo(1000L);
4152
}
4253

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

0 commit comments

Comments
 (0)