Example usage for org.springframework.context ApplicationContextException getCause

List of usage examples for org.springframework.context ApplicationContextException getCause

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContextException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

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

@Test
public void testContextLoaderWithInvalidContext() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(PortletContextLoader.CONTEXT_CLASS_PARAM,
            "org.springframework.web.context.support.InvalidWebApplicationContext");
    ServletContextListener listener = new PortletContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);//from  w w  w .  j a  va  2s  . c  o  m
    try {
        //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);
        PortletApplicationContextUtils2.getPortletApplicationContext(pc);
        fail("Should have thrown ApplicationContextException");
    } catch (ApplicationContextException ex) {
        // expected
        assertTrue(ex.getCause() instanceof ClassNotFoundException);
    }
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializerTest.java

@Test(expected = IllegalArgumentException.class)
public void testInitWhenBasePackagesAndContextConfigLocationsParametersAreUnspecified() throws Throwable {
    assertNull(SpringContextBootstrappingInitializer.applicationContext);

    try {/*w ww  . jav a  2 s.  co  m*/
        new SpringContextBootstrappingInitializer().init(createParameters(
                createParameters(SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER, ""),
                SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER, "  "));
    } catch (ApplicationContextException expected) {
        assertTrue(expected.getMessage().contains("Failed to bootstrap the Spring ApplicationContext!"));
        assertTrue(expected.getCause() instanceof IllegalArgumentException);
        assertEquals("'basePackages', 'configLocations' or 'AnnotatedClasses' must be specified"
                + " in order to construct and configure an instance of the ConfigurableApplicationContext",
                expected.getCause().getMessage());
        throw expected.getCause();
    }
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializerTest.java

@Test(expected = IllegalStateException.class)
public void testInitWhenApplicationContextIsNotRunning() {
    assertNull(SpringContextBootstrappingInitializer.applicationContext);

    final ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
            "testInitWhenApplicationContextIsNotRunning");

    when(mockApplicationContext.getId()).thenReturn("testInitWhenApplicationContextIsNotRunning");
    when(mockApplicationContext.isRunning()).thenReturn(false);

    SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
        @Override/*  w  ww .  j  a va 2  s.  c o  m*/
        protected ConfigurableApplicationContext createApplicationContext(final String[] basePackages,
                final String[] configLocations) {
            return mockApplicationContext;
        }
    };

    try {
        initializer.init(createParameters(SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER,
                "org.example.app, org.example.plugins"));

        SpringContextBootstrappingInitializer.getApplicationContext();
    } catch (ApplicationContextException expected) {
        assertTrue(expected.getMessage().contains("Failed to bootstrap the Spring ApplicationContext!"));
        assertTrue(expected.getCause() instanceof IllegalStateException);
        assertEquals(
                "The Spring ApplicationContext (testInitWhenApplicationContextIsNotRunning) failed to be properly initialized with the context config files ([]) or base packages ([org.example.app, org.example.plugins])!",
                expected.getCause().getMessage());
        throw (IllegalStateException) expected.getCause();
    } finally {
        verify(mockApplicationContext, times(1)).addApplicationListener(same(initializer));
        verify(mockApplicationContext, times(1)).registerShutdownHook();
        verify(mockApplicationContext, times(1)).refresh();

        assertNull(SpringContextBootstrappingInitializer.applicationContext);
    }
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializerTest.java

@Test(expected = IllegalStateException.class)
public void testInitLogsErrors() throws Throwable {
    final Log mockLog = mock(Log.class, "testInitLogsErrors.MockLog");

    SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
        @Override/*from w  ww  .  j  a va 2 s. co m*/
        protected Log initLogger() {
            return mockLog;
        }

        @Override
        protected ConfigurableApplicationContext createApplicationContext(final String[] basePackages,
                final String[] configLocations) {
            throw new IllegalStateException("TEST");
        }
    };

    try {
        initializer
                .init(createParameters(SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER,
                        "classpath/to/spring/application/context.xml"));
    } catch (ApplicationContextException expected) {
        assertTrue(expected.getMessage().contains("Failed to bootstrap the Spring ApplicationContext!"));
        assertTrue(expected.getCause() instanceof IllegalStateException);
        assertEquals("TEST", expected.getCause().getMessage());
        throw expected.getCause();
    } finally {
        verify(mockLog, times(1)).error(eq("Failed to bootstrap the Spring ApplicationContext!"),
                any(RuntimeException.class));
    }
}