DATACMNS-1091 - Fixes sporadic test failures in tests for JSR-310 conversions.
We now use specified date formats for converting various date types to Strings and comparing them. Before, in some cases toString() was used which strips trailing zeros, causing sporadic failures. JDK9 changed the behaviour of Date related toString methods, making tests fail all the time. Therefore this fix is required for executing tests with JDK9. Original pull request: #228.
This commit is contained in:
committed by
Mark Paluch
parent
97bd85e628
commit
c5318a7f3a
@@ -25,12 +25,15 @@ import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.Period;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.Temporal;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -52,6 +55,7 @@ import org.springframework.data.convert.Jsr310ConvertersUnitTests.PeriodConversi
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Barak Schoster
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({ CommonTests.class, DurationConversionTests.class, PeriodConversionTests.class })
|
||||
@@ -75,40 +79,42 @@ public class Jsr310ConvertersUnitTests {
|
||||
|
||||
@Test // DATACMNS-606
|
||||
public void convertsDateToLocalDateTime() {
|
||||
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDateTime.class).toString())
|
||||
.isEqualTo(format(NOW, "yyyy-MM-dd'T'HH:mm:ss.SSS"));
|
||||
|
||||
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDateTime.class)) //
|
||||
.matches(date(NOW, "yyyy-MM-dd'T'HH:mm:ss.SSS"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-606
|
||||
public void convertsLocalDateTimeToDate() {
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "yyyy-MM-dd'T'HH:mm:ss.SSS"))
|
||||
.isEqualTo(now.toString());
|
||||
assertThat(CONVERSION_SERVICE.convert(now, Date.class)) //
|
||||
.matches(temporal(now, "yyyy-MM-dd'T'HH:mm:ss.SSS"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-606
|
||||
public void convertsDateToLocalDate() {
|
||||
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDate.class).toString()).isEqualTo(format(NOW, "yyyy-MM-dd"));
|
||||
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDate.class)).matches(date(NOW, "yyyy-MM-dd"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-606
|
||||
public void convertsLocalDateToDate() {
|
||||
|
||||
LocalDate now = LocalDate.now();
|
||||
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "yyyy-MM-dd")).isEqualTo(now.toString());
|
||||
assertThat(CONVERSION_SERVICE.convert(now, Date.class)).matches(temporal(now, "yyyy-MM-dd"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-606
|
||||
public void convertsDateToLocalTime() {
|
||||
assertThat(CONVERSION_SERVICE.convert(NOW, LocalTime.class).toString()).isEqualTo(format(NOW, "HH:mm:ss.SSS"));
|
||||
|
||||
assertThat(CONVERSION_SERVICE.convert(NOW, LocalTime.class)).matches(date(NOW, "HH:mm:ss.SSS"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-606
|
||||
public void convertsLocalTimeToDate() {
|
||||
|
||||
LocalTime now = LocalTime.now();
|
||||
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "HH:mm:ss.SSS")).isEqualTo(now.toString());
|
||||
assertThat(CONVERSION_SERVICE.convert(now, Date.class)).matches(temporal(now, "HH:mm:ss.SSS"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-623
|
||||
@@ -141,6 +147,18 @@ public class Jsr310ConvertersUnitTests {
|
||||
private static String format(Date date, String format) {
|
||||
return new SimpleDateFormat(format).format(date);
|
||||
}
|
||||
|
||||
private static Predicate<Date> temporal(Temporal expected, String format) {
|
||||
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
||||
return d -> format(d, format).equals(formatter.format(expected));
|
||||
}
|
||||
|
||||
private static Predicate<Temporal> date(Date expected, String format) {
|
||||
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
||||
return d -> formatter.format(d).equals(format(expected, format));
|
||||
}
|
||||
}
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
|
||||
Reference in New Issue
Block a user