| 
15 | 15 |  */  | 
16 | 16 | package org.springframework.data.redis.cache;  | 
17 | 17 | 
 
  | 
 | 18 | +import static org.hamcrest.Matchers.is;  | 
 | 19 | +import static org.hamcrest.Matchers.notNullValue;  | 
 | 20 | +import static org.hamcrest.Matchers.nullValue;  | 
18 | 21 | import static org.hamcrest.core.IsEqual.*;  | 
19 | 22 | import static org.junit.Assert.*;  | 
20 | 23 | import static org.mockito.Matchers.*;  | 
 | 
30 | 33 | import org.junit.runner.RunWith;  | 
31 | 34 | import org.mockito.Mock;  | 
32 | 35 | import org.mockito.runners.MockitoJUnitRunner;  | 
 | 36 | +import org.springframework.cache.Cache;  | 
33 | 37 | import org.springframework.data.redis.RedisSystemException;  | 
34 | 38 | import org.springframework.data.redis.connection.RedisClusterConnection;  | 
35 | 39 | import org.springframework.data.redis.connection.RedisConnection;  | 
 | 
40 | 44 | 
 
  | 
41 | 45 | /**  | 
42 | 46 |  * @author Christoph Strobl  | 
 | 47 | + * @author Mark Paluch  | 
43 | 48 |  */  | 
44 | 49 | @SuppressWarnings("rawtypes")  | 
45 | 50 | @RunWith(MockitoJUnitRunner.class)  | 
@@ -154,6 +159,56 @@ public void putShouldNotExpireKnownKeysSetWhenTtlIsZero() {  | 
154 | 159 | verify(connectionMock, never()).expire(eq(KNOWN_KEYS_SET_NAME_BYTES), anyLong());  | 
155 | 160 | }  | 
156 | 161 | 
 
  | 
 | 162 | +/**  | 
 | 163 | + * @see DATAREDIS-542  | 
 | 164 | + */  | 
 | 165 | +@Test  | 
 | 166 | +public void putIfAbsentShouldExpireWhenValueWasSet() {  | 
 | 167 | + | 
 | 168 | +when(connectionMock.setNX(KEY_BYTES, VALUE_BYTES)).thenReturn(true);  | 
 | 169 | + | 
 | 170 | +cache = new RedisCache(CACHE_NAME, NO_PREFIX_BYTES, templateSpy, 10L);  | 
 | 171 | +Cache.ValueWrapper valueWrapper = cache.putIfAbsent(KEY, VALUE);  | 
 | 172 | + | 
 | 173 | +assertThat(valueWrapper, is(nullValue()));  | 
 | 174 | +verify(connectionMock).setNX(KEY_BYTES, VALUE_BYTES);  | 
 | 175 | +verify(connectionMock).expire(eq(KEY_BYTES), anyLong());  | 
 | 176 | +}  | 
 | 177 | + | 
 | 178 | +/**  | 
 | 179 | + * @see DATAREDIS-542  | 
 | 180 | + */  | 
 | 181 | +@Test  | 
 | 182 | +public void putIfAbsentShouldNotExpireWhenValueWasNotSetAndRedisContainsOtherData() {  | 
 | 183 | + | 
 | 184 | +String other = "other";  | 
 | 185 | +when(connectionMock.setNX(KEY_BYTES, VALUE_BYTES)).thenReturn(false);  | 
 | 186 | +when(connectionMock.get(KEY_BYTES)).thenReturn(other.getBytes());  | 
 | 187 | +when(valueSerializerMock.deserialize(eq(other.getBytes()))).thenReturn(other);  | 
 | 188 | + | 
 | 189 | +cache = new RedisCache(CACHE_NAME, NO_PREFIX_BYTES, templateSpy, 10L);  | 
 | 190 | +Cache.ValueWrapper valueWrapper = cache.putIfAbsent(KEY, VALUE);  | 
 | 191 | + | 
 | 192 | +assertThat(valueWrapper, is(notNullValue()));  | 
 | 193 | +verify(connectionMock, never()).expire(eq(KEY_BYTES), anyLong());  | 
 | 194 | +}  | 
 | 195 | + | 
 | 196 | +/**  | 
 | 197 | + * @see DATAREDIS-542  | 
 | 198 | + */  | 
 | 199 | +@Test  | 
 | 200 | +public void putIfAbsentShouldExpireWhenValueWasNotSetAndRedisContainsSameData() {  | 
 | 201 | + | 
 | 202 | +when(connectionMock.setNX(KEY_BYTES, VALUE_BYTES)).thenReturn(false);  | 
 | 203 | +when(connectionMock.get(KEY_BYTES)).thenReturn(VALUE_BYTES);  | 
 | 204 | + | 
 | 205 | +cache = new RedisCache(CACHE_NAME, NO_PREFIX_BYTES, templateSpy, 10L);  | 
 | 206 | +Cache.ValueWrapper valueWrapper = cache.putIfAbsent(KEY, VALUE);  | 
 | 207 | + | 
 | 208 | +assertThat(valueWrapper, is(notNullValue()));  | 
 | 209 | +verify(connectionMock).expire(eq(KEY_BYTES), anyLong());  | 
 | 210 | +}  | 
 | 211 | + | 
157 | 212 | /**  | 
158 | 213 |  * @see DATAREDIS-443  | 
159 | 214 |  */  | 
 | 
0 commit comments