Skip to content

Commit 25e5dbf

Browse files
committed
Add requireState(..) assertion method to RedisAssertions.
Use RedisAssertions.requireState(..) in RedisAccessor.getRequiredConnectionFactory(). Closes spring-projects#2611
1 parent f3de2d5 commit 25e5dbf

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
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.data.redis.util.RedisAssertions;
2223
import org.springframework.lang.NonNull;
2324
import org.springframework.lang.Nullable;
2425
import org.springframework.util.Assert;
@@ -64,12 +65,7 @@ public RedisConnectionFactory getConnectionFactory() {
6465
*/
6566
@NonNull
6667
public RedisConnectionFactory getRequiredConnectionFactory() {
67-
68-
RedisConnectionFactory connectionFactory = getConnectionFactory();
69-
70-
Assert.state(connectionFactory != null, "RedisConnectionFactory is required");
71-
72-
return connectionFactory;
68+
return RedisAssertions.requireState(getConnectionFactory(), "RedisConnectionFactory is required");
7369
}
7470

7571
/**

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
}

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)