Skip to content

Commit 2b2e050

Browse files
committed
Add requireState(..) assertion method to RedisAssertions.
Use RedisAssertions.requireState(..) in RedisAccessor.getRequiredConnectionFactory(). Closes #2611
1 parent f3de2d5 commit 2b2e050

File tree

4 files changed

+155
-9
lines changed

4 files changed

+155
-9
lines changed

src/main/java/org/springframework/data/redis/core/RedisAccessor.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
import org.apache.commons.logging.LogFactory;
2020
import org.springframework.beans.factory.InitializingBean;
2121
import org.springframework.data.redis.connection.RedisConnectionFactory;
22-
import org.springframework.lang.NonNull;
22+
import org.springframework.data.redis.util.RedisAssertions;
2323
import org.springframework.lang.Nullable;
24-
import org.springframework.util.Assert;
2524

2625
/**
2726
* Base class for {@link RedisTemplate} defining common properties. Not intended to be used directly.
@@ -62,14 +61,8 @@ public RedisConnectionFactory getConnectionFactory() {
6261
* @see #getConnectionFactory()
6362
* @since 2.0
6463
*/
65-
@NonNull
6664
public RedisConnectionFactory getRequiredConnectionFactory() {
67-
68-
RedisConnectionFactory connectionFactory = getConnectionFactory();
69-
70-
Assert.state(connectionFactory != null, "RedisConnectionFactory is required");
71-
72-
return connectionFactory;
65+
return RedisAssertions.requireState(getConnectionFactory(), "RedisConnectionFactory is required");
7366
}
7467

7568
/**

src/main/java/org/springframework/data/redis/util/RedisAssertions.java

+29
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,33 @@ public static <T> T requireObject(@Nullable T target, Supplier<String> message)
5656
Assert.notNull(target, message);
5757
return target;
5858
}
59+
60+
/**
61+
* Asserts the given {@link Object} is not {@literal null}.
62+
*
63+
* @param <T> {@link Class type} of {@link Object} being asserted.
64+
* @param target {@link Object} to evaluate.
65+
* @param message {@link String} containing the message for the thrown exception.
66+
* @param arguments array of {@link Object} arguments used to format the {@link String message}.
67+
* @return the given {@link Object}.
68+
* @throws IllegalArgumentException if the {@link Object target} is {@literal null}.
69+
* @see #requireObject(Object, Supplier)
70+
*/
71+
public static <T> T requireState(@Nullable T target, String message, Object... arguments) {
72+
return requireState(target, () -> String.format(message, arguments));
73+
}
74+
75+
/**
76+
* Asserts the given {@link Object} is not {@literal null}.
77+
*
78+
* @param <T> {@link Class type} of {@link Object} being asserted.
79+
* @param target {@link Object} to evaluate.
80+
* @param message {@link Supplier} supplying the message for the thrown exception.
81+
* @return the given {@link Object}.
82+
* @throws IllegalArgumentException if the {@link Object target} is {@literal null}.
83+
*/
84+
public static <T> T requireState(@Nullable T target, Supplier<String> message) {
85+
Assert.state(target != null, message);
86+
return target;
87+
}
5988
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2017-2023 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+
* https://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.core;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
20+
import static org.mockito.Mockito.doReturn;
21+
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.spy;
23+
import static org.mockito.Mockito.times;
24+
import static org.mockito.Mockito.verify;
25+
import static org.mockito.Mockito.verifyNoInteractions;
26+
import static org.mockito.Mockito.verifyNoMoreInteractions;
27+
28+
import org.junit.jupiter.api.Test;
29+
30+
import org.springframework.data.redis.connection.RedisConnectionFactory;
31+
32+
/**
33+
* Unit Tests for {@link RedisAccessor}.
34+
*
35+
* @author John Blum
36+
* @see org.junit.jupiter.api.Test
37+
* @see org.springframework.data.redis.core.RedisAccessor
38+
* @since 3.2.0
39+
*/
40+
public class RedisAccessorUnitTests {
41+
42+
@Test
43+
public void setAndGetConnectionFactory() {
44+
45+
RedisConnectionFactory mockConnectionFactory = mock(RedisConnectionFactory.class);
46+
47+
RedisAccessor redisAccessor = new RedisAccessor();
48+
49+
assertThat(redisAccessor.getConnectionFactory()).isNull();
50+
51+
redisAccessor.setConnectionFactory(mockConnectionFactory);
52+
53+
assertThat(redisAccessor.getConnectionFactory()).isSameAs(mockConnectionFactory);
54+
assertThat(redisAccessor.getRequiredConnectionFactory()).isSameAs(mockConnectionFactory);
55+
56+
redisAccessor.setConnectionFactory(null);
57+
58+
assertThat(redisAccessor.getConnectionFactory()).isNull();
59+
60+
verifyNoInteractions(mockConnectionFactory);
61+
}
62+
63+
@Test
64+
public void getRequiredConnectionFactoryWhenNull() {
65+
66+
assertThatIllegalStateException()
67+
.isThrownBy(() -> new RedisAccessor().getRequiredConnectionFactory())
68+
.withMessage("RedisConnectionFactory is required")
69+
.withNoCause();
70+
}
71+
72+
@Test
73+
public void afterPropertiesSetCallsGetRequiredConnectionFactory() {
74+
75+
RedisConnectionFactory mockConnectionFactory = mock(RedisConnectionFactory.class);
76+
77+
RedisAccessor redisAccessor = spy(new RedisAccessor());
78+
79+
doReturn(mockConnectionFactory).when(redisAccessor).getRequiredConnectionFactory();
80+
81+
redisAccessor.afterPropertiesSet();
82+
83+
verify(redisAccessor, times(1)).afterPropertiesSet();
84+
verify(redisAccessor, times(1)).getRequiredConnectionFactory();
85+
verifyNoMoreInteractions(redisAccessor);
86+
}
87+
}

src/test/java/org/springframework/data/redis/util/RedisAssertionsUnitTests.java

+37
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
20+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2021
import static org.mockito.Mockito.doReturn;
2122
import static org.mockito.Mockito.times;
2223
import static org.mockito.Mockito.verify;
@@ -79,4 +80,40 @@ public void requireObjectWithSupplierThrowsIllegalArgumentException() {
7980
verify(this.mockSupplier, times(1)).get();
8081
verifyNoMoreInteractions(this.mockSupplier);
8182
}
83+
84+
@Test
85+
public void requireStateWithMessageAndArgumentsIsSuccessful() {
86+
assertThat(RedisAssertions.requireState("test", "Mock message")).isEqualTo("test");
87+
}
88+
89+
@Test
90+
public void requireStateWithMessageAndArgumentsThrowsIllegalStateException() {
91+
92+
assertThatIllegalStateException()
93+
.isThrownBy(() -> RedisAssertions.requireState(null, "This is a %s", "test"))
94+
.withMessage("This is a test")
95+
.withNoCause();
96+
}
97+
98+
@Test
99+
public void requireStateWithSupplierIsSuccessful() {
100+
101+
assertThat(RedisAssertions.requireState("test", this.mockSupplier)).isEqualTo("test");
102+
103+
verifyNoInteractions(this.mockSupplier);
104+
}
105+
106+
@Test
107+
public void requiredStateWithSupplierThrowsIllegalStateException() {
108+
109+
doReturn("Mock message").when(this.mockSupplier).get();
110+
111+
assertThatIllegalStateException()
112+
.isThrownBy(() -> RedisAssertions.requireState(null, this.mockSupplier))
113+
.withMessage("Mock message")
114+
.withNoCause();
115+
116+
verify(this.mockSupplier, times(1)).get();
117+
verifyNoMoreInteractions(this.mockSupplier);
118+
}
82119
}

0 commit comments

Comments
 (0)