Skip to content

Commit

Permalink
Fix for LocalDate.MIN and LocalDate.MAX range value exceptions #483
Browse files Browse the repository at this point in the history
  • Loading branch information
OguzhanKazak authored and fmbenhassine committed Jul 15, 2023
1 parent 5124167 commit 7a5cd4a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,24 @@ protected Instant getDefaultMaxValue() {
return Instant.ofEpochMilli(Long.MAX_VALUE);
}

@Override
public Instant getRandomValue() {
long minEpochMillis = min.toEpochMilli();
long maxEpochMillis = max.toEpochMilli();
@Override
public Instant getRandomValue() {
long minEpochMillis;
long maxEpochMillis;
try {
minEpochMillis = min.toEpochMilli();
} catch (ArithmeticException ex) {
minEpochMillis = Long.MIN_VALUE;
}

try {
maxEpochMillis = max.toEpochMilli();
} catch (ArithmeticException ex) {
maxEpochMillis = Long.MAX_VALUE;
}

long randomEpochMillis = (long) nextDouble(minEpochMillis, maxEpochMillis);
return Instant.ofEpochMilli(randomEpochMillis);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.concurrent.atomic.AtomicLong;

import static java.sql.Date.valueOf;
import static org.jeasy.random.util.ConversionUtils.convertDateToLocalDate;

/**
* Registry for Java built-in types.
Expand Down Expand Up @@ -93,8 +94,10 @@ public void init(EasyRandomParameters parameters) {
randomizers.put(BigDecimal.class, new BigDecimalRandomizer(seed));
randomizers.put(AtomicLong.class, new AtomicLongRandomizer(seed));
randomizers.put(AtomicInteger.class, new AtomicIntegerRandomizer(seed));
Date minDate = valueOf(parameters.getDateRange().getMin());
Date maxDate = valueOf(parameters.getDateRange().getMax());
Date minDate = new Date(Long.MIN_VALUE);
Date maxDate = new Date(Long.MAX_VALUE);
minDate = convertDateToLocalDate(minDate).isAfter(parameters.getDateRange().getMin()) ? minDate : valueOf(parameters.getDateRange().getMin());
maxDate = convertDateToLocalDate(maxDate).isBefore(parameters.getDateRange().getMax()) ? maxDate : valueOf(parameters.getDateRange().getMax());
randomizers.put(Date.class, new DateRangeRandomizer(minDate, maxDate, seed));
randomizers.put(java.sql.Date.class, new SqlDateRangeRandomizer(new java.sql.Date(minDate.getTime()), new java.sql.Date(maxDate.getTime()), seed));
randomizers.put(java.sql.Time.class, new SqlTimeRandomizer(seed));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.stream.Stream;
Expand Down Expand Up @@ -67,6 +64,10 @@ public static Object[] convertArguments(final RandomizerArgument[] declaredArgum
return arguments;
}

public static LocalDate convertDateToLocalDate(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}

private static Object convertArray(Object array, Class<?> targetType) {
Object[] values = (Object[]) array;
Object convertedValuesArray = Array.newInstance(targetType.getComponentType(), values.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ void testDateRange() {
assertThat(timeBean.getLocalDate()).isAfterOrEqualTo(minDate).isBeforeOrEqualTo(maxDate);
}

@Test
void testDateMaxRange() {
// Given
LocalDate minDate = LocalDate.MIN;
LocalDate maxDate = LocalDate.MAX;
EasyRandomParameters parameters = new EasyRandomParameters().dateRange(minDate, maxDate);

// When
TimeBean timeBean = new EasyRandom(parameters).nextObject(TimeBean.class);

// Then
assertThat(timeBean.getLocalDate()).isAfterOrEqualTo(minDate).isBeforeOrEqualTo(maxDate);
}

@Test
void testTimeRange() {
// Given
Expand Down

0 comments on commit 7a5cd4a

Please sign in to comment.