Polishing

This commit is contained in:
Sam Brannen
2023-02-20 14:41:33 +01:00
parent 8a44b6445d
commit 2d56505ea9
7 changed files with 198 additions and 200 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -68,7 +68,7 @@ import static org.springframework.http.MediaType.APPLICATION_XML;
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
*/
public class RequestMappingMessageConversionIntegrationTests extends AbstractRequestMappingIntegrationTests {
class RequestMappingMessageConversionIntegrationTests extends AbstractRequestMappingIntegrationTests {
private static final ParameterizedTypeReference<List<Person>> PERSON_LIST =
new ParameterizedTypeReference<>() {};
@@ -78,15 +78,12 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
@Override
protected ApplicationContext initApplicationContext() {
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
wac.register(WebConfig.class);
wac.refresh();
return wac;
return new AnnotationConfigApplicationContext(WebConfig.class);
}
@ParameterizedHttpServerTest
public void byteBufferResponseBodyWithPublisher(HttpServer httpServer) throws Exception {
void byteBufferResponseBodyWithPublisher(HttpServer httpServer) throws Exception {
startServer(httpServer);
Person expected = new Person("Robert");
@@ -94,7 +91,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void byteBufferResponseBodyWithFlux(HttpServer httpServer) throws Exception {
void byteBufferResponseBodyWithFlux(HttpServer httpServer) throws Exception {
startServer(httpServer);
String expected = "Hello!";
@@ -102,7 +99,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void byteBufferResponseBodyWithMono(HttpServer httpServer) throws Exception {
void byteBufferResponseBodyWithMono(HttpServer httpServer) throws Exception {
startServer(httpServer);
String expected = "Hello!";
@@ -112,7 +109,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void byteBufferResponseBodyWithObservable(HttpServer httpServer) throws Exception {
void byteBufferResponseBodyWithObservable(HttpServer httpServer) throws Exception {
startServer(httpServer);
String expected = "Hello!";
@@ -121,7 +118,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void byteBufferResponseBodyWithFlowable(HttpServer httpServer) throws Exception {
void byteBufferResponseBodyWithFlowable(HttpServer httpServer) throws Exception {
startServer(httpServer);
String expected = "Hello!";
@@ -129,7 +126,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personResponseBody(HttpServer httpServer) throws Exception {
void personResponseBody(HttpServer httpServer) throws Exception {
startServer(httpServer);
Person expected = new Person("Robert");
@@ -139,7 +136,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personResponseBodyWithCompletableFuture(HttpServer httpServer) throws Exception {
void personResponseBodyWithCompletableFuture(HttpServer httpServer) throws Exception {
startServer(httpServer);
Person expected = new Person("Robert");
@@ -149,7 +146,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personResponseBodyWithMono(HttpServer httpServer) throws Exception {
void personResponseBodyWithMono(HttpServer httpServer) throws Exception {
startServer(httpServer);
Person expected = new Person("Robert");
@@ -159,7 +156,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest // SPR-17506
public void personResponseBodyWithEmptyMono(HttpServer httpServer) throws Exception {
void personResponseBodyWithEmptyMono(HttpServer httpServer) throws Exception {
startServer(httpServer);
ResponseEntity<Person> responseEntity = performGet("/person-response/mono-empty", JSON, Person.class);
@@ -174,7 +171,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personResponseBodyWithMonoDeclaredAsObject(HttpServer httpServer) throws Exception {
void personResponseBodyWithMonoDeclaredAsObject(HttpServer httpServer) throws Exception {
startServer(httpServer);
Person expected = new Person("Robert");
@@ -184,7 +181,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personResponseBodyWithSingle(HttpServer httpServer) throws Exception {
void personResponseBodyWithSingle(HttpServer httpServer) throws Exception {
startServer(httpServer);
Person expected = new Person("Robert");
@@ -194,7 +191,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personResponseBodyWithMonoResponseEntity(HttpServer httpServer) throws Exception {
void personResponseBodyWithMonoResponseEntity(HttpServer httpServer) throws Exception {
startServer(httpServer);
Person expected = new Person("Robert");
@@ -204,7 +201,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest // SPR-16172
public void personResponseBodyWithMonoResponseEntityXml(HttpServer httpServer) throws Exception {
void personResponseBodyWithMonoResponseEntityXml(HttpServer httpServer) throws Exception {
startServer(httpServer);
@@ -218,7 +215,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personResponseBodyWithList(HttpServer httpServer) throws Exception {
void personResponseBodyWithList(HttpServer httpServer) throws Exception {
startServer(httpServer);
List<?> expected = asList(new Person("Robert"), new Person("Marie"));
@@ -228,7 +225,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personResponseBodyWithPublisher(HttpServer httpServer) throws Exception {
void personResponseBodyWithPublisher(HttpServer httpServer) throws Exception {
startServer(httpServer);
List<?> expected = asList(new Person("Robert"), new Person("Marie"));
@@ -238,7 +235,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personResponseBodyWithFlux(HttpServer httpServer) throws Exception {
void personResponseBodyWithFlux(HttpServer httpServer) throws Exception {
startServer(httpServer);
List<?> expected = asList(new Person("Robert"), new Person("Marie"));
@@ -246,7 +243,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personResponseBodyWithObservable(HttpServer httpServer) throws Exception {
void personResponseBodyWithObservable(HttpServer httpServer) throws Exception {
startServer(httpServer);
List<?> expected = asList(new Person("Robert"), new Person("Marie"));
@@ -254,7 +251,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void resource(HttpServer httpServer) throws Exception {
void resource(HttpServer httpServer) throws Exception {
startServer(httpServer);
ResponseEntity<byte[]> response = performGet("/resource", new HttpHeaders(), byte[].class);
@@ -267,7 +264,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personTransform(HttpServer httpServer) throws Exception {
void personTransform(HttpServer httpServer) throws Exception {
startServer(httpServer);
assertThat(performPost("/person-transform/person", JSON, new Person("Robert"),
@@ -275,7 +272,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personTransformWithCompletableFuture(HttpServer httpServer) throws Exception {
void personTransformWithCompletableFuture(HttpServer httpServer) throws Exception {
startServer(httpServer);
assertThat(performPost("/person-transform/completable-future", JSON, new Person("Robert"),
@@ -283,7 +280,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personTransformWithMono(HttpServer httpServer) throws Exception {
void personTransformWithMono(HttpServer httpServer) throws Exception {
startServer(httpServer);
assertThat(performPost("/person-transform/mono", JSON, new Person("Robert"),
@@ -291,7 +288,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest // SPR-16759
public void personTransformWithMonoAndXml(HttpServer httpServer) throws Exception {
void personTransformWithMonoAndXml(HttpServer httpServer) throws Exception {
startServer(httpServer);
assertThat(performPost("/person-transform/mono", MediaType.APPLICATION_XML, new Person("Robert"),
@@ -299,7 +296,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personTransformWithSingle(HttpServer httpServer) throws Exception {
void personTransformWithSingle(HttpServer httpServer) throws Exception {
startServer(httpServer);
assertThat(performPost("/person-transform/single", JSON, new Person("Robert"),
@@ -307,7 +304,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personTransformWithMaybe(HttpServer httpServer) throws Exception {
void personTransformWithMaybe(HttpServer httpServer) throws Exception {
startServer(httpServer);
assertThat(performPost("/person-transform/maybe", JSON, new Person("Robert"),
@@ -315,7 +312,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personTransformWithPublisher(HttpServer httpServer) throws Exception {
void personTransformWithPublisher(HttpServer httpServer) throws Exception {
startServer(httpServer);
List<?> req = asList(new Person("Robert"), new Person("Marie"));
@@ -324,7 +321,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personTransformWithFlux(HttpServer httpServer) throws Exception {
void personTransformWithFlux(HttpServer httpServer) throws Exception {
startServer(httpServer);
List<?> req = asList(new Person("Robert"), new Person("Marie"));
@@ -333,7 +330,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personTransformWithObservable(HttpServer httpServer) throws Exception {
void personTransformWithObservable(HttpServer httpServer) throws Exception {
startServer(httpServer);
List<?> req = asList(new Person("Robert"), new Person("Marie"));
@@ -342,7 +339,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personTransformWithFlowable(HttpServer httpServer) throws Exception {
void personTransformWithFlowable(HttpServer httpServer) throws Exception {
startServer(httpServer);
List<?> req = asList(new Person("Robert"), new Person("Marie"));
@@ -351,7 +348,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personCreateWithPublisherJson(HttpServer httpServer) throws Exception {
void personCreateWithPublisherJson(HttpServer httpServer) throws Exception {
startServer(httpServer);
ResponseEntity<Void> entity = performPost("/person-create/publisher", JSON,
@@ -362,7 +359,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personCreateWithPublisherXml(HttpServer httpServer) throws Exception {
void personCreateWithPublisherXml(HttpServer httpServer) throws Exception {
startServer(httpServer);
People people = new People(new Person("Robert"), new Person("Marie"));
@@ -373,7 +370,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personCreateWithMono(HttpServer httpServer) throws Exception {
void personCreateWithMono(HttpServer httpServer) throws Exception {
startServer(httpServer);
ResponseEntity<Void> entity = performPost(
@@ -384,7 +381,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personCreateWithSingle(HttpServer httpServer) throws Exception {
void personCreateWithSingle(HttpServer httpServer) throws Exception {
startServer(httpServer);
ResponseEntity<Void> entity = performPost(
@@ -395,7 +392,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personCreateWithFluxJson(HttpServer httpServer) throws Exception {
void personCreateWithFluxJson(HttpServer httpServer) throws Exception {
startServer(httpServer);
ResponseEntity<Void> entity = performPost("/person-create/flux", JSON,
@@ -406,7 +403,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personCreateWithFluxXml(HttpServer httpServer) throws Exception {
void personCreateWithFluxXml(HttpServer httpServer) throws Exception {
startServer(httpServer);
People people = new People(new Person("Robert"), new Person("Marie"));
@@ -417,7 +414,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personCreateWithObservableJson(HttpServer httpServer) throws Exception {
void personCreateWithObservableJson(HttpServer httpServer) throws Exception {
startServer(httpServer);
ResponseEntity<Void> entity = performPost("/person-create/observable", JSON,
@@ -428,7 +425,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personCreateWithObservableXml(HttpServer httpServer) throws Exception {
void personCreateWithObservableXml(HttpServer httpServer) throws Exception {
startServer(httpServer);
People people = new People(new Person("Robert"), new Person("Marie"));
@@ -439,7 +436,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personCreateWithFlowableJson(HttpServer httpServer) throws Exception {
void personCreateWithFlowableJson(HttpServer httpServer) throws Exception {
startServer(httpServer);
ResponseEntity<Void> entity = performPost("/person-create/flowable", JSON,
@@ -450,7 +447,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest
public void personCreateWithFlowableXml(HttpServer httpServer) throws Exception {
void personCreateWithFlowableXml(HttpServer httpServer) throws Exception {
startServer(httpServer);
People people = new People(new Person("Robert"), new Person("Marie"));
@@ -461,7 +458,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
}
@ParameterizedHttpServerTest // gh-23791
public void personCreateViaDefaultMethodWithGenerics(HttpServer httpServer) throws Exception {
void personCreateViaDefaultMethodWithGenerics(HttpServer httpServer) throws Exception {
startServer(httpServer);
ResponseEntity<String> entity = performPost("/23791", JSON, new Person("Robert"), null, String.class);
@@ -484,29 +481,30 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
private static class RawResponseBodyController {
@GetMapping("/publisher")
public Publisher<ByteBuffer> getPublisher() {
@SuppressWarnings("deprecation")
Publisher<ByteBuffer> getPublisher() {
Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
return encoder.encode(Mono.just(new Person("Robert")), DefaultDataBufferFactory.sharedInstance,
ResolvableType.forClass(Person.class), JSON, Collections.emptyMap()).map(DataBuffer::toByteBuffer);
}
@GetMapping("/flux")
public Flux<ByteBuffer> getFlux() {
Flux<ByteBuffer> getFlux() {
return Flux.just(ByteBuffer.wrap("Hello!".getBytes()));
}
@GetMapping("/mono")
public Mono<ByteBuffer> getMonoString() {
Mono<ByteBuffer> getMonoString() {
return Mono.just(ByteBuffer.wrap("Hello!".getBytes()));
}
@GetMapping("/observable")
public Observable<ByteBuffer> getObservable() {
Observable<ByteBuffer> getObservable() {
return Observable.just(ByteBuffer.wrap("Hello!".getBytes()));
}
@GetMapping("/flowable")
public Flowable<ByteBuffer> getFlowable() {
Flowable<ByteBuffer> getFlowable() {
return Flowable.just(ByteBuffer.wrap("Hello!".getBytes()));
}
}
@@ -518,64 +516,64 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
private static class PersonResponseBodyController {
@GetMapping("/person")
public Person getPerson() {
Person getPerson() {
return new Person("Robert");
}
@GetMapping("/completable-future")
public CompletableFuture<Person> getCompletableFuture() {
CompletableFuture<Person> getCompletableFuture() {
return CompletableFuture.completedFuture(new Person("Robert"));
}
@GetMapping("/mono")
public Mono<Person> getMono() {
Mono<Person> getMono() {
return Mono.just(new Person("Robert"));
}
@GetMapping("/mono-empty")
public Mono<Person> getMonoEmpty() {
Mono<Person> getMonoEmpty() {
return Mono.empty();
}
@GetMapping("/mono-declared-as-object")
public Object getMonoDeclaredAsObject() {
Object getMonoDeclaredAsObject() {
return Mono.just(new Person("Robert"));
}
@GetMapping("/single")
public Single<Person> getSingle() {
Single<Person> getSingle() {
return Single.just(new Person("Robert"));
}
@GetMapping("/mono-response-entity")
public ResponseEntity<Mono<Person>> getMonoResponseEntity() {
ResponseEntity<Mono<Person>> getMonoResponseEntity() {
Mono<Person> body = Mono.just(new Person("Robert"));
return ResponseEntity.ok(body);
}
@GetMapping("/mono-response-entity-xml")
public ResponseEntity<Mono<Person>> getMonoResponseEntityXml() {
ResponseEntity<Mono<Person>> getMonoResponseEntityXml() {
Mono<Person> body = Mono.just(new Person("Robert"));
return ResponseEntity.ok().contentType(MediaType.APPLICATION_XML).body(body);
}
@GetMapping("/list")
public List<Person> getList() {
List<Person> getList() {
return asList(new Person("Robert"), new Person("Marie"));
}
@GetMapping("/publisher")
public Publisher<Person> getPublisher() {
Publisher<Person> getPublisher() {
return Flux.just(new Person("Robert"), new Person("Marie"));
}
@GetMapping("/flux")
public Flux<Person> getFlux() {
Flux<Person> getFlux() {
return Flux.just(new Person("Robert"), new Person("Marie"));
}
@GetMapping("/observable")
public Observable<Person> getObservable() {
Observable<Person> getObservable() {
return Observable.just(new Person("Robert"), new Person("Marie"));
}
}
@@ -586,7 +584,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
private static class ResourceController {
@GetMapping("/resource")
public Resource resource() {
Resource resource() {
return new ClassPathResource("/org/springframework/web/reactive/spring.png");
}
}
@@ -598,47 +596,47 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
private static class PersonTransformationController {
@PostMapping("/person")
public Person transformPerson(@RequestBody Person person) {
Person transformPerson(@RequestBody Person person) {
return new Person(person.getName().toUpperCase());
}
@PostMapping("/completable-future")
public CompletableFuture<Person> transformCompletableFuture(@RequestBody CompletableFuture<Person> future) {
CompletableFuture<Person> transformCompletableFuture(@RequestBody CompletableFuture<Person> future) {
return future.thenApply(person -> new Person(person.getName().toUpperCase()));
}
@PostMapping("/mono")
public Mono<Person> transformMono(@RequestBody Mono<Person> personFuture) {
Mono<Person> transformMono(@RequestBody Mono<Person> personFuture) {
return personFuture.map(person -> new Person(person.getName().toUpperCase()));
}
@PostMapping("/single")
public Single<Person> transformSingle(@RequestBody Single<Person> personFuture) {
Single<Person> transformSingle(@RequestBody Single<Person> personFuture) {
return personFuture.map(person -> new Person(person.getName().toUpperCase()));
}
@PostMapping("/maybe")
public Maybe<Person> transformMaybe(@RequestBody Maybe<Person> personFuture) {
Maybe<Person> transformMaybe(@RequestBody Maybe<Person> personFuture) {
return personFuture.map(person -> new Person(person.getName().toUpperCase()));
}
@PostMapping("/publisher")
public Publisher<Person> transformPublisher(@RequestBody Publisher<Person> persons) {
Publisher<Person> transformPublisher(@RequestBody Publisher<Person> persons) {
return Flux.from(persons).map(person -> new Person(person.getName().toUpperCase()));
}
@PostMapping("/flux")
public Flux<Person> transformFlux(@RequestBody Flux<Person> persons) {
Flux<Person> transformFlux(@RequestBody Flux<Person> persons) {
return persons.map(person -> new Person(person.getName().toUpperCase()));
}
@PostMapping("/observable")
public Observable<Person> transformObservable(@RequestBody Observable<Person> persons) {
Observable<Person> transformObservable(@RequestBody Observable<Person> persons) {
return persons.map(person -> new Person(person.getName().toUpperCase()));
}
@PostMapping("/flowable")
public Flowable<Person> transformFlowable(@RequestBody Flowable<Person> persons) {
Flowable<Person> transformFlowable(@RequestBody Flowable<Person> persons) {
return persons.map(person -> new Person(person.getName().toUpperCase()));
}
}
@@ -652,32 +650,32 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
final List<Person> persons = new ArrayList<>();
@PostMapping("/publisher")
public Publisher<Void> createWithPublisher(@RequestBody Publisher<Person> publisher) {
Publisher<Void> createWithPublisher(@RequestBody Publisher<Person> publisher) {
return Flux.from(publisher).doOnNext(persons::add).then();
}
@PostMapping("/mono")
public Mono<Void> createWithMono(@RequestBody Mono<Person> mono) {
Mono<Void> createWithMono(@RequestBody Mono<Person> mono) {
return mono.doOnNext(persons::add).then();
}
@PostMapping("/single")
public Completable createWithSingle(@RequestBody Single<Person> single) {
Completable createWithSingle(@RequestBody Single<Person> single) {
return single.map(persons::add).ignoreElement();
}
@PostMapping("/flux")
public Mono<Void> createWithFlux(@RequestBody Flux<Person> flux) {
Mono<Void> createWithFlux(@RequestBody Flux<Person> flux) {
return flux.doOnNext(persons::add).then();
}
@PostMapping("/observable")
public Completable createWithObservable(@RequestBody Observable<Person> observable) {
Completable createWithObservable(@RequestBody Observable<Person> observable) {
return observable.toList().doOnSuccess(persons::addAll).ignoreElement();
}
@PostMapping("/flowable")
public Completable createWithFlowable(@RequestBody Flowable<Person> flowable) {
Completable createWithFlowable(@RequestBody Flowable<Person> flowable) {
return flowable.toList().doOnSuccess(persons::addAll).ignoreElement();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -48,12 +48,14 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* @author Rossen Stoyanchev
* @author Sam Brannen
*/
public class FreeMarkerViewTests {
class FreeMarkerViewTests {
private static final String TEMPLATE_PATH =
"classpath*:org/springframework/web/reactive/view/freemarker/";
private final FreeMarkerView freeMarkerView = new FreeMarkerView();
private final MockServerWebExchange exchange =
MockServerWebExchange.from(MockServerHttpRequest.get("/path"));
@@ -63,7 +65,7 @@ public class FreeMarkerViewTests {
@BeforeEach
public void setup() throws Exception {
void setup() throws Exception {
this.context.refresh();
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
@@ -75,42 +77,39 @@ public class FreeMarkerViewTests {
@Test
public void noFreeMarkerConfig() throws Exception {
FreeMarkerView view = new FreeMarkerView();
view.setApplicationContext(this.context);
view.setUrl("anythingButNull");
void noFreeMarkerConfig() {
freeMarkerView.setApplicationContext(this.context);
freeMarkerView.setUrl("anythingButNull");
assertThatExceptionOfType(ApplicationContextException.class)
.isThrownBy(view::afterPropertiesSet)
.isThrownBy(freeMarkerView::afterPropertiesSet)
.withMessageContaining("Must define a single FreeMarkerConfig bean");
}
@Test
public void noTemplateName() throws Exception {
FreeMarkerView freeMarkerView = new FreeMarkerView();
void noTemplateName() {
assertThatIllegalArgumentException()
.isThrownBy(freeMarkerView::afterPropertiesSet)
.withMessageContaining("Property 'url' is required");
}
@Test
public void checkResourceExists() throws Exception {
FreeMarkerView view = new FreeMarkerView();
view.setConfiguration(this.freeMarkerConfig);
view.setUrl("test.ftl");
void checkResourceExists() throws Exception {
freeMarkerView.setConfiguration(this.freeMarkerConfig);
freeMarkerView.setUrl("test.ftl");
assertThat(view.checkResourceExists(Locale.US)).isTrue();
assertThat(freeMarkerView.checkResourceExists(Locale.US)).isTrue();
}
@Test
public void render() {
FreeMarkerView view = new FreeMarkerView();
view.setApplicationContext(this.context);
view.setConfiguration(this.freeMarkerConfig);
view.setUrl("test.ftl");
void render() {
freeMarkerView.setApplicationContext(this.context);
freeMarkerView.setConfiguration(this.freeMarkerConfig);
freeMarkerView.setUrl("test.ftl");
ModelMap model = new ExtendedModelMap();
model.addAttribute("hello", "hi FreeMarker");
view.render(model, null, this.exchange).block(Duration.ofMillis(5000));
freeMarkerView.render(model, null, this.exchange).block(Duration.ofMillis(5000));
StepVerifier.create(this.exchange.getResponse().getBody())
.consumeNextWith(buf -> assertThat(asString(buf)).isEqualTo("<html><body>hi FreeMarker</body></html>"))
@@ -119,21 +118,20 @@ public class FreeMarkerViewTests {
}
@Test // gh-22754
public void subscribeWithoutDemand() {
void subscribeWithoutDemand() {
ZeroDemandResponse response = new ZeroDemandResponse();
ServerWebExchange exchange = new DefaultServerWebExchange(
MockServerHttpRequest.get("/path").build(), response,
new DefaultWebSessionManager(), ServerCodecConfigurer.create(),
new AcceptHeaderLocaleContextResolver());
FreeMarkerView view = new FreeMarkerView();
view.setApplicationContext(this.context);
view.setConfiguration(this.freeMarkerConfig);
view.setUrl("test.ftl");
freeMarkerView.setApplicationContext(this.context);
freeMarkerView.setConfiguration(this.freeMarkerConfig);
freeMarkerView.setUrl("test.ftl");
ModelMap model = new ExtendedModelMap();
model.addAttribute("hello", "hi FreeMarker");
view.render(model, null, exchange).subscribe();
freeMarkerView.render(model, null, exchange).subscribe();
response.cancelWrite();
response.checkForLeaks();
@@ -141,8 +139,9 @@ public class FreeMarkerViewTests {
private static String asString(DataBuffer dataBuffer) {
@SuppressWarnings("deprecation")
ByteBuffer byteBuffer = dataBuffer.toByteBuffer();
final byte[] bytes = new byte[byteBuffer.remaining()];
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
return new String(bytes, StandardCharsets.UTF_8);
}