Use modern language features in tests
This commit is contained in:
@@ -33,21 +33,24 @@ public class LifecycleContextBean extends LifecycleBean implements ApplicationCo
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
super.setBeanFactory(beanFactory);
|
||||
if (this.owningContext != null)
|
||||
if (this.owningContext != null) {
|
||||
throw new RuntimeException("Factory called setBeanFactory after setApplicationContext");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
super.afterPropertiesSet();
|
||||
if (this.owningContext == null)
|
||||
if (this.owningContext == null) {
|
||||
throw new RuntimeException("Factory didn't call setApplicationContext before afterPropertiesSet on lifecycle bean");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
if (this.owningFactory == null)
|
||||
if (this.owningFactory == null) {
|
||||
throw new RuntimeException("Factory called setApplicationContext before setBeanFactory");
|
||||
}
|
||||
|
||||
this.owningContext = applicationContext;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -25,9 +25,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.testfixture.beans.TestBean;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.NoSuchMessageException;
|
||||
@@ -56,24 +54,15 @@ public class XmlWebApplicationContextTests extends AbstractApplicationContextTes
|
||||
MockServletContext sc = new MockServletContext("");
|
||||
root.setServletContext(sc);
|
||||
root.setConfigLocations("/org/springframework/web/context/WEB-INF/applicationContext.xml");
|
||||
root.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
|
||||
root.addBeanFactoryPostProcessor(beanFactory -> beanFactory.addBeanPostProcessor(new BeanPostProcessor() {
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
|
||||
beanFactory.addBeanPostProcessor(new BeanPostProcessor() {
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
|
||||
if (bean instanceof TestBean) {
|
||||
((TestBean) bean).getFriends().add("myFriend");
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
});
|
||||
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
|
||||
if (bean instanceof TestBean) {
|
||||
((TestBean) bean).getFriends().add("myFriend");
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
});
|
||||
}));
|
||||
root.refresh();
|
||||
XmlWebApplicationContext wac = new XmlWebApplicationContext();
|
||||
wac.getEnvironment().addActiveProfile("wacProfile1");
|
||||
@@ -109,7 +98,7 @@ public class XmlWebApplicationContextTests extends AbstractApplicationContextTes
|
||||
@Test
|
||||
@Override
|
||||
public void count() {
|
||||
assertThat(this.applicationContext.getBeanDefinitionCount() == 14).as("should have 14 beans, not "+ this.applicationContext.getBeanDefinitionCount()).isTrue();
|
||||
assertThat(this.applicationContext.getBeanDefinitionCount()).as("should have 14 beans").isEqualTo(14);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -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.Servlet;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -46,24 +44,21 @@ public class HttpRequestHandlerTests {
|
||||
@Test
|
||||
public void testHttpRequestHandlerServletPassThrough() throws Exception {
|
||||
MockServletContext servletContext = new MockServletContext();
|
||||
final MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
final MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.getBeanFactory().registerSingleton("myHandler", new HttpRequestHandler() {
|
||||
@Override
|
||||
public void handleRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
||||
assertThat(req).isSameAs(request);
|
||||
assertThat(res).isSameAs(response);
|
||||
String exception = request.getParameter("exception");
|
||||
if ("ServletException".equals(exception)) {
|
||||
throw new ServletException("test");
|
||||
}
|
||||
if ("IOException".equals(exception)) {
|
||||
throw new IOException("test");
|
||||
}
|
||||
res.getWriter().write("myResponse");
|
||||
wac.getBeanFactory().registerSingleton("myHandler", (HttpRequestHandler) (req, res) -> {
|
||||
assertThat(req).isSameAs(request);
|
||||
assertThat(res).isSameAs(response);
|
||||
String exception = request.getParameter("exception");
|
||||
if ("ServletException".equals(exception)) {
|
||||
throw new ServletException("test");
|
||||
}
|
||||
if ("IOException".equals(exception)) {
|
||||
throw new IOException("test");
|
||||
}
|
||||
res.getWriter().write("myResponse");
|
||||
});
|
||||
wac.setServletContext(servletContext);
|
||||
wac.refresh();
|
||||
@@ -76,13 +71,13 @@ public class HttpRequestHandlerTests {
|
||||
assertThat(response.getContentAsString()).isEqualTo("myResponse");
|
||||
|
||||
request.setParameter("exception", "ServletException");
|
||||
assertThatExceptionOfType(ServletException.class).isThrownBy(() ->
|
||||
servlet.service(request, response))
|
||||
assertThatExceptionOfType(ServletException.class)
|
||||
.isThrownBy(() -> servlet.service(request, response))
|
||||
.withMessage("test");
|
||||
|
||||
request.setParameter("exception", "IOException");
|
||||
assertThatIOException().isThrownBy(() ->
|
||||
servlet.service(request, response))
|
||||
assertThatIOException()
|
||||
.isThrownBy(() -> servlet.service(request, response))
|
||||
.withMessage("test");
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,6 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.testfixture.beans.TestBean;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.io.FileSystemResourceLoader;
|
||||
import org.springframework.format.FormatterRegistry;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -360,12 +359,7 @@ public class WebMvcConfigurationSupportExtensionTests {
|
||||
|
||||
@Override
|
||||
public void addFormatters(FormatterRegistry registry) {
|
||||
registry.addConverter(new Converter<TestBean, String>() {
|
||||
@Override
|
||||
public String convert(TestBean source) {
|
||||
return "converted";
|
||||
}
|
||||
});
|
||||
registry.addConverter(TestBean.class, String.class, testBean -> "converted");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -36,7 +36,6 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
@@ -50,13 +49,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
public class DefaultEntityResponseBuilderTests {
|
||||
|
||||
static final ServerResponse.Context EMPTY_CONTEXT = new ServerResponse.Context() {
|
||||
@Override
|
||||
public List<HttpMessageConverter<?>> messageConverters() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
};
|
||||
static final ServerResponse.Context EMPTY_CONTEXT = () -> Collections.emptyList();
|
||||
|
||||
@Test
|
||||
public void fromObject() {
|
||||
|
||||
@@ -20,7 +20,6 @@ import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
@@ -29,7 +28,6 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
@@ -43,13 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
public class DefaultRenderingResponseTests {
|
||||
|
||||
static final ServerResponse.Context EMPTY_CONTEXT = new ServerResponse.Context() {
|
||||
@Override
|
||||
public List<HttpMessageConverter<?>> messageConverters() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
};
|
||||
static final ServerResponse.Context EMPTY_CONTEXT = () -> Collections.emptyList();
|
||||
|
||||
@Test
|
||||
public void create() throws Exception {
|
||||
|
||||
@@ -318,12 +318,7 @@ class DefaultServerRequestTests {
|
||||
@Test
|
||||
void principal() {
|
||||
MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest("GET", "/", true);
|
||||
Principal principal = new Principal() {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "foo";
|
||||
}
|
||||
};
|
||||
Principal principal = () -> "foo";
|
||||
servletRequest.setUserPrincipal(principal);
|
||||
|
||||
DefaultServerRequest request = new DefaultServerRequest(servletRequest,
|
||||
|
||||
@@ -39,7 +39,6 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
@@ -55,14 +54,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
public class DefaultServerResponseBuilderTests {
|
||||
|
||||
static final ServerResponse.Context EMPTY_CONTEXT = new ServerResponse.Context() {
|
||||
@Override
|
||||
public List<HttpMessageConverter<?>> messageConverters() {
|
||||
return Collections.emptyList();
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
static final ServerResponse.Context EMPTY_CONTEXT = () -> Collections.emptyList();
|
||||
|
||||
@Test
|
||||
public void status() {
|
||||
|
||||
@@ -22,7 +22,6 @@ import java.io.StringReader;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@@ -949,12 +948,7 @@ public class SelectTagTests extends AbstractFormTagTests {
|
||||
}
|
||||
|
||||
private Map getCountryToLocaleMap() {
|
||||
Map map = new TreeMap(new Comparator() {
|
||||
@Override
|
||||
public int compare(Object o1, Object o2) {
|
||||
return ((Country)o1).getName().compareTo(((Country)o2).getName());
|
||||
}
|
||||
});
|
||||
Map map = new TreeMap((o1, o2) -> ((Country)o1).getName().compareTo(((Country)o2).getName()));
|
||||
map.put(Country.COUNTRY_AT, LOCALE_AT);
|
||||
map.put(Country.COUNTRY_NL, LOCALE_NL);
|
||||
map.put(Country.COUNTRY_US, Locale.US);
|
||||
|
||||
Reference in New Issue
Block a user