DATACASS-302 - Polish.

Resolve gh-127.
This commit is contained in:
John Blum
2018-04-10 23:02:34 -07:00
parent 2e20a21843
commit bad5119d9b
10 changed files with 80 additions and 31 deletions

View File

@@ -45,8 +45,6 @@ import com.datastax.driver.core.DataType.Name;
*/
public class CassandraCustomConversions extends org.springframework.data.convert.CustomConversions {
private static final StoreConversions STORE_CONVERSIONS;
private static final List<Object> STORE_CONVERTERS;
/**
@@ -55,6 +53,8 @@ public class CassandraCustomConversions extends org.springframework.data.convert
*/
private final static Set<Class<?>> NATIVE_TIME_TYPE_MARKERS;
private static final StoreConversions STORE_CONVERSIONS;
static {
List<Object> converters = new ArrayList<>();
@@ -74,13 +74,16 @@ public class CassandraCustomConversions extends org.springframework.data.convert
.filter(it -> {
CassandraType annotation = AnnotatedElementUtils.getMergedAnnotation(it, CassandraType.class);
return annotation != null && annotation.type() == Name.TIME;
}) //
.map(it -> {
ResolvableType classType = ResolvableType.forClass(it).as(Converter.class).getGeneric(0);
return classType.getRawClass();
}).collect(Collectors.toList());
})
.collect(Collectors.toList());
NATIVE_TIME_TYPE_MARKERS = new HashSet<>(timeMarkers);
}

View File

@@ -46,7 +46,10 @@ public abstract class CassandraJodaTimeConverters {
/**
* Returns the converters to be registered. Will only return converters in case JodaTime is present on the class path.
*
* @return
* @return a {@link Collection} of Joda Time {@link Converter Converters} to register.
* @see org.springframework.core.convert.converter.Converter
* @see java.util.Collection
* @see org.joda.time
*/
public static Collection<Converter<?, ?>> getConvertersToRegister() {

View File

@@ -37,13 +37,14 @@ import com.datastax.driver.core.DataType.Name;
* the classpath.
*
* @author Mark Paluch
* @see http://www.threeten.org/threetenbp
* @see <a href="http://www.threeten.org/threetenbp">Threeten Backport</a>
* @since 1.5
*/
public abstract class CassandraThreeTenBackPortConverters {
private static final boolean THREE_TEN_BACK_PORT_IS_PRESENT = ClassUtils.isPresent("org.threeten.bp.LocalDateTime",
ThreeTenBackPortConverters.class.getClassLoader());
private static final boolean THREE_TEN_BACK_PORT_IS_PRESENT =
ClassUtils.isPresent("org.threeten.bp.LocalDateTime",
ThreeTenBackPortConverters.class.getClassLoader());
private CassandraThreeTenBackPortConverters() {}
@@ -51,7 +52,10 @@ public abstract class CassandraThreeTenBackPortConverters {
* Returns the converters to be registered. Will only return converters in case ThreeTen Backport is on the class
* path.
*
* @return
* @return a {@link Collection} of ThreeTen Backport {@link Converter Converters} to register.
* @see org.springframework.core.convert.converter.Converter
* @see java.util.Collection
* @see org.joda.time
*/
public static Collection<Converter<?, ?>> getConvertersToRegister() {

View File

@@ -16,7 +16,6 @@
package org.springframework.data.cassandra.core.mapping;
import static org.springframework.data.cassandra.core.cql.keyspace.CreateTableSpecification.createTable;
import static org.springframework.data.cassandra.core.mapping.CassandraSimpleTypeHolder.getDataTypeFor;
import java.util.ArrayList;
import java.util.Arrays;
@@ -615,7 +614,9 @@ public class CassandraMappingContext
DataType dataType = customWriteTarget
.orElseGet(() -> {
Class<?> propertyType = typeInformation.getRequiredActualType().getType();
return this.customConversions.getCustomWriteTarget(propertyType).filter(it -> !typeInformation.isMap())
.map(it -> {
@@ -648,12 +649,13 @@ public class CassandraMappingContext
@Nullable
private DataType doGetDataType(Class<?> propertyType, Class<?> converted) {
if (customConversions instanceof CassandraCustomConversions) {
if (this.customConversions instanceof CassandraCustomConversions) {
CassandraCustomConversions conversions = (CassandraCustomConversions) customConversions;
CassandraCustomConversions conversions = (CassandraCustomConversions) this.customConversions;
if (conversions.isNativeTimeTypeMarker(converted)
|| (conversions.isNativeTimeTypeMarker(propertyType) && Long.class.equals(converted))) {
return DataType.time();
}
}

View File

@@ -15,10 +15,11 @@
*/
package org.springframework.data.cassandra.core.convert;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.joda.time.LocalTime;
import org.junit.Test;
import org.springframework.data.cassandra.core.convert.CassandraJodaTimeConverters.LocalTimeToMillisOfDayConverter;
import org.springframework.data.cassandra.core.convert.CassandraJodaTimeConverters.MillisOfDayToLocalTimeConverter;
@@ -30,14 +31,14 @@ import org.springframework.data.cassandra.core.convert.CassandraJodaTimeConverte
public class CassandraJodaTimeConvertersUnitTests {
@Test // DATACASS-302
public void shouldConvertLocalTimeToLong() {
public void shouldConvertLongToLocalTime() {
assertThat(MillisOfDayToLocalTimeConverter.INSTANCE.convert(3723000L))
.isEqualTo(LocalTime.fromMillisOfDay(3723000L));
}
@Test // DATACASS-302
public void shouldConvertLongToLocalTime() {
public void shouldConvertLocalTimeToLong() {
assertThat(LocalTimeToMillisOfDayConverter.INSTANCE.convert(LocalTime.MIDNIGHT)).isZero();
assertThat(LocalTimeToMillisOfDayConverter.INSTANCE.convert(LocalTime.fromMillisOfDay(3723000L)))

View File

@@ -15,11 +15,12 @@
*/
package org.springframework.data.cassandra.core.convert;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.LocalTime;
import org.junit.Test;
import org.springframework.data.cassandra.core.convert.CassandraJsr310Converters.LocalTimeToMillisOfDayConverter;
import org.springframework.data.cassandra.core.convert.CassandraJsr310Converters.MillisOfDayToLocalTimeConverter;
@@ -31,15 +32,17 @@ import org.springframework.data.cassandra.core.convert.CassandraJsr310Converters
public class CassandraJsr310ConvertersUnitTests {
@Test // DATACASS-302
public void shouldConvertLocalTimeToLong() {
public void shouldConvertLongToLocalTime() {
assertThat(MillisOfDayToLocalTimeConverter.INSTANCE.convert(3723000L)).isEqualTo(LocalTime.of(1, 2, 3));
assertThat(MillisOfDayToLocalTimeConverter.INSTANCE.convert(3723000L))
.isEqualTo(LocalTime.of(1, 2, 3));
}
@Test // DATACASS-302
public void shouldConvertLongToLocalTime() {
public void shouldConvertLocalTimeToLong() {
assertThat(LocalTimeToMillisOfDayConverter.INSTANCE.convert(LocalTime.MIDNIGHT)).isZero();
assertThat(LocalTimeToMillisOfDayConverter.INSTANCE.convert(LocalTime.of(1, 2, 3))).isEqualTo(3723000L);
assertThat(LocalTimeToMillisOfDayConverter.INSTANCE.convert(LocalTime.of(1, 2, 3)))
.isEqualTo(3723000L);
}
}

View File

@@ -15,12 +15,13 @@
*/
package org.springframework.data.cassandra.core.convert;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.threeten.bp.LocalTime;
import org.springframework.data.cassandra.core.convert.CassandraThreeTenBackPortConverters.LocalTimeToMillisOfDayConverter;
import org.springframework.data.cassandra.core.convert.CassandraThreeTenBackPortConverters.MillisOfDayToLocalTimeConverter;
import org.threeten.bp.LocalTime;
/**
* Unit tests for {@link CassandraThreeTenBackPortConverters}.
@@ -30,15 +31,17 @@ import org.threeten.bp.LocalTime;
public class CassandraThreeTenBackPortConvertersUnitTests {
@Test // DATACASS-302
public void shouldConvertLocalTimeToLong() {
public void shouldConvertLongToLocalTime() {
assertThat(MillisOfDayToLocalTimeConverter.INSTANCE.convert(3723000L)).isEqualTo(LocalTime.of(1, 2, 3));
assertThat(MillisOfDayToLocalTimeConverter.INSTANCE.convert(3723000L))
.isEqualTo(LocalTime.of(1, 2, 3));
}
@Test // DATACASS-302
public void shouldConvertLongToLocalTime() {
public void shouldConvertLocalTimeToLong() {
assertThat(LocalTimeToMillisOfDayConverter.INSTANCE.convert(LocalTime.MIDNIGHT)).isZero();
assertThat(LocalTimeToMillisOfDayConverter.INSTANCE.convert(LocalTime.of(1, 2, 3))).isEqualTo(3723000L);
assertThat(LocalTimeToMillisOfDayConverter.INSTANCE.convert(LocalTime.of(1, 2, 3)))
.isEqualTo(3723000L);
}
}

View File

@@ -35,10 +35,10 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;
@@ -511,9 +511,11 @@ public class CassandraTypeMappingIntegrationTests extends AbstractKeyspaceCreati
public void shouldReadAndWriteLocalDate() {
AllPossibleTypes entity = new AllPossibleTypes("1");
entity.setLocalDate(java.time.LocalDate.of(2010, 7, 4));
operations.insert(entity);
AllPossibleTypes loaded = load(entity);
assertThat(loaded.getLocalDate()).isEqualTo(entity.getLocalDate());
@@ -523,9 +525,11 @@ public class CassandraTypeMappingIntegrationTests extends AbstractKeyspaceCreati
public void shouldReadAndWriteLocalDateTime() {
AllPossibleTypes entity = new AllPossibleTypes("1");
entity.setLocalDateTime(java.time.LocalDateTime.of(2010, 7, 4, 1, 2, 3));
operations.insert(entity);
AllPossibleTypes loaded = load(entity);
assertThat(loaded.getLocalDateTime()).isEqualTo(entity.getLocalDateTime());
@@ -535,9 +539,11 @@ public class CassandraTypeMappingIntegrationTests extends AbstractKeyspaceCreati
public void shouldReadAndWriteLocalTime() {
AllPossibleTypes entity = new AllPossibleTypes("1");
entity.setLocalTime(java.time.LocalTime.of(1, 2, 3));
operations.insert(entity);
AllPossibleTypes loaded = load(entity);
assertThat(loaded.getLocalTime()).isEqualTo(entity.getLocalTime());
@@ -547,9 +553,11 @@ public class CassandraTypeMappingIntegrationTests extends AbstractKeyspaceCreati
public void shouldReadAndWriteJodaLocalTime() {
AllPossibleTypes entity = new AllPossibleTypes("1");
entity.setJodaLocalTime(org.joda.time.LocalTime.fromMillisOfDay(50000));
operations.insert(entity);
AllPossibleTypes loaded = load(entity);
assertThat(loaded.getJodaLocalTime()).isEqualTo(entity.getJodaLocalTime());
@@ -559,9 +567,11 @@ public class CassandraTypeMappingIntegrationTests extends AbstractKeyspaceCreati
public void shouldReadAndWriteInstant() {
AllPossibleTypes entity = new AllPossibleTypes("1");
entity.setInstant(java.time.Instant.now());
operations.insert(entity);
AllPossibleTypes loaded = load(entity);
assertThat(loaded.getInstant()).isEqualTo(entity.getInstant());
@@ -571,9 +581,11 @@ public class CassandraTypeMappingIntegrationTests extends AbstractKeyspaceCreati
public void shouldReadAndWriteZoneId() {
AllPossibleTypes entity = new AllPossibleTypes("1");
entity.setZoneId(java.time.ZoneId.of("Europe/Paris"));
operations.insert(entity);
AllPossibleTypes loaded = load(entity);
assertThat(loaded.getZoneId()).isEqualTo(entity.getZoneId());
@@ -583,9 +595,11 @@ public class CassandraTypeMappingIntegrationTests extends AbstractKeyspaceCreati
public void shouldReadAndWriteJodaLocalDate() {
AllPossibleTypes entity = new AllPossibleTypes("1");
entity.setJodaLocalDate(new org.joda.time.LocalDate(2010, 7, 4));
operations.insert(entity);
AllPossibleTypes loaded = load(entity);
assertThat(loaded.getJodaLocalDate()).isEqualTo(entity.getJodaLocalDate());
@@ -595,9 +609,11 @@ public class CassandraTypeMappingIntegrationTests extends AbstractKeyspaceCreati
public void shouldReadAndWriteJodaDateTime() {
AllPossibleTypes entity = new AllPossibleTypes("1");
entity.setJodaDateTime(new org.joda.time.DateTime(2010, 7, 4, 1, 2, 3));
operations.insert(entity);
AllPossibleTypes loaded = load(entity);
assertThat(loaded.getJodaDateTime()).isEqualTo(entity.getJodaDateTime());
@@ -607,9 +623,11 @@ public class CassandraTypeMappingIntegrationTests extends AbstractKeyspaceCreati
public void shouldReadAndWriteBpLocalDate() {
AllPossibleTypes entity = new AllPossibleTypes("1");
entity.setBpLocalDate(org.threeten.bp.LocalDate.of(2010, 7, 4));
operations.insert(entity);
AllPossibleTypes loaded = load(entity);
assertThat(loaded.getBpLocalDate()).isEqualTo(entity.getBpLocalDate());
@@ -619,9 +637,11 @@ public class CassandraTypeMappingIntegrationTests extends AbstractKeyspaceCreati
public void shouldReadAndWriteBpLocalDateTime() {
AllPossibleTypes entity = new AllPossibleTypes("1");
entity.setBpLocalDateTime(org.threeten.bp.LocalDateTime.of(2010, 7, 4, 1, 2, 3));
operations.insert(entity);
AllPossibleTypes loaded = load(entity);
assertThat(loaded.getBpLocalDateTime()).isEqualTo(entity.getBpLocalDateTime());
@@ -631,9 +651,11 @@ public class CassandraTypeMappingIntegrationTests extends AbstractKeyspaceCreati
public void shouldReadAndWriteBpLocalTime() {
AllPossibleTypes entity = new AllPossibleTypes("1");
entity.setBpLocalTime(org.threeten.bp.LocalTime.of(1, 2, 3));
operations.insert(entity);
AllPossibleTypes loaded = load(entity);
assertThat(loaded.getBpLocalTime()).isEqualTo(entity.getBpLocalTime());
@@ -643,9 +665,11 @@ public class CassandraTypeMappingIntegrationTests extends AbstractKeyspaceCreati
public void shouldReadAndWriteBpInstant() {
AllPossibleTypes entity = new AllPossibleTypes("1");
entity.setBpInstant(org.threeten.bp.Instant.now());
operations.insert(entity);
AllPossibleTypes loaded = load(entity);
assertThat(loaded.getBpZoneId()).isEqualTo(entity.getBpZoneId());
@@ -655,9 +679,11 @@ public class CassandraTypeMappingIntegrationTests extends AbstractKeyspaceCreati
public void shouldReadAndWriteBpZoneId() {
AllPossibleTypes entity = new AllPossibleTypes("1");
entity.setBpZoneId(org.threeten.bp.ZoneId.of("Europe/Paris"));
operations.insert(entity);
AllPossibleTypes loaded = load(entity);
assertThat(loaded.getBpZoneId()).isEqualTo(entity.getBpZoneId());
@@ -668,9 +694,11 @@ public class CassandraTypeMappingIntegrationTests extends AbstractKeyspaceCreati
public void shouldReadAndWriteCounter() {
CounterEntity entity = new CounterEntity("1");
entity.setCount(1);
operations.update(entity);
CounterEntity loaded = operations.selectOneById(entity.getId(), CounterEntity.class);
assertThat(loaded.getCount()).isEqualTo(entity.getCount());

View File

@@ -242,7 +242,8 @@ public class UpdateMapperUnitTests {
@Test // DATACASS-302
public void shouldMapTime() {
Update update = this.updateMapper.getMappedObject(Update.empty().set("localTime", LocalTime.of(1, 2, 3)),
Update update = this.updateMapper.getMappedObject(Update.empty()
.set("localTime", LocalTime.of(1, 2, 3)),
this.persistentEntity);
assertThat(update.getUpdateOperations()).hasSize(1);

View File

@@ -456,8 +456,8 @@ public class CassandraMappingContextUnitTests {
mappingContext.setCustomConversions(
new CassandraCustomConversions(Collections.singletonList(HumanToStringConverter.INSTANCE)));
CassandraPersistentEntity<?> persistentEntity = mappingContext
.getRequiredPersistentEntity(TypeWithListOfHumans.class);
CassandraPersistentEntity<?> persistentEntity =
mappingContext.getRequiredPersistentEntity(TypeWithListOfHumans.class);
assertThat(mappingContext.getDataType(persistentEntity.getRequiredPersistentProperty("humans")))
.isEqualTo(DataType.list(DataType.varchar()));
@@ -466,7 +466,8 @@ public class CassandraMappingContextUnitTests {
@Test // DATACASS-302
public void propertyTypeShouldMapToTime() {
CassandraPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(AllPossibleTypes.class);
CassandraPersistentEntity<?> persistentEntity =
mappingContext.getRequiredPersistentEntity(AllPossibleTypes.class);
assertThat(mappingContext.getDataType(persistentEntity.getRequiredPersistentProperty("localTime")))
.isEqualTo(DataType.time());