|
| 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.cache; |
| 17 | + |
| 18 | +import static org.mockito.Matchers.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.runners.MockitoJUnitRunner; |
| 26 | +import org.springframework.data.redis.connection.RedisConnection; |
| 27 | +import org.springframework.data.redis.connection.RedisConnectionFactory; |
| 28 | +import org.springframework.data.redis.connection.ReturnType; |
| 29 | +import org.springframework.data.redis.core.RedisTemplate; |
| 30 | +import org.springframework.data.redis.serializer.RedisSerializer; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Christoph Strobl |
| 34 | + */ |
| 35 | +@SuppressWarnings("rawtypes") |
| 36 | +@RunWith(MockitoJUnitRunner.class) |
| 37 | +public class RedisCacheUnitTests { |
| 38 | + |
| 39 | +private static final String CACHE_NAME = "foo"; |
| 40 | +private static final String PREFIX = "prefix:"; |
| 41 | +private static final byte[] PREFIX_BYTES = "prefix:".getBytes(); |
| 42 | +private static final byte[] KNOWN_KEYS_SET_NAME_BYTES = (CACHE_NAME + "~keys").getBytes(); |
| 43 | + |
| 44 | +private static final String KEY = "key"; |
| 45 | +private static final byte[] KEY_BYTES = KEY.getBytes(); |
| 46 | +private static final byte[] KEY_WITH_PREFIX_BYTES = (PREFIX + KEY).getBytes(); |
| 47 | + |
| 48 | +private static final String VALUE = "value"; |
| 49 | +private static final byte[] VALUE_BYTES = VALUE.getBytes(); |
| 50 | + |
| 51 | +private static final byte[] NO_PREFIX_BYTES = new byte[] {}; |
| 52 | +private static final long EXPIRATION = 1000; |
| 53 | + |
| 54 | +RedisTemplate<?, ?> templateSpy; |
| 55 | +@Mock RedisSerializer keySerializerMock; |
| 56 | +@Mock RedisSerializer valueSerializerMock; |
| 57 | +@Mock RedisConnectionFactory connectionFactoryMock; |
| 58 | +@Mock RedisConnection connectionMock; |
| 59 | + |
| 60 | +RedisCache cache; |
| 61 | + |
| 62 | +@SuppressWarnings("unchecked") |
| 63 | +@Before |
| 64 | +public void setUp() { |
| 65 | + |
| 66 | +RedisTemplate template = new RedisTemplate(); |
| 67 | +template.setConnectionFactory(connectionFactoryMock); |
| 68 | +template.setKeySerializer(keySerializerMock); |
| 69 | +template.setValueSerializer(valueSerializerMock); |
| 70 | +template.afterPropertiesSet(); |
| 71 | + |
| 72 | +templateSpy = spy(template); |
| 73 | + |
| 74 | +when(connectionFactoryMock.getConnection()).thenReturn(connectionMock); |
| 75 | + |
| 76 | +when(keySerializerMock.serialize(any(byte[].class))).thenReturn(KEY_BYTES); |
| 77 | +when(valueSerializerMock.serialize(any(byte[].class))).thenReturn(VALUE_BYTES); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * @see DATAREDIS-369 |
| 82 | + */ |
| 83 | +@Test |
| 84 | +public void putShouldNotKeepTrackOfKnownKeysWhenPrefixIsSet() { |
| 85 | + |
| 86 | +cache = new RedisCache(CACHE_NAME, PREFIX_BYTES, templateSpy, EXPIRATION); |
| 87 | +cache.put(KEY, VALUE); |
| 88 | + |
| 89 | +verify(connectionMock, times(1)).set(eq(KEY_WITH_PREFIX_BYTES), eq(VALUE_BYTES)); |
| 90 | +verify(connectionMock, times(1)).expire(eq(KEY_WITH_PREFIX_BYTES), eq(EXPIRATION)); |
| 91 | +verify(connectionMock, never()).zAdd(eq(KNOWN_KEYS_SET_NAME_BYTES), eq(0D), any(byte[].class)); |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * @see DATAREDIS-369 |
| 96 | + */ |
| 97 | +@Test |
| 98 | +public void putShouldKeepTrackOfKnownKeysWhenNoPrefixIsSet() { |
| 99 | + |
| 100 | +cache = new RedisCache(CACHE_NAME, NO_PREFIX_BYTES, templateSpy, EXPIRATION); |
| 101 | +cache.put(KEY, VALUE); |
| 102 | + |
| 103 | +verify(connectionMock, times(1)).set(eq(KEY_BYTES), eq(VALUE_BYTES)); |
| 104 | +verify(connectionMock, times(1)).expire(eq(KEY_BYTES), eq(EXPIRATION)); |
| 105 | +verify(connectionMock, times(1)).zAdd(eq(KNOWN_KEYS_SET_NAME_BYTES), eq(0D), eq(KEY_BYTES)); |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * @see DATAREDIS-369 |
| 110 | + */ |
| 111 | +@Test |
| 112 | +public void clearShouldRemoveKeysUsingKnownKeysWhenNoPrefixIsSet() { |
| 113 | + |
| 114 | +cache = new RedisCache(CACHE_NAME, NO_PREFIX_BYTES, templateSpy, EXPIRATION); |
| 115 | +cache.clear(); |
| 116 | + |
| 117 | +verify(connectionMock, times(1)).zRange(eq(KNOWN_KEYS_SET_NAME_BYTES), eq(0L), eq(127L)); |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * @see DATAREDIS-369 |
| 122 | + */ |
| 123 | +@Test |
| 124 | +public void clearShouldCallLuaScritpToRemoveKeysWhenPrefixIsSet() { |
| 125 | + |
| 126 | +cache = new RedisCache(CACHE_NAME, PREFIX_BYTES, templateSpy, EXPIRATION); |
| 127 | +cache.clear(); |
| 128 | + |
| 129 | +verify(connectionMock, times(1)).eval(any(byte[].class), eq(ReturnType.INTEGER), eq(0), |
| 130 | +eq((PREFIX + "*").getBytes())); |
| 131 | +} |
| 132 | +} |
0 commit comments