Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Feb 6, 2024
1 parent 5434edd commit 95a8646
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -234,11 +234,9 @@ private boolean isBindingCandidate(String attributeName, Object value) {
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()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -51,7 +51,7 @@
*
* @author Rossen Stoyanchev
*/
public class ModelFactoryTests {
class ModelFactoryTests {

private NativeWebRequest webRequest;

Expand All @@ -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);
Expand All @@ -75,7 +75,7 @@ public void setUp() throws Exception {


@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);
Expand All @@ -84,7 +84,7 @@ public void modelAttributeMethod() throws Exception {
}

@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);
Expand All @@ -93,7 +93,7 @@ public void modelAttributeMethodWithExplicitName() throws Exception {
}

@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);
Expand All @@ -102,7 +102,7 @@ public void modelAttributeMethodWithNameByConvention() throws Exception {
}

@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);
Expand All @@ -112,7 +112,7 @@ public void modelAttributeMethodWithNullReturnValue() throws Exception {
}

@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);
Expand All @@ -122,7 +122,7 @@ public void modelAttributeWithBindingDisabled() throws Exception {
}

@Test
public void modelAttributeFromSessionWithBindingDisabled() throws Exception {
void modelAttributeFromSessionWithBindingDisabled() throws Exception {
Foo foo = new Foo();
this.attributeStore.storeAttribute(this.webRequest, "foo", foo);

Expand All @@ -136,7 +136,7 @@ public void modelAttributeFromSessionWithBindingDisabled() throws Exception {
}

@Test
public void sessionAttribute() throws Exception {
void sessionAttribute() throws Exception {
this.attributeStore.storeAttribute(this.webRequest, "sessionAttr", "sessionAttrValue");

ModelFactory modelFactory = createModelFactory("modelAttr", Model.class);
Expand All @@ -147,21 +147,20 @@ public void sessionAttribute() throws Exception {
}

@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(() ->
modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod));

// 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();
Expand All @@ -181,7 +180,7 @@ public void updateModelBindingResult() throws Exception {
}

@Test
public void updateModelSessionAttributesSaved() throws Exception {
void updateModelSessionAttributesSaved() throws Exception {
String attributeName = "sessionAttr";
String attribute = "value";
ModelAndViewContainer container = new ModelAndViewContainer();
Expand All @@ -199,7 +198,7 @@ public void updateModelSessionAttributesSaved() throws Exception {
}

@Test
public void updateModelSessionAttributesRemoved() throws Exception {
void updateModelSessionAttributesRemoved() throws Exception {
String attributeName = "sessionAttr";
String attribute = "value";
ModelAndViewContainer container = new ModelAndViewContainer();
Expand All @@ -221,7 +220,7 @@ public void updateModelSessionAttributesRemoved() throws Exception {
}

@Test // SPR-12542
public void updateModelWhenRedirecting() throws Exception {
void updateModelWhenRedirecting() throws Exception {
String attributeName = "sessionAttr";
String attribute = "value";
ModelAndViewContainer container = new ModelAndViewContainer();
Expand Down Expand Up @@ -286,7 +285,7 @@ public Boolean nullModelAttr() {
return null;
}

@ModelAttribute(name="foo", binding=false)
@ModelAttribute(name = "foo", binding = false)
public Foo modelAttrWithBindingDisabled() {
return new Foo();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,7 +16,6 @@

package org.springframework.web.method.annotation;


import java.util.HashSet;

import org.junit.jupiter.api.Test;
Expand All @@ -35,43 +34,48 @@

/**
* 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();
assertThat(sessionAttributesHandler.isHandlerSessionAttribute("simple", String.class)).isFalse();
}

@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());
Expand All @@ -90,7 +94,7 @@ public void cleanupAttributes() throws Exception {
}

@Test
public void storeAttributes() throws Exception {
void storeAttributes() {
ModelMap model = new ModelMap();
model.put("attr1", "value1");
model.put("attr2", "value2");
Expand All @@ -105,8 +109,8 @@ public void storeAttributes() throws Exception {
}


@SessionAttributes(names = { "attr1", "attr2" }, types = { TestBean.class })
private static class SessionAttributeHandler {
@SessionAttributes(names = {"attr1", "attr2"}, types = TestBean.class)
private static class TestSessionAttributesHolder {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ protected ModelAndView handleAsyncRequestTimeoutException(AsyncRequestTimeoutExc
* 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
Expand Down

0 comments on commit 95a8646

Please sign in to comment.