Skip to content

Commit

Permalink
#459: Add test and fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrozanec committed Nov 8, 2020
1 parent 7937ce6 commit e3e0574
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
18 changes: 7 additions & 11 deletions src/main/java/com/cronutils/converter/CronConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,22 @@

public class CronConverter {

private static final Logger LOGGER = LoggerFactory
.getLogger(CronConverter.class);
private static final Logger LOGGER = LoggerFactory.getLogger(CronConverter.class);

private static final String CRON_FIELDS_SEPARATOR = " ";

private String[] cronParts;

private Calendar fromCalendar;

private String sourceCron;

private ZoneId sourceZoneId;

private ZoneId targetZoneId;

@Setter

This comment has been minimized.

Copy link
@IndeedSi

IndeedSi Nov 15, 2020

I think this is the only place where lombok is used in the project. You can remove this dependency if you remove the import lombok.Setter from this file.

This comment has been minimized.

Copy link
@jmrozanec

jmrozanec Nov 15, 2020

Author Owner

@IndeedSi thanks! 😄

CronToCalendarTransformer toCalendarConverter = new CronToCalendarTransformer();
private CronToCalendarTransformer toCalendarConverter;
private CalendarToCronTransformer toCronConverter;

@Setter
CalendarToCronTransformer toCronConverter = new CalendarToCronTransformer();
public CronConverter(CronToCalendarTransformer toCalendarConverter, CalendarToCronTransformer toCronConverter){
this.toCalendarConverter = toCalendarConverter;
this.toCronConverter = toCronConverter;
}

public CronConverter using(String cronExpression) {
this.sourceCron = cronExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.cronutils.model.field.value.IntegerFieldValue;
import com.cronutils.model.field.value.SpecialChar;
import com.cronutils.model.field.value.SpecialCharFieldValue;
import com.cronutils.utils.Preconditions;

public class FieldExpressionFactory {
private FieldExpressionFactory() {
Expand Down Expand Up @@ -48,6 +49,7 @@ public static On on(SpecialChar specialChar) {
}

public static On on(int time) {
Preconditions.checkPositive(time, "Time must not be negative");
return new On(new IntegerFieldValue(time));
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/cronutils/model/field/expression/On.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.cronutils.model.field.value.SpecialCharFieldValue;
import com.cronutils.utils.Preconditions;
import com.cronutils.utils.StringUtils;
import org.apache.commons.lang3.Validate;

import static com.cronutils.utils.Preconditions.checkArgument;

Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/cronutils/utils/Preconditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@
* @since 2.0 (imported from Google Collections Library)
*/
public class Preconditions {
private Preconditions() {
}
private Preconditions() {}

/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
Expand All @@ -109,6 +108,12 @@ public static void checkArgument(final boolean expression, final Object errorMes
}
}

public static void checkPositive(final int value, final Object errorMessage) {
if (value < 0) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
}
}

/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/cronutils/Issue459Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.cronutils;

import com.cronutils.builder.CronBuilder;
import com.cronutils.model.definition.CronDefinitionBuilder;
import org.junit.Test;

import static com.cronutils.model.CronType.UNIX;
import static com.cronutils.model.field.expression.FieldExpression.always;
import static com.cronutils.model.field.expression.FieldExpressionFactory.on;

public class Issue459Test {
@Test(expected = RuntimeException.class)
public void testNegativeValuesNotAllowed() {
CronBuilder.cron(CronDefinitionBuilder.instanceDefinitionFor(UNIX))
.withDoM(always())
.withMonth(always())
.withDoW(always())
.withHour(on(-1))
.withMinute(on(5))
.instance();
}
}
11 changes: 4 additions & 7 deletions src/test/java/com/cronutils/converter/CronConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import org.junit.Assert;
import org.junit.Test;
Expand All @@ -28,13 +27,13 @@
@RunWith(Parameterized.class)
public class CronConverterTest {

CronConverter cronConverter = spy(new CronConverter());

private String timezone;

private String inputCronExpression;

private String expectedCronExpression;
private CronConverter cronConverter = spy(new CronConverter(
new CronToCalendarTransformer(),
new CalendarToCronTransformer()
));

public CronConverterTest(String timezone, String inputCronExpression, String expectedCronExpression) {
this.timezone = timezone;
Expand All @@ -56,8 +55,6 @@ public static Collection cronExpressions() {

@Test
public void testCronConverterBuilder() {
cronConverter.setToCalendarConverter(new CronToCalendarTransformer());
cronConverter.setToCronConverter(new CalendarToCronTransformer());
Assert.assertEquals(expectedCronExpression,
cronConverter.using(inputCronExpression)
.from(ZoneId.of(timezone)).to(ZoneId.of("UTC"))
Expand Down

0 comments on commit e3e0574

Please sign in to comment.