|
| 1 | +/* |
| 2 | + * Copyright 2015 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.redis.core; |
| 17 | + |
| 18 | +import static org.hamcrest.core.Is.*; |
| 19 | +import static org.hamcrest.core.IsCollectionContaining.*; |
| 20 | +import static org.junit.Assert.*; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import org.junit.After; |
| 27 | +import org.junit.AfterClass; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.junit.runners.Parameterized; |
| 32 | +import org.junit.runners.Parameterized.Parameters; |
| 33 | +import org.springframework.dao.DataAccessException; |
| 34 | +import org.springframework.data.annotation.Id; |
| 35 | +import org.springframework.data.redis.ConnectionFactoryTracker; |
| 36 | +import org.springframework.data.redis.connection.RedisConnection; |
| 37 | +import org.springframework.data.redis.connection.RedisConnectionFactory; |
| 38 | +import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; |
| 39 | +import org.springframework.data.redis.core.mapping.RedisMappingContext; |
| 40 | +import org.springframework.util.ObjectUtils; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author Christoph Strobl |
| 44 | + */ |
| 45 | +@RunWith(Parameterized.class) |
| 46 | +public class RedisKeyValueTemplateTests { |
| 47 | + |
| 48 | + RedisConnectionFactory connectionFactory; |
| 49 | + RedisKeyValueTemplate template; |
| 50 | + RedisTemplate<Object, Object> nativeTemplate; |
| 51 | + |
| 52 | + public RedisKeyValueTemplateTests(RedisConnectionFactory connectionFactory) { |
| 53 | + |
| 54 | + this.connectionFactory = connectionFactory; |
| 55 | + ConnectionFactoryTracker.add(connectionFactory); |
| 56 | + } |
| 57 | + |
| 58 | + @Parameters |
| 59 | + public static List<RedisConnectionFactory> params() { |
| 60 | + |
| 61 | + JedisConnectionFactory jedis = new JedisConnectionFactory(); |
| 62 | + jedis.afterPropertiesSet(); |
| 63 | + |
| 64 | + return Collections.<RedisConnectionFactory> singletonList(jedis); |
| 65 | + } |
| 66 | + |
| 67 | + @AfterClass |
| 68 | + public static void cleanUp() { |
| 69 | + ConnectionFactoryTracker.cleanUp(); |
| 70 | + } |
| 71 | + |
| 72 | + @Before |
| 73 | + public void setUp() { |
| 74 | + |
| 75 | + nativeTemplate = new RedisTemplate<Object, Object>(); |
| 76 | + nativeTemplate.setConnectionFactory(connectionFactory); |
| 77 | + nativeTemplate.afterPropertiesSet(); |
| 78 | + |
| 79 | + RedisMappingContext context = new RedisMappingContext(); |
| 80 | + |
| 81 | + RedisKeyValueAdapter adapter = new RedisKeyValueAdapter(nativeTemplate, context); |
| 82 | + template = new RedisKeyValueTemplate(adapter, context); |
| 83 | + } |
| 84 | + |
| 85 | + @After |
| 86 | + public void tearDown() { |
| 87 | + |
| 88 | + nativeTemplate.execute(new RedisCallback<Void>() { |
| 89 | + |
| 90 | + @Override |
| 91 | + public Void doInRedis(RedisConnection connection) throws DataAccessException { |
| 92 | + |
| 93 | + connection.flushDb(); |
| 94 | + return null; |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @see DATAREDIS-425 |
| 101 | + */ |
| 102 | + @Test |
| 103 | + public void savesObjectCorrectly() { |
| 104 | + |
| 105 | + final Person rand = new Person(); |
| 106 | + rand.firstname = "rand"; |
| 107 | + |
| 108 | + template.insert(rand); |
| 109 | + |
| 110 | + nativeTemplate.execute(new RedisCallback<Void>() { |
| 111 | + |
| 112 | + @Override |
| 113 | + public Void doInRedis(RedisConnection connection) throws DataAccessException { |
| 114 | + |
| 115 | + assertThat(connection.exists(("template-test-person:" + rand.id).getBytes()), is(true)); |
| 116 | + return null; |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @see DATAREDIS-425 |
| 123 | + */ |
| 124 | + @Test |
| 125 | + public void findProcessesCallbackReturningSingleIdCorrectly() { |
| 126 | + |
| 127 | + Person rand = new Person(); |
| 128 | + rand.firstname = "rand"; |
| 129 | + |
| 130 | + final Person mat = new Person(); |
| 131 | + mat.firstname = "mat"; |
| 132 | + |
| 133 | + template.insert(rand); |
| 134 | + template.insert(mat); |
| 135 | + |
| 136 | + List<Person> result = template.find(new RedisCallback<byte[]>() { |
| 137 | + |
| 138 | + @Override |
| 139 | + public byte[] doInRedis(RedisConnection connection) throws DataAccessException { |
| 140 | + return mat.id.getBytes(); |
| 141 | + } |
| 142 | + }, Person.class); |
| 143 | + |
| 144 | + assertThat(result.size(), is(1)); |
| 145 | + assertThat(result, hasItems(mat)); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @see DATAREDIS-425 |
| 150 | + */ |
| 151 | + @Test |
| 152 | + public void findProcessesCallbackReturningMultipleIdsCorrectly() { |
| 153 | + |
| 154 | + final Person rand = new Person(); |
| 155 | + rand.firstname = "rand"; |
| 156 | + |
| 157 | + final Person mat = new Person(); |
| 158 | + mat.firstname = "mat"; |
| 159 | + |
| 160 | + template.insert(rand); |
| 161 | + template.insert(mat); |
| 162 | + |
| 163 | + List<Person> result = template.find(new RedisCallback<List<byte[]>>() { |
| 164 | + |
| 165 | + @Override |
| 166 | + public List<byte[]> doInRedis(RedisConnection connection) throws DataAccessException { |
| 167 | + return Arrays.asList(rand.id.getBytes(), mat.id.getBytes()); |
| 168 | + } |
| 169 | + }, Person.class); |
| 170 | + |
| 171 | + assertThat(result.size(), is(2)); |
| 172 | + assertThat(result, hasItems(rand, mat)); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * @see DATAREDIS-425 |
| 177 | + */ |
| 178 | + @Test |
| 179 | + public void findProcessesCallbackReturningNullCorrectly() { |
| 180 | + |
| 181 | + Person rand = new Person(); |
| 182 | + rand.firstname = "rand"; |
| 183 | + |
| 184 | + Person mat = new Person(); |
| 185 | + mat.firstname = "mat"; |
| 186 | + |
| 187 | + template.insert(rand); |
| 188 | + template.insert(mat); |
| 189 | + |
| 190 | + List<Person> result = template.find(new RedisCallback<List<byte[]>>() { |
| 191 | + |
| 192 | + @Override |
| 193 | + public List<byte[]> doInRedis(RedisConnection connection) throws DataAccessException { |
| 194 | + return null; |
| 195 | + } |
| 196 | + }, Person.class); |
| 197 | + |
| 198 | + assertThat(result.size(), is(0)); |
| 199 | + } |
| 200 | + |
| 201 | + @RedisHash("template-test-person") |
| 202 | + static class Person { |
| 203 | + |
| 204 | + @Id String id; |
| 205 | + String firstname; |
| 206 | + |
| 207 | + @Override |
| 208 | + public int hashCode() { |
| 209 | + |
| 210 | + int result = ObjectUtils.nullSafeHashCode(firstname); |
| 211 | + return result + ObjectUtils.nullSafeHashCode(id); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public boolean equals(Object obj) { |
| 216 | + if (this == obj) { |
| 217 | + return true; |
| 218 | + } |
| 219 | + if (obj == null) { |
| 220 | + return false; |
| 221 | + } |
| 222 | + if (!(obj instanceof Person)) { |
| 223 | + return false; |
| 224 | + } |
| 225 | + Person that = (Person) obj; |
| 226 | + |
| 227 | + if (!ObjectUtils.nullSafeEquals(this.firstname, that.firstname)) { |
| 228 | + return false; |
| 229 | + } |
| 230 | + |
| 231 | + return ObjectUtils.nullSafeEquals(this.id, that.id); |
| 232 | + } |
| 233 | + |
| 234 | + } |
| 235 | +} |
0 commit comments