Use modern language features in tests

This commit is contained in:
Sam Brannen
2022-02-03 14:50:10 +01:00
parent 82a2544918
commit f8a5a8d7be
91 changed files with 577 additions and 1059 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@@ -72,13 +72,7 @@ public class InterceptingClientHttpRequestFactoryTests {
@Test
public void noExecution() throws Exception {
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
return responseMock;
}
});
interceptors.add((request, body, execution) -> responseMock);
interceptors.add(new NoOpInterceptor());
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, interceptors);
@@ -97,15 +91,11 @@ public class InterceptingClientHttpRequestFactoryTests {
final String headerValue = "Bar";
final String otherValue = "Baz";
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
HttpRequestWrapper wrapper = new HttpRequestWrapper(request);
wrapper.getHeaders().add(headerName, otherValue);
return execution.execute(wrapper, body);
}
};
};
requestMock = new RequestMock() {
@Override
@@ -119,8 +109,7 @@ public class InterceptingClientHttpRequestFactoryTests {
};
requestMock.getHeaders().add(headerName, headerValue);
requestFactory =
new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
ClientHttpRequest request = requestFactory.createRequest(new URI("https://example.com"), HttpMethod.GET);
request.execute();
@@ -130,19 +119,13 @@ public class InterceptingClientHttpRequestFactoryTests {
public void changeURI() throws Exception {
final URI changedUri = new URI("https://example.com/2");
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> execution.execute(new HttpRequestWrapper(request) {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
return execution.execute(new HttpRequestWrapper(request) {
@Override
public URI getURI() {
return changedUri;
}
}, body);
public URI getURI() {
return changedUri;
}
};
}, body);
requestFactoryMock = new RequestFactoryMock() {
@Override
@@ -152,8 +135,7 @@ public class InterceptingClientHttpRequestFactoryTests {
}
};
requestFactory =
new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
ClientHttpRequest request = requestFactory.createRequest(new URI("https://example.com"), HttpMethod.GET);
request.execute();
@@ -163,19 +145,13 @@ public class InterceptingClientHttpRequestFactoryTests {
public void changeMethod() throws Exception {
final HttpMethod changedMethod = HttpMethod.POST;
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> execution.execute(new HttpRequestWrapper(request) {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
return execution.execute(new HttpRequestWrapper(request) {
@Override
public HttpMethod getMethod() {
return changedMethod;
}
}, body);
public HttpMethod getMethod() {
return changedMethod;
}
};
}, body);
requestFactoryMock = new RequestFactoryMock() {
@Override
@@ -185,8 +161,7 @@ public class InterceptingClientHttpRequestFactoryTests {
}
};
requestFactory =
new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
ClientHttpRequest request = requestFactory.createRequest(new URI("https://example.com"), HttpMethod.GET);
request.execute();
@@ -196,16 +171,9 @@ public class InterceptingClientHttpRequestFactoryTests {
public void changeBody() throws Exception {
final byte[] changedBody = "Foo".getBytes();
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
return execution.execute(request, changedBody);
}
};
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> execution.execute(request, changedBody);
requestFactory =
new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
ClientHttpRequest request = requestFactory.createRequest(new URI("https://example.com"), HttpMethod.GET);
request.execute();

View File

@@ -279,9 +279,9 @@ public class MappingJackson2XmlHttpMessageConverterTests {
}
private interface MyJacksonView1 {};
private interface MyJacksonView1 {}
private interface MyJacksonView2 {};
private interface MyJacksonView2 {}
@SuppressWarnings("unused")

View File

@@ -249,13 +249,13 @@ public class ServletRequestDataBinderTests {
m.put("forname", "Tony");
m.put("surname", "Blair");
m.put("age", "50");
for (int i = 0; i < ps.length; i++) {
Object val = m.get(ps[i].getName());
for (PropertyValue element : ps) {
Object val = m.get(element.getName());
assertThat(val != null).as("Can't have unexpected value").isTrue();
boolean condition = val instanceof String;
assertThat(condition).as("Val i string").isTrue();
assertThat(val.equals(ps[i].getValue())).as("val matches expected").isTrue();
m.remove(ps[i].getName());
assertThat(val.equals(element.getValue())).as("val matches expected").isTrue();
m.remove(element.getName());
}
assertThat(m.size() == 0).as("Map size is 0").isTrue();
}

View File

@@ -16,8 +16,6 @@
package org.springframework.web.context.request.async;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.springframework.web.context.request.async.DeferredResult.DeferredResultHandler;
@@ -127,12 +125,7 @@ public class DeferredResultTests {
DeferredResult<String> result = new DeferredResult<>(null, "error result");
result.setResultHandler(handler);
Exception e = new Exception();
result.onError(new Consumer<Throwable>() {
@Override
public void accept(Throwable t) {
sb.append("error event");
}
});
result.onError(t -> sb.append("error event"));
result.getInterceptor().handleError(null, null, e);

View File

@@ -17,7 +17,6 @@
package org.springframework.web.context.request.async;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import javax.servlet.AsyncEvent;
@@ -96,12 +95,7 @@ public class WebAsyncManagerErrorTests {
StubCallable callable = new StubCallable();
WebAsyncTask<Object> webAsyncTask = new WebAsyncTask<>(callable);
webAsyncTask.onError(new Callable<Object>() {
@Override
public Object call() throws Exception {
return 7;
}
});
webAsyncTask.onError(() -> 7);
this.asyncManager.startCallableProcessing(webAsyncTask);
@@ -202,12 +196,7 @@ public class WebAsyncManagerErrorTests {
public void startDeferredResultProcessingErrorAndResumeThroughCallback() throws Exception {
final DeferredResult<Throwable> deferredResult = new DeferredResult<>();
deferredResult.onError(new Consumer<Throwable>() {
@Override
public void accept(Throwable t) {
deferredResult.setResult(t);
}
});
deferredResult.onError(t -> deferredResult.setResult(t));
this.asyncManager.startDeferredResultProcessing(deferredResult);

View File

@@ -97,12 +97,7 @@ public class WebAsyncManagerTimeoutTests {
StubCallable callable = new StubCallable();
WebAsyncTask<Object> webAsyncTask = new WebAsyncTask<>(callable);
webAsyncTask.onTimeout(new Callable<Object>() {
@Override
public Object call() throws Exception {
return 7;
}
});
webAsyncTask.onTimeout(() -> 7);
this.asyncManager.startCallableProcessing(webAsyncTask);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@@ -20,8 +20,6 @@ import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.Test;
@@ -68,14 +66,9 @@ public class HiddenHttpMethodFilterTests {
}
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain filterChain = new FilterChain() {
@Override
public void doFilter(ServletRequest filterRequest,
ServletResponse filterResponse) throws IOException, ServletException {
assertThat(((HttpServletRequest) filterRequest).getMethod()).as("Invalid method").isEqualTo(expectedMethod);
}
};
FilterChain filterChain = (filterRequest, filterResponse) ->
assertThat(((HttpServletRequest) filterRequest).getMethod())
.as("Invalid method").isEqualTo(expectedMethod);
this.filter.doFilter(request, response, filterChain);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@@ -54,27 +54,24 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Rossen Stoyanchev
*/
public class ModelFactoryOrderingTests {
class ModelFactoryOrderingTests {
private static final Log logger = LogFactory.getLog(ModelFactoryOrderingTests.class);
private NativeWebRequest webRequest;
private final NativeWebRequest webRequest = new ServletWebRequest(new MockHttpServletRequest(), new MockHttpServletResponse());
private ModelAndViewContainer mavContainer;
private final ModelAndViewContainer mavContainer = new ModelAndViewContainer();
private SessionAttributeStore sessionAttributeStore;
private final SessionAttributeStore sessionAttributeStore = new DefaultSessionAttributeStore();
@BeforeEach
public void setup() {
this.sessionAttributeStore = new DefaultSessionAttributeStore();
this.webRequest = new ServletWebRequest(new MockHttpServletRequest(), new MockHttpServletResponse());
this.mavContainer = new ModelAndViewContainer();
void setup() {
this.mavContainer.addAttribute("methods", new ArrayList<String>());
}
@Test
public void straightLineDependency() throws Exception {
void straightLineDependency() throws Exception {
runTest(new StraightLineDependencyController());
assertInvokedBefore("getA", "getB1", "getB2", "getC1", "getC2", "getC3", "getC4");
assertInvokedBefore("getB1", "getB2", "getC1", "getC2", "getC3", "getC4");
@@ -85,7 +82,7 @@ public class ModelFactoryOrderingTests {
}
@Test
public void treeDependency() throws Exception {
void treeDependency() throws Exception {
runTest(new TreeDependencyController());
assertInvokedBefore("getA", "getB1", "getB2", "getC1", "getC2", "getC3", "getC4");
assertInvokedBefore("getB1", "getC1", "getC2");
@@ -93,7 +90,7 @@ public class ModelFactoryOrderingTests {
}
@Test
public void InvertedTreeDependency() throws Exception {
void InvertedTreeDependency() throws Exception {
runTest(new InvertedTreeDependencyController());
assertInvokedBefore("getC1", "getA", "getB1");
assertInvokedBefore("getC2", "getA", "getB1");
@@ -104,7 +101,7 @@ public class ModelFactoryOrderingTests {
}
@Test
public void unresolvedDependency() throws Exception {
void unresolvedDependency() throws Exception {
runTest(new UnresolvedDependencyController());
assertInvokedBefore("getA", "getC1", "getC2", "getC3", "getC4");
@@ -133,19 +130,16 @@ public class ModelFactoryOrderingTests {
ModelFactory factory = new ModelFactory(modelMethods, dataBinderFactory, sessionHandler);
factory.initModel(this.webRequest, this.mavContainer, new HandlerMethod(controller, "handle"));
if (logger.isDebugEnabled()) {
StringBuilder sb = new StringBuilder();
for (String name : getInvokedMethods()) {
sb.append(" >> ").append(name);
}
logger.debug(sb);
logger.debug(String.join(" >> ", getInvokedMethods()));
}
}
private void assertInvokedBefore(String beforeMethod, String... afterMethods) {
List<String> actual = getInvokedMethods();
for (String afterMethod : afterMethods) {
assertThat(actual.indexOf(beforeMethod) < actual.indexOf(afterMethod)).as(beforeMethod + " should be before " + afterMethod + ". Actual order: " +
actual.toString()).isTrue();
assertThat(actual.indexOf(beforeMethod) < actual.indexOf(afterMethod))
.as(beforeMethod + " should be before " + afterMethod + ". Actual order: " + actual.toString())
.isTrue();
}
}
@@ -321,13 +315,8 @@ public class ModelFactoryOrderingTests {
private static class C4 { }
private static final ReflectionUtils.MethodFilter METHOD_FILTER = new ReflectionUtils.MethodFilter() {
@Override
public boolean matches(Method method) {
return ((AnnotationUtils.findAnnotation(method, RequestMapping.class) == null) &&
(AnnotationUtils.findAnnotation(method, ModelAttribute.class) != null));
}
};
private static final ReflectionUtils.MethodFilter METHOD_FILTER = method ->
((AnnotationUtils.findAnnotation(method, RequestMapping.class) == null) &&
(AnnotationUtils.findAnnotation(method, ModelAttribute.class) != null));
}