SGF-425 - Allow early initialization and re-initialization of LazyWiringDeclarableSupport instances.
Fixed test failures associated with changes to LazyWiringDeclarableSupport passing the ApplicationContext onApplicationEvents (ContextRefreshedEvent) directly rather than the ApplicationContext's ConfigurableListableBeanFactory required by BeanConfigurerSupport. (cherry picked from commit be6a3aac3e5281fdb2c77ad8b88200c92c0b7883) Signed-off-by: John Blum <jblum@pivotal.io>
This commit is contained in:
@@ -16,33 +16,43 @@
|
||||
|
||||
package org.springframework.data.gemfire;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.access.BeanFactoryReference;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.gemfire.GemfireBeanFactoryLocator;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
* @author John Blum
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "locatorContext.xml" })
|
||||
@SuppressWarnings("unused")
|
||||
public class GemfireBeanFactoryLocatorTest {
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private GemfireBeanFactoryLocator locator1, locator2;
|
||||
|
||||
private String INSTANCE_1 = "instance1";
|
||||
private String INSTANCE_2 = "instance2";
|
||||
|
||||
@@ -122,16 +132,18 @@ public class GemfireBeanFactoryLocatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFactoryLocatorContract() throws Exception {
|
||||
public void factoryLocatorContract() throws Exception {
|
||||
BeanFactoryReference factory1 = locator1.useBeanFactory(INSTANCE_1);
|
||||
assertNotNull(factory1.getFactory());
|
||||
|
||||
assertThat(factory1.getFactory(), is(notNullValue()));
|
||||
|
||||
factory1.release();
|
||||
try {
|
||||
factory1.getFactory();
|
||||
fail("should have received exception");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// it's okay
|
||||
}
|
||||
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("The BeanFactory has already been released or closed");
|
||||
|
||||
factory1.getFactory();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.CoreMatchers.sameInstance;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Properties;
|
||||
@@ -34,7 +36,8 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer;
|
||||
@@ -224,7 +227,13 @@ public class LazyWiringDeclarableSupportTest {
|
||||
|
||||
@Test
|
||||
public void onApplicationEvent() {
|
||||
ApplicationContext mockApplicationContext = mock(ApplicationContext.class, "MockApplicationContext");
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockConfigurableApplicationContext");
|
||||
|
||||
ConfigurableListableBeanFactory mockBeanFactory = mock(ConfigurableListableBeanFactory.class,
|
||||
"MockConfigurableListableBeanFactory");
|
||||
|
||||
when(mockApplicationContext.getBeanFactory()).thenReturn(mockBeanFactory);
|
||||
|
||||
final AtomicBoolean doPostInitCalled = new AtomicBoolean(false);
|
||||
|
||||
@@ -242,11 +251,13 @@ public class LazyWiringDeclarableSupportTest {
|
||||
try {
|
||||
declarable.init(parameters);
|
||||
declarable.onApplicationEvent(new ContextRefreshedEvent(mockApplicationContext));
|
||||
declarable.assertBeanFactory(mockApplicationContext);
|
||||
declarable.assertBeanFactory(mockBeanFactory);
|
||||
declarable.assertParameters(parameters);
|
||||
|
||||
assertThat(declarable.isInitialized(), is(true));
|
||||
assertThat(doPostInitCalled.get(), is(true));
|
||||
|
||||
verify(mockApplicationContext, times(1)).getBeanFactory();
|
||||
}
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(declarable);
|
||||
@@ -264,7 +275,7 @@ public class LazyWiringDeclarableSupportTest {
|
||||
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("The Spring ApplicationContext must not be null");
|
||||
expectedException.expectMessage("The Spring ApplicationContext (null) must be an instance of ConfigurableApplicationContext");
|
||||
|
||||
declarable.onApplicationEvent(mockContextRefreshedEvent);
|
||||
}
|
||||
@@ -279,7 +290,13 @@ public class LazyWiringDeclarableSupportTest {
|
||||
|
||||
@Test
|
||||
public void fullLifecycleOnApplicationEventToDestroy() throws Exception {
|
||||
ApplicationContext mockApplicationContext = mock(ApplicationContext.class, "MockApplicationContext");
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockConfigurableApplicationContext");
|
||||
|
||||
ConfigurableListableBeanFactory mockBeanFactory = mock(ConfigurableListableBeanFactory.class,
|
||||
"MockConfigurableListableBeanFactory");
|
||||
|
||||
when(mockApplicationContext.getBeanFactory()).thenReturn(mockBeanFactory);
|
||||
|
||||
final AtomicBoolean doPostInitCalled = new AtomicBoolean(false);
|
||||
|
||||
@@ -307,7 +324,7 @@ public class LazyWiringDeclarableSupportTest {
|
||||
|
||||
assertThat(declarable.isInitialized(), is(true));
|
||||
assertThat(doPostInitCalled.get(), is(true));
|
||||
declarable.assertBeanFactory(mockApplicationContext);
|
||||
declarable.assertBeanFactory(mockBeanFactory);
|
||||
declarable.assertParameters(parameters);
|
||||
|
||||
doPostInitCalled.set(false);
|
||||
@@ -322,6 +339,8 @@ public class LazyWiringDeclarableSupportTest {
|
||||
assertThat(declarable.isInitialized(), is(false));
|
||||
assertThat(declarable.nullSafeGetParameters(), is(not(sameInstance(parameters))));
|
||||
assertThat(doPostInitCalled.get(), is(false));
|
||||
|
||||
verify(mockApplicationContext, times(1)).getBeanFactory();
|
||||
}
|
||||
finally {
|
||||
initializer.onApplicationEvent(new ContextClosedEvent(mockApplicationContext));
|
||||
@@ -330,10 +349,15 @@ public class LazyWiringDeclarableSupportTest {
|
||||
|
||||
@Test
|
||||
public void initThenOnApplicationEventThenInitWhenInitialized() {
|
||||
ApplicationContext mockApplicationContext = mock(ApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
BeanFactory mockBeanFactory = mock(BeanFactory.class, "MockBeanFactory");
|
||||
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
ConfigurableListableBeanFactory mockConfigurableListableBeanFactory = mock(ConfigurableListableBeanFactory.class,
|
||||
"MockConfigurableListableBeanFactory");
|
||||
|
||||
when(mockApplicationContext.getBeanFactory()).thenReturn(mockConfigurableListableBeanFactory);
|
||||
|
||||
GemfireBeanFactoryLocator locator = new GemfireBeanFactoryLocator();
|
||||
locator.setBeanName("MockBeanFactory");
|
||||
locator.setBeanFactory(mockBeanFactory);
|
||||
@@ -389,6 +413,8 @@ public class LazyWiringDeclarableSupportTest {
|
||||
assertThat(declarable.isInitialized(), is(true));
|
||||
assertThat(declarable.nullSafeGetParameters(), is(sameInstance(parameters)));
|
||||
assertThat(doPostInitCalled.get(), is(true));
|
||||
|
||||
verify(mockApplicationContext, times(1)).getBeanFactory();
|
||||
}
|
||||
finally {
|
||||
locator.destroy();
|
||||
|
||||
Reference in New Issue
Block a user