SGF-476 - Polish Spring (Data GemFire) provided, GemFire ServerLauncherCacheProvider implementation and tests.

Closes #87.

(cherry picked from commit 7ef14587e207ad0d8144333add2656aef3abcfd1)
Signed-off-by: John Blum <jblum@pivotal.io>
This commit is contained in:
John Blum
2016-03-29 18:04:17 -07:00
parent b02abf2c1e
commit 91e9cdc9f8
7 changed files with 243 additions and 238 deletions

View File

@@ -1,93 +0,0 @@
/*
* Copyright 2016 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.support;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Collections;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.gemfire.support.SpringContextBootstrappingInitializerIntegrationTest.UserDataStoreCacheLoader;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheClosedException;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.distributed.AbstractLauncher.Status;
import com.gemstone.gemfire.distributed.ServerLauncher;
import com.gemstone.gemfire.distributed.ServerLauncher.ServerState;
import com.gemstone.gemfire.distributed.internal.DistributionConfig;
import com.gemstone.gemfire.internal.util.CollectionUtils;
/**
* The SpringServerLauncherCacheProviderTest class is a test suite of test cases testing the contract and functionality
* of the SpringServerLauncherCacheProvider class. This test class focuses on testing isolated units of functionality in
* the ServerLauncherCacheProvider class directly, mocking any dependencies as appropriate, in order for the class to
* uphold it's contract.
*
* @author Dan Smith
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.context.ApplicationContext
* @see org.springframework.context.ConfigurableApplicationContext
* @see org.springframework.data.gemfire.support.SpringServerLauncherCacheProvider
*/
public class SpringServerLauncherCacheProviderIntegrationTest {
@After
public void tearDown() {
System.clearProperty(DistributionConfig.GEMFIRE_PREFIX + DistributionConfig.NAME_NAME);
SpringContextBootstrappingInitializer.getApplicationContext().close();
tearDownCache();
}
private void tearDownCache() {
try {
Cache cache = CacheFactory.getAnyInstance();
if (cache != null) {
cache.close();
}
}
catch (CacheClosedException ignore) {
// CacheClosedExceptions happen when the Cache reference returned by GemFireCacheImpl.getInstance()
// inside the CacheFactory.getAnyInstance() is null, or the Cache is already closed with calling
// Cache.close();
}
}
@Test
public void createCacheWithSpecifiedConfig() {
String xmlLocation = getClass().getSimpleName() + "-context.xml";
ServerLauncher launcher = mock(ServerLauncher.class);
ServerLauncher.Builder builder = new ServerLauncher.Builder();
builder.setSpringXmlLocation(xmlLocation);
builder.setMemberName("membername");
launcher = builder.build();
ServerState state = launcher.start();
assertEquals(Status.ONLINE, state.getStatus());
ConfigurableApplicationContext ctx = SpringContextBootstrappingInitializer.getApplicationContext();
Cache cache = ctx.getBean(Cache.class);
assertNotNull(cache);
assertEquals(55, cache.getResourceManager().getCriticalHeapPercentage(), 0.1);
assertEquals(Status.STOPPED, launcher.stop().getStatus());
}
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2016 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.support;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import org.apache.geode.cache.Cache;
import org.apache.geode.distributed.AbstractLauncher.Status;
import org.apache.geode.distributed.ServerLauncher;
import org.apache.geode.distributed.ServerLauncher.ServerState;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.gemfire.GemfireUtils;
/**
* Integration tests for {@link SpringServerLauncherCacheProvider}.
*
* @author Dan Smith
* @author John Blum
* @see org.junit.Test
* @see org.springframework.context.ApplicationContext
* @see org.springframework.context.ConfigurableApplicationContext
* @see org.springframework.data.gemfire.support.SpringServerLauncherCacheProvider
* @see org.apache.geode.cache.Cache
* @see org.apache.geode.distributed.ServerLauncher
*/
public class SpringServerLauncherCacheProviderIntegrationTests {
@After
public void tearDown() {
System.clearProperty(gemfireName());
SpringContextBootstrappingInitializer.getApplicationContext().close();
GemfireUtils.closeClientCache();
}
String gemfireName() {
return (GemfireUtils.GEMFIRE_PREFIX + GemfireUtils.NAME_PROPERTY_NAME);
}
@Test
public void createCacheWithSpring() {
String springXmlLocation = getClass().getSimpleName() + "-context.xml";
ServerLauncher.Builder builder = new ServerLauncher.Builder();
builder.setSpringXmlLocation(springXmlLocation);
builder.setMemberName("membername");
ServerLauncher launcher = builder.build();
ServerState state = launcher.start();
assertThat(state.getStatus(), is(equalTo(Status.ONLINE)));
ConfigurableApplicationContext applicationContext =
SpringContextBootstrappingInitializer.getApplicationContext();
Cache cache = applicationContext.getBean(Cache.class);
assertThat(cache, is(notNullValue()));
assertThat(cache.getResourceManager().getCriticalHeapPercentage(), is(equalTo(55.0f)));
state = launcher.stop();
assertThat(state.getStatus(), is(equalTo(Status.STOPPED)));
}
}

View File

@@ -1,92 +0,0 @@
/*
* Copyright 2016 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.support;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Collections;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.distributed.ServerLauncher;
import com.gemstone.gemfire.distributed.internal.DistributionConfig;
import com.gemstone.gemfire.internal.util.CollectionUtils;
/**
* The SpringServerLauncherCacheProviderTest class is a test suite of test cases testing the contract and functionality
* of the SpringServerLauncherCacheProvider class. This test class focuses on testing isolated units of functionality in
* the ServerLauncherCacheProvider class directly, mocking any dependencies as appropriate, in order for the class to
* uphold it's contract.
*
* @author Dan Smith
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.context.ApplicationContext
* @see org.springframework.context.ConfigurableApplicationContext
* @see org.springframework.data.gemfire.support.SpringServerLauncherCacheProvider
*/
public class SpringServerLauncherCacheProviderTest {
@After
public void tearDown() {
System.clearProperty(DistributionConfig.GEMFIRE_PREFIX + DistributionConfig.NAME_NAME);
SpringContextBootstrappingInitializer.applicationContext = null;
}
@Test
public void doesNothingWhenSpringXmlLocationNotSpecified() {
SpringServerLauncherCacheProvider provider = new SpringServerLauncherCacheProvider();
ServerLauncher launcher = mock(ServerLauncher.class);
when(launcher.isSpringXmlLocationSpecified()).thenReturn(false);
assertEquals(null, provider.createCache(null, launcher));
verify(launcher).isSpringXmlLocationSpecified();
}
@Test
public void createCacheWithSpecifiedConfig() {
String xmlLocation = "xml/location";
ServerLauncher launcher = mock(ServerLauncher.class);
when(launcher.isSpringXmlLocationSpecified()).thenReturn(true);
when(launcher.getSpringXmlLocation()).thenReturn(xmlLocation);
when(launcher.getMemberName()).thenReturn("membername");
final SpringContextBootstrappingInitializer initializer = mock(SpringContextBootstrappingInitializer.class);
ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
SpringContextBootstrappingInitializer.applicationContext = context;
Cache cache = mock(Cache.class);
when(context.getBean(eq(Cache.class))).thenReturn(cache );
SpringServerLauncherCacheProvider provider = new SpringServerLauncherCacheProvider() {
@Override
public SpringContextBootstrappingInitializer createSpringContextBootstrappingInitializer() {
return initializer;
}
};
assertEquals(cache, provider.createCache(null, launcher));
verify(launcher).isSpringXmlLocationSpecified();
verify(initializer).init(CollectionUtils.createProperties(
Collections.singletonMap(SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER,
xmlLocation)));
}
}

View File

@@ -0,0 +1,113 @@
/*
* Copyright 2016 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.support;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.eq;
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 org.apache.geode.cache.Cache;
import org.apache.geode.distributed.ServerLauncher;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.gemfire.GemfireUtils;
/**
* Unit tests for {@link SpringServerLauncherCacheProvider}.
*
* @author Dan Smith
* @author John Blum
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.apache.geode.cache.Cache
* @see org.apache.geode.distributed.ServerLauncher
* @see org.apache.geode.distributed.ServerLauncherCacheProvider
* @see org.springframework.context.ApplicationContext
* @see org.springframework.context.ConfigurableApplicationContext
* @see org.springframework.data.gemfire.support.SpringServerLauncherCacheProvider
*/
public class SpringServerLauncherCacheProviderUnitTests {
String gemfireName() {
return (GemfireUtils.GEMFIRE_PREFIX + GemfireUtils.NAME_PROPERTY_NAME);
}
Properties singletonProperties(String propertyName, String propertyValue) {
Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue);
return properties;
}
@After
public void tearDown() {
System.clearProperty(gemfireName());
SpringContextBootstrappingInitializer.applicationContext = null;
}
@Test
public void createsCacheWhenSpringXmlLocationIsSpecified() {
Cache mockCache = mock(Cache.class);
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class);
ServerLauncher mockServerLauncher = mock(ServerLauncher.class);
SpringContextBootstrappingInitializer.applicationContext = mockApplicationContext;
when(mockServerLauncher.isSpringXmlLocationSpecified()).thenReturn(true);
when(mockServerLauncher.getSpringXmlLocation()).thenReturn("test-context.xml");
when(mockServerLauncher.getMemberName()).thenReturn("TEST");
when(mockApplicationContext.getBean(eq(Cache.class))).thenReturn(mockCache);
final SpringContextBootstrappingInitializer initializer = mock(SpringContextBootstrappingInitializer.class);
SpringServerLauncherCacheProvider provider = new SpringServerLauncherCacheProvider() {
@Override
public SpringContextBootstrappingInitializer newSpringContextBootstrappingInitializer() {
return initializer;
}
};
Properties expectedParameters = singletonProperties(
SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER, "test-context.xml");
assertThat(provider.createCache(null, mockServerLauncher), is(equalTo(mockCache)));
verify(mockServerLauncher, times(1)).isSpringXmlLocationSpecified();
verify(mockServerLauncher, times(1)).getSpringXmlLocation();
verify(mockServerLauncher, times(1)).getMemberName();
verify(mockApplicationContext, times(1)).getBean(eq(Cache.class));
verify(initializer).init(eq(expectedParameters));
}
@Test
public void doesNothingWhenSpringXmlLocationNotSpecified() {
ServerLauncher launcher = mock(ServerLauncher.class);
when(launcher.isSpringXmlLocationSpecified()).thenReturn(false);
assertThat(new SpringServerLauncherCacheProvider().createCache(null, launcher), is(nullValue()));
verify(launcher, times(1)).isSpringXmlLocationSpecified();
}
}