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

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

Introduction

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

Prototype

public static boolean register(Class<?> annotatedClass) 

Source Link

Document

Registers the specified Spring annotated POJO class, which will be used to configure and initialize the Spring ApplicationContext.

Usage

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

@Test
public void testInitWithAnnotatedClasses() {
    final AnnotationConfigApplicationContext mockApplicationContext = mock(
            AnnotationConfigApplicationContext.class, "testInitWithAnnotatedClasses");

    doNothing().when(mockApplicationContext).addApplicationListener(any(ApplicationListener.class));
    doNothing().when(mockApplicationContext).registerShutdownHook();
    doNothing().when(mockApplicationContext).refresh();
    doNothing().when(mockApplicationContext).register(Matchers.<Class<?>[]>anyVararg());
    //doNothing().when(mockApplicationContext).register(annotatedClasses(TestAppConfigOne.class, TestAppConfigTwo.class));

    when(mockApplicationContext.getId()).thenReturn("testInitWithAnnotatedClasses");
    when(mockApplicationContext.isRunning()).thenReturn(true);

    assertNull(SpringContextBootstrappingInitializer.applicationContext);

    SpringContextBootstrappingInitializer.register(TestAppConfigOne.class);
    SpringContextBootstrappingInitializer.register(TestAppConfigTwo.class);

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

    initializer.init(createParameters("test", "test"));

    verify(mockApplicationContext, times(1)).addApplicationListener(same(initializer));
    verify(mockApplicationContext, times(1)).registerShutdownHook();
    verify(mockApplicationContext, times(1)).register(TestAppConfigOne.class, TestAppConfigTwo.class);
    //verify(mockApplicationContext, times(1)).register(annotatedClasses(TestAppConfigOne.class, TestAppConfigTwo.class));
    //verify(mockApplicationContext, times(1)).register(Matchers.<Class<?>[]>anyVararg());
    verify(mockApplicationContext, never()).scan(any(String[].class));

    assertEquals(mockApplicationContext, SpringContextBootstrappingInitializer.getApplicationContext());
}

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

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

    try {/*w  w w  .j a  v a2s  .  c  o m*/
        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 {// w  ww.j a  va 2s  .com
        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.  ja  v a  2  s . co  m*/
        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 {//  w  ww  . j  a  va  2s  . c o  m
        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 testNotifyOnExistingContextRefreshedEventBeforeApplicationContextExists() {
    assertNull(SpringContextBootstrappingInitializer.contextRefreshedEvent);

    TestApplicationListener testApplicationListener = new TestApplicationListener(
            "testNotifyOnExistingContextRefreshedEventBeforeApplicationContextExists");

    try {//from w w w . ja va  2 s . c om
        testApplicationListener = SpringContextBootstrappingInitializer.register(testApplicationListener);
        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 {/*  ww w  .  ja  va 2s  .  co  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 . ja  va2 s. c om
        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);
    }
}