From 3244981a00945246e53622bf45ea4151d812e6f5 Mon Sep 17 00:00:00 2001 From: Jennifer Hickey Date: Tue, 16 Jul 2013 13:40:18 -0700 Subject: [PATCH] Fix StringRedisConnection not passed to callbacks in StringTemplate.execute() DATAREDIS-222 --- .../data/redis/core/RedisTemplate.java | 13 +-- .../data/redis/core/SessionTest.java | 14 ++-- .../redis/core/StringRedisTemplateTests.java | 79 +++++++++++++++++++ .../core/StringRedisTemplateTests-context.xml | 22 ++++++ 4 files changed, 115 insertions(+), 13 deletions(-) create mode 100644 src/test/java/org/springframework/data/redis/core/StringRedisTemplateTests.java create mode 100644 src/test/resources/org/springframework/data/redis/core/StringRedisTemplateTests-context.xml diff --git a/src/main/java/org/springframework/data/redis/core/RedisTemplate.java b/src/main/java/org/springframework/data/redis/core/RedisTemplate.java index 5a8a04617..aedab9995 100644 --- a/src/main/java/org/springframework/data/redis/core/RedisTemplate.java +++ b/src/main/java/org/springframework/data/redis/core/RedisTemplate.java @@ -157,23 +157,24 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation conn = RedisConnectionUtils.getConnection(factory); boolean existingConnection = TransactionSynchronizationManager.hasResource(factory); - preProcessConnection(conn, existingConnection); - boolean pipelineStatus = conn.isPipelined(); + RedisConnection connToUse = preProcessConnection(conn, existingConnection); + + boolean pipelineStatus = connToUse.isPipelined(); if (pipeline && !pipelineStatus) { - conn.openPipeline(); + connToUse.openPipeline(); } - RedisConnection connToExpose = (exposeConnection ? conn : createRedisConnectionProxy(conn)); + RedisConnection connToExpose = (exposeConnection ? connToUse : createRedisConnectionProxy(connToUse)); T result = action.doInRedis(connToExpose); // close pipeline if (pipeline && !pipelineStatus) { - conn.closePipeline(); + connToUse.closePipeline(); } // TODO: any other connection processing? - return postProcessResult(result, conn, existingConnection); + return postProcessResult(result, connToUse, existingConnection); } finally { RedisConnectionUtils.releaseConnection(conn, factory); } diff --git a/src/test/java/org/springframework/data/redis/core/SessionTest.java b/src/test/java/org/springframework/data/redis/core/SessionTest.java index c5757030f..76cae91b4 100644 --- a/src/test/java/org/springframework/data/redis/core/SessionTest.java +++ b/src/test/java/org/springframework/data/redis/core/SessionTest.java @@ -22,6 +22,7 @@ import org.junit.Test; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.StringRedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.RedisTemplate; @@ -36,18 +37,19 @@ public class SessionTest { @Test public void testSession() throws Exception { final RedisConnection conn = mock(RedisConnection.class); + final StringRedisConnection stringConn = mock(StringRedisConnection.class); RedisConnectionFactory factory = mock(RedisConnectionFactory.class); - + final StringRedisTemplate template = spy(new StringRedisTemplate(factory)); when(factory.getConnection()).thenReturn(conn); - final StringRedisTemplate template = new StringRedisTemplate(factory); + doReturn(stringConn).when(template).preProcessConnection(eq(conn), anyBoolean()); template.execute(new SessionCallback() { - + @SuppressWarnings("rawtypes") public Object execute(RedisOperations operations) { - checkConnection(template, conn); + checkConnection(template, stringConn); template.discard(); assertSame(template, operations); - checkConnection(template, conn); + checkConnection(template, stringConn); return null; } }); @@ -55,8 +57,6 @@ public class SessionTest { private void checkConnection(RedisTemplate template, final RedisConnection expectedConnection) { template.execute(new RedisCallback() { - - public Object doInRedis(RedisConnection connection) throws DataAccessException { assertSame(expectedConnection, connection); return null; diff --git a/src/test/java/org/springframework/data/redis/core/StringRedisTemplateTests.java b/src/test/java/org/springframework/data/redis/core/StringRedisTemplateTests.java new file mode 100644 index 000000000..62a140a4c --- /dev/null +++ b/src/test/java/org/springframework/data/redis/core/StringRedisTemplateTests.java @@ -0,0 +1,79 @@ +/* + * Copyright 2013 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.core; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.StringRedisConnection; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * + * Integration test of {@link StringRedisTemplate} + * + * @author Jennifer Hickey + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class StringRedisTemplateTests { + + @Autowired + private StringRedisTemplate redisTemplate; + + @After + public void tearDown() { + redisTemplate.execute(new RedisCallback() { + public Object doInRedis(RedisConnection connection) { + connection.flushDb(); + return null; + } + }); + } + + @Test + public void testKeys() throws Exception { + redisTemplate.opsForValue().set("foo", "bar"); + assertNotNull(redisTemplate.keys("*")); + } + + @SuppressWarnings("rawtypes") + @Test(expected = IllegalArgumentException.class) + public void testTemplateNotInitialized() throws Exception { + RedisTemplate tpl = new RedisTemplate(); + tpl.setConnectionFactory(redisTemplate.getConnectionFactory()); + tpl.exec(); + } + + @Test + public void testStringTemplateExecutesWithStringConn() { + String value = redisTemplate.execute(new RedisCallback() { + public String doInRedis(RedisConnection connection) { + StringRedisConnection stringConn = (StringRedisConnection) connection; + stringConn.set("test", "it"); + return stringConn.get("test"); + } + }); + assertEquals(value,"it"); + } +} diff --git a/src/test/resources/org/springframework/data/redis/core/StringRedisTemplateTests-context.xml b/src/test/resources/org/springframework/data/redis/core/StringRedisTemplateTests-context.xml new file mode 100644 index 000000000..4114b87f0 --- /dev/null +++ b/src/test/resources/org/springframework/data/redis/core/StringRedisTemplateTests-context.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file