Skip to content

Commit 672157b

Browse files
fix: Centralize @ParameterizedClass, @MethodSource in AbstractDataBufferAllocatingTests
Signed-off-by: CodingMasterLSW <spqjekdl1004@gmail.com>
1 parent 151df60 commit 672157b

File tree

6 files changed

+51
-99
lines changed

6 files changed

+51
-99
lines changed

spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import java.util.List;
2727

2828
import org.junit.jupiter.api.Test;
29-
import org.junit.jupiter.params.ParameterizedClass;
30-
import org.junit.jupiter.params.provider.MethodSource;
3129

3230
import org.springframework.core.testfixture.io.buffer.AbstractDataBufferAllocatingTests;
3331

@@ -40,14 +38,8 @@
4038
* @author Arjen Poutsma
4139
* @author Sam Brannen
4240
*/
43-
@ParameterizedClass
44-
@MethodSource("org.springframework.core.testfixture.io.buffer.AbstractDataBufferAllocatingTests#dataBufferFactories()")
4541
class DataBufferTests extends AbstractDataBufferAllocatingTests {
4642

47-
DataBufferTests(DataBufferFactory bufferFactory) {
48-
DataBufferTests.this.bufferFactory = bufferFactory;
49-
}
50-
5143
@Test
5244
void byteCountsAndPositions() {
5345
DataBuffer buffer = createDataBuffer(2);

spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferUtilsTests.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
import io.netty.buffer.PooledByteBufAllocator;
4545
import org.junit.jupiter.api.Nested;
4646
import org.junit.jupiter.api.Test;
47-
import org.junit.jupiter.params.ParameterizedClass;
48-
import org.junit.jupiter.params.provider.MethodSource;
4947
import org.mockito.stubbing.Answer;
5048
import org.reactivestreams.Publisher;
5149
import org.reactivestreams.Subscription;
@@ -73,7 +71,7 @@
7371
* @author Arjen Poutsma
7472
* @author Sam Brannen
7573
*/
76-
class DataBufferUtilsTests extends AbstractDataBufferAllocatingTests {
74+
class DataBufferUtilsTests {
7775

7876
private final Resource resource;
7977

@@ -86,13 +84,7 @@ class DataBufferUtilsTests extends AbstractDataBufferAllocatingTests {
8684
}
8785

8886
@Nested
89-
@ParameterizedClass
90-
@MethodSource("org.springframework.core.testfixture.io.buffer.AbstractDataBufferAllocatingTests#dataBufferFactories()")
91-
class ParameterizedDataBufferUtilsTests {
92-
93-
ParameterizedDataBufferUtilsTests(DataBufferFactory bufferFactory) {
94-
DataBufferUtilsTests.this.bufferFactory = bufferFactory;
95-
}
87+
class ParameterizedDataBufferUtilsTests extends AbstractDataBufferAllocatingTests {
9688

9789
@Test
9890
void readInputStream() {
@@ -206,8 +198,7 @@ void readAsynchronousFileChannelCancel() throws Exception {
206198
.verify();
207199
}
208200

209-
@Test
210-
// gh-22107
201+
@Test // gh-22107
211202
void readAsynchronousFileChannelCancelWithoutDemand() throws Exception {
212203
URI uri = resource.getURI();
213204
Flux<DataBuffer> flux = DataBufferUtils.readAsynchronousFileChannel(

spring-core/src/test/java/org/springframework/core/io/buffer/support/DataBufferTestUtilsTests.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
import java.nio.charset.StandardCharsets;
2020

2121
import org.junit.jupiter.api.Test;
22-
import org.junit.jupiter.params.ParameterizedClass;
23-
import org.junit.jupiter.params.provider.MethodSource;
2422

2523
import org.springframework.core.io.buffer.DataBuffer;
26-
import org.springframework.core.io.buffer.DataBufferFactory;
2724
import org.springframework.core.testfixture.io.buffer.AbstractDataBufferAllocatingTests;
2825
import org.springframework.core.testfixture.io.buffer.DataBufferTestUtils;
2926

@@ -33,14 +30,8 @@
3330
* @author Arjen Poutsma
3431
* @author Sam Brannen
3532
*/
36-
@ParameterizedClass
37-
@MethodSource("org.springframework.core.testfixture.io.buffer.AbstractDataBufferAllocatingTests#dataBufferFactories()")
3833
class DataBufferTestUtilsTests extends AbstractDataBufferAllocatingTests {
3934

40-
DataBufferTestUtilsTests(DataBufferFactory bufferFactory) {
41-
DataBufferTestUtilsTests.this.bufferFactory = bufferFactory;
42-
}
43-
4435
@Test
4536
void dumpBytes() {
4637
DataBuffer buffer = this.bufferFactory.allocateBuffer(4);

spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/AbstractDataBufferAllocatingTests.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
package org.springframework.core.testfixture.io.buffer;
1818

19-
import java.lang.annotation.ElementType;
20-
import java.lang.annotation.Retention;
21-
import java.lang.annotation.RetentionPolicy;
22-
import java.lang.annotation.Target;
2319
import java.nio.charset.Charset;
2420
import java.nio.charset.StandardCharsets;
2521
import java.time.Duration;
@@ -37,7 +33,8 @@
3733
import org.junit.jupiter.api.BeforeAll;
3834
import org.junit.jupiter.api.extension.AfterEachCallback;
3935
import org.junit.jupiter.api.extension.RegisterExtension;
40-
import org.junit.jupiter.params.ParameterizedTest;
36+
import org.junit.jupiter.params.Parameter;
37+
import org.junit.jupiter.params.ParameterizedClass;
4138
import org.junit.jupiter.params.provider.Arguments;
4239
import org.junit.jupiter.params.provider.MethodSource;
4340
import reactor.core.publisher.Mono;
@@ -60,6 +57,8 @@
6057
* @author Rossen Stoyanchev
6158
* @author Sam Brannen
6259
*/
60+
@ParameterizedClass
61+
@MethodSource("dataBufferFactories")
6362
public abstract class AbstractDataBufferAllocatingTests {
6463

6564
private static UnpooledByteBufAllocator netty4OffHeapUnpooled;
@@ -74,6 +73,7 @@ public abstract class AbstractDataBufferAllocatingTests {
7473
@RegisterExtension
7574
AfterEachCallback leakDetector = context -> waitForDataBufferRelease(Duration.ofSeconds(2));
7675

76+
@Parameter
7777
protected DataBufferFactory bufferFactory;
7878

7979

@@ -169,14 +169,6 @@ public static void createAllocators() {
169169
netty4OffHeapPooled = new PooledByteBufAllocator(true, 1, 1, 4096, 4, 0, 0, 0, true);
170170
}
171171

172-
173-
@Retention(RetentionPolicy.RUNTIME)
174-
@Target(ElementType.METHOD)
175-
@ParameterizedTest
176-
@MethodSource("org.springframework.core.testfixture.io.buffer.AbstractDataBufferAllocatingTests#dataBufferFactories()")
177-
public @interface ParameterizedDataBufferAllocatingTest {
178-
}
179-
180172
public static Stream<Arguments> dataBufferFactories() {
181173
return Stream.of(
182174
// Netty 4

spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageWriterTests.java

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Collections;
2323
import java.util.Map;
2424

25+
import org.junit.jupiter.api.Test;
2526
import org.reactivestreams.Publisher;
2627
import reactor.core.publisher.Flux;
2728
import reactor.core.publisher.Mono;
@@ -30,7 +31,6 @@
3031
import tools.jackson.databind.json.JsonMapper;
3132

3233
import org.springframework.core.ResolvableType;
33-
import org.springframework.core.io.buffer.DataBufferFactory;
3434
import org.springframework.core.io.buffer.DataBufferUtils;
3535
import org.springframework.core.testfixture.io.buffer.AbstractDataBufferAllocatingTests;
3636
import org.springframework.http.MediaType;
@@ -57,9 +57,8 @@ class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAllocating
5757
new ServerSentEventHttpMessageWriter(new JacksonJsonEncoder());
5858

5959

60-
@ParameterizedDataBufferAllocatingTest
61-
void canWrite(DataBufferFactory bufferFactory) {
62-
super.bufferFactory = bufferFactory;
60+
@Test
61+
void canWrite() {
6362

6463
assertThat(this.messageWriter.canWrite(forClass(Object.class), null)).isTrue();
6564
assertThat(this.messageWriter.canWrite(forClass(Object.class), new MediaType("foo", "bar"))).isFalse();
@@ -72,14 +71,12 @@ void canWrite(DataBufferFactory bufferFactory) {
7271
assertThat(this.messageWriter.canWrite(ResolvableType.NONE, new MediaType("foo", "bar"))).isFalse();
7372
}
7473

75-
@ParameterizedDataBufferAllocatingTest
76-
void writeServerSentEvent(DataBufferFactory bufferFactory) {
77-
super.bufferFactory = bufferFactory;
78-
74+
@Test
75+
void writeServerSentEvent() {
7976
ServerSentEvent<?> event = ServerSentEvent.builder().data("bar").id("c42").event("foo")
8077
.comment("bla\nbla bla\nbla bla bla").retry(Duration.ofMillis(123L)).build();
8178

82-
MockServerHttpResponse outputMessage = new MockServerHttpResponse(super.bufferFactory);
79+
MockServerHttpResponse outputMessage = new MockServerHttpResponse(bufferFactory);
8380
Mono<ServerSentEvent> source = Mono.just(event);
8481
testWrite(source, outputMessage, ServerSentEvent.class);
8582

@@ -90,11 +87,9 @@ void writeServerSentEvent(DataBufferFactory bufferFactory) {
9087
.verify();
9188
}
9289

93-
@ParameterizedDataBufferAllocatingTest
94-
void writeString(DataBufferFactory bufferFactory) {
95-
super.bufferFactory = bufferFactory;
96-
97-
MockServerHttpResponse outputMessage = new MockServerHttpResponse(super.bufferFactory);
90+
@Test
91+
void writeString() {
92+
MockServerHttpResponse outputMessage = new MockServerHttpResponse(bufferFactory);
9893
Flux<String> source = Flux.just("foo", "bar");
9994
testWrite(source, outputMessage, String.class);
10095

@@ -105,11 +100,9 @@ void writeString(DataBufferFactory bufferFactory) {
105100
.verify();
106101
}
107102

108-
@ParameterizedDataBufferAllocatingTest
109-
void writeMultiLineString(DataBufferFactory bufferFactory) {
110-
super.bufferFactory = bufferFactory;
111-
112-
MockServerHttpResponse outputMessage = new MockServerHttpResponse(super.bufferFactory);
103+
@Test
104+
void writeMultiLineString() {
105+
MockServerHttpResponse outputMessage = new MockServerHttpResponse();
113106
Flux<String> source = Flux.just("foo\nbar", "foo\nbaz");
114107
testWrite(source, outputMessage, String.class);
115108

@@ -120,11 +113,9 @@ void writeMultiLineString(DataBufferFactory bufferFactory) {
120113
.verify();
121114
}
122115

123-
@ParameterizedDataBufferAllocatingTest // SPR-16516
124-
void writeStringWithCustomCharset(DataBufferFactory bufferFactory) {
125-
super.bufferFactory = bufferFactory;
126-
127-
MockServerHttpResponse outputMessage = new MockServerHttpResponse(super.bufferFactory);
116+
@Test // SPR-16516
117+
void writeStringWithCustomCharset() {
118+
MockServerHttpResponse outputMessage = new MockServerHttpResponse(bufferFactory);
128119
Flux<String> source = Flux.just("\u00A3");
129120
Charset charset = StandardCharsets.ISO_8859_1;
130121
MediaType mediaType = new MediaType("text", "event-stream", charset);
@@ -141,11 +132,9 @@ void writeStringWithCustomCharset(DataBufferFactory bufferFactory) {
141132
.verify();
142133
}
143134

144-
@ParameterizedDataBufferAllocatingTest
145-
void writePojo(DataBufferFactory bufferFactory) {
146-
super.bufferFactory = bufferFactory;
147-
148-
MockServerHttpResponse outputMessage = new MockServerHttpResponse(super.bufferFactory);
135+
@Test
136+
void writePojo() {
137+
MockServerHttpResponse outputMessage = new MockServerHttpResponse(bufferFactory);
149138
Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
150139
testWrite(source, outputMessage, Pojo.class);
151140

@@ -160,14 +149,12 @@ void writePojo(DataBufferFactory bufferFactory) {
160149
.verify();
161150
}
162151

163-
@ParameterizedDataBufferAllocatingTest // SPR-14899
164-
void writePojoWithPrettyPrint(DataBufferFactory bufferFactory) {
165-
super.bufferFactory = bufferFactory;
166-
152+
@Test // SPR-14899
153+
void writePojoWithPrettyPrint() {
167154
JsonMapper mapper = JsonMapper.builder().enable(SerializationFeature.INDENT_OUTPUT).build();
168155
this.messageWriter = new ServerSentEventHttpMessageWriter(new JacksonJsonEncoder(mapper));
169156

170-
MockServerHttpResponse outputMessage = new MockServerHttpResponse(super.bufferFactory);
157+
MockServerHttpResponse outputMessage = new MockServerHttpResponse(bufferFactory);
171158
Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
172159
testWrite(source, outputMessage, Pojo.class);
173160

@@ -190,11 +177,9 @@ void writePojoWithPrettyPrint(DataBufferFactory bufferFactory) {
190177
.verify();
191178
}
192179

193-
@ParameterizedDataBufferAllocatingTest // SPR-16516, SPR-16539
194-
void writePojoWithCustomEncoding(DataBufferFactory bufferFactory) {
195-
super.bufferFactory = bufferFactory;
196-
197-
MockServerHttpResponse outputMessage = new MockServerHttpResponse(super.bufferFactory);
180+
@Test // SPR-16516, SPR-16539
181+
void writePojoWithCustomEncoding() {
182+
MockServerHttpResponse outputMessage = new MockServerHttpResponse(bufferFactory);
198183
Flux<Pojo> source = Flux.just(new Pojo("foo\uD834\uDD1E", "bar\uD834\uDD1E"));
199184
Charset charset = StandardCharsets.UTF_16LE;
200185
MediaType mediaType = new MediaType("text", "event-stream", charset);

spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientDataBufferAllocatingTests.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import mockwebserver3.MockWebServer;
2929
import org.junit.jupiter.api.AfterAll;
3030
import org.junit.jupiter.api.BeforeAll;
31+
import org.junit.jupiter.api.Test;
3132
import org.junit.jupiter.api.TestInstance;
3233
import reactor.core.publisher.Mono;
3334
import reactor.test.StepVerifier;
@@ -97,8 +98,8 @@ private ReactorClientHttpConnector initConnector() {
9798
}
9899

99100

100-
@ParameterizedDataBufferAllocatingTest
101-
void bodyToMonoVoid(DataBufferFactory bufferFactory) throws IOException {
101+
@Test
102+
void bodyToMonoVoid() throws IOException {
102103
setUp(bufferFactory);
103104

104105
this.server.enqueue(new MockResponse.Builder().
@@ -116,8 +117,8 @@ void bodyToMonoVoid(DataBufferFactory bufferFactory) throws IOException {
116117
assertThat(this.server.getRequestCount()).isEqualTo(1);
117118
}
118119

119-
@ParameterizedDataBufferAllocatingTest // SPR-17482
120-
void bodyToMonoVoidWithoutContentType(DataBufferFactory bufferFactory) throws IOException {
120+
@Test // SPR-17482
121+
void bodyToMonoVoidWithoutContentType() throws IOException {
121122
setUp(bufferFactory);
122123

123124
this.server.enqueue(new MockResponse.Builder()
@@ -134,40 +135,40 @@ void bodyToMonoVoidWithoutContentType(DataBufferFactory bufferFactory) throws IO
134135
assertThat(this.server.getRequestCount()).isEqualTo(1);
135136
}
136137

137-
@ParameterizedDataBufferAllocatingTest
138-
void onStatusWithBodyNotConsumed(DataBufferFactory bufferFactory) throws IOException {
138+
@Test
139+
void onStatusWithBodyNotConsumed() throws IOException {
139140
setUp(bufferFactory);
140141

141142
RuntimeException ex = new RuntimeException("response error");
142143
testOnStatus(ex, response -> Mono.just(ex));
143144
}
144145

145-
@ParameterizedDataBufferAllocatingTest
146-
void onStatusWithBodyConsumed(DataBufferFactory bufferFactory) throws IOException {
146+
@Test
147+
void onStatusWithBodyConsumed() throws IOException {
147148
setUp(bufferFactory);
148149

149150
RuntimeException ex = new RuntimeException("response error");
150151
testOnStatus(ex, response -> response.bodyToMono(Void.class).thenReturn(ex));
151152
}
152153

153-
@ParameterizedDataBufferAllocatingTest // SPR-17473
154-
void onStatusWithMonoErrorAndBodyNotConsumed(DataBufferFactory bufferFactory) throws IOException {
154+
@Test // SPR-17473
155+
void onStatusWithMonoErrorAndBodyNotConsumed() throws IOException {
155156
setUp(bufferFactory);
156157

157158
RuntimeException ex = new RuntimeException("response error");
158159
testOnStatus(ex, response -> Mono.error(ex));
159160
}
160161

161-
@ParameterizedDataBufferAllocatingTest
162-
void onStatusWithMonoErrorAndBodyConsumed(DataBufferFactory bufferFactory) throws IOException {
162+
@Test
163+
void onStatusWithMonoErrorAndBodyConsumed() throws IOException {
163164
setUp(bufferFactory);
164165

165166
RuntimeException ex = new RuntimeException("response error");
166167
testOnStatus(ex, response -> response.bodyToMono(Void.class).then(Mono.error(ex)));
167168
}
168169

169-
@ParameterizedDataBufferAllocatingTest // gh-23230
170-
void onStatusWithImmediateErrorAndBodyNotConsumed(DataBufferFactory bufferFactory) throws IOException {
170+
@Test // gh-23230
171+
void onStatusWithImmediateErrorAndBodyNotConsumed() throws IOException {
171172
setUp(bufferFactory);
172173

173174
RuntimeException ex = new RuntimeException("response error");
@@ -176,8 +177,8 @@ void onStatusWithImmediateErrorAndBodyNotConsumed(DataBufferFactory bufferFactor
176177
});
177178
}
178179

179-
@ParameterizedDataBufferAllocatingTest
180-
void releaseBody(DataBufferFactory bufferFactory) throws IOException {
180+
@Test
181+
void releaseBody() throws IOException {
181182
setUp(bufferFactory);
182183

183184
this.server.enqueue(new MockResponse.Builder()
@@ -194,8 +195,8 @@ void releaseBody(DataBufferFactory bufferFactory) throws IOException {
194195
.verify(Duration.ofSeconds(3));
195196
}
196197

197-
@ParameterizedDataBufferAllocatingTest
198-
void exchangeToBodilessEntity(DataBufferFactory bufferFactory) throws IOException {
198+
@Test
199+
void exchangeToBodilessEntity() throws IOException {
199200
setUp(bufferFactory);
200201

201202
this.server.enqueue(new MockResponse.Builder()

0 commit comments

Comments
 (0)