Example usage for org.springframework.mock.web MockServletContext getAttribute

List of usage examples for org.springframework.mock.web MockServletContext getAttribute

Introduction

In this page you can find the example usage for org.springframework.mock.web MockServletContext getAttribute.

Prototype

@Override
    @Nullable
    public Object getAttribute(String name) 

Source Link

Usage

From source file:com.cloudera.oryx.lambda.serving.ModelManagerListenerIT.java

@Test
public void testListener() throws Exception {
    Map<String, Object> overlayConfig = new HashMap<>();
    overlayConfig.put("oryx.serving.model-manager-class", MockServingModelManager.class.getName());
    Config config = ConfigUtils.overlayOn(overlayConfig, getConfig());
    String serializedConfig = ConfigUtils.serialize(config);

    MockServletContext mockContext = new MockServletContext();
    mockContext.addInitParameter(ConfigUtils.class.getName() + ".serialized", serializedConfig);

    startMessaging();/*from w ww  .j  ava  2  s .c om*/

    ModelManagerListener<?, ?, ?> listener = new ModelManagerListener<>();
    listener.init(mockContext);
    try {
        listener.contextInitialized(new ServletContextEvent(mockContext));
        ServingModelManager<?> manager = (ServingModelManager<?>) mockContext
                .getAttribute(ModelManagerListener.MANAGER_KEY);
        assertNotNull(manager);
        assertFalse(manager.isReadOnly());
        assertNotNull(manager.getConfig());
    } finally {
        listener.contextDestroyed(new ServletContextEvent(mockContext));
    }
}

From source file:org.jasig.springframework.web.portlet.context.PortletContextLoaderTests.java

@Test
public void testContextLoaderListenerWithDefaultContext() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(PortletContextLoader.CONFIG_LOCATION_PARAM,
            "/org/springframework/web/context/WEB-INF/applicationContext.xml "
                    + "/org/springframework/web/context/WEB-INF/context-addition.xml");
    ServletContextListener listener = new PortletContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);/*from   www .  java  2 s . c  om*/
    PortletContextLoader contextLoader = (PortletContextLoader) sc
            .getAttribute(PortletApplicationContextUtils2.ROOT_PORTLET_APPLICATION_CONTEXT_LOADER_ATTRIBUTE);
    assertNotNull(contextLoader);

    //initialize the portlet application context, needed because PortletContextLoaderListener.contextInitialized doesn't actually create
    //the portlet app context due to lack of PortletContext reference
    MockPortletContext pc = new MockPortletContext(sc);
    PortletApplicationContext context = PortletApplicationContextUtils2.getPortletApplicationContext(pc);

    assertTrue("Correct PortletApplicationContext exposed in PortletContext",
            context instanceof ContribXmlPortletApplicationContext);
    assertTrue(PortletContextLoader
            .getCurrentPortletApplicationContext() instanceof ContribXmlPortletApplicationContext);
    LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
    assertTrue("Has father", context.containsBean("father"));
    assertTrue("Has rod", context.containsBean("rod"));
    assertTrue("Has kerry", context.containsBean("kerry"));
    assertTrue("Not destroyed", !lb.isDestroyed());
    assertFalse(context.containsBean("beans1.bean1"));
    assertFalse(context.containsBean("beans1.bean2"));
    listener.contextDestroyed(event);
    assertTrue("Destroyed", lb.isDestroyed());
    assertNull(sc.getAttribute(PortletApplicationContext.ROOT_PORTLET_APPLICATION_CONTEXT_ATTRIBUTE));
    assertNull(PortletContextLoader.getCurrentPortletApplicationContext());
}