use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class SessionAttributesHandlerTests method retrieveAttributes.
@Test
public void retrieveAttributes() {
WebSession session = new MockWebSession();
session.getAttributes().put("attr1", "value1");
session.getAttributes().put("attr2", "value2");
session.getAttributes().put("attr3", new TestBean());
session.getAttributes().put("attr4", new TestBean());
assertThat(sessionAttributesHandler.retrieveAttributes(session).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(session).keySet()).as("Named attributes (attr1, attr2) and resolved attribute (att3) should be 'known'").isEqualTo(new HashSet<>(asList("attr1", "attr2", "attr3")));
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class AbstractViewTests method resolveAsyncAttributes.
@Test
public void resolveAsyncAttributes() {
TestBean testBean1 = new TestBean("Bean1");
TestBean testBean2 = new TestBean("Bean2");
Map<String, Object> inMap = new HashMap<>();
inMap.put("attr1", Mono.just(testBean1).delayElement(Duration.ofMillis(10)));
inMap.put("attr2", Flux.just(testBean1, testBean2).delayElements(Duration.ofMillis(10)));
inMap.put("attr3", Single.just(testBean2));
inMap.put("attr4", Observable.just(testBean1, testBean2));
inMap.put("attr5", Mono.empty());
this.exchange.getAttributes().put(View.BINDING_CONTEXT_ATTRIBUTE, new BindingContext());
TestView view = new TestView();
StepVerifier.create(view.render(inMap, null, this.exchange)).verifyComplete();
Map<String, Object> outMap = view.attributes;
assertThat(outMap.get("attr1")).isEqualTo(testBean1);
assertThat(outMap.get("attr2")).isEqualTo(Arrays.asList(testBean1, testBean2));
assertThat(outMap.get("attr3")).isEqualTo(testBean2);
assertThat(outMap.get("attr4")).isEqualTo(Arrays.asList(testBean1, testBean2));
assertThat(outMap.get("attr5")).isNull();
assertThat(outMap.get(BindingResult.MODEL_KEY_PREFIX + "attr1")).isNotNull();
assertThat(outMap.get(BindingResult.MODEL_KEY_PREFIX + "attr3")).isNotNull();
assertThat(outMap.get(BindingResult.MODEL_KEY_PREFIX + "attr2")).isNull();
assertThat(outMap.get(BindingResult.MODEL_KEY_PREFIX + "attr4")).isNull();
assertThat(outMap.get(BindingResult.MODEL_KEY_PREFIX + "attr5")).isNull();
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class FreeMarkerMacroTests method springMacroRequestContextIsAutomaticallyExposedAsModelAttribute.
@Test
public void springMacroRequestContextIsAutomaticallyExposedAsModelAttribute() throws Exception {
storeTemplateInTempDir("<@spring.bind \"testBean.name\"/>\nHi ${spring.status.value}");
FreeMarkerView view = new FreeMarkerView() {
@Override
protected Mono<Void> renderInternal(Map<String, Object> renderAttributes, MediaType contentType, ServerWebExchange exchange) {
Object value = renderAttributes.get(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE);
assertThat(value).isInstanceOf(RequestContext.class);
BindStatus status = ((RequestContext) value).getBindStatus("testBean.name");
assertThat(status.getExpression()).isEqualTo("name");
assertThat(status.getValue()).isEqualTo("Dilbert");
return super.renderInternal(renderAttributes, contentType, exchange);
}
};
view.setApplicationContext(this.applicationContext);
view.setBeanName("myView");
view.setUrl("tmp.ftl");
view.setConfiguration(this.freeMarkerConfig);
view.render(singletonMap("testBean", new TestBean("Dilbert", 99)), null, this.exchange).subscribe();
assertThat(getOutput()).containsExactly("Hi Dilbert");
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class FreeMarkerMacroTests method getMacroOutput.
private List<String> getMacroOutput(String name) throws Exception {
String macro = fetchMacro(name);
assertThat(macro).isNotNull();
storeTemplateInTempDir(macro);
Map<String, String> msgMap = new HashMap<>();
msgMap.put("hello", "Howdy");
msgMap.put("world", "Mundo");
TestBean darren = new TestBean("Darren", 99);
TestBean fred = new TestBean("Fred");
fred.setJedi(true);
darren.setSpouse(fred);
darren.setJedi(true);
darren.setStringArray(new String[] { "John", "Fred" });
Map<String, String> names = new HashMap<>();
names.put("Darren", "Darren Davison");
names.put("John", "John Doe");
names.put("Fred", "Fred Bloggs");
names.put("Rob&Harrop", "Rob Harrop");
ModelMap model = new ExtendedModelMap();
DummyMacroRequestContext rc = new DummyMacroRequestContext(this.exchange, model, this.applicationContext);
rc.setMessageMap(msgMap);
rc.setContextPath("/springtest");
model.put("command", darren);
model.put(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, rc);
model.put("msgArgs", new Object[] { "World" });
model.put("nameOptionMap", names);
model.put("options", names.values());
FreeMarkerView view = new FreeMarkerView();
view.setApplicationContext(this.applicationContext);
view.setBeanName("myView");
view.setUrl("tmp.ftl");
view.setExposeSpringMacroHelpers(false);
view.setConfiguration(freeMarkerConfig);
view.render(model, null, this.exchange).subscribe();
return getOutput();
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class ModelAndViewResolverMethodReturnValueHandlerTests method handleNonSimpleType.
@Test
public void handleNonSimpleType() throws Exception {
MethodParameter returnType = new MethodParameter(getClass().getDeclaredMethod("testBeanReturnValue"), -1);
handler.handleReturnValue(new TestBean(), returnType, mavContainer, request);
assertThat(mavContainer.containsAttribute("testBean")).isTrue();
}
Aggregations