DATAREDIS-593 - Use Enum.name() to represent enum values.

We now serialize Enum values using their name (Enum.name()) instead of using their string representation (Enum.toString()). toString can be customized in Enums to no longer report their name but a different value. A customized value cannot be used to reinstantiate the Enum value, only its name.

This change affects serialization and will only affect new/updated values. Stored values with a customized toString representation still can't be deserialized until they were updated in Redis.

Original Pull Request: #235
This commit is contained in:
Mark Paluch
2017-01-19 17:44:44 +01:00
committed by Christoph Strobl
parent 78d6d1169e
commit 35bc928fc7
3 changed files with 27 additions and 28 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2017 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.
@@ -15,6 +15,8 @@
*/
package org.springframework.data.redis.core.convert;
import lombok.EqualsAndHashCode;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
@@ -36,10 +38,9 @@ import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.TimeToLive;
import org.springframework.data.redis.core.index.Indexed;
import lombok.EqualsAndHashCode;
/**
* @author Christoph Strobl
* @author Mark Paluch
*/
public class ConversionTestEntities {
@@ -97,7 +98,13 @@ public class ConversionTestEntities {
}
public static enum Gender {
MALE, FEMALE
MALE, FEMALE {
@Override
public String toString() {
return "Superwoman";
}
}
}
public static class AddressWithPostcode extends Address {

View File

@@ -17,7 +17,7 @@ package org.springframework.data.redis.core.convert;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
import static org.springframework.data.redis.core.convert.ConversionTestEntities.*;
import static org.springframework.data.redis.test.util.IsBucketMatcher.*;
@@ -56,20 +56,8 @@ import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.redis.core.PartialUpdate;
import org.springframework.data.redis.core.convert.ConversionTestEntities.Address;
import org.springframework.data.redis.core.convert.ConversionTestEntities.AddressWithId;
import org.springframework.data.redis.core.convert.ConversionTestEntities.AddressWithPostcode;
import org.springframework.data.redis.core.convert.ConversionTestEntities.ExipringPersonWithExplicitProperty;
import org.springframework.data.redis.core.convert.ConversionTestEntities.ExpiringPerson;
import org.springframework.data.redis.core.convert.ConversionTestEntities.Gender;
import org.springframework.data.redis.core.convert.ConversionTestEntities.Location;
import org.springframework.data.redis.core.convert.ConversionTestEntities.Person;
import org.springframework.data.redis.core.convert.ConversionTestEntities.Species;
import org.springframework.data.redis.core.convert.ConversionTestEntities.TaVeren;
import org.springframework.data.redis.core.convert.ConversionTestEntities.TheWheelOfTime;
import org.springframework.data.redis.core.convert.ConversionTestEntities.TypeWithObjectValueTypes;
import org.springframework.data.redis.core.convert.ConversionTestEntities.WithArrays;
import org.springframework.data.redis.core.convert.KeyspaceConfiguration.KeyspaceSettings;
import org.springframework.data.redis.core.convert.ConversionTestEntities.*;
import org.springframework.data.redis.core.mapping.RedisMappingContext;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.util.StringUtils;
@@ -78,8 +66,11 @@ import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Unit tests for {@link MappingRedisConverter}.
*
* @author Christoph Strobl
* @author Greg Turnquist
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class MappingRedisConverterUnitTests {
@@ -621,21 +612,21 @@ public class MappingRedisConverterUnitTests {
assertThat(target.period, is(Period.parse("P1Y2M25D")));
}
@Test // DATAREDIS-425
@Test // DATAREDIS-425, DATAREDIS-593
public void writesEnumValuesCorrectly() {
rand.gender = Gender.MALE;
rand.gender = Gender.FEMALE;
assertThat(write(rand).getBucket(), isBucket().containingUtf8String("gender", "MALE"));
assertThat(write(rand).getBucket(), isBucket().containingUtf8String("gender", "FEMALE"));
}
@Test // DATAREDIS-425
@Test // DATAREDIS-425, DATAREDIS-593
public void readsEnumValuesCorrectly() {
Person target = converter.read(Person.class,
new RedisData(Bucket.newBucketFromStringMap(Collections.singletonMap("gender", "MALE"))));
new RedisData(Bucket.newBucketFromStringMap(Collections.singletonMap("gender", "FEMALE"))));
assertThat(target.gender, is(Gender.MALE));
assertThat(target.gender, is(Gender.FEMALE));
}
@Test // DATAREDIS-425