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:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2016 the original author or authors.
|
||||
* Copyright 2013-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.
|
||||
@@ -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);
|
||||
}
|
||||
@@ -131,12 +131,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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value.
|
||||
*
|
||||
@@ -161,16 +165,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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomically sets to the given value and returns the old value.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2016 the original author or authors.
|
||||
* Copyright 2011-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.
|
||||
@@ -112,7 +112,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 give value and return the old value.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2016 the original author or authors.
|
||||
* Copyright 2011-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.
|
||||
@@ -90,7 +90,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
this.operations = generalOps.opsForValue();
|
||||
|
||||
if (initialValue == null) {
|
||||
setIfAbsent(0);
|
||||
initializeIfAbsent();
|
||||
} else {
|
||||
set(initialValue);
|
||||
}
|
||||
@@ -138,12 +138,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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value.
|
||||
*
|
||||
@@ -168,16 +172,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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomically sets to the given value and returns the old value.
|
||||
*
|
||||
|
||||
@@ -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.runners.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);
|
||||
}
|
||||
}
|
||||
@@ -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.runners.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);
|
||||
}
|
||||
}
|
||||
@@ -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.runners.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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user