|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.scheduling.quartz;
|
18 | 18 |
|
| 19 | +import java.lang.reflect.Field; |
19 | 20 | import java.text.ParseException;
|
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.stream.Stream; |
20 | 23 |
|
21 | 24 | import org.junit.jupiter.api.Test;
|
22 | 25 | import org.quartz.CronTrigger;
|
23 | 26 |
|
| 27 | +import org.springframework.util.ReflectionUtils; |
| 28 | + |
24 | 29 | 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; |
25 | 32 |
|
26 | 33 | /**
|
| 34 | + * Tests for {@link CronTriggerFactoryBean}. |
| 35 | + * |
27 | 36 | * @author Stephane Nicoll
|
| 37 | + * @author Sam Brannen |
28 | 38 | */
|
29 |
| -public class CronTriggerFactoryBeanTests { |
| 39 | +class CronTriggerFactoryBeanTests { |
| 40 | + |
| 41 | + private final CronTriggerFactoryBean factory = new CronTriggerFactoryBean(); |
| 42 | + |
30 | 43 |
|
31 | 44 | @Test
|
32 |
| - public void createWithoutJobDetail() throws ParseException { |
33 |
| - CronTriggerFactoryBean factory = new CronTriggerFactoryBean(); |
| 45 | + void createWithoutJobDetail() throws ParseException { |
34 | 46 | factory.setName("myTrigger");
|
35 | 47 | factory.setCronExpression("0 15 10 ? * *");
|
36 | 48 | factory.afterPropertiesSet();
|
37 | 49 | CronTrigger trigger = factory.getObject();
|
38 | 50 | assertThat(trigger.getCronExpression()).isEqualTo("0 15 10 ? * *");
|
39 | 51 | }
|
40 | 52 |
|
| 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 | + |
41 | 77 | }
|
0 commit comments