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

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

Introduction

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

Prototype

SpringContextBootstrappingInitializer

Source Link

Usage

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

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

    try {// ww  w . j  a v  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 w w  . jav a2s  . c om
        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  v a  2s  .  c  o  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));
    }
}

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

@Test
public void testOnApplicationEvent() {
    TestApplicationListener testApplicationListener = new TestApplicationListener("testOnApplicationEvent");

    try {/* w  ww .j  a  v a2 s  .  c om*/
        testApplicationListener = SpringContextBootstrappingInitializer.register(testApplicationListener);

        assertUnnotified(testApplicationListener);

        ContextRefreshedEvent testContextRefreshedEvent = new ContextRefreshedEvent(
                mock(ApplicationContext.class, "testOnApplicationEvent"));

        new SpringContextBootstrappingInitializer().onApplicationEvent(testContextRefreshedEvent);

        assertNotified(testApplicationListener, testContextRefreshedEvent);
    } finally {
        SpringContextBootstrappingInitializer.unregister(testApplicationListener);
    }
}

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

@Test
public void testOnApplicationEventWithContextStartedEvent() {
    TestApplicationListener testApplicationListener = new TestApplicationListener(
            "testOnApplicationEventWithContextStartedEvent");

    try {//from  w ww .jav a 2 s .c  o  m
        testApplicationListener = SpringContextBootstrappingInitializer.register(testApplicationListener);

        assertUnnotified(testApplicationListener);

        ContextStartedEvent testContextStartedEvent = mock(ContextStartedEvent.class,
                "testOnApplicationEventWithContextStartedEvent");

        new SpringContextBootstrappingInitializer().onApplicationEvent(testContextStartedEvent);

        assertUnnotified(testApplicationListener);
    } finally {
        SpringContextBootstrappingInitializer.unregister(testApplicationListener);
    }
}

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

@Test
public void testOnApplicationEventWithMultipleRegisteredApplicationListeners() {
    TestApplicationListener testApplicationListenerOne = new TestApplicationListener(
            "testOnApplicationEventWithMultipleRegisteredApplicationListeners.1");

    TestApplicationListener testApplicationListenerTwo = new TestApplicationListener(
            "testOnApplicationEventWithMultipleRegisteredApplicationListeners.2");

    TestApplicationListener testApplicationListenerThree = new TestApplicationListener(
            "testOnApplicationEventWithMultipleRegisteredApplicationListeners.3");

    try {//from  w  w w  .j av a2 s.  c om
        testApplicationListenerOne = SpringContextBootstrappingInitializer.register(testApplicationListenerOne);
        testApplicationListenerTwo = SpringContextBootstrappingInitializer.register(testApplicationListenerTwo);
        testApplicationListenerThree = SpringContextBootstrappingInitializer
                .register(testApplicationListenerThree);

        assertUnnotified(testApplicationListenerOne);
        assertUnnotified(testApplicationListenerTwo);
        assertUnnotified(testApplicationListenerThree);

        ContextRefreshedEvent testContextRefreshedEvent = new ContextRefreshedEvent(mock(
                ApplicationContext.class, "testOnApplicationEventWithMultipleRegisteredApplicationListeners"));

        new SpringContextBootstrappingInitializer().onApplicationEvent(testContextRefreshedEvent);

        assertNotified(testApplicationListenerOne, testContextRefreshedEvent);
        assertNotified(testApplicationListenerTwo, testContextRefreshedEvent);
        assertNotified(testApplicationListenerThree, testContextRefreshedEvent);
    } finally {
        SpringContextBootstrappingInitializer.unregister(testApplicationListenerOne);
        SpringContextBootstrappingInitializer.unregister(testApplicationListenerTwo);
        SpringContextBootstrappingInitializer.unregister(testApplicationListenerThree);
    }
}

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

@Test
public void testOnApplicationEventWithUnregisteredApplicationListener() {
    TestApplicationListener testApplicationListener = new TestApplicationListener(
            "testOnApplicationEventWithUnregisteredApplicationListener");

    try {/*from w  ww. j  a va 2  s . com*/
        testApplicationListener = SpringContextBootstrappingInitializer
                .unregister(SpringContextBootstrappingInitializer.register(testApplicationListener));

        assertUnnotified(testApplicationListener);

        ContextRefreshedEvent testContextRefreshedEvent = new ContextRefreshedEvent(
                mock(ApplicationContext.class, "testRegisterThenUnregisterWithOnApplicationEvent"));

        new SpringContextBootstrappingInitializer().onApplicationEvent(testContextRefreshedEvent);

        assertUnnotified(testApplicationListener);
    } finally {
        SpringContextBootstrappingInitializer.unregister(testApplicationListener);
    }
}

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

@Test
public void testNotifyOnExistingContextRefreshedEventAfterContextRefreshed() {
    ContextRefreshedEvent testContextRefreshedEvent = new ContextRefreshedEvent(mock(ApplicationContext.class));

    new SpringContextBootstrappingInitializer().onApplicationEvent(testContextRefreshedEvent);

    TestApplicationListener testApplicationListener = new TestApplicationListener(
            "testNotifyOnExistingContextRefreshedEventAfterContextRefreshed");

    try {//w w  w. ja  v a  2s  . c  o  m
        testApplicationListener = SpringContextBootstrappingInitializer.register(testApplicationListener);
        assertNotified(testApplicationListener, testContextRefreshedEvent);
    } finally {
        SpringContextBootstrappingInitializer.unregister(testApplicationListener);
    }
}

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

@Test
public void testOnApplicationEventAndNotifyOnExistingContextRefreshedEvent() {
    ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
            "testOnApplicationEventAndNotifyOnExistingContextRefreshedEvent");

    SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer();

    TestApplicationListener testApplicationListenerOne = new TestApplicationListener(
            "testOnApplicationEventAndNotifyOnExistingContextRefreshedEvent.1");

    TestApplicationListener testApplicationListenerTwo = new TestApplicationListener(
            "testOnApplicationEventAndNotifyOnExistingContextRefreshedEvent.2");

    TestApplicationListener testApplicationListenerThree = new TestApplicationListener(
            "testOnApplicationEventAndNotifyOnExistingContextRefreshedEvent.3");

    try {/*from   ww w.j ava 2s  .  c  o m*/
        testApplicationListenerOne = SpringContextBootstrappingInitializer.register(testApplicationListenerOne);

        assertUnnotified(testApplicationListenerOne);
        assertUnnotified(testApplicationListenerTwo);
        assertUnnotified(testApplicationListenerThree);

        ContextRefreshedEvent testContextRefreshedEvent = new ContextRefreshedEvent(mockApplicationContext);

        initializer.onApplicationEvent(testContextRefreshedEvent);

        assertNotified(testApplicationListenerOne, testContextRefreshedEvent);
        assertUnnotified(testApplicationListenerTwo);
        assertUnnotified(testApplicationListenerThree);

        testApplicationListenerTwo = SpringContextBootstrappingInitializer.register(testApplicationListenerTwo);

        assertNotified(testApplicationListenerTwo, testContextRefreshedEvent);
        assertUnnotified(testApplicationListenerOne);
        assertUnnotified(testApplicationListenerThree);

        ContextStoppedEvent testContextStoppedEvent = new ContextStoppedEvent(mockApplicationContext);

        initializer.onApplicationEvent(testContextStoppedEvent);

        assertUnnotified(testApplicationListenerOne);
        assertUnnotified(testApplicationListenerTwo);
        assertUnnotified(testApplicationListenerThree);

        initializer.onApplicationEvent(testContextRefreshedEvent);

        assertNotified(testApplicationListenerOne, testContextRefreshedEvent);
        assertNotified(testApplicationListenerTwo, testContextRefreshedEvent);
        assertUnnotified(testApplicationListenerThree);

        ContextClosedEvent testContextClosedEvent = new ContextClosedEvent(mockApplicationContext);

        initializer.onApplicationEvent(testContextClosedEvent);

        assertUnnotified(testApplicationListenerOne);
        assertUnnotified(testApplicationListenerTwo);
        assertUnnotified(testApplicationListenerThree);

        SpringContextBootstrappingInitializer.register(testApplicationListenerThree);

        assertUnnotified(testApplicationListenerOne);
        assertUnnotified(testApplicationListenerTwo);
        assertUnnotified(testApplicationListenerThree);
    } finally {
        SpringContextBootstrappingInitializer.unregister(testApplicationListenerOne);
        SpringContextBootstrappingInitializer.unregister(testApplicationListenerTwo);
        SpringContextBootstrappingInitializer.unregister(testApplicationListenerThree);
    }
}