DATAREDIS-687 - Adapt Redis Cache tests to Spring 5.

Original pull request: #273.
This commit is contained in:
Christoph Strobl
2017-09-05 14:50:36 +02:00
committed by Mark Paluch
parent fc07828b2f
commit 6e855f779e
2 changed files with 62 additions and 27 deletions

View File

@@ -43,7 +43,9 @@ import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueRetrievalException;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.core.SpringVersion;
import org.springframework.data.redis.ConnectionFactoryTracker;
import org.springframework.data.redis.ObjectFactory;
import org.springframework.data.redis.StringObjectFactory;
@@ -301,7 +303,7 @@ public class RedisCacheTest extends AbstractNativeCacheTest<RedisTemplate> {
assertThat(wrapper.get(), equalTo(value));
}
@Test // DATAREDIS-510
@Test // DATAREDIS-510, DATAREDIS-687
public void cachePutWithNullShouldNotAddStuffToRedis() {
assumeThat(getAllowCacheNullValues(), is(false));
@@ -309,9 +311,16 @@ public class RedisCacheTest extends AbstractNativeCacheTest<RedisTemplate> {
Object key = getKey();
Object value = getValue();
cache.put(key, null);
try {
assertThat(cache.get(key), is(nullValue()));
cache.put(key, null);
assertThat(cache.get(key), is(nullValue()));
} catch (IllegalArgumentException e) {
if (!SpringVersion.getVersion().startsWith("5")) {
throw e;
}
}
}
@Test // DATAREDIS-510
@@ -326,9 +335,16 @@ public class RedisCacheTest extends AbstractNativeCacheTest<RedisTemplate> {
assertThat(cache.get(key).get(), is(equalTo(value)));
cache.put(key, null);
try {
assertThat(cache.get(key), is(nullValue()));
cache.put(key, null);
assertThat(cache.get(key), is(nullValue()));
} catch (IllegalArgumentException e) {
if (!SpringVersion.getVersion().startsWith("5")) {
throw e;
}
}
}
@Test // DATAREDIS-443, DATAREDIS-452
@@ -371,23 +387,32 @@ public class RedisCacheTest extends AbstractNativeCacheTest<RedisTemplate> {
assertThat(cache.get(key).get(), is(nullValue()));
}
@Test // DATAREDIS-553
@Test // DATAREDIS-553, DATAREDIS-687
public void testCacheGetSynchronizedNullNotAllowingNull() {
assumeThat(getAllowCacheNullValues(), is(false));
assumeThat(cache, instanceOf(RedisCache.class));
assumeThat(template.getValueSerializer(), not(instanceOf(StringRedisSerializer.class)));
Object key = getKey();
Object value = cache.get(key, new Callable<Object>() {
@Override
public Object call() throws Exception {
return null;
}
});
try {
assertThat(value, is(nullValue()));
assertThat(cache.get(key), is(nullValue()));
Object value = cache.get(key, new Callable<Object>() {
@Override
public Object call() throws Exception {
return null;
}
});
assertThat(value, is(nullValue()));
assertThat(cache.get(key), is(nullValue()));
} catch (ValueRetrievalException e) {
if (!SpringVersion.getVersion().startsWith("5")) {
throw e;
}
}
}
@Test // DATAREDIS-553

View File

@@ -34,7 +34,9 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueRetrievalException;
import org.springframework.cache.support.NullValue;
import org.springframework.core.SpringVersion;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisConnection;
@@ -132,8 +134,7 @@ public class RedisCacheUnitTests {
cache = new RedisCache(CACHE_NAME, PREFIX_BYTES, templateSpy, EXPIRATION);
cache.clear();
verify(connectionMock).eval(any(byte[].class), eq(ReturnType.INTEGER), eq(0),
eq((PREFIX + "*").getBytes()));
verify(connectionMock).eval(any(byte[].class), eq(ReturnType.INTEGER), eq(0), eq((PREFIX + "*").getBytes()));
}
@Test // DATAREDIS-402
@@ -209,23 +210,32 @@ public class RedisCacheUnitTests {
});
}
@Test // DATAREDIS-553
@Test // DATAREDIS-553, DATAREDIS-687
@SuppressWarnings("unchecked")
public void getWithCallableShouldStoreNullNotAllowingNull() throws ClassNotFoundException {
cache = new RedisCache(CACHE_NAME, NO_PREFIX_BYTES, templateSpy, 0L, false);
cache.get(KEY, new Callable<Object>() {
@Override
public Object call() throws Exception {
return null;
}
});
try {
verify(connectionMock).get(eq(KEY_BYTES));
verify(connectionMock).multi();
verify(connectionMock).del(eq(KEY_BYTES));
verify(connectionMock).exec();
cache.get(KEY, new Callable<Object>() {
@Override
public Object call() throws Exception {
return null;
}
});
verify(connectionMock).get(eq(KEY_BYTES));
verify(connectionMock).multi();
verify(connectionMock).del(eq(KEY_BYTES));
verify(connectionMock).exec();
} catch (ValueRetrievalException e) {
if (!SpringVersion.getVersion().startsWith("5")) {
throw e;
}
}
}
@Test // DATAREDIS-553