From 95a86463091b05aadc34e2d1b6fba8ff808c2656 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 6 Feb 2024 17:06:05 +0100 Subject: [PATCH] Polishing --- .../web/method/annotation/ModelFactory.java | 4 +-- .../method/annotation/ModelFactoryTests.java | 33 +++++++++---------- .../SessionAttributesHandlerTests.java | 30 +++++++++-------- .../DefaultHandlerExceptionResolver.java | 2 +- 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java b/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java index 62af5c3e4a6..884f7caaf2a 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * 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. @@ -234,11 +234,9 @@ public final class ModelFactory { if (attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) { return false; } - if (this.sessionAttributesHandler.isHandlerSessionAttribute(attributeName, value.getClass())) { return true; } - return (!value.getClass().isArray() && !(value instanceof Collection) && !(value instanceof Map) && !BeanUtils.isSimpleValueType(value.getClass())); } diff --git a/spring-web/src/test/java/org/springframework/web/method/annotation/ModelFactoryTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/ModelFactoryTests.java index c9091e10844..0433dac2efa 100644 --- a/spring-web/src/test/java/org/springframework/web/method/annotation/ModelFactoryTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/annotation/ModelFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * 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. @@ -51,7 +51,7 @@ import static org.mockito.Mockito.mock; * * @author Rossen Stoyanchev */ -public class ModelFactoryTests { +class ModelFactoryTests { private NativeWebRequest webRequest; @@ -65,7 +65,7 @@ public class ModelFactoryTests { @BeforeEach - public void setUp() throws Exception { + void setup() { this.webRequest = new ServletWebRequest(new MockHttpServletRequest()); this.attributeStore = new DefaultSessionAttributeStore(); this.attributeHandler = new SessionAttributesHandler(TestController.class, this.attributeStore); @@ -75,7 +75,7 @@ public class ModelFactoryTests { @Test - public void modelAttributeMethod() throws Exception { + void modelAttributeMethod() throws Exception { ModelFactory modelFactory = createModelFactory("modelAttr", Model.class); HandlerMethod handlerMethod = createHandlerMethod("handle"); modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod); @@ -84,7 +84,7 @@ public class ModelFactoryTests { } @Test - public void modelAttributeMethodWithExplicitName() throws Exception { + void modelAttributeMethodWithExplicitName() throws Exception { ModelFactory modelFactory = createModelFactory("modelAttrWithName"); HandlerMethod handlerMethod = createHandlerMethod("handle"); modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod); @@ -93,7 +93,7 @@ public class ModelFactoryTests { } @Test - public void modelAttributeMethodWithNameByConvention() throws Exception { + void modelAttributeMethodWithNameByConvention() throws Exception { ModelFactory modelFactory = createModelFactory("modelAttrConvention"); HandlerMethod handlerMethod = createHandlerMethod("handle"); modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod); @@ -102,7 +102,7 @@ public class ModelFactoryTests { } @Test - public void modelAttributeMethodWithNullReturnValue() throws Exception { + void modelAttributeMethodWithNullReturnValue() throws Exception { ModelFactory modelFactory = createModelFactory("nullModelAttr"); HandlerMethod handlerMethod = createHandlerMethod("handle"); modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod); @@ -112,7 +112,7 @@ public class ModelFactoryTests { } @Test - public void modelAttributeWithBindingDisabled() throws Exception { + void modelAttributeWithBindingDisabled() throws Exception { ModelFactory modelFactory = createModelFactory("modelAttrWithBindingDisabled"); HandlerMethod handlerMethod = createHandlerMethod("handle"); modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod); @@ -122,7 +122,7 @@ public class ModelFactoryTests { } @Test - public void modelAttributeFromSessionWithBindingDisabled() throws Exception { + void modelAttributeFromSessionWithBindingDisabled() throws Exception { Foo foo = new Foo(); this.attributeStore.storeAttribute(this.webRequest, "foo", foo); @@ -136,7 +136,7 @@ public class ModelFactoryTests { } @Test - public void sessionAttribute() throws Exception { + void sessionAttribute() throws Exception { this.attributeStore.storeAttribute(this.webRequest, "sessionAttr", "sessionAttrValue"); ModelFactory modelFactory = createModelFactory("modelAttr", Model.class); @@ -147,7 +147,7 @@ public class ModelFactoryTests { } @Test - public void sessionAttributeNotPresent() throws Exception { + void sessionAttributeNotPresent() throws Exception { ModelFactory modelFactory = new ModelFactory(null, null, this.attributeHandler); HandlerMethod handlerMethod = createHandlerMethod("handleSessionAttr", String.class); assertThatExceptionOfType(HttpSessionRequiredException.class).isThrownBy(() -> @@ -155,13 +155,12 @@ public class ModelFactoryTests { // Now add attribute and try again this.attributeStore.storeAttribute(this.webRequest, "sessionAttr", "sessionAttrValue"); - modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod); assertThat(this.mavContainer.getModel().get("sessionAttr")).isEqualTo("sessionAttrValue"); } @Test - public void updateModelBindingResult() throws Exception { + void updateModelBindingResult() throws Exception { String commandName = "attr1"; Object command = new Object(); ModelAndViewContainer container = new ModelAndViewContainer(); @@ -181,7 +180,7 @@ public class ModelFactoryTests { } @Test - public void updateModelSessionAttributesSaved() throws Exception { + void updateModelSessionAttributesSaved() throws Exception { String attributeName = "sessionAttr"; String attribute = "value"; ModelAndViewContainer container = new ModelAndViewContainer(); @@ -199,7 +198,7 @@ public class ModelFactoryTests { } @Test - public void updateModelSessionAttributesRemoved() throws Exception { + void updateModelSessionAttributesRemoved() throws Exception { String attributeName = "sessionAttr"; String attribute = "value"; ModelAndViewContainer container = new ModelAndViewContainer(); @@ -221,7 +220,7 @@ public class ModelFactoryTests { } @Test // SPR-12542 - public void updateModelWhenRedirecting() throws Exception { + void updateModelWhenRedirecting() throws Exception { String attributeName = "sessionAttr"; String attribute = "value"; ModelAndViewContainer container = new ModelAndViewContainer(); @@ -286,7 +285,7 @@ public class ModelFactoryTests { return null; } - @ModelAttribute(name="foo", binding=false) + @ModelAttribute(name = "foo", binding = false) public Foo modelAttrWithBindingDisabled() { return new Foo(); } diff --git a/spring-web/src/test/java/org/springframework/web/method/annotation/SessionAttributesHandlerTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/SessionAttributesHandlerTests.java index 9c856d2d533..782ec55c1e3 100644 --- a/spring-web/src/test/java/org/springframework/web/method/annotation/SessionAttributesHandlerTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/annotation/SessionAttributesHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * 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. @@ -16,7 +16,6 @@ package org.springframework.web.method.annotation; - import java.util.HashSet; import org.junit.jupiter.api.Test; @@ -35,20 +34,21 @@ import static org.assertj.core.api.Assertions.assertThat; /** * Test fixture with {@link SessionAttributesHandler}. + * * @author Rossen Stoyanchev */ -public class SessionAttributesHandlerTests { +class SessionAttributesHandlerTests { private final SessionAttributeStore sessionAttributeStore = new DefaultSessionAttributeStore(); - private final SessionAttributesHandler sessionAttributesHandler = new SessionAttributesHandler( - SessionAttributeHandler.class, sessionAttributeStore); + private final SessionAttributesHandler sessionAttributesHandler = + new SessionAttributesHandler(TestSessionAttributesHolder.class, sessionAttributeStore); private final NativeWebRequest request = new ServletWebRequest(new MockHttpServletRequest()); @Test - public void isSessionAttribute() throws Exception { + void isSessionAttribute() { assertThat(sessionAttributesHandler.isHandlerSessionAttribute("attr1", String.class)).isTrue(); assertThat(sessionAttributesHandler.isHandlerSessionAttribute("attr2", String.class)).isTrue(); assertThat(sessionAttributesHandler.isHandlerSessionAttribute("simple", TestBean.class)).isTrue(); @@ -56,22 +56,26 @@ public class SessionAttributesHandlerTests { } @Test - public void retrieveAttributes() throws Exception { + void retrieveAttributes() { sessionAttributeStore.storeAttribute(request, "attr1", "value1"); sessionAttributeStore.storeAttribute(request, "attr2", "value2"); sessionAttributeStore.storeAttribute(request, "attr3", new TestBean()); sessionAttributeStore.storeAttribute(request, "attr4", new TestBean()); - assertThat(sessionAttributesHandler.retrieveAttributes(request).keySet()).as("Named attributes (attr1, attr2) should be 'known' right away").isEqualTo(new HashSet<>(asList("attr1", "attr2"))); + assertThat(sessionAttributesHandler.retrieveAttributes(request).keySet()) + .as("Named attributes (attr1, attr2) should be 'known' right away") + .isEqualTo(new HashSet<>(asList("attr1", "attr2"))); // Resolve 'attr3' by type sessionAttributesHandler.isHandlerSessionAttribute("attr3", TestBean.class); - assertThat(sessionAttributesHandler.retrieveAttributes(request).keySet()).as("Named attributes (attr1, attr2) and resolved attribute (att3) should be 'known'").isEqualTo(new HashSet<>(asList("attr1", "attr2", "attr3"))); + assertThat(sessionAttributesHandler.retrieveAttributes(request).keySet()) + .as("Named attributes (attr1, attr2) and resolved attribute (attr3) should be 'known'") + .isEqualTo(new HashSet<>(asList("attr1", "attr2", "attr3"))); } @Test - public void cleanupAttributes() throws Exception { + void cleanupAttributes() { sessionAttributeStore.storeAttribute(request, "attr1", "value1"); sessionAttributeStore.storeAttribute(request, "attr2", "value2"); sessionAttributeStore.storeAttribute(request, "attr3", new TestBean()); @@ -90,7 +94,7 @@ public class SessionAttributesHandlerTests { } @Test - public void storeAttributes() throws Exception { + void storeAttributes() { ModelMap model = new ModelMap(); model.put("attr1", "value1"); model.put("attr2", "value2"); @@ -105,8 +109,8 @@ public class SessionAttributesHandlerTests { } - @SessionAttributes(names = { "attr1", "attr2" }, types = { TestBean.class }) - private static class SessionAttributeHandler { + @SessionAttributes(names = {"attr1", "attr2"}, types = TestBean.class) + private static class TestSessionAttributesHolder { } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java index 4067573c7e5..ce6d4aacdc5 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java @@ -438,7 +438,7 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes * Handle an {@link ErrorResponse} exception. *

The default implementation sets status and the headers of the response * to those obtained from the {@code ErrorResponse}. If available, the - * {@link ProblemDetail#getDetail()} is used as the message for + * {@link ProblemDetail#getDetail()} is used as the message for * {@link HttpServletResponse#sendError(int, String)}. * @param errorResponse the exception to be handled * @param request current HTTP request