From 6b3cb791a98a76b20d24f142d7fa2a5d4ff97a04 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 19 Oct 2018 12:27:01 +0200 Subject: [PATCH] DATAREDIS-872 - Polishing. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Create unit tests for RedisAtomic counter initialization. Refactor setIfAbsent(…) method to initializeIfAbsent() to not expose additional API methods. Original pull request: #367. --- .../support/atomic/RedisAtomicDouble.java | 20 +++----- .../support/atomic/RedisAtomicInteger.java | 20 +++----- .../redis/support/atomic/RedisAtomicLong.java | 20 +++----- .../atomic/RedisAtomicDoubleUnitTests.java | 51 +++++++++++++++++++ .../atomic/RedisAtomicIntegerUnitTests.java | 51 +++++++++++++++++++ .../atomic/RedisAtomicLongUnitTests.java | 51 +++++++++++++++++++ 6 files changed, 174 insertions(+), 39 deletions(-) create mode 100644 src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleUnitTests.java create mode 100644 src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerUnitTests.java create mode 100644 src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongUnitTests.java diff --git a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java index 47b016927..970c78160 100644 --- a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java +++ b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java @@ -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. * diff --git a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java index 74c620414..b0a01d636 100644 --- a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java +++ b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java @@ -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. * diff --git a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java index 9b983116b..b59842be6 100644 --- a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java +++ b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java @@ -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. * diff --git a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleUnitTests.java b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleUnitTests.java new file mode 100644 index 000000000..05f9a14c8 --- /dev/null +++ b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleUnitTests.java @@ -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 operationsMock; + @Mock ValueOperations 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); + } +} diff --git a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerUnitTests.java b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerUnitTests.java new file mode 100644 index 000000000..238cb2969 --- /dev/null +++ b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerUnitTests.java @@ -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 operationsMock; + @Mock ValueOperations 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); + } +} diff --git a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongUnitTests.java b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongUnitTests.java new file mode 100644 index 000000000..35a206429 --- /dev/null +++ b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongUnitTests.java @@ -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 operationsMock; + @Mock ValueOperations 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); + } +}