Example usage for org.springframework.data.gemfire.support SpringContextBootstrappingInitializer BASE_PACKAGES_PARAMETER

List of usage examples for org.springframework.data.gemfire.support SpringContextBootstrappingInitializer BASE_PACKAGES_PARAMETER

Introduction

In this page you can find the example usage for org.springframework.data.gemfire.support SpringContextBootstrappingInitializer BASE_PACKAGES_PARAMETER.

Prototype

String BASE_PACKAGES_PARAMETER

To view the source code for org.springframework.data.gemfire.support SpringContextBootstrappingInitializer BASE_PACKAGES_PARAMETER.

Click Source Link

Usage

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

@Test
public void testInitWhenApplicationContextIsInactive() {
    ConfigurableApplicationContext mockInactiveApplicationContext = mock(ConfigurableApplicationContext.class,
            "testInitWhenApplicationContextIsInactive.Inactive");

    when(mockInactiveApplicationContext.isActive()).thenReturn(false);

    SpringContextBootstrappingInitializer.applicationContext = mockInactiveApplicationContext;

    assertSame(mockInactiveApplicationContext, SpringContextBootstrappingInitializer.getApplicationContext());

    final ConfigurableApplicationContext mockNewApplicationContext = mock(ConfigurableApplicationContext.class,
            "testInitWhenApplicationContextIsInactive.New");

    when(mockNewApplicationContext.getId()).thenReturn("testInitWhenApplicationContextIsInactive.New");
    when(mockNewApplicationContext.isRunning()).thenReturn(true);

    SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
        @Override/*from  www  . j  a  v a2 s. c o  m*/
        protected ConfigurableApplicationContext createApplicationContext(final String[] basePackages,
                final String[] configLocations) {
            return mockNewApplicationContext;
        }
    };

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

    verify(mockNewApplicationContext, times(1)).addApplicationListener(same(initializer));
    verify(mockNewApplicationContext, times(1)).registerShutdownHook();
    verify(mockNewApplicationContext, times(1)).refresh();

    assertSame(mockNewApplicationContext, SpringContextBootstrappingInitializer.getApplicationContext());
}

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

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

    try {/*from   ww w .j a  va 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//from   w ww .j a v  a2  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);
    }
}