DATAREDIS-317 - Report inappropriate RedisTemplate more eagerly in RedisAtomic wrappers.

We now validate the given RedisTemplate in the constructor of RedisAtomicLong/RedisAtomicInteger/RedisAtomicDouble to report an inappropriate RedisTemplate more eagerly.

Original pull request: #83.
This commit is contained in:
Thomas Darimont
2014-06-18 12:52:26 +02:00
parent 42ff3ee1e1
commit 5f48ea8ed4
7 changed files with 235 additions and 25 deletions

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2014 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
*
* http://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.redis.support.atomic;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
/**
* @author Thomas Darimont
*/
public abstract class AbstractRedisAtomicsTests {
@Rule public ExpectedException expectedException = ExpectedException.none();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 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,11 +15,9 @@
*/
package org.springframework.data.redis.support.atomic;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import java.util.Collection;
import java.util.Date;
@@ -37,14 +35,18 @@ import org.springframework.data.redis.RedisTestProfileValueSource;
import org.springframework.data.redis.connection.ConnectionUtils;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Integration test of {@link RedisAtomicDouble}
*
* @author Jennifer Hickey
* @author Thomas Darimont
*/
@RunWith(Parameterized.class)
public class RedisAtomicDoubleTests {
public class RedisAtomicDoubleTests extends AbstractRedisAtomicsTests {
private RedisAtomicDouble doubleCounter;
@@ -164,4 +166,48 @@ public class RedisAtomicDoubleTests {
assertEquals("5.6", new String(factory.getConnection().get("foodouble".getBytes())));
assertNull(factory.getConnection().get((getClass().getSimpleName() + ":double").getBytes()));
}
/**
* @see DATAREDIS-317
*/
@Test
public void testShouldThrowExceptionIfRedisAtomicDoubleIsUsedWithRedisTemplateAndNoKeySerializer() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("a valid key serializer in template is required");
new RedisAtomicDouble("foo", new RedisTemplate<String, Double>());
}
/**
* @see DATAREDIS-317
*/
@Test
public void testShouldThrowExceptionIfRedisAtomicDoubleIsUsedWithRedisTemplateAndNoValueSerializer() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("a valid value serializer in template is required");
RedisTemplate<String, Double> template = new RedisTemplate<String, Double>();
template.setKeySerializer(new StringRedisSerializer());
new RedisAtomicDouble("foo", template);
}
/**
* @see DATAREDIS-317
*/
@Test
public void testShouldBeAbleToUseRedisAtomicDoubleWithProperlyConfiguredRedisTemplate() {
RedisTemplate<String, Double> template = new RedisTemplate<String, Double>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericToStringSerializer<Double>(Double.class));
template.afterPropertiesSet();
RedisAtomicDouble ral = new RedisAtomicDouble("DATAREDIS-317.atomicDouble", template);
ral.set(32.23);
assertThat(ral.get(), is(32.23));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 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,10 +15,9 @@
*/
package org.springframework.data.redis.support.atomic;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import java.util.Collection;
import java.util.concurrent.CountDownLatch;
@@ -35,15 +34,19 @@ import org.springframework.data.redis.ConnectionFactoryTracker;
import org.springframework.data.redis.connection.ConnectionUtils;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Integration test of {@link RedisAtomicInteger}
*
* @author Costin Leau
* @author Jennifer Hickey
* @author Thomas Darimont
*/
@RunWith(Parameterized.class)
public class RedisAtomicIntegerTests {
public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
private RedisAtomicInteger intCounter;
private RedisConnectionFactory factory;
@@ -133,4 +136,48 @@ public class RedisAtomicIntegerTests {
assertFalse("counter already modified", failed.get());
}
/**
* @see DATAREDIS-317
*/
@Test
public void testShouldThrowExceptionIfRedisAtomicIntegerIsUsedWithRedisTemplateAndNoKeySerializer() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("a valid key serializer in template is required");
new RedisAtomicInteger("foo", new RedisTemplate<String, Integer>());
}
/**
* @see DATAREDIS-317
*/
@Test
public void testShouldThrowExceptionIfRedisAtomicIntegerIsUsedWithRedisTemplateAndNoValueSerializer() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("a valid value serializer in template is required");
RedisTemplate<String, Integer> template = new RedisTemplate<String, Integer>();
template.setKeySerializer(new StringRedisSerializer());
new RedisAtomicInteger("foo", template);
}
/**
* @see DATAREDIS-317
*/
@Test
public void testShouldBeAbleToUseRedisAtomicIntegerWithProperlyConfiguredRedisTemplate() {
RedisTemplate<String, Integer> template = new RedisTemplate<String, Integer>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericToStringSerializer<Integer>(Integer.class));
template.afterPropertiesSet();
RedisAtomicInteger ral = new RedisAtomicInteger("DATAREDIS-317.atomicInteger", template);
ral.set(32);
assertThat(ral.get(), is(32));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 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,10 +15,9 @@
*/
package org.springframework.data.redis.support.atomic;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import java.util.Collection;
@@ -32,15 +31,19 @@ import org.springframework.data.redis.ConnectionFactoryTracker;
import org.springframework.data.redis.connection.ConnectionUtils;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Integration test of {@link RedisAtomicLong}
*
* @author Costin Leau
* @author Jennifer Hickey
* @author Thomas Darimont
*/
@RunWith(Parameterized.class)
public class RedisAtomicLongTests {
public class RedisAtomicLongTests extends AbstractRedisAtomicsTests {
private RedisAtomicLong longCounter;
private RedisConnectionFactory factory;
@@ -103,4 +106,49 @@ public class RedisAtomicLongTests {
RedisAtomicLong keyCopy = new RedisAtomicLong(longCounter.getKey(), factory);
assertEquals(longCounter.get(), keyCopy.get());
}
/**
* @see DATAREDIS-317
*/
@Test
public void testShouldThrowExceptionIfAtomicLongIsUsedWithRedisTemplateAndNoKeySerializer() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("a valid key serializer in template is required");
RedisTemplate<String, Long> template = new RedisTemplate<String, Long>();
new RedisAtomicLong("foo", template);
}
/**
* @see DATAREDIS-317
*/
@Test
public void testShouldThrowExceptionIfAtomicLongIsUsedWithRedisTemplateAndNoValueSerializer() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("a valid value serializer in template is required");
RedisTemplate<String, Long> template = new RedisTemplate<String, Long>();
template.setKeySerializer(new StringRedisSerializer());
new RedisAtomicLong("foo", template);
}
/**
* @see DATAREDIS-317
*/
@Test
public void testShouldBeAbleToUseRedisAtomicLongWithProperlyConfiguredRedisTemplate() {
RedisTemplate<String, Long> template = new RedisTemplate<String, Long>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
template.afterPropertiesSet();
RedisAtomicLong ral = new RedisAtomicLong("DATAREDIS-317.atomicLong", template);
ral.set(32L);
assertThat(ral.get(), is(32L));
}
}