Browse Source

Polishing

pull/32357/head
Juergen Hoeller 2 years ago
parent
commit
95a8646309
  1. 4
      spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java
  2. 33
      spring-web/src/test/java/org/springframework/web/method/annotation/ModelFactoryTests.java
  3. 30
      spring-web/src/test/java/org/springframework/web/method/annotation/SessionAttributesHandlerTests.java
  4. 2
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java

4
spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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()));
}

33
spring-web/src/test/java/org/springframework/web/method/annotation/ModelFactoryTests.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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();
}

30
spring-web/src/test/java/org/springframework/web/method/annotation/SessionAttributesHandlerTests.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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 {
}
}

2
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java

@ -438,7 +438,7 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes @@ -438,7 +438,7 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
* Handle an {@link ErrorResponse} exception.
* <p>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

Loading…
Cancel
Save