DATAGEODE-187 - Configure and start GemFire Locators with SDG config.

This commit is contained in:
John Blum
2019-05-12 23:18:31 -07:00
parent ffc20a50fb
commit ad0ad251a1
10 changed files with 1521 additions and 6 deletions

View File

@@ -0,0 +1,389 @@
/*
* Copyright 2018 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.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import org.apache.geode.distributed.Locator;
import org.apache.geode.distributed.LocatorLauncher;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mockito;
import org.springframework.data.gemfire.config.annotation.LocatorConfigurer;
/**
* Unit Tests for {@link LocatorFactoryBean}.
*
* @author John Blum
* @see java.util.Properties
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.mockito.Spy
* @see org.apache.geode.distributed.Locator
* @see org.apache.geode.distributed.LocatorLauncher
* @see org.springframework.data.gemfire.LocatorFactoryBean
* @see org.springframework.data.gemfire.config.annotation.LocatorConfigurer
* @since 2.2.0
*/
public class LocatorFactoryBeanUnitTests {
public LocatorFactoryBean locatorFactoryBean;
@Before
public void setup() {
this.locatorFactoryBean = spy(new LocatorFactoryBean());
}
@Test
public void afterPropertiesSetCallsApplyLocatorConfigurersAndInit() throws Exception {
doNothing().when(this.locatorFactoryBean).applyLocatorConfigurers(any(LocatorConfigurer.class));
doNothing().when(this.locatorFactoryBean).init();
this.locatorFactoryBean.afterPropertiesSet();
InOrder inOrder = Mockito.inOrder(this.locatorFactoryBean);
inOrder.verify(this.locatorFactoryBean, times(1)).getCompositeLocatorConfigurer();
inOrder.verify(this.locatorFactoryBean, times(1)).applyLocatorConfigurers(any(LocatorConfigurer.class));
inOrder.verify(this.locatorFactoryBean, times(1)).init();
}
@Test
@SuppressWarnings("unchecked")
public void appliesArrayOfLocatorConfigurersCallsIterableOfLocatorConfigurers() {
LocatorConfigurer mockLocatorConfigurerOne = mock(LocatorConfigurer.class);
LocatorConfigurer mockLocatorConfigurerTwo = mock(LocatorConfigurer.class);
doCallRealMethod().when(this.locatorFactoryBean).applyLocatorConfigurers(any(LocatorConfigurer.class));
doAnswer(invocation -> {
Iterable<LocatorConfigurer> locatorConfigurers = invocation.getArgument(0, Iterable.class);
assertThat(locatorConfigurers).isNotNull();
assertThat(locatorConfigurers).containsExactly(mockLocatorConfigurerOne, mockLocatorConfigurerTwo);
return null;
}).when(this.locatorFactoryBean).applyLocatorConfigurers(any(Iterable.class));
this.locatorFactoryBean.applyLocatorConfigurers(mockLocatorConfigurerOne, mockLocatorConfigurerTwo);
verify(this.locatorFactoryBean, times(1)).applyLocatorConfigurers(any(Iterable.class));
}
@Test
@SuppressWarnings("unchecked")
public void appliesIterableOfLocatorConfigurersInvokesLocatorConfigurers() {
LocatorConfigurer mockLocatorConfigurerOne = mock(LocatorConfigurer.class);
LocatorConfigurer mockLocatorConfigurerTwo = mock(LocatorConfigurer.class);
doCallRealMethod().when(this.locatorFactoryBean).applyLocatorConfigurers(any(Iterable.class));
this.locatorFactoryBean.setBeanName("TestLocator");
this.locatorFactoryBean
.applyLocatorConfigurers(Arrays.asList(mockLocatorConfigurerOne, mockLocatorConfigurerTwo));
verify(mockLocatorConfigurerOne, times(1))
.configure(eq("TestLocator"), eq(this.locatorFactoryBean));
verify(mockLocatorConfigurerTwo, times(1))
.configure(eq("TestLocator"), eq(this.locatorFactoryBean));
}
@Test
public void initBuildsLocator() throws Exception {
Locator mockLocator = mock(Locator.class);
LocatorLauncher mockLocatorLauncher = mock(LocatorLauncher.class);
LocatorLauncher.Builder locatorBuilderSpy = spy(new LocatorLauncher.Builder());
when(mockLocatorLauncher.getLocator()).thenReturn(mockLocator);
when(locatorBuilderSpy.build()).thenReturn(mockLocatorLauncher);
when(this.locatorFactoryBean.newLocatorLauncherBuilder()).thenReturn(locatorBuilderSpy);
String testBindAddress = InetAddress.getLocalHost().getHostAddress();
this.locatorFactoryBean.setBeanName("TestLocatorBean");
this.locatorFactoryBean.setBindAddress(testBindAddress);
this.locatorFactoryBean.setHostnameForClients("skullbox");
this.locatorFactoryBean.setName("TestMember");
this.locatorFactoryBean.setPort(54321);
this.locatorFactoryBean.init();
assertThat(this.locatorFactoryBean.getLocator()).isEqualTo(mockLocator);
assertThat(this.locatorFactoryBean.getLocatorLauncher()).isEqualTo(mockLocatorLauncher);
verify(locatorBuilderSpy, times(1)).set(eq("log-level"), eq("config"));
verify(locatorBuilderSpy, times(1)).setBindAddress(eq(testBindAddress));
verify(locatorBuilderSpy, times(1)).setHostnameForClients(eq("skullbox"));
verify(locatorBuilderSpy, times(1)).setMemberName(eq("TestMember"));
verify(locatorBuilderSpy, times(1)).setPort(eq(54321));
verify(this.locatorFactoryBean, times(1)).postProcess(eq(locatorBuilderSpy));
verify(this.locatorFactoryBean, times(1)).postProcess(eq(mockLocatorLauncher));
verify(mockLocatorLauncher, times(1)).start();
}
@Test
public void configuresLocatorLauncherBuilderGemFireProperties() {
Properties gemfireProperties = new Properties();
gemfireProperties.setProperty("name", "TEST");
gemfireProperties.setProperty("log-level", "config");
gemfireProperties.setProperty("locators", "localhost[11235],skullbox[12480]");
LocatorLauncher.Builder locatorBuilderSpy = spy(new LocatorLauncher.Builder());
this.locatorFactoryBean.setGemFireProperties(gemfireProperties);
this.locatorFactoryBean.configureGemfireProperties(locatorBuilderSpy);
gemfireProperties.stringPropertyNames().forEach(propertyName ->
verify(locatorBuilderSpy, times(1))
.set(eq(propertyName), eq(gemfireProperties.getProperty(propertyName))));
}
@Test
public void getObjectReturnsLocator() throws Exception {
Locator mockLocator = mock(Locator.class);
doReturn(mockLocator).when(this.locatorFactoryBean).getLocator();
assertThat(this.locatorFactoryBean.getObject()).isEqualTo(mockLocator);
verify(this.locatorFactoryBean, times(1)).getLocator();
}
@Test(expected = IllegalStateException.class)
public void getObjectThrowsIllegalStateException() throws Exception {
try {
this.locatorFactoryBean.getObject();
}
catch (IllegalStateException expected) {
assertThat(expected).hasMessage("Locator was not configured and initialized");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test
public void getObjectTypeEqualsLocatorClass() {
assertThat(this.locatorFactoryBean.getObjectType()).isEqualTo(Locator.class);
verify(this.locatorFactoryBean, times(1)).getLocator();
}
@Test
public void getObjectTypeIsALocatorType() {
Locator mockLocator = mock(Locator.class);
doReturn(mockLocator).when(this.locatorFactoryBean).getLocator();
Class<?> objectType = this.locatorFactoryBean.getObjectType();
assertThat(Locator.class).isAssignableFrom(objectType);
assertThat(objectType).isEqualTo(mockLocator.getClass());
verify(this.locatorFactoryBean, times(1)).getLocator();
}
@Test
public void setAndGetBindAddress() {
this.locatorFactoryBean.setBindAddress("10.105.210.16");
assertThat(this.locatorFactoryBean.getBindAddress().orElse(null)).isEqualTo("10.105.210.16");
this.locatorFactoryBean.setBindAddress(null);
assertThat(this.locatorFactoryBean.getBindAddress().orElse(null)).isNull();
this.locatorFactoryBean.setBindAddress(" ");
assertThat(this.locatorFactoryBean.getBindAddress().orElse(null)).isNull();
}
@Test
public void setAndGetGemFireProperties() {
Properties testGemFireProperties = new Properties();
testGemFireProperties.setProperty("testKey", "testValue");
this.locatorFactoryBean.setGemFireProperties(testGemFireProperties);
assertThat(this.locatorFactoryBean.getGemFireProperties()).isEqualTo(testGemFireProperties);
this.locatorFactoryBean.setGemFireProperties(null);
Properties actualGemFireProperties = this.locatorFactoryBean.getGemFireProperties();
assertThat(actualGemFireProperties).isNotNull();
assertThat(actualGemFireProperties).isNotEqualTo(testGemFireProperties);
assertThat(actualGemFireProperties).isEmpty();
}
@Test
public void setAndGetHostnameForClients() {
this.locatorFactoryBean.setHostnameForClients("skullbox");
assertThat(this.locatorFactoryBean.getHostnameForClients().orElse(null)).isEqualTo("skullbox");
this.locatorFactoryBean.setHostnameForClients(null);
assertThat(this.locatorFactoryBean.getHostnameForClients().orElse(null)).isNull();
this.locatorFactoryBean.setHostnameForClients(" ");
assertThat(this.locatorFactoryBean.getHostnameForClients().orElse(null)).isNull();
}
@Test
public void setArrayOfLocatorConfigurersCallsListOfLocatorConfigurers() {
LocatorConfigurer mockLocatorConfigurerOne = mock(LocatorConfigurer.class);
LocatorConfigurer mockLocatorConfigurerTwo = mock(LocatorConfigurer.class);
this.locatorFactoryBean.setLocatorConfigurers(mockLocatorConfigurerOne, mockLocatorConfigurerTwo);
List<LocatorConfigurer> locatorconfigurers = Arrays.asList(mockLocatorConfigurerOne, mockLocatorConfigurerTwo);
verify(this.locatorFactoryBean, times(1)).setLocatorConfigurers(eq(locatorconfigurers));
}
@Test
public void setListOfLocatorConfigurersAddsAll() {
LocatorConfigurer mockLocatorConfigurerOne = mock(LocatorConfigurer.class);
LocatorConfigurer mockLocatorConfigurerTwo = mock(LocatorConfigurer.class);
List<LocatorConfigurer> locatorconfigurers = Arrays.asList(mockLocatorConfigurerOne, mockLocatorConfigurerTwo);
this.locatorFactoryBean.setLocatorConfigurers(locatorconfigurers);
LocatorConfigurer compositeLocatorConfigurer = this.locatorFactoryBean.getCompositeLocatorConfigurer();
assertThat(compositeLocatorConfigurer).isNotNull();
compositeLocatorConfigurer.configure("TestLocator", this.locatorFactoryBean);
verify(mockLocatorConfigurerOne, times(1))
.configure(eq("TestLocator"), eq(this.locatorFactoryBean));
verify(mockLocatorConfigurerTwo, times(1))
.configure(eq("TestLocator"), eq(this.locatorFactoryBean));
}
@Test
public void setAndGetLogLevel() {
this.locatorFactoryBean.setLogLevel("info");
assertThat(this.locatorFactoryBean.getLogLevel()).isEqualTo("info");
this.locatorFactoryBean.setLogLevel(null);
assertThat(this.locatorFactoryBean.getLogLevel()).isEqualTo(LocatorFactoryBean.DEFAULT_LOG_LEVEL);
this.locatorFactoryBean.setLogLevel(" ");
assertThat(this.locatorFactoryBean.getLogLevel()).isEqualTo(LocatorFactoryBean.DEFAULT_LOG_LEVEL);
}
@Test
public void setAndGetName() {
this.locatorFactoryBean.setName("TestLocator");
assertThat(this.locatorFactoryBean.getName().orElse(null)).isEqualTo("TestLocator");
this.locatorFactoryBean.setName(null);
assertThat(this.locatorFactoryBean.getName().orElse(null)).isNull();
this.locatorFactoryBean.setName(" ");
assertThat(this.locatorFactoryBean.getName().orElse(null)).isNull();
}
@Test
public void setPortToValidValue() {
this.locatorFactoryBean.setPort(54321);
assertThat(this.locatorFactoryBean.getPort()).isEqualTo(54321);
}
@Test(expected = IllegalArgumentException.class)
public void setPortToOverflowValue() {
try {
this.locatorFactoryBean.setPort(65536);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Network port [65536] is not valid");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test(expected = IllegalArgumentException.class)
public void setPortToUnderflowValue() {
try {
this.locatorFactoryBean.setPort(-1);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Network port [-1] is not valid");
assertThat(expected).hasNoCause();
throw expected;
}
}
}

View File

@@ -0,0 +1,122 @@
/*
* Copyright 2018 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.config.annotation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Fail.fail;
import static org.mockito.Mockito.mock;
import java.util.Optional;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.distributed.Locator;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects;
/**
* Integration Tests for {@link LocatorApplication} and {@link LocatorApplicationConfiguration} asserting that
* {@link GemFireCache} and {@link Locator} instances are mutually exclusive.
*
* @author John Blum
* @see org.junit.Test
* @see org.apache.geode.cache.GemFireCache
* @see org.apache.geode.cache.client.ClientCache
* @see org.apache.geode.distributed.Locator
* @see org.springframework.data.gemfire.config.annotation.LocatorApplication
* @see org.springframework.data.gemfire.config.annotation.LocatorApplicationConfiguration
* @see org.springframework.context.ConfigurableApplicationContext
* @see org.springframework.context.annotation.AnnotationConfigApplicationContext
* @see org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects
* @since 2.2.0
*/
@SuppressWarnings("unused")
public class LocatorApplicationCannotCoexistWithCacheApplicationIntegrationTests {
private static final String GEMFIRE_LOG_LEVEL = "error";
private ConfigurableApplicationContext applicationContext;
@After
public void tearDown() {
Optional.ofNullable(this.applicationContext)
.ifPresent(ConfigurableApplicationContext::close);
}
private ConfigurableApplicationContext newApplicationContext(Class<?>... annotatedClasses) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(annotatedClasses);
applicationContext.registerShutdownHook();
applicationContext.refresh();
return applicationContext;
}
private void testCacheAndLocatorApplication(Class<?> testConfiguration) {
try {
this.applicationContext = newApplicationContext(testConfiguration);
this.applicationContext.getBean(GemFireCache.class);
this.applicationContext.getBean(Locator.class);
fail("Caches and Locators cannot coexist!");
}
catch (BeanDefinitionStoreException expected) {
assertThat(expected)
.hasMessage(LocatorApplicationConfiguration.EXCLUSIVE_LOCATOR_APPLICATION_ERROR_MESSAGE);
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test(expected = BeanDefinitionStoreException.class)
public void clientCacheAndLocatorApplicationThrowsException() {
testCacheAndLocatorApplication(ClientCacheAndLocatorTestConfiguration.class);
}
@Test(expected = BeanDefinitionStoreException.class)
public void locatorAndPeerCacheApplicationThrowsException() {
testCacheAndLocatorApplication(LocatorAndPeerCacheTestConfiguration.class);
}
@EnableGemFireMockObjects
@LocatorApplication(logLevel = GEMFIRE_LOG_LEVEL, port = 0)
static class ClientCacheAndLocatorTestConfiguration {
@Bean
ClientCache mockClientCache() {
return mock(ClientCache.class);
}
}
@EnableGemFireMockObjects
@LocatorApplication(logLevel = GEMFIRE_LOG_LEVEL, port = 0)
@PeerCacheApplication(logLevel = GEMFIRE_LOG_LEVEL)
static class LocatorAndPeerCacheTestConfiguration { }
}

View File

@@ -0,0 +1,122 @@
/*
* Copyright 2018 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.config.annotation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.data.gemfire.LocatorFactoryBean;
import org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects;
import org.springframework.lang.Nullable;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration Tests for {@link LocatorApplication} and {@link LocatorConfigurer}.
*
* @author John Blum
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.data.gemfire.config.annotation.LocatorApplication
* @see org.springframework.data.gemfire.config.annotation.LocatorApplicationConfiguration
* @see org.springframework.data.gemfire.config.annotation.LocatorConfigurer
* @see org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringRunner
* @since 2.2.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration
@SuppressWarnings("unused")
public class LocatorApplicationConfigurerIntegrationTests {
private static final String GEMFIRE_LOG_LEVEL = "error";
@Autowired
private LocatorFactoryBean locatorFactoryBean;
@Autowired
@Qualifier("locatorConfigurerOne")
private LocatorConfigurer locatorConfigurerOne;
@Autowired
@Qualifier("locatorConfigurerTwo")
private LocatorConfigurer locatorConfigurerTwo;
@Test
public void locatorConfigurersInvoked() {
assertThat(this.locatorFactoryBean).isNotNull();
assertThat(this.locatorConfigurerOne).isNotNull();
assertThat(this.locatorConfigurerTwo).isNotNull();
Arrays.asList(this.locatorConfigurerOne, this.locatorConfigurerTwo).forEach(locatorConfigurer ->
verify(locatorConfigurer, times(1))
.configure(eq("locatorApplication"), eq(this.locatorFactoryBean)));
}
@EnableGemFireMockObjects
@LocatorApplication(logLevel = GEMFIRE_LOG_LEVEL, port = 0)
static class TestConfiguration {
@Bean
BeanPostProcessor locatorFactoryBeanPostProcessor() {
return new BeanPostProcessor() {
@Nullable @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof LocatorFactoryBean) {
LocatorFactoryBean locatorFactoryBean = spy((LocatorFactoryBean) bean);
doNothing().when(locatorFactoryBean).init();
bean = locatorFactoryBean;
}
return bean;
}
};
}
@Bean
LocatorConfigurer locatorConfigurerOne() {
return mock(LocatorConfigurer.class);
}
@Bean
LocatorConfigurer locatorConfigurerTwo() {
return mock(LocatorConfigurer.class);
}
}
}

View File

@@ -0,0 +1,106 @@
/*
* Copyright 2018 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.config.annotation;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Properties;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.CacheFactory;
import org.apache.geode.distributed.DistributedSystem;
import org.apache.geode.distributed.Locator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.gemfire.GemfireUtils;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration Tests for {@link LocatorApplication} and {@link LocatorApplicationConfiguration} asserting that
* an Apache Geode / Pivotal GemFire {@link Cache} application can connect to the {@link Locator} configured
* and bootstrapped by {@link LocatorApplication}.
*
* @author John Blum
* @see java.util.Properties
* @see org.junit.Test
* @see org.apache.geode.cache.Cache
* @see org.apache.geode.cache.CacheFactory
* @see org.apache.geode.distributed.DistributedSystem
* @see org.apache.geode.distributed.Locator
* @see org.springframework.data.gemfire.config.annotation.LocatorApplication
* @see org.springframework.data.gemfire.config.annotation.LocatorApplicationConfiguration
* @see org.springframework.test.context.junit4.SpringRunner
* @since 2.2.0
*/
@RunWith(SpringRunner.class)
@SuppressWarnings("unused")
public class LocatorApplicationIntegrationTests {
private static final String GEMFIRE_LOG_LEVEL = "error";
@Autowired
private Locator locator;
@Before
public void setup() {
assertThat(this.locator).isNotNull();
}
@Test
public void gemfireCacheCanConnectToLocator() {
DistributedSystem distributedSystem = this.locator.getDistributedSystem();
assertThat(distributedSystem).isNotNull();
Properties distributedSystemProperties = distributedSystem.getProperties();
assertThat(distributedSystemProperties).isNotNull();
Cache peerCache = null;
try {
peerCache = new CacheFactory()
.set("name", LocatorApplicationIntegrationTests.class.getSimpleName())
.set("bind-address", distributedSystemProperties.getProperty("bind-address"))
.set("cache-xml-file", distributedSystemProperties.getProperty("cache-xml-file"))
.set("jmx-manager", distributedSystemProperties.getProperty("jmx-manager"))
.set("locators", distributedSystemProperties.getProperty("locators"))
.set("log-file", distributedSystemProperties.getProperty("log-file"))
.create();
assertThat(peerCache).isNotNull();
assertThat(peerCache.getDistributedSystem()).isNotNull();
assertThat(peerCache.getDistributedSystem().getProperties()).isNotNull();
assertThat(peerCache.getDistributedSystem().getProperties().getProperty("locators"))
.isEqualTo(distributedSystemProperties.getProperty("locators"));
}
finally {
GemfireUtils.close(peerCache);
}
}
@LocatorApplication(
name = "LocatorApplicationIntegrationTests",
bindAddress = "localhost",
logLevel = GEMFIRE_LOG_LEVEL,
port = 0
)
static class TestConfiguration { }
}

View File

@@ -0,0 +1,139 @@
/*
* Copyright 2018 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.config.annotation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.spy;
import java.util.Optional;
import java.util.Properties;
import org.apache.geode.distributed.Locator;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.data.gemfire.LocatorFactoryBean;
import org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects;
import org.springframework.lang.Nullable;
import org.springframework.mock.env.MockPropertySource;
/**
* Integration Tests for {@link LocatorApplication} and {@link LocatorApplicationConfiguration} asserting that
* the {@link Locator} is configured with {@link Properties}.
*
* @author John Blum
* @see java.util.Properties
* @see org.junit.Test
* @see org.apache.geode.distributed.Locator
* @see org.springframework.context.ConfigurableApplicationContext
* @see org.springframework.context.annotation.AnnotationConfigApplicationContext
* @see org.springframework.core.env.PropertySource
* @see org.springframework.data.gemfire.LocatorFactoryBean
* @see org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects
* @see org.springframework.mock.env.MockPropertySource
* @since 2.2.0
*/
@SuppressWarnings("unused")
public class LocatorApplicationPropertiesIntegrationTests {
private ConfigurableApplicationContext applicationContext;
@After
public void tearDown() {
Optional.ofNullable(this.applicationContext)
.ifPresent(ConfigurableApplicationContext::close);
}
private ConfigurableApplicationContext newApplicationContext(PropertySource<?> testPropertySource,
Class<?>... annotatedClasses) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
propertySources.addFirst(testPropertySource);
applicationContext.register(annotatedClasses);
applicationContext.registerShutdownHook();
applicationContext.refresh();
return applicationContext;
}
@Test
public void locatorIsConfiguredWithProperties() {
MockPropertySource testPropertySource = new MockPropertySource()
.withProperty("spring.data.gemfire.locator.bind-address", "10.120.240.32")
.withProperty("spring.data.gemfire.locator.hostname-for-clients", "skullbox")
.withProperty("spring.data.gemfire.locator.log-level", "error")
.withProperty("spring.data.gemfire.locator.name", "MockLocator")
.withProperty("spring.data.gemfire.locator.port", 54321);
this.applicationContext = newApplicationContext(testPropertySource, TestConfiguration.class);
LocatorFactoryBean locatorFactoryBean = this.applicationContext.getBean(LocatorFactoryBean.class);
assertThat(locatorFactoryBean).isNotNull();
assertThat(locatorFactoryBean.getBindAddress().orElse(null)).isEqualTo("10.120.240.32");
assertThat(locatorFactoryBean.getHostnameForClients().orElse(null)).isEqualTo("skullbox");
assertThat(locatorFactoryBean.getLogLevel()).isEqualTo("error");
assertThat(locatorFactoryBean.getName().orElse(null)).isEqualTo("MockLocator");
assertThat(locatorFactoryBean.getPort()).isEqualTo(54321);
}
@EnableGemFireMockObjects
@LocatorApplication(
bindAddress = "10.105.210.16",
hostnameForClients = "mailbox",
logLevel = "info",
name = "TestLocator",
port = 12345
)
static class TestConfiguration {
@Bean
BeanPostProcessor locatorFactoryBeanPostProcessor() {
return new BeanPostProcessor() {
@Nullable @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof LocatorFactoryBean) {
LocatorFactoryBean locatorFactoryBean = spy((LocatorFactoryBean) bean);
doNothing().when(locatorFactoryBean).init();
bean = locatorFactoryBean;
}
return bean;
}
};
}
}
}