DATAREDIS-786 - Polishing.

Update Javadoc and add setIfPresent to BoundValueOperations.

Original Pull Request: #334
This commit is contained in:
Christoph Strobl
2018-04-26 13:45:21 +02:00
parent b0873d29b6
commit f1be9b736d
5 changed files with 138 additions and 15 deletions

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2018 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.mockito.Mockito.*;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
/**
* Unit tests for {@link DefaultBoundValueOperations}
*
* @author Christoph Strobl
*/
@RunWith(MockitoJUnitRunner.class)
public class DefaultBoundValueOperationsUnitTests {
DefaultBoundValueOperations<String, Object> boundValueOps;
@Mock ValueOperations<String, Object> valueOps;
static final String KEY = "key-1";
static final Object VALUE = "value";
@Before
public void setUp() {
RedisOperations<String, Object> redisOps = mock(RedisOperations.class);
when(redisOps.opsForValue()).thenReturn(valueOps);
boundValueOps = new DefaultBoundValueOperations<>(KEY, redisOps);
}
@Test // DATAREDIS-786
public void setIfPresentShouldDelegateCorrectly() {
boundValueOps.setIfPresent(VALUE);
verify(valueOps).setIfPresent(eq(KEY), eq(VALUE));
}
@Test // DATAREDIS-786
public void setIfPresentWithTimeoutShouldDelegateCorrectly() {
boundValueOps.setIfPresent(VALUE, 10, TimeUnit.SECONDS);
verify(valueOps).setIfPresent(eq(KEY), eq(VALUE), eq(10L), eq(TimeUnit.SECONDS));
}
}

View File

@@ -345,13 +345,12 @@ public class DefaultValueOperationsTests<K, V> {
}
@Test // DATAREDIS-786
public void testSetIfPresent() {
public void setIfPresentReturnsTrueWhenKeyExists() {
K key = keyFactory.instance();
V value1 = valueFactory.instance();
V value2 = valueFactory.instance();
assertFalse(valueOps.setIfPresent(key, value1));
valueOps.set(key, value1);
assertTrue(valueOps.setIfPresent(key, value2));
@@ -359,7 +358,12 @@ public class DefaultValueOperationsTests<K, V> {
}
@Test // DATAREDIS-786
public void testSetIfPresentWithExpiration() {
public void setIfPresentReturnsFalseWhenKeyDoesNotExist() {
assertFalse(valueOps.setIfPresent(keyFactory.instance(), valueFactory.instance()));
}
@Test // DATAREDIS-786
public void setIfPresentShouldSetExpirationCorrectly() {
K key = keyFactory.instance();
V value1 = valueFactory.instance();