Search in sources :

Example 1 with TestBean

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")));
}
Also used : MockWebSession(org.springframework.web.testfixture.server.MockWebSession) WebSession(org.springframework.web.server.WebSession) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockWebSession(org.springframework.web.testfixture.server.MockWebSession) Test(org.junit.jupiter.api.Test)

Example 2 with TestBean

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();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) HashMap(java.util.HashMap) BindingContext(org.springframework.web.reactive.BindingContext) Test(org.junit.jupiter.api.Test)

Example 3 with TestBean

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");
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) TestBean(org.springframework.beans.testfixture.beans.TestBean) MediaType(org.springframework.http.MediaType) RequestContext(org.springframework.web.reactive.result.view.RequestContext) DummyMacroRequestContext(org.springframework.web.reactive.result.view.DummyMacroRequestContext) BindStatus(org.springframework.web.reactive.result.view.BindStatus) HashMap(java.util.HashMap) ModelMap(org.springframework.ui.ModelMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Test(org.junit.jupiter.api.Test)

Example 4 with TestBean

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();
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) HashMap(java.util.HashMap) TestBean(org.springframework.beans.testfixture.beans.TestBean) ModelMap(org.springframework.ui.ModelMap) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) DummyMacroRequestContext(org.springframework.web.reactive.result.view.DummyMacroRequestContext)

Example 5 with TestBean

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();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Aggregations

TestBean (org.springframework.beans.testfixture.beans.TestBean)808 Test (org.junit.jupiter.api.Test)765 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)472 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)268 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)183 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)167 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)162 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)118 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)96 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)40 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)40 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)39 Properties (java.util.Properties)38 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)37 HashMap (java.util.HashMap)36 Errors (org.springframework.validation.Errors)35 PropertyEditorSupport (java.beans.PropertyEditorSupport)34 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)29 List (java.util.List)28 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)28