Polishing

(cherry picked from commit dd4468a)
This commit is contained in:
Juergen Hoeller
2018-07-24 16:16:52 +02:00
parent 207e8c2409
commit 0c44b5224f
5 changed files with 40 additions and 43 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -52,10 +52,8 @@ import org.springframework.web.server.handler.ExceptionHandlingWebHandler;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.junit.Assert.*;
import static org.springframework.http.MediaType.*;
/**
* Test the effect of exceptions at different stages of request processing by
@@ -72,7 +70,7 @@ public class DispatcherHandlerErrorTests {
@Before
public void setup() throws Exception {
public void setup() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(TestConfig.class);
ctx.refresh();
@@ -81,20 +79,21 @@ public class DispatcherHandlerErrorTests {
@Test
public void noHandler() throws Exception {
public void noHandler() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/does-not-exist"));
Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
StepVerifier.create(publisher)
.consumeErrorWith(error -> {
assertThat(error, instanceOf(ResponseStatusException.class));
assertThat(error.getMessage(), is("Response status 404 with reason \"No matching handler\""));
assertThat(error.getMessage(),
is("Response status 404 with reason \"No matching handler\""));
})
.verify();
}
@Test
public void controllerReturnsMonoError() throws Exception {
public void controllerReturnsMonoError() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/error-signal"));
Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
@@ -104,7 +103,7 @@ public class DispatcherHandlerErrorTests {
}
@Test
public void controllerThrowsException() throws Exception {
public void controllerThrowsException() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/raise-exception"));
Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
@@ -114,7 +113,7 @@ public class DispatcherHandlerErrorTests {
}
@Test
public void unknownReturnType() throws Exception {
public void unknownReturnType() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/unknown-return-type"));
Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
@@ -127,7 +126,7 @@ public class DispatcherHandlerErrorTests {
}
@Test
public void responseBodyMessageConversionError() throws Exception {
public void responseBodyMessageConversionError() {
ServerWebExchange exchange = MockServerWebExchange.from(
MockServerHttpRequest.post("/request-body").accept(APPLICATION_JSON).body("body"));
@@ -139,10 +138,10 @@ public class DispatcherHandlerErrorTests {
}
@Test
public void requestBodyError() throws Exception {
public void requestBodyError() {
ServerWebExchange exchange = MockServerWebExchange.from(
MockServerHttpRequest.post("/request-body").body(Mono.error(EXCEPTION)));
Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
StepVerifier.create(publisher)
@@ -151,7 +150,7 @@ public class DispatcherHandlerErrorTests {
}
@Test
public void webExceptionHandler() throws Exception {
public void webExceptionHandler() {
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/unknown-argument-type"));
List<WebExceptionHandler> handlers = Collections.singletonList(new ServerError500ExceptionHandler());
@@ -201,12 +200,12 @@ public class DispatcherHandlerErrorTests {
}
@RequestMapping("/raise-exception")
public void raiseException() throws Exception {
public void raiseException() {
throw EXCEPTION;
}
@RequestMapping("/unknown-return-type")
public Foo unknownReturnType() throws Exception {
public Foo unknownReturnType() {
return new Foo();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive;
import java.nio.charset.StandardCharsets;
@@ -32,11 +33,9 @@ import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.web.method.ResolvableMethod;
import org.springframework.web.server.ServerWebExchange;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
/**
* Unit tests for {@link DispatcherHandler}.
@@ -49,8 +48,7 @@ public class DispatcherHandlerTests {
@Test
public void handlerMappingOrder() throws Exception {
public void handlerMappingOrder() {
HandlerMapping hm1 = mock(HandlerMapping.class, withSettings().extraInterfaces(Ordered.class));
HandlerMapping hm2 = mock(HandlerMapping.class, withSettings().extraInterfaces(Ordered.class));
when(((Ordered) hm1).getOrder()).thenReturn(1);
@@ -90,6 +88,7 @@ public class DispatcherHandlerTests {
}
}
private static class StringHandlerResultHandler implements HandlerResultHandler {
@Override
@@ -105,4 +104,5 @@ public class DispatcherHandlerTests {
return exchange.getResponse().writeWith(Mono.just(dataBuffer));
}
}
}

View File

@@ -37,7 +37,10 @@ import org.springframework.web.reactive.function.client.WebClient;
import static org.junit.Assert.*;
/**
* Integration tests for server response flushing behavior.
*
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
* @since 5.0
*/
public class FlushingIntegrationTests extends AbstractHttpHandlerIntegrationTests {
@@ -83,7 +86,8 @@ public class FlushingIntegrationTests extends AbstractHttpHandlerIntegrationTest
}
catch (AssertionError err) {
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("windows") && err.getMessage().startsWith("VerifySubscriber timed out")) {
if (os.contains("windows") && err.getMessage() != null &&
err.getMessage().startsWith("VerifySubscriber timed out")) {
// TODO: Reactor usually times out on Windows ...
err.printStackTrace();
return;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -114,7 +114,6 @@ public abstract class AbstractWebSocketIntegrationTests {
@Before
public void setup() throws Exception {
this.server.setHandler(createHttpHandler());
this.server.afterPropertiesSet();
this.server.start();
@@ -128,7 +127,7 @@ public abstract class AbstractWebSocketIntegrationTests {
}
@After
public void stop() throws Exception {
public void stop() {
if (this.client instanceof Lifecycle) {
((Lifecycle) this.client).stop();
}

View File

@@ -38,11 +38,11 @@ import org.springframework.http.HttpHeaders;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;
/**
* Integration tests with server-side {@link WebSocketHandler}s.
*
* @author Rossen Stoyanchev
*/
public class WebSocketIntegrationTests extends AbstractWebSocketIntegrationTests {
@@ -64,17 +64,11 @@ public class WebSocketIntegrationTests extends AbstractWebSocketIntegrationTests
Flux<String> input = Flux.range(1, count).map(index -> "msg-" + index);
ReplayProcessor<Object> output = ReplayProcessor.create(count);
this.client.execute(getUrl("/echo"),
session -> {
logger.debug("Starting to send messages");
return session
.send(input.doOnNext(s -> logger.debug("outbound " + s)).map(session::textMessage))
.thenMany(session.receive().take(count).map(WebSocketMessage::getPayloadAsText))
.subscribeWith(output)
.doOnNext(s -> logger.debug("inbound " + s))
.then();
})
.doOnSuccessOrError((aVoid, ex) -> logger.debug("Done: " + (ex != null ? ex.getMessage() : "success")))
this.client.execute(getUrl("/echo"), session -> session
.send(input.map(session::textMessage))
.thenMany(session.receive().take(count).map(WebSocketMessage::getPayloadAsText))
.subscribeWith(output)
.then())
.block(TIMEOUT);
assertEquals(input.collectList().block(TIMEOUT), output.collectList().block(TIMEOUT));
@@ -181,7 +175,7 @@ public class WebSocketIntegrationTests extends AbstractWebSocketIntegrationTests
@Override
public Mono<Void> handle(WebSocketSession session) {
String protocol = session.getHandshakeInfo().getSubProtocol();
WebSocketMessage message = session.textMessage(protocol);
WebSocketMessage message = session.textMessage(protocol != null ? protocol : "none");
return session.send(Mono.just(message));
}
}
@@ -198,6 +192,7 @@ public class WebSocketIntegrationTests extends AbstractWebSocketIntegrationTests
}
}
private static class SessionClosingHandler implements WebSocketHandler {
@Override