Skip to content

Commit e423851

Browse files
committed
Polish for #2696.
Closes #2701 Original pull request: #2696
1 parent b13690c commit e423851

File tree

2 files changed

+199
-2
lines changed

2 files changed

+199
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ public class ConvertingCursor<S, T> implements Cursor<T> {
4040
*/
4141
public ConvertingCursor(Cursor<S> cursor, Converter<S, T> converter) {
4242

43-
Assert.notNull(cursor, "Cursor delegate must not be 'null'");
44-
Assert.notNull(converter, "Converter must not be 'null'");
43+
Assert.notNull(cursor, "Cursor must not be null");
44+
Assert.notNull(converter, "Converter must not be null");
45+
4546
this.delegate = cursor;
4647
this.converter = converter;
4748
}
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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.assertThatIllegalArgumentException;
20+
import static org.mockito.ArgumentMatchers.any;
21+
import static org.mockito.ArgumentMatchers.eq;
22+
import static org.mockito.Mockito.doAnswer;
23+
import static org.mockito.Mockito.doReturn;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.times;
26+
import static org.mockito.Mockito.verify;
27+
import static org.mockito.Mockito.verifyNoInteractions;
28+
import static org.mockito.Mockito.verifyNoMoreInteractions;
29+
30+
import org.junit.jupiter.api.Test;
31+
32+
import org.springframework.core.convert.converter.Converter;
33+
34+
/**
35+
* Unit Tests for {@link ConvertingCursor}.
36+
*
37+
* @author John Blum
38+
*/
39+
@SuppressWarnings("unchecked")
40+
class ConvertingCursorUnitTests {
41+
42+
@Test // #2701
43+
void constructConvertingCursor() {
44+
45+
Converter<Object, Object> mockConverter = mock(Converter.class);
46+
Cursor<Object> mockCursor = mock(Cursor.class);
47+
48+
new ConvertingCursor<>(mockCursor, mockConverter);
49+
50+
verifyNoInteractions(mockConverter, mockCursor);
51+
}
52+
53+
@Test // #2701
54+
@SuppressWarnings("all")
55+
void constructConvertingCursorWithNullConverter() {
56+
57+
Cursor<Object> mockCursor = mock(Cursor.class);
58+
59+
assertThatIllegalArgumentException()
60+
.isThrownBy(() -> new ConvertingCursor<>(mockCursor, null))
61+
.withMessage("Converter must not be null")
62+
.withNoCause();
63+
64+
verifyNoInteractions(mockCursor);
65+
}
66+
67+
@Test // #2701
68+
@SuppressWarnings("all")
69+
void constructConvertingCursorWithNullCursor() {
70+
71+
Converter<Object, Object> mockConverter = mock(Converter.class);
72+
73+
assertThatIllegalArgumentException()
74+
.isThrownBy(() -> new ConvertingCursor<>(null, mockConverter))
75+
.withMessage("Cursor must not be null")
76+
.withNoCause();
77+
78+
verifyNoInteractions(mockConverter);
79+
}
80+
81+
@Test
82+
void hasNextDelegatesToCursor() {
83+
84+
Converter<Object, Object> mockConverter = mock(Converter.class);
85+
Cursor<Object> mockCursor = mock(Cursor.class);
86+
87+
doReturn(true).when(mockCursor).hasNext();
88+
89+
ConvertingCursor<?, ?> convertingCursor = new ConvertingCursor<>(mockCursor, mockConverter);
90+
91+
assertThat(convertingCursor.hasNext()).isTrue();
92+
93+
verify(mockCursor, times(1)).hasNext();
94+
verifyNoMoreInteractions(mockCursor);
95+
verifyNoInteractions(mockConverter);
96+
}
97+
98+
@Test
99+
void nextDelegatesToCursorAndIsConverted() {
100+
101+
Converter<Object, Object> mockConverter = mock(Converter.class);
102+
Cursor<Object> mockCursor = mock(Cursor.class);
103+
104+
doReturn("test").when(mockCursor).next();
105+
doAnswer(invocation -> invocation.getArgument(0).toString().toUpperCase()).when(mockConverter).convert(any());
106+
107+
ConvertingCursor<?, ?> convertingCursor = new ConvertingCursor<>(mockCursor, mockConverter);
108+
109+
assertThat(convertingCursor.next()).isEqualTo("TEST");
110+
111+
verify(mockCursor, times(1)).next();
112+
verify(mockConverter, times(1)).convert(eq("test"));
113+
verifyNoMoreInteractions(mockCursor, mockConverter);
114+
}
115+
116+
@Test
117+
void removeDelegatesToCursor() {
118+
119+
Converter<Object, Object> mockConverter = mock(Converter.class);
120+
Cursor<Object> mockCursor = mock(Cursor.class);
121+
122+
ConvertingCursor<?, ?> convertingCursor = new ConvertingCursor<>(mockCursor, mockConverter);
123+
124+
convertingCursor.remove();
125+
126+
verify(mockCursor, times(1)).remove();
127+
verifyNoMoreInteractions(mockCursor);
128+
verifyNoInteractions(mockConverter);
129+
}
130+
131+
@Test
132+
void closeDelegatesToCursor() {
133+
134+
Converter<Object, Object> mockConverter = mock(Converter.class);
135+
Cursor<Object> mockCursor = mock(Cursor.class);
136+
137+
ConvertingCursor<?, ?> convertingCursor = new ConvertingCursor<>(mockCursor, mockConverter);
138+
139+
convertingCursor.close();
140+
141+
verify(mockCursor, times(1)).close();
142+
verifyNoMoreInteractions(mockCursor);
143+
verifyNoInteractions(mockConverter);
144+
}
145+
146+
@Test
147+
void getCursorIdDelegatesToCursor() {
148+
149+
Converter<Object, Object> mockConverter = mock(Converter.class);
150+
Cursor<Object> mockCursor = mock(Cursor.class);
151+
152+
doReturn(1L).when(mockCursor).getCursorId();
153+
154+
ConvertingCursor<?, ?> convertingCursor = new ConvertingCursor<>(mockCursor, mockConverter);
155+
156+
assertThat(convertingCursor.getCursorId()).isOne();
157+
158+
verify(mockCursor, times(1)).getCursorId();
159+
verifyNoMoreInteractions(mockCursor);
160+
verifyNoInteractions(mockConverter);
161+
}
162+
163+
@Test
164+
void isClosedDelegatesToCursor() {
165+
166+
Converter<Object, Object> mockConverter = mock(Converter.class);
167+
Cursor<Object> mockCursor = mock(Cursor.class);
168+
169+
doReturn(false).when(mockCursor).isClosed();
170+
171+
ConvertingCursor<?, ?> convertingCursor = new ConvertingCursor<>(mockCursor, mockConverter);
172+
173+
assertThat(convertingCursor.isClosed()).isFalse();
174+
175+
verify(mockCursor, times(1)).isClosed();
176+
verifyNoMoreInteractions(mockCursor);
177+
verifyNoInteractions(mockConverter);
178+
}
179+
180+
@Test
181+
void getPositionDelegatesToCursor() {
182+
183+
Converter<Object, Object> mockConverter = mock(Converter.class);
184+
Cursor<Object> mockCursor = mock(Cursor.class);
185+
186+
doReturn(12L).when(mockCursor).getPosition();
187+
188+
ConvertingCursor<?, ?> convertingCursor = new ConvertingCursor<>(mockCursor, mockConverter);
189+
190+
assertThat(convertingCursor.getPosition()).isEqualTo(12L);
191+
192+
verify(mockCursor, times(1)).getPosition();
193+
verifyNoMoreInteractions(mockCursor);
194+
verifyNoInteractions(mockConverter);
195+
}
196+
}

0 commit comments

Comments
 (0)