Skip to content

Commit fc3cf75

Browse files
committed
#528: Fixes for issues revailed in failing tests.
1 parent 15f124e commit fc3cf75

6 files changed

Lines changed: 35 additions & 10 deletions

File tree

src/main/java/com/cronutils/descriptor/CronDescriptor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.cronutils.descriptor;
1515

1616
import com.cronutils.model.Cron;
17+
import com.cronutils.model.RebootCron;
1718
import com.cronutils.model.field.CronField;
1819
import com.cronutils.model.field.CronFieldName;
1920
import com.cronutils.model.field.definition.FieldDefinition;
@@ -66,6 +67,9 @@ private CronDescriptor() {
6667
*/
6768
public String describe(final Cron cron) {
6869
Preconditions.checkNotNull(cron, "Cron must not be null");
70+
if(cron instanceof RebootCron){
71+
return resourceBundle.getString("on_reboot");
72+
}
6973
final Map<CronFieldName, CronField> expressions = cron.retrieveFieldsAsMap();
7074
final Map<CronFieldName, FieldDefinition> fieldDefinitions = cron.getCronDefinition().retrieveFieldDefinitionsAsMap();
7175

src/main/java/com/cronutils/mapper/CronMapper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
import com.cronutils.Function;
1717
import com.cronutils.model.Cron;
1818
import com.cronutils.model.CronType;
19+
import com.cronutils.model.RebootCron;
1920
import com.cronutils.model.SingleCron;
2021
import com.cronutils.model.definition.CronDefinition;
2122
import com.cronutils.model.definition.CronDefinitionBuilder;
23+
import com.cronutils.model.definition.CronNicknames;
2224
import com.cronutils.model.field.CronField;
2325
import com.cronutils.model.field.CronFieldName;
2426
import com.cronutils.model.field.constraint.FieldConstraints;
@@ -73,6 +75,13 @@ public CronMapper(final CronDefinition from, final CronDefinition to, final Func
7375
*/
7476
public Cron map(final Cron cron) {
7577
Preconditions.checkNotNull(cron, "Cron must not be null");
78+
if(cron instanceof RebootCron){
79+
if(this.to.getCronNicknames().contains(CronNicknames.REBOOT)){
80+
return new RebootCron(this.to);
81+
} else {
82+
throw new IllegalArgumentException("The target cron definition does not support @reboot nickname");
83+
}
84+
}
7685
final List<CronField> fields = new ArrayList<>();
7786
for (final CronFieldName name : CronFieldName.values()) {
7887
if (mappings.containsKey(name)) {

src/main/java/com/cronutils/model/time/ExecutionTime.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ default List<ZonedDateTime> getExecutionDates(ZonedDateTime startDate, ZonedDate
184184
ZonedDateTime nextExecutionDate = nextExecution(startDate).orElse(null);
185185

186186
if (nextExecutionDate == null) return Collections.emptyList();
187-
while(nextExecutionDate != null && nextExecutionDate.isBefore(endDate)){
187+
while(nextExecutionDate != null && (nextExecutionDate.isBefore(endDate) || nextExecutionDate.equals(endDate))){
188188
executions.add(nextExecutionDate);
189189
nextExecutionDate = nextExecution(nextExecutionDate).orElse(null);
190190
}

src/main/resources/CronUtilsI18N.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ every_one=every
2929
every_multi=every
3030
oneSecond=second
3131
multipleSeconds={2} seconds
32+
on_reboot=on reboot
3233

3334
pattern_at_second=at second {0}

src/test/java/com/cronutils/Issue528Test.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,35 @@
77
import com.cronutils.model.definition.CronDefinitionBuilder;
88
import com.cronutils.model.time.ExecutionTime;
99
import com.cronutils.parser.CronParser;
10+
import org.junit.Test;
1011
import org.junit.jupiter.api.Assertions;
11-
import org.junit.jupiter.api.Test;
1212

1313
import java.time.ZonedDateTime;
1414
import java.util.Locale;
1515
import java.util.Optional;
1616

17-
class Issue528Test {
18-
19-
static final CronDefinition REBOOT_CRON_DEFINITION = CronDefinitionBuilder.defineCron()
17+
public class Issue528Test {
18+
private static final CronDefinition REBOOT_CRON_DEFINITION = CronDefinitionBuilder.defineCron()
2019
.withSupportedNicknameReboot()
2120
.instance();
2221

2322
@Test
24-
void testRebootExecutionTime() {
23+
public void testRebootExecutionTime() {
2524
Cron cron = new CronParser(REBOOT_CRON_DEFINITION).parse("@reboot");
2625
ExecutionTime executionTime = ExecutionTime.forCron(cron);
2726
Assertions.assertEquals(Optional.empty(), executionTime.nextExecution(ZonedDateTime.now()));
2827
Assertions.assertEquals(Optional.empty(), executionTime.lastExecution(ZonedDateTime.now()));
2928
}
3029

3130
@Test
32-
void testCronDescriptor() {
31+
public void testCronDescriptor() {
3332
Cron cron = new CronParser(REBOOT_CRON_DEFINITION).parse("@reboot");
3433
String description = CronDescriptor.instance(Locale.UK).describe(cron);
3534
Assertions.assertEquals("on reboot", description);
3635
}
3736

38-
3937
@Test
40-
void testCronMapper() {
38+
public void testCronMapperRebootSupportedOnTarget() {
4139
Cron cron = new CronParser(REBOOT_CRON_DEFINITION).parse("@reboot");
4240
CronDefinition unix = CronDefinitionBuilder.defineCron()
4341
.withMinutes().withValidRange(0, 59).withStrictRange().and()
@@ -50,4 +48,18 @@ void testCronMapper() {
5048
Cron mapped = CronMapper.sameCron(unix).map(cron);
5149
Assertions.assertEquals(cron.asString(), mapped.asString());
5250
}
51+
52+
@Test(expected = IllegalArgumentException.class)
53+
public void testCronMapperRebootNotSupportedOnTarget() {
54+
Cron cron = new CronParser(REBOOT_CRON_DEFINITION).parse("@reboot");
55+
CronDefinition unix = CronDefinitionBuilder.defineCron()
56+
.withMinutes().withValidRange(0, 59).withStrictRange().and()
57+
.withHours().withValidRange(0, 23).withStrictRange().and()
58+
.withDayOfMonth().withValidRange(1, 31).withStrictRange().and()
59+
.withMonth().withValidRange(1, 12).withStrictRange().and()
60+
.withDayOfWeek().withValidRange(0, 7).withMondayDoWValue(1).withIntMapping(7, 0).withStrictRange().and()
61+
.instance();
62+
Cron mapped = CronMapper.sameCron(unix).map(cron);
63+
Assertions.assertEquals(cron.asString(), mapped.asString());
64+
}
5365
}

src/test/java/com/cronutils/parser/CronParserQuartzIntegrationTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.junit.Before;
2525
import org.junit.Rule;
2626
import org.junit.Test;
27-
import org.junit.internal.matchers.ThrowableMessageMatcher;
2827
import org.junit.rules.ExpectedException;
2928

3029
import java.time.ZonedDateTime;

0 commit comments

Comments
 (0)