committed by
Jens Schauder
parent
f417c4ca13
commit
582319a5fe
@@ -15,9 +15,9 @@
|
||||
*/
|
||||
package org.springframework.data.auditing;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.springframework.data.annotation.CreatedBy;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedBy;
|
||||
@@ -35,7 +35,7 @@ class AnnotatedUser {
|
||||
Object createdBy;
|
||||
|
||||
@CreatedDate
|
||||
DateTime createdDate;
|
||||
LocalDateTime createdDate;
|
||||
|
||||
@LastModifiedBy
|
||||
Object lastModifiedBy;
|
||||
|
||||
@@ -20,7 +20,6 @@ import static org.assertj.core.api.Assertions.*;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Optional;
|
||||
@@ -69,12 +68,12 @@ class DefaultAuditableBeanWrapperFactoryUnitTests {
|
||||
}
|
||||
|
||||
@Test // DATACMNS-643
|
||||
void setsJsr310AndThreeTenBpTypes() {
|
||||
void setsJsr310Types() {
|
||||
|
||||
Jsr310ThreeTenBpAuditedUser user = new Jsr310ThreeTenBpAuditedUser();
|
||||
Jsr310AuditedUser user = new Jsr310AuditedUser();
|
||||
Instant instant = Instant.now();
|
||||
|
||||
Optional<AuditableBeanWrapper<Jsr310ThreeTenBpAuditedUser>> wrapper = factory.getBeanWrapperFor(user);
|
||||
Optional<AuditableBeanWrapper<Jsr310AuditedUser>> wrapper = factory.getBeanWrapperFor(user);
|
||||
|
||||
assertThat(wrapper).hasValueSatisfying(it -> {
|
||||
|
||||
@@ -86,20 +85,6 @@ class DefaultAuditableBeanWrapperFactoryUnitTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-867
|
||||
void errorsWhenUnableToConvertDateViaIntermediateJavaUtilDateConversion() {
|
||||
|
||||
Jsr310ThreeTenBpAuditedUser user = new Jsr310ThreeTenBpAuditedUser();
|
||||
ZonedDateTime zonedDateTime = ZonedDateTime.now();
|
||||
|
||||
Optional<AuditableBeanWrapper<Jsr310ThreeTenBpAuditedUser>> wrapper = factory.getBeanWrapperFor(user);
|
||||
|
||||
assertThat(wrapper).isNotEmpty();
|
||||
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> wrapper.ifPresent(it -> it.setLastModifiedDate(zonedDateTime)));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1259
|
||||
void lastModifiedDateAsLongIsAvailableViaWrapper() {
|
||||
|
||||
|
||||
@@ -23,8 +23,8 @@ import org.springframework.data.annotation.LastModifiedDate;
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
class Jsr310ThreeTenBpAuditedUser {
|
||||
class Jsr310AuditedUser {
|
||||
|
||||
@CreatedDate LocalDateTime createdDate;
|
||||
@LastModifiedDate org.threeten.bp.LocalDateTime lastModifiedDate;
|
||||
@LastModifiedDate LocalDateTime lastModifiedDate;
|
||||
}
|
||||
@@ -40,9 +40,7 @@ import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedBy;
|
||||
import org.springframework.data.annotation.LastModifiedDate;
|
||||
import org.springframework.data.auditing.DefaultAuditableBeanWrapperFactory.AuditableInterfaceBeanWrapper;
|
||||
import org.springframework.data.convert.JodaTimeConverters;
|
||||
import org.springframework.data.convert.Jsr310Converters;
|
||||
import org.springframework.data.convert.ThreeTenBackPortConverters;
|
||||
import org.springframework.data.domain.Auditable;
|
||||
import org.springframework.data.mapping.context.PersistentEntities;
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
@@ -132,15 +130,6 @@ class MappingAuditableBeanWrapperFactoryUnitTests {
|
||||
Jsr310Converters.DateToLocalDateTimeConverter.INSTANCE.convert(reference));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-638
|
||||
void returnsLastModificationDateTimeAsCalendar() {
|
||||
|
||||
org.joda.time.LocalDateTime reference = new org.joda.time.LocalDateTime();
|
||||
|
||||
assertLastModificationDate(reference,
|
||||
JodaTimeConverters.LocalDateTimeToJsr310Converter.INSTANCE.convert(reference));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-638
|
||||
void returnsLastModificationDateAsCalendar() {
|
||||
|
||||
@@ -158,15 +147,6 @@ class MappingAuditableBeanWrapperFactoryUnitTests {
|
||||
assertLastModificationDate(reference, reference);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-638, DATACMNS-43
|
||||
void returnsLastModificationThreeTenBpDateTimeAsCalendar() {
|
||||
|
||||
org.threeten.bp.LocalDateTime reference = org.threeten.bp.LocalDateTime.now();
|
||||
|
||||
assertLastModificationDate(reference,
|
||||
ThreeTenBackPortConverters.LocalDateTimeToJsr310LocalDateTimeConverter.INSTANCE.convert(reference));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1109
|
||||
void exposesInstantAsModificationDate() {
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ import static org.assertj.core.api.Assertions.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
@@ -55,7 +54,7 @@ class ReflectionAuditingBeanWrapperUnitTests {
|
||||
void setsDateTimeFieldCorrectly() {
|
||||
|
||||
wrapper.setCreatedDate(time);
|
||||
assertThat(user.createdDate).isEqualTo(new DateTime(LocalDateTimeToDateConverter.INSTANCE.convert(time)));
|
||||
assertThat(user.createdDate).isEqualTo(time);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -28,7 +28,6 @@ import java.util.function.Predicate;
|
||||
|
||||
import org.jmolecules.ddd.types.Association;
|
||||
import org.jmolecules.ddd.types.Identifier;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
@@ -43,12 +42,9 @@ import org.springframework.data.convert.ConverterBuilder.ConverterAware;
|
||||
import org.springframework.data.convert.CustomConversions.ConverterConfiguration;
|
||||
import org.springframework.data.convert.CustomConversions.StoreConversions;
|
||||
import org.springframework.data.convert.Jsr310Converters.LocalDateTimeToDateConverter;
|
||||
import org.springframework.data.convert.ThreeTenBackPortConverters.LocalDateTimeToJavaTimeInstantConverter;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
|
||||
import org.threeten.bp.LocalDateTime;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link CustomConversions}.
|
||||
*
|
||||
@@ -131,25 +127,6 @@ class CustomConversionsUnitTests {
|
||||
assertThat(conversions.hasCustomWriteTarget(String.class, Integer.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-795, DATACMNS-1035
|
||||
void favorsCustomConverterForIndeterminedTargetType() {
|
||||
|
||||
CustomConversions conversions = new CustomConversions(StoreConversions.NONE,
|
||||
Arrays.asList(DateTimeToStringConverter.INSTANCE));
|
||||
assertThat(conversions.getCustomWriteTarget(DateTime.class)).hasValue(String.class);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-881, DATACMNS-1035
|
||||
void customConverterOverridesDefault() {
|
||||
|
||||
CustomConversions conversions = new CustomConversions(StoreConversions.NONE,
|
||||
Arrays.asList(CustomDateTimeConverter.INSTANCE));
|
||||
GenericConversionService conversionService = new DefaultConversionService();
|
||||
conversions.registerConvertersIn(conversionService);
|
||||
|
||||
assertThat(conversionService.convert(new DateTime(), Date.class)).isEqualTo(new Date(0));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1001, DATACMNS-1035
|
||||
void shouldSelectPropertCustomWriteTargetForCglibProxiedType() {
|
||||
|
||||
@@ -174,14 +151,6 @@ class CustomConversionsUnitTests {
|
||||
assertThat(customConversions.hasCustomWriteTarget(java.time.LocalDateTime.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1131, DATACMNS-1035
|
||||
void registersConvertersForThreeTenBackPort() {
|
||||
|
||||
CustomConversions customConversions = new CustomConversions(StoreConversions.NONE, Collections.emptyList());
|
||||
|
||||
assertThat(customConversions.hasCustomWriteTarget(LocalDateTime.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1302, DATACMNS-1035
|
||||
void registersConverterFactoryCorrectly() {
|
||||
|
||||
@@ -220,7 +189,7 @@ class CustomConversionsUnitTests {
|
||||
new CustomConversions(StoreConversions.of(DATE_EXCLUDING_SIMPLE_TYPE_HOLDER), Collections.emptyList())
|
||||
.registerConvertersIn(registry);
|
||||
|
||||
verify(registry, never()).addConverter(any(LocalDateTimeToJavaTimeInstantConverter.class));
|
||||
verify(registry, never()).addConverter(any(Jsr310Converters.LocalDateTimeToInstantConverter.class));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1665
|
||||
@@ -244,9 +213,10 @@ class CustomConversionsUnitTests {
|
||||
ConverterRegistry registry = mock(ConverterRegistry.class);
|
||||
|
||||
new CustomConversions(StoreConversions.of(DATE_EXCLUDING_SIMPLE_TYPE_HOLDER),
|
||||
Collections.singletonList(LocalDateTimeToJavaTimeInstantConverter.INSTANCE)).registerConvertersIn(registry);
|
||||
Collections.singletonList(Jsr310Converters.LocalDateTimeToInstantConverter.INSTANCE))
|
||||
.registerConvertersIn(registry);
|
||||
|
||||
verify(registry).addConverter(any(LocalDateTimeToJavaTimeInstantConverter.class));
|
||||
verify(registry).addConverter(any(Jsr310Converters.LocalDateTimeToInstantConverter.class));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1615
|
||||
@@ -346,26 +316,6 @@ class CustomConversionsUnitTests {
|
||||
}
|
||||
}
|
||||
|
||||
enum DateTimeToStringConverter implements Converter<DateTime, String> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public String convert(DateTime source) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
enum CustomDateTimeConverter implements Converter<DateTime, Date> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public Date convert(DateTime source) {
|
||||
return new Date(0);
|
||||
}
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
enum CustomTypeToStringConverter implements Converter<CustomType, String> {
|
||||
|
||||
|
||||
@@ -1,127 +0,0 @@
|
||||
/*
|
||||
* Copyright 2015-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.convert;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.threeten.bp.DateTimeUtils.*;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.support.GenericConversionService;
|
||||
import org.threeten.bp.Instant;
|
||||
import org.threeten.bp.LocalDate;
|
||||
import org.threeten.bp.LocalDateTime;
|
||||
import org.threeten.bp.LocalTime;
|
||||
import org.threeten.bp.ZoneId;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ThreeTenBackPortConverters}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @since 1.10
|
||||
*/
|
||||
class ThreeTenBackPortConvertersUnitTests {
|
||||
|
||||
static final Date NOW = new Date();
|
||||
static final ConversionService CONVERSION_SERVICE;
|
||||
|
||||
static {
|
||||
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
|
||||
for (Converter<?, ?> converter : ThreeTenBackPortConverters.getConvertersToRegister()) {
|
||||
conversionService.addConverter(converter);
|
||||
}
|
||||
|
||||
CONVERSION_SERVICE = conversionService;
|
||||
}
|
||||
|
||||
@Test // DATACMNS-606
|
||||
void convertsDateToLocalDateTime() {
|
||||
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDateTime.class).toString())
|
||||
.isEqualTo(format(NOW, "yyyy-MM-dd'T'HH:mm:ss.SSS"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-606
|
||||
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());
|
||||
}
|
||||
|
||||
@Test // DATACMNS-606
|
||||
void convertsDateToLocalDate() {
|
||||
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDate.class).toString()).isEqualTo(format(NOW, "yyyy-MM-dd"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-606
|
||||
void convertsLocalDateToDate() {
|
||||
|
||||
LocalDate now = LocalDate.now();
|
||||
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "yyyy-MM-dd")).isEqualTo(now.toString());
|
||||
}
|
||||
|
||||
@Test // DATACMNS-606
|
||||
void convertsDateToLocalTime() {
|
||||
assertThat(CONVERSION_SERVICE.convert(NOW, LocalTime.class).toString()).isEqualTo(format(NOW, "HH:mm:ss.SSS"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-606
|
||||
void convertsLocalTimeToDate() {
|
||||
|
||||
LocalTime now = LocalTime.now();
|
||||
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "HH:mm:ss.SSS")).isEqualTo(now.toString());
|
||||
}
|
||||
|
||||
@Test // DATACMNS-623
|
||||
void convertsDateToInstant() {
|
||||
|
||||
Date now = new Date();
|
||||
assertThat(CONVERSION_SERVICE.convert(now, Instant.class)).isEqualTo(toInstant(now));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-623
|
||||
void convertsInstantToDate() {
|
||||
|
||||
Date now = new Date();
|
||||
assertThat(CONVERSION_SERVICE.convert(toInstant(now), Date.class)).isEqualTo(now);
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertsZoneIdToStringAndBack() {
|
||||
|
||||
Map<String, ZoneId> ids = new HashMap<>();
|
||||
ids.put("Europe/Berlin", ZoneId.of("Europe/Berlin"));
|
||||
ids.put("+06:00", ZoneId.of("+06:00"));
|
||||
|
||||
for (Entry<String, ZoneId> entry : ids.entrySet()) {
|
||||
assertThat(CONVERSION_SERVICE.convert(entry.getValue(), String.class)).isEqualTo(entry.getKey());
|
||||
assertThat(CONVERSION_SERVICE.convert(entry.getKey(), ZoneId.class)).isEqualTo(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private static String format(Date date, String format) {
|
||||
return new SimpleDateFormat(format).format(date);
|
||||
}
|
||||
}
|
||||
@@ -19,12 +19,8 @@ import static org.assertj.core.api.Assertions.*;
|
||||
import static org.assertj.core.api.Assumptions.*;
|
||||
import static org.springframework.test.util.ReflectionTestUtils.*;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.List;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -185,19 +181,6 @@ class QuerydslPredicateBuilderUnitTests {
|
||||
assertThat(constant.getConstant()).isEqualTo("rivers,two");
|
||||
}
|
||||
|
||||
@Test // DATACMNS-734
|
||||
void bindsDateCorrectly() throws ParseException {
|
||||
|
||||
DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd");
|
||||
String date = format.print(new DateTime());
|
||||
|
||||
values.add("dateOfBirth", format.print(new DateTime()));
|
||||
|
||||
Predicate predicate = builder.getPredicate(USER_TYPE, values, DEFAULT_BINDINGS);
|
||||
|
||||
assertThat(predicate).isEqualTo(QUser.user.dateOfBirth.eq(format.parseDateTime(date).toDate()));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-883
|
||||
void automaticallyInsertsAnyStepInCollectionReference() {
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ class MapDataBinderUnitTests {
|
||||
Date reference = new Date();
|
||||
|
||||
MutablePropertyValues values = new MutablePropertyValues();
|
||||
values.add("foo.date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(reference));
|
||||
values.add("foo.date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(reference));
|
||||
|
||||
Map<String, Object> nested = new HashMap<>();
|
||||
nested.put("date", reference);
|
||||
|
||||
Reference in New Issue
Block a user