DATAGEODE-188 - Refactor and simplify codebase.

This commit is contained in:
John Blum
2019-05-12 23:25:07 -07:00
parent ad0ad251a1
commit ef4e32e43d
7 changed files with 47 additions and 64 deletions

View File

@@ -1350,6 +1350,7 @@ public class CacheFactoryBean extends AbstractFactoryBeanSupport<GemFireCache>
* @see org.apache.geode.cache.CacheFactory * @see org.apache.geode.cache.CacheFactory
* @see org.apache.geode.cache.client.ClientCacheFactory * @see org.apache.geode.cache.client.ClientCacheFactory
*/ */
@FunctionalInterface
public interface CacheFactoryInitializer<T> { public interface CacheFactoryInitializer<T> {
/** /**

View File

@@ -14,7 +14,6 @@
* limitations under the License. * limitations under the License.
* *
*/ */
package org.springframework.data.gemfire.config.annotation; package org.springframework.data.gemfire.config.annotation;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;

View File

@@ -19,6 +19,7 @@ package org.springframework.data.gemfire.snapshot;
import static java.util.Arrays.stream; import static java.util.Arrays.stream;
import static org.apache.geode.cache.snapshot.SnapshotOptions.SnapshotFormat; import static org.apache.geode.cache.snapshot.SnapshotOptions.SnapshotFormat;
import static org.springframework.data.gemfire.snapshot.SnapshotServiceFactoryBean.SnapshotServiceAdapter; import static org.springframework.data.gemfire.snapshot.SnapshotServiceFactoryBean.SnapshotServiceAdapter;
import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException; import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException; import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
@@ -88,11 +89,6 @@ public class SnapshotServiceFactoryBean<K, V> extends AbstractFactoryBeanSupport
private SnapshotServiceAdapter<K, V> snapshotServiceAdapter; private SnapshotServiceAdapter<K, V> snapshotServiceAdapter;
@SuppressWarnings("unchecked")
static <K, V> SnapshotMetadata<K, V>[] nullSafeArray(SnapshotMetadata<K, V>[] configurations) {
return configurations != null ? configurations : EMPTY_ARRAY;
}
static boolean nullSafeIsDirectory(File file) { static boolean nullSafeIsDirectory(File file) {
return file != null && file.isDirectory(); return file != null && file.isDirectory();
} }
@@ -210,8 +206,9 @@ public class SnapshotServiceFactoryBean<K, V> extends AbstractFactoryBeanSupport
* @return an array of snapshot meta-data used for each export. * @return an array of snapshot meta-data used for each export.
* @see SnapshotServiceFactoryBean.SnapshotMetadata * @see SnapshotServiceFactoryBean.SnapshotMetadata
*/ */
@SuppressWarnings("unchecked")
protected SnapshotMetadata<K, V>[] getExports() { protected SnapshotMetadata<K, V>[] getExports() {
return nullSafeArray(exports); return nullSafeArray(exports, SnapshotMetadata.class);
} }
/** /**
@@ -232,8 +229,9 @@ public class SnapshotServiceFactoryBean<K, V> extends AbstractFactoryBeanSupport
* @return an array of snapshot meta-data used for each import. * @return an array of snapshot meta-data used for each import.
* @see SnapshotServiceFactoryBean.SnapshotMetadata * @see SnapshotServiceFactoryBean.SnapshotMetadata
*/ */
@SuppressWarnings("unchecked")
protected SnapshotMetadata<K, V>[] getImports() { protected SnapshotMetadata<K, V>[] getImports() {
return nullSafeArray(imports); return nullSafeArray(imports, SnapshotMetadata.class);
} }
/** /**
@@ -455,7 +453,7 @@ public class SnapshotServiceFactoryBean<K, V> extends AbstractFactoryBeanSupport
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void doExport(SnapshotMetadata<K, V>... configurations) { public void doExport(SnapshotMetadata<K, V>... configurations) {
stream(nullSafeArray(configurations)).forEach(configuration -> stream(nullSafeArray(configurations, SnapshotMetadata.class)).forEach(configuration ->
save(configuration.getLocation(), configuration.getFormat(), createOptions(configuration))); save(configuration.getLocation(), configuration.getFormat(), createOptions(configuration)));
} }
@@ -463,7 +461,7 @@ public class SnapshotServiceFactoryBean<K, V> extends AbstractFactoryBeanSupport
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void doImport(SnapshotMetadata<K, V>... configurations) { public void doImport(SnapshotMetadata<K, V>... configurations) {
stream(nullSafeArray(configurations)).forEach(configuration -> stream(nullSafeArray(configurations, SnapshotMetadata.class)).forEach(configuration ->
load(configuration.getFormat(), createOptions(configuration), handleLocation(configuration))); load(configuration.getFormat(), createOptions(configuration), handleLocation(configuration)));
} }

View File

@@ -16,6 +16,8 @@
package org.springframework.data.gemfire.snapshot.filter; package org.springframework.data.gemfire.snapshot.filter;
import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray;
import java.util.Map; import java.util.Map;
import org.apache.geode.cache.snapshot.SnapshotFilter; import org.apache.geode.cache.snapshot.SnapshotFilter;
@@ -69,17 +71,12 @@ public class ComposableSnapshotFilter<K, V> implements SnapshotFilter<K, V> {
* @see org.apache.geode.cache.snapshot.SnapshotFilter * @see org.apache.geode.cache.snapshot.SnapshotFilter
*/ */
private ComposableSnapshotFilter(SnapshotFilter<K, V> leftOperand, Operator operator, SnapshotFilter<K, V> rightOperand) { private ComposableSnapshotFilter(SnapshotFilter<K, V> leftOperand, Operator operator, SnapshotFilter<K, V> rightOperand) {
this.leftOperand = leftOperand; this.leftOperand = leftOperand;
this.operator = operator; this.operator = operator;
this.rightOperand = rightOperand; this.rightOperand = rightOperand;
} }
/* (non-Javadoc) */
@SuppressWarnings("unchecked")
static <K, V> SnapshotFilter<K, V>[] nullSafeArray(SnapshotFilter<K, V>... array) {
return (array != null ? array : new SnapshotFilter[0]);
}
/** /**
* Composes the array of SnapshotFilters into a logical boolean expression using the specified Operator. * Composes the array of SnapshotFilters into a logical boolean expression using the specified Operator.
* *
@@ -92,10 +89,12 @@ public class ComposableSnapshotFilter<K, V> implements SnapshotFilter<K, V> {
* @see ComposableSnapshotFilter.Operator * @see ComposableSnapshotFilter.Operator
* @see org.apache.geode.cache.snapshot.SnapshotFilter * @see org.apache.geode.cache.snapshot.SnapshotFilter
*/ */
@SuppressWarnings("unchecked")
protected static <K, V> SnapshotFilter<K, V> compose(Operator operator, SnapshotFilter<K, V>... snapshotFilters) { protected static <K, V> SnapshotFilter<K, V> compose(Operator operator, SnapshotFilter<K, V>... snapshotFilters) {
SnapshotFilter<K, V> composedSnapshotFilter = null; SnapshotFilter<K, V> composedSnapshotFilter = null;
for (SnapshotFilter<K, V> snapshotFilter : nullSafeArray(snapshotFilters)) { for (SnapshotFilter<K, V> snapshotFilter : nullSafeArray(snapshotFilters, SnapshotFilter.class)) {
composedSnapshotFilter = (composedSnapshotFilter == null ? snapshotFilter composedSnapshotFilter = (composedSnapshotFilter == null ? snapshotFilter
: new ComposableSnapshotFilter<K, V>(snapshotFilter, operator, composedSnapshotFilter)); : new ComposableSnapshotFilter<K, V>(snapshotFilter, operator, composedSnapshotFilter));
} }
@@ -144,7 +143,7 @@ public class ComposableSnapshotFilter<K, V> implements SnapshotFilter<K, V> {
*/ */
@Override @Override
public boolean accept(final Map.Entry<K, V> entry) { public boolean accept(final Map.Entry<K, V> entry) {
return operator.operate(leftOperand.accept(entry), rightOperand.accept(entry)); return this.operator.operate(this.leftOperand.accept(entry), this.rightOperand.accept(entry));
} }
} }

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.gemfire.config.annotation; package org.springframework.data.gemfire.config.annotation;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@@ -40,21 +39,30 @@ import org.springframework.mock.env.MockPropertySource;
* Integration tests for {@link PeerCacheApplication}. * Integration tests for {@link PeerCacheApplication}.
* *
* @author John Blum * @author John Blum
* @see java.util.Properties
* @see org.junit.Test * @see org.junit.Test
* @see org.springframework.context.ConfigurableApplicationContext
* @see org.springframework.context.annotation.AnnotationConfigApplicationContext
* @see org.springframework.core.env.PropertySource
* @see org.springframework.data.gemfire.config.annotation.PeerCacheApplication * @see org.springframework.data.gemfire.config.annotation.PeerCacheApplication
* @see org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects
* @see org.springframework.mock.env.MockPropertySource
* @since 2.0.0 * @since 2.0.0
*/ */
@SuppressWarnings("unused")
public class PeerCachePropertiesIntegrationTests { public class PeerCachePropertiesIntegrationTests {
private ConfigurableApplicationContext applicationContext; private ConfigurableApplicationContext applicationContext;
@After @After
public void tearDown() { public void tearDown() {
Optional.ofNullable(this.applicationContext).ifPresent(ConfigurableApplicationContext::close);
Optional.ofNullable(this.applicationContext)
.ifPresent(ConfigurableApplicationContext::close);
} }
private ConfigurableApplicationContext newApplicationContext(PropertySource<?> testPropertySource, private ConfigurableApplicationContext newApplicationContext(PropertySource<?> testPropertySource,
Class<?>... annotatedClasses) { Class<?>... annotatedClasses) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
@@ -62,8 +70,8 @@ public class PeerCachePropertiesIntegrationTests {
propertySources.addFirst(testPropertySource); propertySources.addFirst(testPropertySource);
applicationContext.registerShutdownHook();
applicationContext.register(annotatedClasses); applicationContext.register(annotatedClasses);
applicationContext.registerShutdownHook();
applicationContext.refresh(); applicationContext.refresh();
return applicationContext; return applicationContext;
@@ -171,7 +179,6 @@ public class PeerCachePropertiesIntegrationTests {
@EnablePdx(ignoreUnreadFields = true, readSerialized = true, serializerBeanName = "mockPdxSerializer") @EnablePdx(ignoreUnreadFields = true, readSerialized = true, serializerBeanName = "mockPdxSerializer")
@PeerCacheApplication(name = "TestPeerCache", criticalHeapPercentage = 90.0f, evictionHeapPercentage = 75.0f, @PeerCacheApplication(name = "TestPeerCache", criticalHeapPercentage = 90.0f, evictionHeapPercentage = 75.0f,
lockLease = 300, lockTimeout = 120) lockLease = 300, lockTimeout = 120)
@SuppressWarnings("unused")
static class TestPeerCacheConfiguration { static class TestPeerCacheConfiguration {
@Bean @Bean
@@ -187,7 +194,6 @@ public class PeerCachePropertiesIntegrationTests {
@EnableGemFireMockObjects @EnableGemFireMockObjects
@PeerCacheApplication(name = "TestPeerCache") @PeerCacheApplication(name = "TestPeerCache")
@SuppressWarnings("unused") static class TestDynamicPeerCacheConfiguration { }
static class TestDynamicPeerCacheConfiguration {
}
} }

View File

@@ -161,20 +161,6 @@ public class SnapshotServiceFactoryBeanTest {
factoryBean.setRegion(null); factoryBean.setRegion(null);
} }
@Test
public void nullSafeArrayWithNonNullArray() {
SnapshotMetadata[] expectedConfigurations = new SnapshotMetadata[0];
assertThat(SnapshotServiceFactoryBean.nullSafeArray(expectedConfigurations),
is(sameInstance(expectedConfigurations)));
}
@Test
public void nullSafeArrayWithNullArray() {
assertThat(SnapshotServiceFactoryBean.nullSafeArray(null), is(equalTo(SnapshotServiceFactoryBean.EMPTY_ARRAY)));
}
@Test @Test
public void nullSafeIsDirectoryWithDirectory() { public void nullSafeIsDirectoryWithDirectory() {
assertThat(SnapshotServiceFactoryBean.nullSafeIsDirectory(new File(System.getProperty("user.dir"))), is(true)); assertThat(SnapshotServiceFactoryBean.nullSafeIsDirectory(new File(System.getProperty("user.dir"))), is(true));

View File

@@ -13,16 +13,18 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.gemfire.snapshot.filter; package org.springframework.data.gemfire.snapshot.filter;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.isA; import static org.hamcrest.CoreMatchers.isA;
import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.apache.geode.cache.snapshot.SnapshotFilter; import org.apache.geode.cache.snapshot.SnapshotFilter;
@@ -48,16 +50,18 @@ public class ComposableSnapshotFilterTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected SnapshotFilter mockSnapshotFilter(boolean accept) { protected SnapshotFilter mockSnapshotFilter(boolean accept) {
SnapshotFilter mockSnapshotFilter = mock(SnapshotFilter.class, String.format(
"MockSnapshotFilter-%1$d", ID_SEQUENCE.incrementAndGet()));
when(mockSnapshotFilter.accept((Map.Entry) any())).thenReturn(accept); SnapshotFilter mockSnapshotFilter = mock(SnapshotFilter.class,
String.format("MockSnapshotFilter-%1$d", ID_SEQUENCE.incrementAndGet()));
when(mockSnapshotFilter.accept(any())).thenReturn(accept);
return mockSnapshotFilter; return mockSnapshotFilter;
} }
@Test @Test
public void operatorIdentityIsSuccessful() { public void operatorIdentityIsSuccessful() {
assertThat(Operator.AND.isAnd(), is(true)); assertThat(Operator.AND.isAnd(), is(true));
assertThat(Operator.AND.isOr(), is(false)); assertThat(Operator.AND.isOr(), is(false));
assertThat(Operator.OR.isAnd(), is(false)); assertThat(Operator.OR.isAnd(), is(false));
@@ -66,6 +70,7 @@ public class ComposableSnapshotFilterTest {
@Test @Test
public void andOperatorOperationIsValid() { public void andOperatorOperationIsValid() {
assertThat(Operator.AND.operate(true, true), is(true)); assertThat(Operator.AND.operate(true, true), is(true));
assertThat(Operator.AND.operate(true, false), is(false)); assertThat(Operator.AND.operate(true, false), is(false));
assertThat(Operator.AND.operate(false, true), is(false)); assertThat(Operator.AND.operate(false, true), is(false));
@@ -74,29 +79,16 @@ public class ComposableSnapshotFilterTest {
@Test @Test
public void orOperatorOperationIsValid() { public void orOperatorOperationIsValid() {
assertThat(Operator.OR.operate(true, true), is(true)); assertThat(Operator.OR.operate(true, true), is(true));
assertThat(Operator.OR.operate(true, false), is(true)); assertThat(Operator.OR.operate(true, false), is(true));
assertThat(Operator.OR.operate(false, true), is(true)); assertThat(Operator.OR.operate(false, true), is(true));
assertThat(Operator.OR.operate(false, false), is(false)); assertThat(Operator.OR.operate(false, false), is(false));
} }
@Test
public void nullSafeArrayWithNonNullArray() {
SnapshotFilter[] expectedArray = {};
assertThat(ComposableSnapshotFilter.nullSafeArray(expectedArray), is(sameInstance(expectedArray)));
}
@Test
public void nullSafeArrayWithNullArray() {
SnapshotFilter[] actualArray = ComposableSnapshotFilter.nullSafeArray((SnapshotFilter[]) null);
assertThat(actualArray, is(notNullValue()));
assertThat(actualArray.length, is(equalTo(0)));
}
@Test @Test
public void composeSingle() { public void composeSingle() {
SnapshotFilter mockSnapshotFilter = mockSnapshotFilter(false); SnapshotFilter mockSnapshotFilter = mockSnapshotFilter(false);
SnapshotFilter composedFilter = ComposableSnapshotFilter.compose(Operator.AND, mockSnapshotFilter); SnapshotFilter composedFilter = ComposableSnapshotFilter.compose(Operator.AND, mockSnapshotFilter);
@@ -105,6 +97,7 @@ public class ComposableSnapshotFilterTest {
@Test @Test
public void composeMultiple() throws Exception { public void composeMultiple() throws Exception {
SnapshotFilter mockSnapshotFilterOne = mockSnapshotFilter(false); SnapshotFilter mockSnapshotFilterOne = mockSnapshotFilter(false);
SnapshotFilter mockSnapshotFilterTwo = mockSnapshotFilter(true); SnapshotFilter mockSnapshotFilterTwo = mockSnapshotFilter(true);
@@ -124,6 +117,7 @@ public class ComposableSnapshotFilterTest {
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void composeAndThenAccept() { public void composeAndThenAccept() {
SnapshotFilter falseFilter = mockSnapshotFilter(false); SnapshotFilter falseFilter = mockSnapshotFilter(false);
SnapshotFilter trueFilter = mockSnapshotFilter(true); SnapshotFilter trueFilter = mockSnapshotFilter(true);
@@ -146,6 +140,7 @@ public class ComposableSnapshotFilterTest {
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void composeOrThenAccept() { public void composeOrThenAccept() {
SnapshotFilter falseFilter = mockSnapshotFilter(false); SnapshotFilter falseFilter = mockSnapshotFilter(false);
SnapshotFilter trueFilter = mockSnapshotFilter(true); SnapshotFilter trueFilter = mockSnapshotFilter(true);
@@ -164,5 +159,4 @@ public class ComposableSnapshotFilterTest {
assertThat((ComposableSnapshotFilter) composedFilter, isA(ComposableSnapshotFilter.class)); assertThat((ComposableSnapshotFilter) composedFilter, isA(ComposableSnapshotFilter.class));
assertThat(composedFilter.accept(null), is(false)); assertThat(composedFilter.accept(null), is(false));
} }
} }