DATAREDIS-421 - Fix NPE when using HashMapper implementations.

We now make sure the available HashMapper implementations avoid exposing null values. Prior to this null values would have been added to the converted Hash causing trouble when saving those to redis since hashes are expected to not contain any null values
This commit is contained in:
Christoph Strobl
2015-08-19 14:45:44 +02:00
committed by Oliver Gierke
parent 35424bb9da
commit 74ea61c51c
6 changed files with 81 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2015 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.
@@ -19,6 +19,10 @@ import static org.junit.Assert.*;
import java.util.Map;
import org.hamcrest.core.IsCollectionContaining;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.IsNot;
import org.hamcrest.core.IsNull;
import org.junit.Test;
import org.springframework.data.redis.Address;
import org.springframework.data.redis.Person;
@@ -26,24 +30,43 @@ import org.springframework.data.redis.hash.HashMapper;
/**
* @author Costin Leau
* @author Christoph Strobl
*/
public abstract class AbstractHashMapperTest {
protected abstract HashMapper mapperFor(Class t);
private void test(Object o) {
HashMapper<Object, Object, Object> mapper = mapperFor(o.getClass());
@SuppressWarnings("rawtypes")
protected abstract <T> HashMapper mapperFor(Class<T> t);
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void assertBackAndForwardMapping(Object o) {
HashMapper mapper = mapperFor(o.getClass());
Map hash = mapper.toHash(o);
System.out.println("object hash " + hash.size() + " is " + hash);
assertEquals(o, mapper.fromHash(hash));
assertThat(mapper.fromHash(hash), IsEqual.equalTo(o));
}
@Test
public void testSimpleBean() throws Exception {
test(new Address("Broadway", 1));
assertBackAndForwardMapping(new Address("Broadway", 1));
}
@Test
public void testNestedBean() throws Exception {
test(new Person("George", "Enescu", 74, new Address("liveni", 19)));
assertBackAndForwardMapping(new Person("George", "Enescu", 74, new Address("liveni", 19)));
}
/**
* @see DATAREDIS-421
*/
@Test
public void toHashShouldTreatNullValuesCorrectly() {
Person source = new Person("rand", null, 19);
assertBackAndForwardMapping(source);
assertThat((Iterable<Object>) mapperFor(Person.class).toHash(source).values(),
IsNot.not(IsCollectionContaining.hasItems(IsNull.nullValue())));
}
}

View File

@@ -17,19 +17,20 @@ package org.springframework.data.redis.mapping;
import org.junit.Test;
import org.springframework.data.redis.hash.BeanUtilsHashMapper;
import org.springframework.data.redis.hash.HashMapper;
/**
* @author Costin Leau
* @author Christoph Strobl
*/
public class BeanUtilsHashMapperTest extends AbstractHashMapperTest {
protected HashMapper mapperFor(Class t) {
return new BeanUtilsHashMapper(t);
protected <T> BeanUtilsHashMapper<T> mapperFor(Class<T> t) {
return new BeanUtilsHashMapper<T>(t);
}
@Test(expected = Exception.class)
public void testNestedBean() throws Exception {
super.testNestedBean();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2015 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,12 +15,16 @@
*/
package org.springframework.data.redis.mapping;
import org.springframework.data.redis.hash.HashMapper;
import org.springframework.data.redis.hash.JacksonHashMapper;
/**
* @author Costin Leau
* @author Christoph Strobl
*/
public class JacksonHashMapperTest extends AbstractHashMapperTest {
protected HashMapper mapperFor(Class t) {
return new JacksonHashMapper(t);
protected <T> JacksonHashMapper<T> mapperFor(Class<T> t) {
return new JacksonHashMapper<T>(t);
}
}