Remove deprecated web APIs

See gh-33809
This commit is contained in:
rstoyanchev
2025-01-15 13:02:20 +00:00
parent 83c020eea5
commit 4920086225
38 changed files with 65 additions and 1305 deletions
@@ -64,6 +64,7 @@ import org.springframework.web.servlet.mvc.ParameterizableViewController;
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.support.RequestContextUtils;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.util.WebUtils;
@@ -118,10 +119,7 @@ public class ComplexWebApplicationContext extends StaticWebApplicationContext {
registerSingleton("noviewController", NoViewController.class);
pvs = new MutablePropertyValues();
pvs.add("order", 0);
pvs.add("basename", "org.springframework.web.servlet.complexviews");
registerSingleton("viewResolver",
org.springframework.web.servlet.view.ResourceBundleViewResolver.class, pvs);
registerSingleton("viewResolver", TestViewResolver.class, pvs);
pvs = new MutablePropertyValues();
pvs.add("suffix", ".jsp");
@@ -511,6 +509,25 @@ public class ComplexWebApplicationContext extends StaticWebApplicationContext {
}
private static class TestViewResolver implements ViewResolver, Ordered {
@Override
public int getOrder() {
return 0;
}
@Override
public @Nullable View resolveViewName(String viewName, Locale locale) throws Exception {
if (viewName.equalsIgnoreCase("form")) {
InternalResourceView view = new InternalResourceView("myform.jsp");
view.setRequestContextAttribute("rc");
return view;
}
return null;
}
}
public static class TestApplicationListener implements ApplicationListener<RequestHandledEvent> {
public int counter = 0;
@@ -24,7 +24,6 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
@@ -36,7 +35,6 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver;
* @author Juergen Hoeller
* @since 21.05.2003
*/
@SuppressWarnings("deprecation")
public class SimpleWebApplicationContext extends StaticWebApplicationContext {
@Override
@@ -51,14 +49,11 @@ public class SimpleWebApplicationContext extends StaticWebApplicationContext {
registerSingleton("handlerMapping", BeanNameUrlHandlerMapping.class);
registerSingleton("viewResolver", InternalResourceViewResolver.class);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("location", "org/springframework/web/context/WEB-INF/sessionContext.xml");
registerSingleton("viewResolver2", org.springframework.web.servlet.view.XmlViewResolver.class, pvs);
super.refresh();
}
@SuppressWarnings("deprecation")
public static class LocaleChecker implements Controller, org.springframework.web.servlet.mvc.LastModified {
@Override
@@ -316,6 +316,7 @@ public class MvcNamespaceTests {
doTestCustomValidator("mvc-config-custom-validator.xml");
}
@SuppressWarnings("removal")
private void doTestCustomValidator(String xml) throws Exception {
loadBeanDefinitions(xml);
@@ -26,8 +26,8 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.Ordered;
import org.springframework.http.server.RequestPath;
import org.springframework.ui.ModelMap;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;
@@ -37,6 +37,7 @@ import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapt
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import org.springframework.web.util.ServletRequestPathUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
@@ -178,12 +179,13 @@ public class InterceptorRegistryTests {
}
private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) {
PathMatcher pathMatcher = new AntPathMatcher();
private List<HandlerInterceptor> getInterceptorsForPath(@Nullable String lookupPath) {
lookupPath = (lookupPath != null ? lookupPath : "");
this.request.setAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE, RequestPath.parse(lookupPath, null));
List<HandlerInterceptor> result = new ArrayList<>();
for (Object interceptor : this.registry.getInterceptors()) {
if (interceptor instanceof MappedInterceptor mappedInterceptor) {
if (mappedInterceptor.matches(lookupPath, pathMatcher)) {
if (mappedInterceptor.matches(this.request)) {
result.add(mappedInterceptor.getInterceptor());
}
}
@@ -117,12 +117,12 @@ class RequestMappingInfoHandlerMappingTests {
@PathPatternsParameterizedTest
void getMappingPathPatterns(TestRequestMappingInfoHandlerMapping mapping) {
void getDirectPaths(TestRequestMappingInfoHandlerMapping mapping) {
String[] patterns = {"/foo/*", "/foo", "/bar/*", "/bar"};
RequestMappingInfo info = mapping.createInfo(patterns);
Set<String> actual = mapping.getMappingPathPatterns(info);
Set<String> actual = mapping.getDirectPaths(info);
assertThat(actual).isEqualTo(new HashSet<>(Arrays.asList(patterns)));
assertThat(actual).containsExactly("/foo", "/bar");
}
@PathPatternsParameterizedTest
@@ -69,6 +69,7 @@ import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
import org.springframework.web.servlet.resource.NoResourceFoundException;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import org.springframework.web.testfixture.servlet.MockServletConfig;
@@ -261,9 +262,8 @@ class ResponseEntityExceptionHandlerTests {
}
@Test
@SuppressWarnings("deprecation")
public void httpMessageNotReadable() {
testException(new HttpMessageNotReadableException("message"));
testException(new HttpMessageNotReadableException("message", new MockHttpInputMessage(new byte[0])));
}
@Test
@@ -49,6 +49,7 @@ import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.resource.NoResourceFoundException;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
@@ -153,9 +154,8 @@ class DefaultHandlerExceptionResolverTests {
}
@Test
@SuppressWarnings("deprecation")
public void handleHttpMessageNotReadable() {
HttpMessageNotReadableException ex = new HttpMessageNotReadableException("foo");
HttpMessageNotReadableException ex = new HttpMessageNotReadableException("foo", new MockHttpInputMessage(new byte[0]));
ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
assertThat(mav).as("No ModelAndView returned").isNotNull();
assertThat(mav.isEmpty()).as("No Empty ModelAndView returned").isTrue();
@@ -1,29 +0,0 @@
/*
* Copyright 2002-2024 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.view;
/**
* @author Rod Johnson
*/
class ResourceBundleViewResolverNoCacheTests extends ResourceBundleViewResolverTests {
@Override
protected boolean getCache() {
return false;
}
}
@@ -1,177 +0,0 @@
/*
* Copyright 2002-2024 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.view;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanIsAbstractException;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.servlet.View;
import org.springframework.web.testfixture.servlet.MockServletContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
/**
* @author Rod Johnson
* @author Juergen Hoeller
* @author Sam Brannen
*/
@SuppressWarnings("deprecation")
public class ResourceBundleViewResolverTests {
/** Comes from this package */
private static final String PROPS_FILE = "org.springframework.web.servlet.view.testviews";
private final ResourceBundleViewResolver rb = new ResourceBundleViewResolver();
private final StaticWebApplicationContext wac = new StaticWebApplicationContext();
@BeforeEach
void setUp() {
rb.setBasename(PROPS_FILE);
rb.setCache(getCache());
rb.setDefaultParentView("testParent");
wac.setServletContext(new MockServletContext());
wac.refresh();
// This will be propagated to views, so we need it.
rb.setApplicationContext(wac);
}
/**
* Not a constant: allows overrides.
* Controls whether to cache views.
*/
protected boolean getCache() {
return true;
}
@Test
void parentsAreAbstract() {
assertThatExceptionOfType(BeanIsAbstractException.class).isThrownBy(() ->
rb.resolveViewName("debug.Parent", Locale.ENGLISH));
assertThatExceptionOfType(BeanIsAbstractException.class).isThrownBy(() ->
rb.resolveViewName("testParent", Locale.ENGLISH));
}
@Test
void debugViewEnglish() throws Exception {
View v = rb.resolveViewName("debugView", Locale.ENGLISH);
assertThat(v).isInstanceOf(InternalResourceView.class);
InternalResourceView jv = (InternalResourceView) v;
assertThat(jv.getUrl()).as("debugView must have correct URL").isEqualTo("jsp/debug/debug.jsp");
Map<String, Object> m = jv.getStaticAttributes();
assertThat(m.size()).as("Must have 2 static attributes").isEqualTo(2);
assertThat(m.get("foo")).as("attribute foo").isEqualTo("bar");
assertThat(m.get("postcode")).as("attribute postcode").isEqualTo("SE10 9JY");
assertThat(jv.getContentType()).as("Correct default content type").isEqualTo(AbstractView.DEFAULT_CONTENT_TYPE);
}
@Test
void debugViewFrench() throws Exception {
View v = rb.resolveViewName("debugView", Locale.FRENCH);
assertThat(v).isInstanceOf(InternalResourceView.class);
InternalResourceView jv = (InternalResourceView) v;
assertThat(jv.getUrl()).as("French debugView must have correct URL").isEqualTo("jsp/debug/deboug.jsp");
assertThat(jv.getContentType()).as("Correct overridden (XML) content type").isEqualTo("text/xml;charset=ISO-8859-1");
}
@Test
void eagerInitialization() throws Exception {
ResourceBundleViewResolver rb = new ResourceBundleViewResolver();
rb.setBasename(PROPS_FILE);
rb.setCache(getCache());
rb.setDefaultParentView("testParent");
rb.setLocalesToInitialize(Locale.ENGLISH, Locale.FRENCH);
rb.setApplicationContext(wac);
View v = rb.resolveViewName("debugView", Locale.FRENCH);
assertThat(v).isInstanceOf(InternalResourceView.class);
InternalResourceView jv = (InternalResourceView) v;
assertThat(jv.getUrl()).as("French debugView must have correct URL").isEqualTo("jsp/debug/deboug.jsp");
assertThat(jv.getContentType()).as("Correct overridden (XML) content type").isEqualTo("text/xml;charset=ISO-8859-1");
}
@Test
void sameBundleOnlyCachedOnce() throws Exception {
assumeTrue(rb.isCache());
View v1 = rb.resolveViewName("debugView", Locale.ENGLISH);
View v2 = rb.resolveViewName("debugView", Locale.UK);
assertThat(v2).isSameAs(v1);
}
@Test
void noSuchViewEnglish() throws Exception {
assertThat(rb.resolveViewName("xxxxxxweorqiwuopeir", Locale.ENGLISH)).isNull();
}
@Test
void onSetContextCalledOnce() throws Exception {
TestView tv = (TestView) rb.resolveViewName("test", Locale.ENGLISH);
tv = (TestView) rb.resolveViewName("test", Locale.ENGLISH);
tv = (TestView) rb.resolveViewName("test", Locale.ENGLISH);
assertThat(tv.getBeanName()).as("test has correct name").isEqualTo("test");
assertThat(tv.initCount).as("test should have been initialized once, not ").isEqualTo(1);
}
@Test
void noSuchBasename() {
rb.setBasename("weoriwoierqupowiuer");
assertThatExceptionOfType(MissingResourceException.class).isThrownBy(() ->
rb.resolveViewName("debugView", Locale.ENGLISH));
}
static class TestView extends AbstractView {
public int initCount;
public void setLocation(Resource location) {
if (!(location instanceof ServletContextResource)) {
throw new IllegalArgumentException("Expecting ServletContextResource, not " + location.getClass().getName());
}
}
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
HttpServletResponse response) {
}
@Override
protected void initApplicationContext() {
++initCount;
}
}
}
@@ -34,9 +34,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.context.support.StaticWebApplicationContext;
@@ -51,19 +49,16 @@ import org.springframework.web.testfixture.servlet.MockRequestDispatcher;
import org.springframework.web.testfixture.servlet.MockServletContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link BeanNameViewResolver}, {@link UrlBasedViewResolver},
* {@link InternalResourceViewResolver}, {@link org.springframework.web.servlet.view.XmlViewResolver},
* and {@link AbstractCachingViewResolver}.
* {@link InternalResourceViewResolver}, and {@link AbstractCachingViewResolver}.
*
* @author Juergen Hoeller
* @author Chris Beams
* @author Sam Brannen
* @since 18.06.2003
*/
@SuppressWarnings("deprecation")
class ViewResolverTests {
private final StaticWebApplicationContext wac = new StaticWebApplicationContext();
@@ -396,80 +391,6 @@ class ViewResolverTests {
assertThat(lc.getResourceBundle().getString("code2")).isEqualTo("message2");
}
@Test
@SuppressWarnings("deprecation")
void xmlViewResolver() throws Exception {
this.wac.registerSingleton("testBean", TestBean.class);
this.wac.refresh();
TestBean testBean = (TestBean) this.wac.getBean("testBean");
org.springframework.web.servlet.view.XmlViewResolver vr = new org.springframework.web.servlet.view.XmlViewResolver();
vr.setLocation(new ClassPathResource("org/springframework/web/servlet/view/views.xml"));
vr.setApplicationContext(this.wac);
View view1 = vr.resolveViewName("example1", Locale.getDefault());
assertThat(TestView.class.equals(view1.getClass())).as("Correct view class").isTrue();
assertThat(((InternalResourceView) view1).getUrl()).as("Correct URL").isEqualTo("/example1.jsp");
View view2 = vr.resolveViewName("example2", Locale.getDefault());
assertThat(JstlView.class.equals(view2.getClass())).as("Correct view class").isTrue();
assertThat(((InternalResourceView) view2).getUrl()).as("Correct URL").isEqualTo("/example2new.jsp");
Map<String, Object> model = new HashMap<>();
TestBean tb = new TestBean();
model.put("tb", tb);
this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
view1.render(model, this.request, this.response);
assertThat(tb.equals(this.request.getAttribute("tb"))).as("Correct tb attribute").isTrue();
assertThat(this.request.getAttribute("test1")).as("Correct test1 attribute").isEqualTo("testvalue1");
assertThat(testBean.equals(this.request.getAttribute("test2"))).as("Correct test2 attribute").isTrue();
this.request.clearAttributes();
this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
view2.render(model, this.request, this.response);
assertThat(tb.equals(this.request.getAttribute("tb"))).as("Correct tb attribute").isTrue();
assertThat(this.request.getAttribute("test1")).as("Correct test1 attribute").isEqualTo("testvalue1");
assertThat(this.request.getAttribute("test2")).as("Correct test2 attribute").isEqualTo("testvalue2");
}
@Test
@SuppressWarnings("deprecation")
void xmlViewResolverDefaultLocation() {
StaticWebApplicationContext wac = new StaticWebApplicationContext() {
@Override
protected Resource getResourceByPath(String path) {
assertThat(path).as("Correct default location").isEqualTo(XmlViewResolver.DEFAULT_LOCATION);
return super.getResourceByPath(path);
}
};
wac.setServletContext(this.sc);
wac.refresh();
org.springframework.web.servlet.view.XmlViewResolver vr = new org.springframework.web.servlet.view.XmlViewResolver();
vr.setApplicationContext(wac);
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(vr::afterPropertiesSet);
}
@Test
@SuppressWarnings("deprecation")
void xmlViewResolverWithoutCache() {
StaticWebApplicationContext wac = new StaticWebApplicationContext() {
@Override
protected Resource getResourceByPath(String path) {
assertThat(path).as("Correct default location").isEqualTo(XmlViewResolver.DEFAULT_LOCATION);
return super.getResourceByPath(path);
}
};
wac.setServletContext(this.sc);
wac.refresh();
org.springframework.web.servlet.view.XmlViewResolver vr = new org.springframework.web.servlet.view.XmlViewResolver();
vr.setCache(false);
vr.setApplicationContext(wac);
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() ->
vr.resolveViewName("example1", Locale.getDefault()));
}
@Test
void cacheRemoval() throws Exception {
this.wac.refresh();