DATAREDIS-872 - Polishing.

Create unit tests for RedisAtomic counter initialization. Refactor setIfAbsent(…) method to initializeIfAbsent() to not expose additional API methods.

Original pull request: #367.
This commit is contained in:
Mark Paluch
2018-10-19 12:27:01 +02:00
parent e6c26ad93e
commit 598875df75
6 changed files with 171 additions and 36 deletions

View File

@@ -88,7 +88,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
this.operations = generalOps.opsForValue();
if (initialValue == null) {
setIfAbsent(0);
initializeIfAbsent();
} else {
set(initialValue);
}
@@ -132,12 +132,16 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
this.operations = generalOps.opsForValue();
if (initialValue == null) {
setIfAbsent(0);
initializeIfAbsent();
} else {
set(initialValue);
}
}
private void initializeIfAbsent() {
operations.setIfAbsent(key, (double) 0);
}
/**
* Get the current value.
*
@@ -162,16 +166,6 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
operations.set(key, newValue);
}
/**
* Sets to the given value, only if {@code key} does not exist.
*
* @param newValue the new value.
* @return true if successful. False return indicates that {@code key} already existed.
*/
public Boolean setIfAbsent(double newValue) {
return operations.setIfAbsent(key, newValue);
}
/**
* Set to the given value and return the old value.
*

View File

@@ -111,7 +111,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
this.operations = generalOps.opsForValue();
if (initialValue == null) {
setIfAbsent(0);
initializeIfAbsent();
} else {
set(initialValue);
}
@@ -130,12 +130,16 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
this.operations = generalOps.opsForValue();
if (initialValue == null) {
setIfAbsent(0);
initializeIfAbsent();
} else {
set(initialValue);
}
}
private void initializeIfAbsent() {
operations.setIfAbsent(key, 0);
}
/**
* Get the current value.
*
@@ -160,16 +164,6 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
operations.set(key, newValue);
}
/**
* Sets to the given value, only if {@code key} does not exist.
*
* @param newValue the new value.
* @return true if successful. False return indicates that {@code key} already existed.
*/
public Boolean setIfAbsent(int newValue) {
return operations.setIfAbsent(key, newValue);
}
/**
* Set to the given value and return the old value.
*

View File

@@ -89,7 +89,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
this.operations = generalOps.opsForValue();
if (initialValue == null) {
setIfAbsent(0);
initializeIfAbsent();
} else {
set(initialValue);
}
@@ -137,12 +137,16 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
this.operations = generalOps.opsForValue();
if (initialValue == null) {
setIfAbsent(0);
initializeIfAbsent();
} else {
set(initialValue);
}
}
private void initializeIfAbsent() {
operations.setIfAbsent(key, (long) 0);
}
/**
* Get the current value.
*
@@ -167,16 +171,6 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
operations.set(key, newValue);
}
/**
* Sets to the given value, only if {@code key} does not exist.
*
* @param newValue the new value.
* @return true if successful. False return indicates that {@code key} already existed.
*/
public Boolean setIfAbsent(long newValue) {
return operations.setIfAbsent(key, newValue);
}
/**
* Set to the given value and return the old value.
*

View File

@@ -0,0 +1,51 @@
/*
* 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.support.atomic;
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* Unit tests for {@link RedisAtomicDouble}.
*
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class RedisAtomicDoubleUnitTests {
@Mock RedisOperations<String, Double> operationsMock;
@Mock ValueOperations<String, Double> valueOperationsMock;
@Test // DATAREDIS-872
@SuppressWarnings("unchecked")
public void shouldUseSetIfAbsentForInitialValue() {
when(operationsMock.opsForValue()).thenReturn(valueOperationsMock);
when(operationsMock.getKeySerializer()).thenReturn(mock(RedisSerializer.class));
when(operationsMock.getValueSerializer()).thenReturn(mock(RedisSerializer.class));
new RedisAtomicDouble("id", operationsMock);
verify(valueOperationsMock).setIfAbsent("id", 0d);
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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.support.atomic;
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* Unit tests for {@link RedisAtomicInteger}.
*
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class RedisAtomicIntegerUnitTests {
@Mock RedisOperations<String, Integer> operationsMock;
@Mock ValueOperations<String, Integer> valueOperationsMock;
@Test // DATAREDIS-872
@SuppressWarnings("unchecked")
public void shouldUseSetIfAbsentForInitialValue() {
when(operationsMock.opsForValue()).thenReturn(valueOperationsMock);
when(operationsMock.getKeySerializer()).thenReturn(mock(RedisSerializer.class));
when(operationsMock.getValueSerializer()).thenReturn(mock(RedisSerializer.class));
new RedisAtomicInteger("id", operationsMock);
verify(valueOperationsMock).setIfAbsent("id", 0);
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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.support.atomic;
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* Unit tests for {@link RedisAtomicLong}.
*
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class RedisAtomicLongUnitTests {
@Mock RedisOperations<String, Long> operationsMock;
@Mock ValueOperations<String, Long> valueOperationsMock;
@Test // DATAREDIS-872
@SuppressWarnings("unchecked")
public void shouldUseSetIfAbsentForInitialValue() {
when(operationsMock.opsForValue()).thenReturn(valueOperationsMock);
when(operationsMock.getKeySerializer()).thenReturn(mock(RedisSerializer.class));
when(operationsMock.getValueSerializer()).thenReturn(mock(RedisSerializer.class));
new RedisAtomicLong("id", operationsMock);
verify(valueOperationsMock).setIfAbsent("id", 0L);
}
}