SGF-425 - Allow early initialization and re-initialization of LazyWiringDeclarableSupport instances.
(cherry picked from commit 93280bf2047081e5387a896e27e19e5d7e5cce12) Signed-off-by: John Blum <jblum@pivotal.io>
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
* Copyright 2010-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.gemfire;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.gemfire.function.sample.HelloFunctionExecution;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.gemstone.gemfire.cache.execute.Function;
|
||||
import com.gemstone.gemfire.cache.execute.FunctionContext;
|
||||
import com.gemstone.gemfire.management.internal.cli.util.spring.StringUtils;
|
||||
|
||||
/**
|
||||
* The LazyWiringDeclarableSupportFunctionBasedIntegrationTest class is a test suite of test cases testing the contract
|
||||
* and functionality of a GemFire Function implementing LazyWiringDeclarableSupport, defined using native GemFire
|
||||
* configuration metadata (cache.xml).
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.junit.Test
|
||||
* @see org.junit.runner.RunWith
|
||||
* @see org.springframework.test.context.ContextConfiguration
|
||||
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
|
||||
* @since 1.7.0
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
@SuppressWarnings("unused")
|
||||
public class LazyWiringDeclarableSupportFunctionBasedIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private HelloFunctionExecution helloFunctionExecution;
|
||||
|
||||
/*
|
||||
@BeforeClass
|
||||
public static void setupBeforeClass() {
|
||||
Cache gemfireCache = new CacheFactory()
|
||||
.set("name", LazyWiringDeclarableSupportFunctionBasedIntegrationTest.class.getSimpleName())
|
||||
.set("mcast-port", "0")
|
||||
.set("log-level", "config")
|
||||
.set("cache-xml-file", null)
|
||||
.create();
|
||||
|
||||
assertThat(gemfireCache, is(notNullValue()));
|
||||
assertThat(SpringContextBootstrappingInitializer.getApplicationContext(), is(notNullValue()));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownAfterClass() {
|
||||
CacheFactory.getAnyInstance().close();
|
||||
}
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void helloGreeting() {
|
||||
assertThat(helloFunctionExecution.hello(null), is(equalTo("Hello Everyone!")));
|
||||
}
|
||||
|
||||
protected static abstract class FunctionAdaptor extends LazyWiringDeclarableSupport implements Function {
|
||||
|
||||
private final String id;
|
||||
|
||||
public FunctionAdaptor(final String id) {
|
||||
Assert.hasText(id, "The Function ID must be specified!");
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasResult() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isHA() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean optimizeForWrite() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static class HelloGemFireFunction extends FunctionAdaptor {
|
||||
|
||||
protected static final String ADDRESS_TO_PARAMETER = "hello.address.to";
|
||||
protected static final String DEFAULT_ADDRESS_TO = "World";
|
||||
protected static final String HELLO_GREETING = "Hello %1$s!";
|
||||
protected static final String ID = "hello";
|
||||
|
||||
@Value("${hello.default.address.to}")
|
||||
private String defaultAddressTo;
|
||||
|
||||
private String addressTo;
|
||||
|
||||
public HelloGemFireFunction() {
|
||||
super(ID);
|
||||
}
|
||||
|
||||
protected String getAddressTo() {
|
||||
return addressTo;
|
||||
}
|
||||
|
||||
protected String getDefaultAddressTo() {
|
||||
return (StringUtils.hasText(defaultAddressTo) ? defaultAddressTo : DEFAULT_ADDRESS_TO);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPostInit(final Properties parameters) {
|
||||
addressTo = parameters.getProperty(ADDRESS_TO_PARAMETER, getDefaultAddressTo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(final FunctionContext context) {
|
||||
context.getResultSender().lastResult(formatHelloGreeting(addressTo(context)));
|
||||
}
|
||||
|
||||
// precedence is... 1. Caller 2. GemFire 3. Spring
|
||||
protected String addressTo(FunctionContext context) {
|
||||
Object arguments = context.getArguments();
|
||||
String addressTo = null;
|
||||
|
||||
if (arguments instanceof Object[]) {
|
||||
Object[] args = (Object[]) arguments;
|
||||
addressTo = (args.length > 0 && args[0] != null ? String.valueOf(args[0]) : null);
|
||||
}
|
||||
else if (arguments != null) {
|
||||
addressTo = String.valueOf(arguments);
|
||||
}
|
||||
|
||||
return (StringUtils.hasText(addressTo) ? addressTo : getAddressTo());
|
||||
}
|
||||
|
||||
protected String formatHelloGreeting(String addressTo) {
|
||||
return String.format(HELLO_GREETING, addressTo);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,25 +16,25 @@
|
||||
|
||||
package org.springframework.data.gemfire;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
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;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer;
|
||||
@@ -46,6 +46,7 @@ import org.springframework.data.gemfire.support.SpringContextBootstrappingInitia
|
||||
* it's contract.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.junit.Rule
|
||||
* @see org.junit.Test
|
||||
* @see org.mockito.Mockito
|
||||
* @see org.springframework.data.gemfire.LazyWiringDeclarableSupport
|
||||
@@ -53,6 +54,15 @@ import org.springframework.data.gemfire.support.SpringContextBootstrappingInitia
|
||||
*/
|
||||
public class LazyWiringDeclarableSupportTest {
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
protected static void assertParameters(Properties parameters, String expectedKey, String expectedValue) {
|
||||
assertThat(parameters, is(notNullValue()));
|
||||
assertThat(parameters.containsKey(expectedKey), is(true));
|
||||
assertThat(parameters.getProperty(expectedKey), is(equalTo(expectedValue)));
|
||||
}
|
||||
|
||||
protected static Properties createParameters(final String parameter, final String value) {
|
||||
Properties parameters = new Properties();
|
||||
parameters.setProperty(parameter, value);
|
||||
@@ -60,7 +70,7 @@ public class LazyWiringDeclarableSupportTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssertInitialized() {
|
||||
public void assertInitialized() {
|
||||
LazyWiringDeclarableSupport declarable = new TestLazyWiringDeclarableSupport() {
|
||||
@Override protected boolean isInitialized() {
|
||||
return true;
|
||||
@@ -75,25 +85,30 @@ public class LazyWiringDeclarableSupportTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testAssertInitializedWhenUninitialized() {
|
||||
LazyWiringDeclarableSupport declarable = new TestLazyWiringDeclarableSupport();
|
||||
@Test
|
||||
public void assertInitializedWhenUninitialized() {
|
||||
LazyWiringDeclarableSupport declarable = new TestLazyWiringDeclarableSupport() {
|
||||
@Override protected boolean isInitialized() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage(String.format(
|
||||
"This Declarable object (%1$s) has not been properly configured and initialized",
|
||||
declarable.getClass().getName()));
|
||||
|
||||
declarable.assertInitialized();
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
assertEquals(String.format("This Declarable object (%1$s) has not been properly configured and initialized!",
|
||||
TestLazyWiringDeclarableSupport.class.getName()), expected.getMessage());
|
||||
throw expected;
|
||||
}
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(declarable);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssertUninitialized() {
|
||||
public void assertUninitialized() {
|
||||
LazyWiringDeclarableSupport declarable = new TestLazyWiringDeclarableSupport();
|
||||
|
||||
try {
|
||||
@@ -104,8 +119,8 @@ public class LazyWiringDeclarableSupportTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testAssertUninitializedWhenInitialized() {
|
||||
@Test
|
||||
public void assertUninitializedWhenInitialized() {
|
||||
LazyWiringDeclarableSupport declarable = new TestLazyWiringDeclarableSupport() {
|
||||
@Override protected boolean isInitialized() {
|
||||
return true;
|
||||
@@ -113,13 +128,34 @@ public class LazyWiringDeclarableSupportTest {
|
||||
};
|
||||
|
||||
try {
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage(String.format(
|
||||
"This Declarable object (%1$s) has already been configured and initialized",
|
||||
declarable.getClass().getName()));
|
||||
|
||||
declarable.assertUninitialized();
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
assertEquals(String.format(
|
||||
"This Declarable object (%1$s) has already been configured and initialized, and is currently active!",
|
||||
declarable.getClass().getName()), expected.getMessage());
|
||||
throw expected;
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(declarable);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void init() {
|
||||
LazyWiringDeclarableSupport declarable = new TestLazyWiringDeclarableSupport();
|
||||
|
||||
try {
|
||||
assertThat(declarable.isInitialized(), is(false));
|
||||
|
||||
declarable.init(createParameters("param", "value"));
|
||||
|
||||
assertParameters(declarable.nullSafeGetParameters(), "param", "value");
|
||||
|
||||
declarable.init(createParameters("newParam", "newValue"));
|
||||
|
||||
assertParameters(declarable.nullSafeGetParameters(), "newParam", "newValue");
|
||||
assertThat(declarable.isInitialized(), is(false));
|
||||
}
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(declarable);
|
||||
@@ -127,63 +163,59 @@ public class LazyWiringDeclarableSupportTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInit() {
|
||||
LazyWiringDeclarableSupport declarable = new TestLazyWiringDeclarableSupport();
|
||||
|
||||
try {
|
||||
assertFalse(declarable.isInitialized());
|
||||
declarable.init(createParameters("param", "value"));
|
||||
declarable.init(createParameters("newParam", "newValue"));
|
||||
}
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(declarable);
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testInitWhenInitialized() {
|
||||
public void isInitialized() {
|
||||
LazyWiringDeclarableSupport declarable = new TestLazyWiringDeclarableSupport() {
|
||||
@Override protected boolean isInitialized() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
assertThat(declarable.isInitialized(), is(true));
|
||||
assertThat(declarable.isNotInitialized(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUninitialized() {
|
||||
LazyWiringDeclarableSupport declarable = new TestLazyWiringDeclarableSupport() {
|
||||
@Override protected boolean isInitialized() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
assertThat(declarable.isInitialized(), is(false));
|
||||
assertThat(declarable.isNotInitialized(), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void locateBeanFactory() {
|
||||
BeanFactory mockBeanFactory = mock(BeanFactory.class, "MockBeanFactory");
|
||||
|
||||
GemfireBeanFactoryLocator locator = new GemfireBeanFactoryLocator();
|
||||
|
||||
locator.setBeanName("MockBeanFactory");
|
||||
locator.setBeanFactory(mockBeanFactory);
|
||||
|
||||
try {
|
||||
declarable.init(createParameters("param", "value"));
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
assertEquals(String.format(
|
||||
"This Declarable object (%1$s) has already been configured and initialized, and is currently active!",
|
||||
declarable.getClass().getName()), expected.getMessage());
|
||||
throw expected;
|
||||
locator.afterPropertiesSet();
|
||||
|
||||
assertThat(new TestLazyWiringDeclarableSupport().locateBeanFactory(null), is(sameInstance(mockBeanFactory)));
|
||||
}
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(declarable);
|
||||
locator.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullSafeGetParameters() {
|
||||
public void nullSafeGetParametersWithNullReference() {
|
||||
LazyWiringDeclarableSupport declarable = new TestLazyWiringDeclarableSupport();
|
||||
|
||||
try {
|
||||
declarable.init(createParameters("param", "value"));
|
||||
declarable.init(null);
|
||||
|
||||
Properties parameters = declarable.nullSafeGetParameters();
|
||||
|
||||
assertNotNull(parameters);
|
||||
assertFalse(parameters.isEmpty());
|
||||
assertEquals(1, parameters.size());
|
||||
assertEquals("value", parameters.getProperty("param"));
|
||||
|
||||
declarable.init(createParameters("newParam", "newValue"));
|
||||
parameters = declarable.nullSafeGetParameters();
|
||||
|
||||
assertNotNull(parameters);
|
||||
assertFalse(parameters.isEmpty());
|
||||
assertEquals(1, parameters.size());
|
||||
assertFalse(parameters.containsKey("param"));
|
||||
assertEquals("newValue", parameters.getProperty("newParam"));
|
||||
assertThat(parameters, is(notNullValue()));
|
||||
assertThat(parameters.isEmpty(), is(true));
|
||||
}
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(declarable);
|
||||
@@ -191,49 +223,8 @@ public class LazyWiringDeclarableSupportTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullSafeGetParametersWithNullReference() {
|
||||
LazyWiringDeclarableSupport declarable = new TestLazyWiringDeclarableSupport();
|
||||
|
||||
try {
|
||||
Properties parameters = declarable.nullSafeGetParameters();
|
||||
|
||||
assertNotNull(parameters);
|
||||
assertTrue(parameters.isEmpty());
|
||||
}
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(declarable);
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testOnApplicationEventWithNonConfigurableApplicationContext() {
|
||||
ApplicationContext mockApplicationContext = mock(ApplicationContext.class,
|
||||
"testOnApplicationEventWithNonConfigurableApplicationContext");
|
||||
|
||||
LazyWiringDeclarableSupport declarable = new TestLazyWiringDeclarableSupport();
|
||||
|
||||
try {
|
||||
declarable.onApplicationEvent(new ContextRefreshedEvent(mockApplicationContext));
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
assertEquals(String.format("The Spring ApplicationContext (%1$s) must be an instance of ConfigurableApplicationContext.",
|
||||
mockApplicationContext.getClass().getName()), expected.getMessage());
|
||||
throw expected;
|
||||
}
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(declarable);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnApplicationEventAndDoPostInit() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testOnApplicationEventAndDoPostInit.ApplicationContext");
|
||||
|
||||
ConfigurableListableBeanFactory mockBeanFactory = mock(ConfigurableListableBeanFactory.class,
|
||||
"testOnApplicationEventAndDoPostInit.BeanFactory");
|
||||
|
||||
when(mockApplicationContext.getBeanFactory()).thenReturn(mockBeanFactory);
|
||||
public void onApplicationEvent() {
|
||||
ApplicationContext mockApplicationContext = mock(ApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
final AtomicBoolean doPostInitCalled = new AtomicBoolean(false);
|
||||
|
||||
@@ -241,6 +232,7 @@ public class LazyWiringDeclarableSupportTest {
|
||||
@Override protected void doPostInit(final Properties parameters) {
|
||||
super.doPostInit(parameters);
|
||||
assertInitialized();
|
||||
LazyWiringDeclarableSupportTest.assertParameters(parameters, "param", "value");
|
||||
doPostInitCalled.set(true);
|
||||
}
|
||||
};
|
||||
@@ -250,12 +242,11 @@ public class LazyWiringDeclarableSupportTest {
|
||||
try {
|
||||
declarable.init(parameters);
|
||||
declarable.onApplicationEvent(new ContextRefreshedEvent(mockApplicationContext));
|
||||
declarable.assertEquals(parameters);
|
||||
declarable.assertSame(mockBeanFactory);
|
||||
declarable.assertBeanFactory(mockApplicationContext);
|
||||
declarable.assertParameters(parameters);
|
||||
|
||||
assertTrue(doPostInitCalled.get());
|
||||
|
||||
verify(mockApplicationContext, times(1)).getBeanFactory();
|
||||
assertThat(declarable.isInitialized(), is(true));
|
||||
assertThat(doPostInitCalled.get(), is(true));
|
||||
}
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(declarable);
|
||||
@@ -263,14 +254,32 @@ public class LazyWiringDeclarableSupportTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFullLifecycleIntegrationWithDestroy() throws Exception {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testFullLifecycleIntegrationWithDestroy.ApplicationContext");
|
||||
public void onApplicationEventWithNullApplicationContext() throws Throwable {
|
||||
LazyWiringDeclarableSupport declarable = new TestLazyWiringDeclarableSupport();
|
||||
|
||||
ConfigurableListableBeanFactory mockBeanFactory = mock(ConfigurableListableBeanFactory.class,
|
||||
"testFullLifecycleIntegrationWithDestroy.BeanFactory");
|
||||
try {
|
||||
ContextRefreshedEvent mockContextRefreshedEvent = mock(ContextRefreshedEvent.class, "MockContextRefreshedEvent");
|
||||
|
||||
when(mockApplicationContext.getBeanFactory()).thenReturn(mockBeanFactory);
|
||||
when(mockContextRefreshedEvent.getApplicationContext()).thenReturn(null);
|
||||
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("The Spring ApplicationContext must not be null");
|
||||
|
||||
declarable.onApplicationEvent(mockContextRefreshedEvent);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
assertThat(declarable.isInitialized(), is(false));
|
||||
throw t;
|
||||
}
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(declarable);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fullLifecycleOnApplicationEventToDestroy() throws Exception {
|
||||
ApplicationContext mockApplicationContext = mock(ApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
final AtomicBoolean doPostInitCalled = new AtomicBoolean(false);
|
||||
|
||||
@@ -278,6 +287,7 @@ public class LazyWiringDeclarableSupportTest {
|
||||
@Override protected void doPostInit(final Properties parameters) {
|
||||
super.doPostInit(parameters);
|
||||
assertInitialized();
|
||||
LazyWiringDeclarableSupportTest.assertParameters(parameters, "param", "value");
|
||||
doPostInitCalled.set(true);
|
||||
}
|
||||
};
|
||||
@@ -289,52 +299,123 @@ public class LazyWiringDeclarableSupportTest {
|
||||
try {
|
||||
declarable.init(parameters);
|
||||
|
||||
assertFalse(declarable.isInitialized());
|
||||
assertFalse(doPostInitCalled.get());
|
||||
assertSame(parameters, declarable.nullSafeGetParameters());
|
||||
assertThat(declarable.isInitialized(), is(false));
|
||||
assertThat(declarable.nullSafeGetParameters(), is(sameInstance(parameters)));
|
||||
assertThat(doPostInitCalled.get(), is(false));
|
||||
|
||||
initializer.onApplicationEvent(new ContextRefreshedEvent(mockApplicationContext));
|
||||
|
||||
assertTrue(declarable.isInitialized());
|
||||
assertTrue(doPostInitCalled.get());
|
||||
declarable.assertEquals(parameters);
|
||||
declarable.assertSame(mockBeanFactory);
|
||||
assertThat(declarable.isInitialized(), is(true));
|
||||
assertThat(doPostInitCalled.get(), is(true));
|
||||
declarable.assertBeanFactory(mockApplicationContext);
|
||||
declarable.assertParameters(parameters);
|
||||
|
||||
declarable.destroy();
|
||||
doPostInitCalled.set(false);
|
||||
declarable.destroy();
|
||||
|
||||
assertFalse(declarable.isInitialized());
|
||||
assertFalse(doPostInitCalled.get());
|
||||
assertNotSame(parameters, declarable.nullSafeGetParameters());
|
||||
assertThat(declarable.isInitialized(), is(false));
|
||||
assertThat(declarable.nullSafeGetParameters(), is(not(sameInstance(parameters))));
|
||||
assertThat(doPostInitCalled.get(), is(false));
|
||||
|
||||
initializer.onApplicationEvent(new ContextRefreshedEvent(mockApplicationContext));
|
||||
|
||||
assertFalse(declarable.isInitialized());
|
||||
assertFalse(doPostInitCalled.get());
|
||||
assertThat(declarable.isInitialized(), is(false));
|
||||
assertThat(declarable.nullSafeGetParameters(), is(not(sameInstance(parameters))));
|
||||
assertThat(doPostInitCalled.get(), is(false));
|
||||
}
|
||||
finally {
|
||||
initializer.onApplicationEvent(new ContextClosedEvent(mockApplicationContext));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initThenOnApplicationEventThenInitWhenInitialized() {
|
||||
ApplicationContext mockApplicationContext = mock(ApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
BeanFactory mockBeanFactory = mock(BeanFactory.class, "MockBeanFactory");
|
||||
|
||||
GemfireBeanFactoryLocator locator = new GemfireBeanFactoryLocator();
|
||||
locator.setBeanName("MockBeanFactory");
|
||||
locator.setBeanFactory(mockBeanFactory);
|
||||
|
||||
final AtomicBoolean doPostInitCalled = new AtomicBoolean(false);
|
||||
final AtomicReference<String> expectedKey = new AtomicReference<String>("testParam");
|
||||
final AtomicReference<String> expectedValue = new AtomicReference<String>("testValue");
|
||||
|
||||
TestLazyWiringDeclarableSupport declarable = new TestLazyWiringDeclarableSupport() {
|
||||
@Override protected void doPostInit(final Properties parameters) {
|
||||
super.doPostInit(parameters);
|
||||
assertInitialized();
|
||||
LazyWiringDeclarableSupportTest.assertParameters(parameters, expectedKey.get(), expectedValue.get());
|
||||
doPostInitCalled.set(true);
|
||||
}
|
||||
};
|
||||
|
||||
Properties parameters = createParameters("testParam", "testValue");
|
||||
|
||||
try {
|
||||
locator.afterPropertiesSet();
|
||||
|
||||
assertThat(declarable.isInitialized(), is(false));
|
||||
assertThat(declarable.nullSafeGetParameters(), is(not(sameInstance(parameters))));
|
||||
assertThat(doPostInitCalled.get(), is(false));
|
||||
|
||||
declarable.init(parameters);
|
||||
declarable.assertBeanFactory(mockBeanFactory);
|
||||
declarable.assertParameters(parameters);
|
||||
|
||||
assertThat(declarable.isInitialized(), is(true));
|
||||
assertThat(declarable.nullSafeGetParameters(), is(sameInstance(parameters)));
|
||||
assertThat(doPostInitCalled.get(), is(true));
|
||||
|
||||
doPostInitCalled.set(false);
|
||||
declarable.onApplicationEvent(new ContextRefreshedEvent(mockApplicationContext));
|
||||
declarable.assertBeanFactory(mockBeanFactory);
|
||||
declarable.assertParameters(parameters);
|
||||
|
||||
assertThat(declarable.isInitialized(), is(true));
|
||||
assertThat(declarable.nullSafeGetParameters(), is(sameInstance(parameters)));
|
||||
assertThat(doPostInitCalled.get(), is(true));
|
||||
|
||||
doPostInitCalled.set(false);
|
||||
expectedKey.set("mockKey");
|
||||
expectedValue.set("mockValue");
|
||||
parameters = createParameters("mockKey", "mockValue");
|
||||
|
||||
declarable.init(parameters);
|
||||
declarable.assertBeanFactory(mockBeanFactory);
|
||||
declarable.assertParameters(parameters);
|
||||
|
||||
assertThat(declarable.isInitialized(), is(true));
|
||||
assertThat(declarable.nullSafeGetParameters(), is(sameInstance(parameters)));
|
||||
assertThat(doPostInitCalled.get(), is(true));
|
||||
}
|
||||
finally {
|
||||
locator.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
protected static class TestLazyWiringDeclarableSupport extends LazyWiringDeclarableSupport {
|
||||
|
||||
private ConfigurableListableBeanFactory actualBeanFactory;
|
||||
private BeanFactory actualBeanFactory;
|
||||
private Properties actualParameters;
|
||||
|
||||
protected void assertEquals(final Properties expectedParameters) {
|
||||
Assert.assertEquals(expectedParameters, actualParameters);
|
||||
protected void assertBeanFactory(final BeanFactory expectedBeanFactory) {
|
||||
assertThat(actualBeanFactory, is(sameInstance(expectedBeanFactory)));
|
||||
}
|
||||
|
||||
protected void assertSame(final ConfigurableListableBeanFactory expectedBeanFactory) {
|
||||
Assert.assertSame(expectedBeanFactory, actualBeanFactory);
|
||||
protected void assertParameters(final Properties expectedParameters) {
|
||||
assertThat(actualParameters, is(equalTo(expectedParameters)));
|
||||
}
|
||||
|
||||
@Override
|
||||
void doInit(final ConfigurableListableBeanFactory beanFactory, final Properties parameters) {
|
||||
this.actualBeanFactory = beanFactory;
|
||||
void doInit(final BeanFactory beanFactory, final Properties parameters) {
|
||||
if (!isInitialized()) {
|
||||
this.actualBeanFactory = beanFactory;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
this.actualParameters = parameters;
|
||||
initialized = true;
|
||||
doPostInit(parameters);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2010-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.gemfire.function.sample;
|
||||
|
||||
import org.springframework.data.gemfire.function.annotation.OnMember;
|
||||
|
||||
/**
|
||||
* The HelloFunctionExecution interface is a Spring Data GemFire Function Execution interface
|
||||
* for the 'hello' GemFire Function and hello greetings.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.springframework.data.gemfire.function.annotation.OnMember
|
||||
* @since 1.7.0
|
||||
*/
|
||||
@OnMember(groups = "HelloGroup")
|
||||
@SuppressWarnings("unused")
|
||||
public interface HelloFunctionExecution {
|
||||
|
||||
String hello(String addressTo);
|
||||
|
||||
}
|
||||
@@ -16,10 +16,17 @@
|
||||
|
||||
package org.springframework.data.gemfire.support;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.isA;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.CoreMatchers.sameInstance;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.argThat;
|
||||
@@ -37,8 +44,9 @@ import java.util.Properties;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.ArgumentMatcher;
|
||||
import org.mockito.Matchers;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -47,6 +55,7 @@ import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.ApplicationContextEvent;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.context.event.ContextStartedEvent;
|
||||
@@ -72,6 +81,9 @@ import org.springframework.util.ObjectUtils;
|
||||
@SuppressWarnings("unused")
|
||||
public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
SpringContextBootstrappingInitializer.applicationContext = null;
|
||||
@@ -93,37 +105,35 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetApplicationContext() {
|
||||
public void getInitializedApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testGetApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.applicationContext = mockApplicationContext;
|
||||
|
||||
assertSame(mockApplicationContext, SpringContextBootstrappingInitializer.getApplicationContext());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testGetApplicationContextUninitialized() {
|
||||
try {
|
||||
SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
assertEquals("The Spring ApplicationContext was not configured and initialized properly!",
|
||||
expected.getMessage());
|
||||
throw expected;
|
||||
}
|
||||
assertThat(SpringContextBootstrappingInitializer.getApplicationContext(),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetBeanClassLoader() {
|
||||
assertNull(SpringContextBootstrappingInitializer.applicationContext);
|
||||
public void getUninitializedApplicationContext() {
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectMessage("The Spring ApplicationContext was not configured and initialized properly!");
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
|
||||
SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setBeanClassLoaderWithCurrentThreadContextClassLoader() {
|
||||
assertThat(SpringContextBootstrappingInitializer.applicationContext, is(nullValue()));
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetBeanClassLoaderWhenApplicationContextIsInactive() {
|
||||
public void setBeanClassLoaderWithCurrentThreadContextClassLoaderWhenApplicationContextIsInactive() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testSetBeanClassLoaderWhenApplicationContextIsInactive.MockApplicationContext");
|
||||
"MockApplicationContext");
|
||||
|
||||
when(mockApplicationContext.isActive()).thenReturn(false);
|
||||
|
||||
@@ -133,42 +143,71 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
verify(mockApplicationContext, times(1)).isActive();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testSetBeanClassLoaderWhenApplicationContextIsActive() {
|
||||
@Test
|
||||
public void setBeanClassLoaderWithCurrentThreadContextClassLoaderWhenApplicationContextIsActive() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testSetBeanClassLoaderWhenApplicationContextIsActive.MockApplicationContext");
|
||||
"MockApplicationContext");
|
||||
|
||||
when(mockApplicationContext.isActive()).thenReturn(true);
|
||||
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("The Spring ApplicationContext has already been initialized!");
|
||||
|
||||
try {
|
||||
SpringContextBootstrappingInitializer.applicationContext = mockApplicationContext;
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
assertEquals("The Spring ApplicationContext has already been initialized!", expected.getMessage());
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCreateApplicationContextWhenBasePackagesAndConfigLocationsAreUnspecified() {
|
||||
try {
|
||||
new SpringContextBootstrappingInitializer().createApplicationContext(null, null);
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
assertEquals("'basePackages', 'configLocations' or 'AnnotatedClasses' must be specified in order to"
|
||||
+ " construct and configure an instance of the ConfigurableApplicationContext", expected.getMessage());
|
||||
throw expected;
|
||||
finally {
|
||||
verify(mockApplicationContext, times(1)).isActive();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateAnnotationApplicationContext() {
|
||||
final ConfigurableApplicationContext mockXmlApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testCreateAnnotationApplicationContext.MockXmlApplicationContext");
|
||||
public void createApplicationContextWhenAnnotatedClassesBasePackagesAndConfigLocationsAreUnspecified() {
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("'AnnotatedClasses', 'basePackages' or 'configLocations' must be specified"
|
||||
+ " in order to construct and configure an instance of the ConfigurableApplicationContext");
|
||||
|
||||
new SpringContextBootstrappingInitializer().createApplicationContext(null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAnnotationApplicationContextWithAnnotatedClasses() {
|
||||
final AnnotationConfigApplicationContext mockAnnotationApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"testCreateAnnotationApplicationContext.MockAnnotationApplicationContext");
|
||||
"MockAnnotationApplicationContext");
|
||||
|
||||
final ConfigurableApplicationContext mockXmlApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockXmlApplicationContext");
|
||||
|
||||
Class<?>[] annotatedClasses = { TestAppConfigOne.class, TestAppConfigTwo.class };
|
||||
|
||||
SpringContextBootstrappingInitializer.register(annotatedClasses[0]);
|
||||
SpringContextBootstrappingInitializer.register(annotatedClasses[1]);
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
@Override ConfigurableApplicationContext createApplicationContext(final String[] configLocations) {
|
||||
return (ObjectUtils.isEmpty(configLocations) ? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext);
|
||||
}
|
||||
};
|
||||
|
||||
ConfigurableApplicationContext actualApplicationContext = initializer.createApplicationContext(null, null);
|
||||
|
||||
assertThat(actualApplicationContext,
|
||||
is(sameInstance((ConfigurableApplicationContext) mockAnnotationApplicationContext)));
|
||||
|
||||
verify(mockAnnotationApplicationContext, times(1)).register(annotatedClasses[0], annotatedClasses[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAnnotationApplicationContextWithBasePackages() {
|
||||
final AnnotationConfigApplicationContext mockAnnotationApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"MockAnnotationApplicationContext");
|
||||
|
||||
final ConfigurableApplicationContext mockXmlApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockXmlApplicationContext");
|
||||
|
||||
String[] basePackages = { "org.example.app" };
|
||||
|
||||
@@ -181,18 +220,19 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
ConfigurableApplicationContext actualApplicationContext = initializer.createApplicationContext(basePackages, null);
|
||||
|
||||
assertSame(mockAnnotationApplicationContext, actualApplicationContext);
|
||||
assertThat(actualApplicationContext,
|
||||
is(sameInstance((ConfigurableApplicationContext) mockAnnotationApplicationContext)));
|
||||
|
||||
verify(mockAnnotationApplicationContext, times(1)).scan(eq("org.example.app"));
|
||||
verify(mockAnnotationApplicationContext, times(1)).scan(eq(basePackages[0]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateXmlApplicationContext() {
|
||||
final ConfigurableApplicationContext mockXmlApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testCreateXmlApplicationContext.MockXmlApplicationContext");
|
||||
|
||||
public void createXmlApplicationContext() {
|
||||
final ConfigurableApplicationContext mockAnnotationApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testCreateXmlApplicationContext.MockAnnotationApplicationContext");
|
||||
"MockAnnotationApplicationContext");
|
||||
|
||||
final ConfigurableApplicationContext mockXmlApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockXmlApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
@Override ConfigurableApplicationContext createApplicationContext(final String[] configLocations) {
|
||||
@@ -204,137 +244,155 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
ConfigurableApplicationContext actualApplicationContext = initializer.createApplicationContext(null,
|
||||
new String[] { "/path/to/application/context.xml" });
|
||||
|
||||
assertSame(mockXmlApplicationContext, actualApplicationContext);
|
||||
assertThat(actualApplicationContext, is(sameInstance(mockXmlApplicationContext)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitApplicationContext() {
|
||||
public void initApplicationContext() {
|
||||
AbstractApplicationContext mockApplicationContext = mock(AbstractApplicationContext.class,
|
||||
"testInitApplicationContext.MockApplicationContext");
|
||||
"MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer();
|
||||
|
||||
initializer.initApplicationContext(mockApplicationContext);
|
||||
assertSame(mockApplicationContext, initializer.initApplicationContext(mockApplicationContext));
|
||||
|
||||
verify(mockApplicationContext, times(1)).addApplicationListener(same(initializer));
|
||||
verify(mockApplicationContext, times(1)).registerShutdownHook();
|
||||
verify(mockApplicationContext, times(1)).setClassLoader(eq(Thread.currentThread().getContextClassLoader()));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testInitApplicationContextWithNull() {
|
||||
try {
|
||||
new SpringContextBootstrappingInitializer().initApplicationContext(null);
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
assertEquals("The ConfigurableApplicationContext reference must not be null!", expected.getMessage());
|
||||
throw expected;
|
||||
}
|
||||
@Test
|
||||
public void initApplicationContextWithNull() {
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("The ConfigurableApplicationContext reference must not be null");
|
||||
|
||||
new SpringContextBootstrappingInitializer().initApplicationContext(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRefreshApplicationContext() {
|
||||
public void refreshApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testRefreshApplicationContext");
|
||||
"MockApplicationContext");
|
||||
|
||||
new SpringContextBootstrappingInitializer().refreshApplicationContext(mockApplicationContext);
|
||||
assertThat(new SpringContextBootstrappingInitializer().refreshApplicationContext(mockApplicationContext),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, times(1)).refresh();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testRefreshApplicationContextWithNull() {
|
||||
try {
|
||||
new SpringContextBootstrappingInitializer().refreshApplicationContext(null);
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
assertEquals("The ConfigurableApplicationContext reference must not be null!", expected.getMessage());
|
||||
throw expected;
|
||||
}
|
||||
@Test
|
||||
public void refreshApplicationContextWithNull() {
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("The ConfigurableApplicationContext reference must not be null");
|
||||
|
||||
new SpringContextBootstrappingInitializer().refreshApplicationContext(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullSafeGetApplicationContextIdWithNullReference() {
|
||||
assertNull(new SpringContextBootstrappingInitializer().nullSafeGetApplicationContextId(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullSafeGetApplicationContextIdWithNonNullReference() {
|
||||
ApplicationContext mockApplicationContext = mock(ApplicationContext.class,
|
||||
"testNullSafeGetApplicationContextIdWithNonNullReference");
|
||||
|
||||
when(mockApplicationContext.getId()).thenReturn("testNullSafeGetApplicationContextIdWithNonNullReference");
|
||||
|
||||
assertEquals("testNullSafeGetApplicationContextIdWithNonNullReference",
|
||||
new SpringContextBootstrappingInitializer().nullSafeGetApplicationContextId(mockApplicationContext));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterAnnotatedClasses() {
|
||||
public void registerAnnotatedClasses() {
|
||||
AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"testRegisterAnnotatedClasses");
|
||||
"MockApplicationContext");
|
||||
|
||||
Class<?>[] annotatedClasses = { TestAppConfigOne.class, TestAppConfigTwo.class };
|
||||
|
||||
new SpringContextBootstrappingInitializer().registerAnnotatedClasses(mockApplicationContext, annotatedClasses);
|
||||
assertThat(new SpringContextBootstrappingInitializer()
|
||||
.registerAnnotatedClasses(mockApplicationContext, annotatedClasses),
|
||||
is(sameInstance((ConfigurableApplicationContext) mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, times(1)).register(annotatedClasses);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterAnnotatedClassesWithEmptyAnnotatedClassesArray() {
|
||||
public void registerAnnotatedClassesWithEmptyAnnotatedClassesArray() {
|
||||
AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"testRegisterAnnotatedClassesWithEmptyAnnotatedClassesArray");
|
||||
"MockApplicationContext");
|
||||
|
||||
new SpringContextBootstrappingInitializer().registerAnnotatedClasses(mockApplicationContext, new Class<?>[0]);
|
||||
assertThat(new SpringContextBootstrappingInitializer().registerAnnotatedClasses(mockApplicationContext,
|
||||
new Class<?>[0]),
|
||||
is(sameInstance((ConfigurableApplicationContext) mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, never()).register(any(Class.class));
|
||||
verify(mockApplicationContext, never()).register(any(Class[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScanBasePackages() {
|
||||
public void registerAnnotatedClassesWithNonAnnotationBasedApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().registerAnnotatedClasses(mockApplicationContext,
|
||||
new Class<?>[] { TestAppConfigOne.class }), is(sameInstance(mockApplicationContext)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scanBasePackages() {
|
||||
AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"testScanBasePackages");
|
||||
"MockApplicationContext");
|
||||
|
||||
String[] basePackages = { "org.example.app", "org.example.plugins" };
|
||||
|
||||
new SpringContextBootstrappingInitializer().scanBasePackages(mockApplicationContext, basePackages);
|
||||
assertThat(new SpringContextBootstrappingInitializer().scanBasePackages(mockApplicationContext, basePackages),
|
||||
is(sameInstance((ConfigurableApplicationContext) mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, times(1)).scan(basePackages);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScanBasePackagesWithEmptyBasePackagesArray() {
|
||||
public void scanBasePackagesWithEmptyBasePackagesArray() {
|
||||
AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"testScanBasePackages");
|
||||
"MockApplicationContext");
|
||||
|
||||
new SpringContextBootstrappingInitializer().scanBasePackages(mockApplicationContext, null);
|
||||
assertThat(new SpringContextBootstrappingInitializer().scanBasePackages(mockApplicationContext, null),
|
||||
is(sameInstance((ConfigurableApplicationContext) mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, never()).scan(any(String[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetClassLoader() {
|
||||
public void scanBasePackagesWithNonAnnotationBasedApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().scanBasePackages(mockApplicationContext,
|
||||
new String[] { "org.example.app" }), is(sameInstance(mockApplicationContext)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setClassLoader() {
|
||||
AbstractApplicationContext mockApplicationContext = mock(AbstractApplicationContext.class,
|
||||
"testSetClassLoader.MockApplicationContext");
|
||||
"MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
|
||||
new SpringContextBootstrappingInitializer().setClassLoader(mockApplicationContext);
|
||||
assertThat(new SpringContextBootstrappingInitializer().setClassLoader(mockApplicationContext),
|
||||
is(sameInstance((ConfigurableApplicationContext) mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, times(1)).setClassLoader(eq(Thread.currentThread().getContextClassLoader()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetClassLoaderWhenClassLoaderIsNull() {
|
||||
public void setClassLoaderWithNonSettableClassLoaderApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().setClassLoader(mockApplicationContext),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setClassLoaderWithNullClassLoader() {
|
||||
AbstractApplicationContext mockApplicationContext = mock(AbstractApplicationContext.class,
|
||||
"testSetClassLoaderWhenClassLoaderIsNull.MockApplicationContext");
|
||||
"MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(null);
|
||||
|
||||
new SpringContextBootstrappingInitializer().setClassLoader(mockApplicationContext);
|
||||
assertThat(new SpringContextBootstrappingInitializer().setClassLoader(mockApplicationContext),
|
||||
is(sameInstance((ConfigurableApplicationContext) mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, never()).setClassLoader(any(ClassLoader.class));
|
||||
}
|
||||
@@ -342,12 +400,27 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
private Class<?>[] annotatedClasses(final Class<?>... annotatedClasses) {
|
||||
return argThat(new ArgumentMatcher<Class<?>[]>() {
|
||||
@Override public boolean matches(final Object argument) {
|
||||
assertTrue(argument instanceof Class<?>[]);
|
||||
assertThat(argument instanceof Class<?>[], is(true));
|
||||
return Arrays.equals(annotatedClasses, (Class<?>[]) argument);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullSafeGetApplicationContextIdWithNullReference() {
|
||||
assertThat(new SpringContextBootstrappingInitializer().nullSafeGetApplicationContextId(null), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullSafeGetApplicationContextIdWithNonNullReference() {
|
||||
ApplicationContext mockApplicationContext = mock(ApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
when(mockApplicationContext.getId()).thenReturn("123");
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().nullSafeGetApplicationContextId(mockApplicationContext),
|
||||
is(equalTo("123")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitWithAnnotatedClasses() {
|
||||
final AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
@@ -471,23 +544,17 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
assertSame(mockNewApplicationContext, SpringContextBootstrappingInitializer.getApplicationContext());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void testInitWhenBasePackagesAndContextConfigLocationsParametersAreUnspecified() throws Throwable {
|
||||
assertNull(SpringContextBootstrappingInitializer.applicationContext);
|
||||
assertThat(SpringContextBootstrappingInitializer.applicationContext, is(nullValue()));
|
||||
|
||||
try {
|
||||
new SpringContextBootstrappingInitializer().init(createParameters(createParameters(
|
||||
expectedException.expect(ApplicationContextException.class);
|
||||
expectedException.expectCause(isA(IllegalArgumentException.class));
|
||||
expectedException.expectMessage(containsString("Failed to bootstrap the Spring ApplicationContext"));
|
||||
|
||||
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();
|
||||
}
|
||||
SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER, " "));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
@@ -515,7 +582,7 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
}
|
||||
catch (ApplicationContextException expected) {
|
||||
assertTrue(expected.getMessage().contains("Failed to bootstrap the Spring ApplicationContext!"));
|
||||
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());
|
||||
@@ -550,63 +617,48 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
"classpath/to/spring/application/context.xml"));
|
||||
}
|
||||
catch (ApplicationContextException expected) {
|
||||
assertTrue(expected.getMessage().contains("Failed to bootstrap the Spring ApplicationContext!"));
|
||||
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!"),
|
||||
verify(mockLog, times(1)).error(eq("Failed to bootstrap the Spring ApplicationContext"),
|
||||
any(RuntimeException.class));
|
||||
}
|
||||
}
|
||||
|
||||
protected static void assertNotified(TestApplicationListener listener, ContextRefreshedEvent expectedEvent) {
|
||||
assertTrue(listener.isNotified());
|
||||
Assert.assertSame(expectedEvent, listener.getActualEvent());
|
||||
protected static void assertNotified(TestApplicationListener listener, ApplicationContextEvent expectedEvent) {
|
||||
assertThat(listener, is(notNullValue()));
|
||||
assertThat(listener.isNotified(), is(true));
|
||||
assertThat(listener.getActualEvent(), is(sameInstance(expectedEvent)));
|
||||
}
|
||||
|
||||
protected static void assertUnnotified(TestApplicationListener listener) {
|
||||
assertFalse(listener.isNotified());
|
||||
assertNull(listener.getActualEvent());
|
||||
assertThat(listener, is(notNullValue()));
|
||||
assertThat(listener.isNotified(), is(false));
|
||||
assertThat(listener.getActualEvent(), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnApplicationEvent() {
|
||||
TestApplicationListener testApplicationListener = new TestApplicationListener("testOnApplicationEvent");
|
||||
|
||||
try {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnApplicationEventWithContextStartedEvent() {
|
||||
public void onContextClosedApplicationEvent() {
|
||||
TestApplicationListener testApplicationListener = new TestApplicationListener(
|
||||
"testOnApplicationEventWithContextStartedEvent");
|
||||
"testOnContextClosedApplicationEvent");
|
||||
|
||||
try {
|
||||
testApplicationListener = SpringContextBootstrappingInitializer.register(testApplicationListener);
|
||||
|
||||
assertUnnotified(testApplicationListener);
|
||||
|
||||
ContextStartedEvent testContextStartedEvent = mock(ContextStartedEvent.class,
|
||||
"testOnApplicationEventWithContextStartedEvent");
|
||||
SpringContextBootstrappingInitializer.contextRefreshedEvent = mock(ContextRefreshedEvent.class,
|
||||
"MockContextRefreshedEvent");
|
||||
|
||||
new SpringContextBootstrappingInitializer().onApplicationEvent(testContextStartedEvent);
|
||||
assertThat(SpringContextBootstrappingInitializer.contextRefreshedEvent, isA(ContextRefreshedEvent.class));
|
||||
|
||||
new SpringContextBootstrappingInitializer().onApplicationEvent(mock(ContextClosedEvent.class,
|
||||
"MockContextClosedEvent"));
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.contextRefreshedEvent, is(nullValue()));
|
||||
assertUnnotified(testApplicationListener);
|
||||
}
|
||||
finally {
|
||||
@@ -615,33 +667,108 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnApplicationEventWithMultipleRegisteredApplicationListeners() {
|
||||
TestApplicationListener testApplicationListenerOne = new TestApplicationListener(
|
||||
"testOnApplicationEventWithMultipleRegisteredApplicationListeners.1");
|
||||
public void onContextRefreshedApplicationEvent() {
|
||||
TestApplicationListener testApplicationListener = new TestApplicationListener(
|
||||
"testOnContextRefreshedApplicationEvent");
|
||||
|
||||
TestApplicationListener testApplicationListenerTwo = new TestApplicationListener(
|
||||
"testOnApplicationEventWithMultipleRegisteredApplicationListeners.2");
|
||||
try {
|
||||
testApplicationListener = SpringContextBootstrappingInitializer.register(testApplicationListener);
|
||||
|
||||
TestApplicationListener testApplicationListenerThree = new TestApplicationListener(
|
||||
"testOnApplicationEventWithMultipleRegisteredApplicationListeners.3");
|
||||
assertThat(SpringContextBootstrappingInitializer.contextRefreshedEvent, is(nullValue()));
|
||||
assertUnnotified(testApplicationListener);
|
||||
|
||||
ContextRefreshedEvent mockContextRefreshedEvent = mock(ContextRefreshedEvent.class,
|
||||
"MockContextRefreshedEvent");
|
||||
|
||||
new SpringContextBootstrappingInitializer().onApplicationEvent(mockContextRefreshedEvent);
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.contextRefreshedEvent, is(sameInstance(mockContextRefreshedEvent)));
|
||||
assertNotified(testApplicationListener, mockContextRefreshedEvent);
|
||||
}
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(testApplicationListener);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onContextStartedApplicationEvent() {
|
||||
TestApplicationListener testApplicationListener = new TestApplicationListener(
|
||||
"testOnContextStartedApplicationEvent");
|
||||
|
||||
try {
|
||||
testApplicationListener = SpringContextBootstrappingInitializer.register(testApplicationListener);
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.contextRefreshedEvent, is(nullValue()));
|
||||
assertUnnotified(testApplicationListener);
|
||||
|
||||
new SpringContextBootstrappingInitializer().onApplicationEvent(mock(ContextStartedEvent.class,
|
||||
"MockContextStartedEvent"));
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.contextRefreshedEvent, is(nullValue()));
|
||||
assertUnnotified(testApplicationListener);
|
||||
}
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(testApplicationListener);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onContextStoppedApplicationEvent() {
|
||||
TestApplicationListener testApplicationListener = new TestApplicationListener(
|
||||
"testOnContextStartedApplicationEvent");
|
||||
|
||||
try {
|
||||
testApplicationListener = SpringContextBootstrappingInitializer.register(testApplicationListener);
|
||||
|
||||
assertUnnotified(testApplicationListener);
|
||||
|
||||
ContextRefreshedEvent mockContextRefreshedEvent = mock(ContextRefreshedEvent.class,
|
||||
"MockContextRefreshedEvent");
|
||||
|
||||
SpringContextBootstrappingInitializer.contextRefreshedEvent = mockContextRefreshedEvent;
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.contextRefreshedEvent, is(sameInstance(
|
||||
mockContextRefreshedEvent)));
|
||||
|
||||
new SpringContextBootstrappingInitializer().onApplicationEvent(mock(ContextStoppedEvent.class,
|
||||
"MockContextStoppedEvent"));
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.contextRefreshedEvent, is(sameInstance(mockContextRefreshedEvent)));
|
||||
assertUnnotified(testApplicationListener);
|
||||
}
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(testApplicationListener);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onApplicationEventWithMultipleRegisteredApplicationListeners() {
|
||||
TestApplicationListener testApplicationListenerOne = new TestApplicationListener("TestApplicationListener.1");
|
||||
|
||||
TestApplicationListener testApplicationListenerTwo = new TestApplicationListener("TestApplicationListener.2");
|
||||
|
||||
TestApplicationListener testApplicationListenerThree = new TestApplicationListener("TestApplicationListener.3");
|
||||
|
||||
try {
|
||||
testApplicationListenerOne = SpringContextBootstrappingInitializer.register(testApplicationListenerOne);
|
||||
testApplicationListenerTwo = SpringContextBootstrappingInitializer.register(testApplicationListenerTwo);
|
||||
testApplicationListenerThree = SpringContextBootstrappingInitializer.register(testApplicationListenerThree);
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.contextRefreshedEvent, is(nullValue()));
|
||||
assertUnnotified(testApplicationListenerOne);
|
||||
assertUnnotified(testApplicationListenerTwo);
|
||||
assertUnnotified(testApplicationListenerThree);
|
||||
|
||||
ContextRefreshedEvent testContextRefreshedEvent = new ContextRefreshedEvent(mock(ApplicationContext.class,
|
||||
"testOnApplicationEventWithMultipleRegisteredApplicationListeners"));
|
||||
ContextRefreshedEvent mockContextRefreshedEvent = mock(ContextRefreshedEvent.class,
|
||||
"MockContextRefreshedEvent");
|
||||
|
||||
new SpringContextBootstrappingInitializer().onApplicationEvent(testContextRefreshedEvent);
|
||||
new SpringContextBootstrappingInitializer().onApplicationEvent(mockContextRefreshedEvent);
|
||||
|
||||
assertNotified(testApplicationListenerOne, testContextRefreshedEvent);
|
||||
assertNotified(testApplicationListenerTwo, testContextRefreshedEvent);
|
||||
assertNotified(testApplicationListenerThree, testContextRefreshedEvent);
|
||||
assertThat(SpringContextBootstrappingInitializer.contextRefreshedEvent,
|
||||
is(sameInstance(mockContextRefreshedEvent)));
|
||||
assertNotified(testApplicationListenerOne, mockContextRefreshedEvent);
|
||||
assertNotified(testApplicationListenerTwo, mockContextRefreshedEvent);
|
||||
assertNotified(testApplicationListenerThree, mockContextRefreshedEvent);
|
||||
}
|
||||
finally {
|
||||
SpringContextBootstrappingInitializer.unregister(testApplicationListenerOne);
|
||||
@@ -651,9 +778,8 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnApplicationEventWithUnregisteredApplicationListener() {
|
||||
TestApplicationListener testApplicationListener = new TestApplicationListener(
|
||||
"testOnApplicationEventWithUnregisteredApplicationListener");
|
||||
public void onApplicationEventWithNoRegisteredApplicationListener() {
|
||||
TestApplicationListener testApplicationListener = new TestApplicationListener("TestApplicationListener");
|
||||
|
||||
try {
|
||||
testApplicationListener = SpringContextBootstrappingInitializer.unregister(
|
||||
@@ -661,10 +787,8 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
assertUnnotified(testApplicationListener);
|
||||
|
||||
ContextRefreshedEvent testContextRefreshedEvent = new ContextRefreshedEvent(mock(ApplicationContext.class,
|
||||
"testRegisterThenUnregisterWithOnApplicationEvent"));
|
||||
|
||||
new SpringContextBootstrappingInitializer().onApplicationEvent(testContextRefreshedEvent);
|
||||
new SpringContextBootstrappingInitializer().onApplicationEvent(mock(ContextRefreshedEvent.class,
|
||||
"MockContextRefreshedEvent"));
|
||||
|
||||
assertUnnotified(testApplicationListener);
|
||||
}
|
||||
@@ -794,7 +918,7 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
private volatile boolean notified = false;
|
||||
|
||||
private volatile ContextRefreshedEvent actualEvent;
|
||||
private volatile ApplicationContextEvent actualEvent;
|
||||
|
||||
private final String name;
|
||||
|
||||
@@ -802,8 +926,8 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public ContextRefreshedEvent getActualEvent() {
|
||||
ContextRefreshedEvent localActualEvent = this.actualEvent;
|
||||
public ApplicationContextEvent getActualEvent() {
|
||||
ApplicationContextEvent localActualEvent = this.actualEvent;
|
||||
this.actualEvent = null;
|
||||
return localActualEvent;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user