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 d462f283e..450c5991e 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/DefaultSetOperationsTests.java b/src/test/java/org/springframework/data/redis/core/DefaultSetOperationsTests.java index 8d591b467..22558f4dd 100644 --- a/src/test/java/org/springframework/data/redis/core/DefaultSetOperationsTests.java +++ b/src/test/java/org/springframework/data/redis/core/DefaultSetOperationsTests.java @@ -38,7 +38,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration("RedisTemplateTests-context.xml") +@ContextConfiguration("StringRedisTemplateTests-context.xml") @ProfileValueSourceConfiguration(RedisTestProfileValueSource.class) public class DefaultSetOperationsTests { diff --git a/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java b/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java index f8c538c27..6ab0cfac1 100644 --- a/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java +++ b/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java @@ -27,7 +27,9 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.RedisTestProfileValueSource; import org.springframework.test.annotation.IfProfileValue; +import org.springframework.test.annotation.ProfileValueSourceConfiguration; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -38,7 +40,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration("RedisTemplateTests-context.xml") +@ContextConfiguration("StringRedisTemplateTests-context.xml") +@ProfileValueSourceConfiguration(RedisTestProfileValueSource.class) public class DefaultValueOperationsTests { @Autowired 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/RedisTemplateTests.java b/src/test/java/org/springframework/data/redis/core/StringRedisTemplateTests.java similarity index 73% rename from src/test/java/org/springframework/data/redis/core/RedisTemplateTests.java rename to src/test/java/org/springframework/data/redis/core/StringRedisTemplateTests.java index 8f803ec9f..3b310d747 100644 --- a/src/test/java/org/springframework/data/redis/core/RedisTemplateTests.java +++ b/src/test/java/org/springframework/data/redis/core/StringRedisTemplateTests.java @@ -25,28 +25,38 @@ 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.RedisTestProfileValueSource; import org.springframework.data.redis.TestCondition; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.StringRedisConnection; import org.springframework.test.annotation.IfProfileValue; +import org.springframework.test.annotation.ProfileValueSourceConfiguration; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * - * Integration test of {@link RedisTemplate} - * + * Integration test of {@link StringRedisTemplate} + * * @author Jennifer Hickey * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration -public class RedisTemplateTests { +@ProfileValueSourceConfiguration(RedisTestProfileValueSource.class) +public class StringRedisTemplateTests { @Autowired - private RedisTemplate redisTemplate; + private StringRedisTemplate redisTemplate; @After public void tearDown() { - redisTemplate.getConnectionFactory().getConnection().flushDb(); + redisTemplate.execute(new RedisCallback() { + public Object doInRedis(RedisConnection connection) { + connection.flushDb(); + return null; + } + }); } @Test @@ -90,4 +100,15 @@ public class RedisTemplateTests { 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/RedisTemplateTests-context.xml b/src/test/resources/org/springframework/data/redis/core/StringRedisTemplateTests-context.xml similarity index 100% rename from src/test/resources/org/springframework/data/redis/core/RedisTemplateTests-context.xml rename to src/test/resources/org/springframework/data/redis/core/StringRedisTemplateTests-context.xml