Example usage for org.springframework.web.context WebApplicationContext getBeansOfType

List of usage examples for org.springframework.web.context WebApplicationContext getBeansOfType

Introduction

In this page you can find the example usage for org.springframework.web.context WebApplicationContext getBeansOfType.

Prototype

<T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
        throws BeansException;

Source Link

Document

Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.

Usage

From source file:org.hdiv.web.servlet.view.freemarker.FreeMarkerViewTests.java

@Test
public void testNoFreeMarkerConfig() throws Exception {
    FreeMarkerView fv = new FreeMarkerView();

    MockControl wmc = MockControl.createControl(WebApplicationContext.class);
    WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
    wac.getBeansOfType(FreeMarkerConfig.class, true, false);
    wmc.setReturnValue(new HashMap());
    wac.getParentBeanFactory();//from w  w w .j a va2  s  .c  o  m
    wmc.setReturnValue(null);
    wac.getServletContext();
    wmc.setReturnValue(new MockServletContext());
    wmc.replay();

    fv.setUrl("anythingButNull");
    try {
        fv.setApplicationContext(wac);
        fv.afterPropertiesSet();
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (ApplicationContextException ex) {
        // Check there's a helpful error message
        assertTrue(ex.getMessage().indexOf("FreeMarkerConfig") != -1);
    }

    wmc.verify();
}

From source file:org.hdiv.web.servlet.view.freemarker.FreeMarkerViewTests.java

@Test
public void testValidTemplateName() throws Exception {
    FreeMarkerView fv = new FreeMarkerView();

    MockControl wmc = MockControl.createNiceControl(WebApplicationContext.class);
    WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
    MockServletContext sc = new MockServletContext();

    wac.getBeansOfType(FreeMarkerConfig.class, true, false);
    Map configs = new HashMap();
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new FreeMarkerTestConfiguration());
    configs.put("configurer", configurer);
    wmc.setReturnValue(configs);/*  ww  w. j av a 2  s .c  o m*/
    wac.getParentBeanFactory();
    wmc.setReturnValue(null);
    wac.getServletContext();
    wmc.setReturnValue(sc, 5);
    wmc.replay();

    fv.setUrl("templateName");
    fv.setApplicationContext(wac);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.US);
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    HttpServletResponse response = new MockHttpServletResponse();

    Map model = new HashMap();
    model.put("myattr", "myvalue");
    fv.render(model, request, response);

    wmc.verify();
    assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType());
}

From source file:org.hdiv.web.servlet.view.freemarker.FreeMarkerViewTests.java

@Test
public void testKeepExistingContentType() throws Exception {
    FreeMarkerView fv = new FreeMarkerView();

    MockControl wmc = MockControl.createNiceControl(WebApplicationContext.class);
    WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
    MockServletContext sc = new MockServletContext();

    wac.getBeansOfType(FreeMarkerConfig.class, true, false);
    Map configs = new HashMap();
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new FreeMarkerTestConfiguration());
    configs.put("configurer", configurer);
    wmc.setReturnValue(configs);//  w w w . jav  a2 s.  c  om
    wac.getParentBeanFactory();
    wmc.setReturnValue(null);
    wac.getServletContext();
    wmc.setReturnValue(sc, 5);
    wmc.replay();

    fv.setUrl("templateName");
    fv.setApplicationContext(wac);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.US);
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    HttpServletResponse response = new MockHttpServletResponse();
    response.setContentType("myContentType");

    Map model = new HashMap();
    model.put("myattr", "myvalue");
    fv.render(model, request, response);

    wmc.verify();
    assertEquals("myContentType", response.getContentType());
}