SGF-548 - Configure Regions with annotations.

Related JIRA: SGF-250 - @EnableGemfireRegions for @Region.
This commit is contained in:
John Blum
2016-12-08 16:13:56 -08:00
parent ba87a716d0
commit aca2edbce5
56 changed files with 3183 additions and 257 deletions

View File

@@ -45,7 +45,7 @@ import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.client.ClientCacheFactoryBean;
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
import org.springframework.data.gemfire.client.PoolFactoryBean;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.gemfire.process.ProcessExecutor;
import org.springframework.data.gemfire.process.ProcessWrapper;
import org.springframework.data.gemfire.server.CacheServerFactoryBean;

View File

@@ -26,8 +26,7 @@ import org.apache.geode.cache.PartitionResolver;
import org.junit.Test;
/**
* The PartitionAttributesFactoryBeanTest class is test suite of test cases testing the contract and functionality of
* the PartitionAttributesFactoryBean class.
* Unit tests for {@link PartitionAttributesFactoryBean}.
*
* @author John Blum
* @see org.junit.Test
@@ -52,11 +51,12 @@ public class PartitionAttributesFactoryBeanTest {
partitionAttributesFactoryBean.setColocatedWith("mockColocatedRegion");
partitionAttributesFactoryBean.setLocalMaxMemory(1024);
partitionAttributesFactoryBean.setPartitionResolver(createMockPartitionResolver("mockPartitionResolver"));
partitionAttributesFactoryBean.setRecoveryDelay(1000l);
partitionAttributesFactoryBean.setRecoveryDelay(1000L);
partitionAttributesFactoryBean.setRedundantCopies(1);
partitionAttributesFactoryBean.setStartupRecoveryDelay(60000l);
partitionAttributesFactoryBean.setTotalMaxMemory(8192l);
partitionAttributesFactoryBean.setStartupRecoveryDelay(60000L);
partitionAttributesFactoryBean.setTotalMaxMemory(8192L);
partitionAttributesFactoryBean.setTotalNumBuckets(42);
partitionAttributesFactoryBean.afterPropertiesSet();
PartitionAttributes partitionAttributes = partitionAttributesFactoryBean.getObject();
@@ -65,10 +65,10 @@ public class PartitionAttributesFactoryBeanTest {
assertEquals(1024, partitionAttributes.getLocalMaxMemory());
assertNotNull(partitionAttributes.getPartitionResolver());
assertEquals("mockPartitionResolver", partitionAttributes.getPartitionResolver().getName());
assertEquals(1000l, partitionAttributes.getRecoveryDelay());
assertEquals(1000L, partitionAttributes.getRecoveryDelay());
assertEquals(1, partitionAttributes.getRedundantCopies());
assertEquals(60000l, partitionAttributes.getStartupRecoveryDelay());
assertEquals(8192l, partitionAttributes.getTotalMaxMemory());
assertEquals(60000L, partitionAttributes.getStartupRecoveryDelay());
assertEquals(8192L, partitionAttributes.getTotalMaxMemory());
assertEquals(42, partitionAttributes.getTotalNumBuckets());
}
@@ -76,14 +76,13 @@ public class PartitionAttributesFactoryBeanTest {
public void testValidationOnRedundantCopiesWhenExceedsBound() throws Exception {
PartitionAttributesFactoryBean partitionAttributesFactoryBean = new PartitionAttributesFactoryBean();
partitionAttributesFactoryBean.setRedundantCopies(4);
partitionAttributesFactoryBean.getObject();
partitionAttributesFactoryBean.afterPropertiesSet();
}
@Test(expected = IllegalStateException.class)
public void testValidationOnRedundantCopiesWhenPrecedesBound() throws Exception {
PartitionAttributesFactoryBean partitionAttributesFactoryBean = new PartitionAttributesFactoryBean();
partitionAttributesFactoryBean.setRedundantCopies(-1);
partitionAttributesFactoryBean.getObject();
partitionAttributesFactoryBean.afterPropertiesSet();
}
}

View File

@@ -0,0 +1,522 @@
/*
* 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.config.annotation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.DataPolicy;
import org.apache.geode.cache.DiskStore;
import org.apache.geode.cache.FixedPartitionAttributes;
import org.apache.geode.cache.PartitionAttributes;
import org.apache.geode.cache.PartitionResolver;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.RegionAttributes;
import org.apache.geode.cache.RegionFactory;
import org.apache.geode.cache.Scope;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.ClientRegionFactory;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.junit.After;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.gemfire.config.annotation.test.entities.ClientRegionEntity;
import org.springframework.data.gemfire.config.annotation.test.entities.CollocatedPartitionRegionEntity;
import org.springframework.data.gemfire.config.annotation.test.entities.GenericRegionEntity;
import org.springframework.data.gemfire.config.annotation.test.entities.NonEntity;
import org.springframework.data.gemfire.config.xml.GemfireConstants;
import org.springframework.data.gemfire.mapping.annotation.ClientRegion;
import org.springframework.data.gemfire.mapping.annotation.LocalRegion;
import org.springframework.data.gemfire.mapping.annotation.PartitionRegion;
import org.springframework.data.gemfire.mapping.annotation.ReplicateRegion;
import org.springframework.data.gemfire.support.ClientRegionShortcutWrapper;
import org.springframework.data.gemfire.util.CollectionUtils;
/**
* Unit tests for the {@link EnableEntityDefinedRegions} annotation and {@link EntityDefinedRegionsConfiguration} class.
*
* @author John Blum
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions
* @see org.springframework.data.gemfire.config.annotation.EntityDefinedRegionsConfiguration
* @since 1.9.0
*/
public class EnableEntityDefinedRegionsConfigurationUnitTests {
private static final AtomicInteger MOCK_ID = new AtomicInteger(0);
private ConfigurableApplicationContext applicationContext;
@After
public void tearDown() {
if (applicationContext != null) {
applicationContext.close();
}
}
/* (non-Javadoc) */
protected void assertRegion(Region<?, ?> region, String name) {
assertRegion(region, name, null, null);
}
/* (non-Javadoc) */
protected <K, V> void assertRegion(Region<?, ?> region, String name,
Class<K> keyConstraint, Class<V> valueConstraint) {
assertRegion(region, name, String.format("%1$s%2$s", Region.SEPARATOR, name), keyConstraint, valueConstraint);
}
/* (non-Javadoc) */
@SuppressWarnings("unused")
protected void assertRegion(Region<?, ?> region, String name, String fullPath) {
assertRegion(region, name, fullPath, null, null);
}
/* (non-Javadoc) */
protected <K, V> void assertRegion(Region<?, ?> region, String name, String fullPath,
Class<K> keyConstraint, Class<V> valueConstraint) {
assertThat(region).isNotNull();
assertThat(region.getName()).isEqualTo(name);
assertThat(region.getFullPath()).isEqualTo(fullPath);
assertThat(region.getAttributes()).isNotNull();
assertThat(region.getAttributes().getKeyConstraint()).isEqualTo(keyConstraint);
assertThat(region.getAttributes().getValueConstraint()).isEqualTo(valueConstraint);
}
/* (non-Javadoc) */
protected void assertRegionAttributes(RegionAttributes<?, ?> regionAttributes, DataPolicy dataPolicy,
String diskStoreName, Boolean diskSynchronous, Boolean ignoreJta, String poolName, Scope scope) {
assertThat(regionAttributes).isNotNull();
assertThat(regionAttributes.getDataPolicy()).isEqualTo(dataPolicy);
assertThat(regionAttributes.getDiskStoreName()).isEqualTo(diskStoreName);
assertThat(regionAttributes.isDiskSynchronous()).isEqualTo(diskSynchronous);
assertThat(regionAttributes.getIgnoreJTA()).isEqualTo(ignoreJta);
assertThat(regionAttributes.getPoolName()).isEqualToIgnoringCase(poolName);
assertThat(regionAttributes.getScope()).isEqualTo(scope);
}
/* (non-Javadoc) */
protected void assertPartitionAttributes(PartitionAttributes<?, ?> partitionAttributes,
String collocatedWith, PartitionResolver partitionResolver, Integer redundantCopies) {
assertThat(partitionAttributes).isNotNull();
assertThat(partitionAttributes.getColocatedWith()).isEqualTo(collocatedWith);
assertThat(partitionAttributes.getPartitionResolver()).isEqualTo(partitionResolver);
assertThat(partitionAttributes.getRedundantCopies()).isEqualTo(redundantCopies);
}
/* (non-Javadoc) */
protected void assertFixedPartitionAttributes(FixedPartitionAttributes fixedPartitionAttributes,
String partitionName, boolean primary, int numBuckets) {
assertThat(fixedPartitionAttributes).isNotNull();
assertThat(fixedPartitionAttributes.getPartitionName()).isEqualTo(partitionName);
assertThat(fixedPartitionAttributes.isPrimary()).isEqualTo(primary);
assertThat(fixedPartitionAttributes.getNumBuckets()).isEqualTo(numBuckets);
}
/* (non-Javadoc) */
@SuppressWarnings("unchecked")
protected FixedPartitionAttributes findFixedPartitionAttributes(PartitionAttributes partitionAttributes,
String partitionName) {
assertThat(partitionAttributes).isNotNull();
List<FixedPartitionAttributes> fixedPartitionAttributes =
CollectionUtils.nullSafeList(partitionAttributes.getFixedPartitionAttributes());
for (FixedPartitionAttributes attributes : fixedPartitionAttributes) {
if (attributes.getPartitionName().equals(partitionName)) {
return attributes;
}
}
return null;
}
/* (non-Javadoc) */
protected ConfigurableApplicationContext newApplicationContext(Class<?>... annotatedClasses) {
ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(annotatedClasses);
applicationContext.registerShutdownHook();
return applicationContext;
}
@Test
@SuppressWarnings("unchecked")
public void entityClientRegionsDefined() {
applicationContext = newApplicationContext(ClientPersistentEntitiesConfiguration.class);
Region<String, ClientRegionEntity> sessions = applicationContext.getBean("Sessions", Region.class);
assertRegion(sessions, "Sessions", String.class, ClientRegionEntity.class);
assertRegionAttributes(sessions.getAttributes(), DataPolicy.NORMAL, null, true, false,
GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME, null);
Region<Long, GenericRegionEntity> genericRegionEntity =
applicationContext.getBean("GenericRegionEntity", Region.class);
assertRegion(genericRegionEntity, "GenericRegionEntity", Long.class, GenericRegionEntity.class);
assertRegionAttributes(genericRegionEntity.getAttributes(), DataPolicy.EMPTY, null, true, false,
GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME, null);
assertThat(applicationContext.containsBean("CollocatedPartitionRegionEntity")).isFalse();
assertThat(applicationContext.containsBean("ContactEvents")).isFalse();
assertThat(applicationContext.containsBean("NonEntity")).isFalse();
assertThat(applicationContext.containsBean("Accounts")).isFalse();
assertThat(applicationContext.containsBean("Customers")).isFalse();
assertThat(applicationContext.containsBean("LocalRegionEntity")).isFalse();
assertThat(applicationContext.containsBean("PartitionRegionEntity")).isFalse();
assertThat(applicationContext.containsBean("ReplicateRegionEntity")).isFalse();
}
@Test
@SuppressWarnings("unchecked")
public void entityPeerPartitionRegionsDefined() {
applicationContext = newApplicationContext(PeerPartitionRegionPersistentEntitiesConfiguration.class);
Region<Object, Object> customers = applicationContext.getBean("Customers", Region.class);
assertRegion(customers, "Customers");
assertRegionAttributes(customers.getAttributes(), DataPolicy.PERSISTENT_PARTITION, null, true, false, null,
Scope.DISTRIBUTED_NO_ACK);
assertPartitionAttributes(customers.getAttributes().getPartitionAttributes(), null, null, 1);
assertFixedPartitionAttributes(findFixedPartitionAttributes(
customers.getAttributes().getPartitionAttributes(), "one"), "one", true, 16);
assertFixedPartitionAttributes(findFixedPartitionAttributes(
customers.getAttributes().getPartitionAttributes(), "two"), "two", false, 21);
Region<Object, Object> contactEvents = applicationContext.getBean("ContactEvents", Region.class);
assertRegion(contactEvents, "ContactEvents");
assertRegionAttributes(contactEvents.getAttributes(), DataPolicy.PERSISTENT_PARTITION, "mockDiskStore",
false, true, null, Scope.DISTRIBUTED_NO_ACK);
assertPartitionAttributes(contactEvents.getAttributes().getPartitionAttributes(), "Customers",
applicationContext.getBean("mockPartitionResolver", PartitionResolver.class), 2);
assertThat(applicationContext.getBean("mockDiskStore")).isInstanceOf(DiskStore.class);
assertThat(applicationContext.containsBean("ClientRegion")).isFalse();
assertThat(applicationContext.containsBean("NonEntity")).isFalse();
assertThat(applicationContext.containsBean("Accounts")).isFalse();
assertThat(applicationContext.containsBean("LocalRegionEntity")).isFalse();
assertThat(applicationContext.containsBean("PartitionRegionEntity")).isFalse();
assertThat(applicationContext.containsBean("ReplicateRegionEntity")).isFalse();
assertThat(applicationContext.containsBean("Sessions")).isFalse();
}
@Test
@SuppressWarnings("unchecked")
public void entityServerRegionsDefined() {
applicationContext = newApplicationContext(AllServerPersistentEntitiesConfiguration.class);
Region<Object, Object> accounts = applicationContext.getBean("Accounts", Region.class);
assertRegion(accounts, "Accounts");
assertRegionAttributes(accounts.getAttributes(), DataPolicy.REPLICATE, null, true, false, null,
Scope.DISTRIBUTED_ACK);
Region<Object, Object> customers = applicationContext.getBean("Customers", Region.class);
assertRegion(customers, "Customers");
assertRegionAttributes(customers.getAttributes(), DataPolicy.PERSISTENT_PARTITION, null, true, false, null,
Scope.DISTRIBUTED_NO_ACK);
assertPartitionAttributes(customers.getAttributes().getPartitionAttributes(), null, null, 1);
Region<Object, Object> localRegionEntity = applicationContext.getBean("LocalRegionEntity", Region.class);
assertRegion(localRegionEntity, "LocalRegionEntity");
assertRegionAttributes(localRegionEntity.getAttributes(), DataPolicy.NORMAL,
null, true, false, null, Scope.LOCAL);
Region<Object, Object> genericRegionEntity = applicationContext.getBean("GenericRegionEntity", Region.class);
assertRegion(genericRegionEntity, "GenericRegionEntity");
assertRegionAttributes(genericRegionEntity.getAttributes(), DataPolicy.NORMAL, null, true, false, null,
Scope.DISTRIBUTED_NO_ACK);
assertThat(applicationContext.containsBean("CollocatedPartitionRegionEntity")).isFalse();
assertThat(applicationContext.containsBean("ContactEvents")).isFalse();
assertThat(applicationContext.containsBean("NonEntity")).isFalse();
assertThat(applicationContext.containsBean("PartitionRegionEntity")).isFalse();
assertThat(applicationContext.containsBean("ReplicateRegionEntity")).isFalse();
assertThat(applicationContext.containsBean("Sessions")).isFalse();
}
/* (non-Javadoc) */
protected static String mockName(String baseMockName) {
return String.format("%s%d", baseMockName, MOCK_ID.incrementAndGet());
}
/* (non-Javadoc) */
@SuppressWarnings("unchecked")
protected static <K, V> Cache mockCache() {
Cache mockCache = mock(Cache.class);
Answer<RegionFactory<K, V>> createRegionFactory = invocation -> {
RegionAttributes<K, V> defaultRegionAttributes =
mockRegionAttributes(null, null, true, false, null, null, null, Scope.DISTRIBUTED_NO_ACK, null);
RegionAttributes<K, V> regionAttributes = (invocation.getArguments().length == 1
? invocation.getArgumentAt(0, RegionAttributes.class) : defaultRegionAttributes);
return mockRegionFactory(regionAttributes);
};
when(mockCache.createRegionFactory()).thenAnswer(createRegionFactory);
when(mockCache.createRegionFactory(any(RegionAttributes.class))).thenAnswer(createRegionFactory);
return mockCache;
}
protected static <K, V> ClientCache mockClientCache() {
ClientCache mockClientCache = mock(ClientCache.class, mockName("ClientCache"));
Answer<ClientRegionFactory<K, V>> createClientRegionFactory =
invocation -> mockClientRegionFactory(invocation.getArgumentAt(0, ClientRegionShortcut.class));
when(mockClientCache.createClientRegionFactory(any(ClientRegionShortcut.class)))
.thenAnswer(createClientRegionFactory);
return mockClientCache;
}
/* (non-Javadoc) */
@SuppressWarnings("unchecked")
protected static <K, V> ClientRegionFactory<K, V> mockClientRegionFactory(ClientRegionShortcut shortcut) {
ClientRegionFactory<K, V> mockClientRegionFactory =
mock(ClientRegionFactory.class, mockName("MockClientRegionFactory"));
AtomicReference<String> diskStoreName = new AtomicReference<>();
AtomicReference<Boolean> diskSynchronous = new AtomicReference<>(true);
AtomicReference<Class> keyConstraint = new AtomicReference<>(null);
AtomicReference<String> poolName = new AtomicReference<>();
AtomicReference<Class> valueConstraint = new AtomicReference<>(null);
when(mockClientRegionFactory.setDiskStoreName(anyString())).thenAnswer(
newSetter(String.class, diskStoreName, mockClientRegionFactory));
when(mockClientRegionFactory.setDiskSynchronous(anyBoolean())).thenAnswer(
newSetter(Boolean.TYPE, diskSynchronous, mockClientRegionFactory));
when(mockClientRegionFactory.setKeyConstraint(any(Class.class))).thenAnswer(
newSetter(Class.class, keyConstraint, mockClientRegionFactory));
when(mockClientRegionFactory.setPoolName(anyString())).thenAnswer(
newSetter(String.class, poolName, mockClientRegionFactory));
when(mockClientRegionFactory.setValueConstraint(any(Class.class))).thenAnswer(
newSetter(Class.class, valueConstraint, mockClientRegionFactory));
final RegionAttributes<K, V> mockRegionAttributes =
mock(RegionAttributes.class, mockName("MockClientRegionAttributes"));
when(mockRegionAttributes.getDataPolicy()).thenReturn(
ClientRegionShortcutWrapper.valueOf(shortcut).getDataPolicy());
when(mockRegionAttributes.getDiskStoreName()).thenAnswer(newGetter(diskStoreName));
when(mockRegionAttributes.isDiskSynchronous()).thenAnswer(newGetter(diskSynchronous));
when(mockRegionAttributes.getKeyConstraint()).thenAnswer(newGetter(keyConstraint));
when(mockRegionAttributes.getPoolName()).thenAnswer(newGetter(poolName));
when(mockRegionAttributes.getValueConstraint()).thenAnswer(newGetter(valueConstraint));
when(mockClientRegionFactory.create(anyString())).thenAnswer(new Answer<Region<K, V>>() {
@Override
public Region<K, V> answer(InvocationOnMock invocation) throws Throwable {
return mockRegion(invocation.getArgumentAt(0, String.class), mockRegionAttributes);
}
});
return mockClientRegionFactory;
}
/* (non-Javadoc) */
@SuppressWarnings("unchecked")
protected static <K, V> RegionAttributes<K, V> mockRegionAttributes(DataPolicy dataPolicy,
String diskStoreName, boolean diskSynchronous, boolean ignoreJta, Class<K> keyConstraint,
PartitionAttributes<K, V> partitionAttributes, String poolName, Scope scope, Class<V> valueConstraint) {
RegionAttributes<K, V> mockRegionAttributes = mock(RegionAttributes.class, mockName("MockRegionAttributes"));
when(mockRegionAttributes.getDataPolicy()).thenReturn(dataPolicy);
when(mockRegionAttributes.getDiskStoreName()).thenReturn(diskStoreName);
when(mockRegionAttributes.isDiskSynchronous()).thenReturn(diskSynchronous);
when(mockRegionAttributes.getIgnoreJTA()).thenReturn(ignoreJta);
when(mockRegionAttributes.getKeyConstraint()).thenReturn(keyConstraint);
when(mockRegionAttributes.getPartitionAttributes()).thenReturn(partitionAttributes);
when(mockRegionAttributes.getPoolName()).thenReturn(poolName);
when(mockRegionAttributes.getScope()).thenReturn(scope);
when(mockRegionAttributes.getValueConstraint()).thenReturn(valueConstraint);
return mockRegionAttributes;
}
/* (non-Javadoc) */
@SuppressWarnings("unchecked")
protected static <K, V> RegionFactory<K, V> mockRegionFactory(RegionAttributes<K, V> regionAttributes) {
RegionFactory<K, V> mockRegionFactory = mock(RegionFactory.class, mockName("MockRegionFactory"));
AtomicReference<DataPolicy> dataPolicy = new AtomicReference<>(regionAttributes.getDataPolicy());
AtomicReference<String> diskStoreName = new AtomicReference<>(regionAttributes.getDiskStoreName());
AtomicReference<Boolean> diskSynchronous = new AtomicReference<>(regionAttributes.isDiskSynchronous());
AtomicReference<Boolean> ignoreJta = new AtomicReference<>(regionAttributes.getIgnoreJTA());
AtomicReference<Class> keyConstraint = new AtomicReference<>(null);
AtomicReference<PartitionAttributes> partitionAttributes = new AtomicReference<>(
regionAttributes.getPartitionAttributes());
AtomicReference<Scope> scope = new AtomicReference<>(regionAttributes.getScope());
AtomicReference<Class> valueConstraint = new AtomicReference<>(null);
when(mockRegionFactory.setDataPolicy(any(DataPolicy.class))).thenAnswer(
newSetter(DataPolicy.class, dataPolicy, mockRegionFactory));
when(mockRegionFactory.setDiskStoreName(anyString())).thenAnswer(
newSetter(String.class, diskStoreName, mockRegionFactory));
when(mockRegionFactory.setDiskSynchronous(anyBoolean())).thenAnswer(
newSetter(Boolean.TYPE, diskSynchronous, mockRegionFactory));
when(mockRegionFactory.setIgnoreJTA(anyBoolean())).thenAnswer(
newSetter(Boolean.TYPE, ignoreJta, mockRegionFactory));
when(mockRegionFactory.setKeyConstraint(any(Class.class))).thenAnswer(
newSetter(Class.class, keyConstraint, mockRegionFactory));
when(mockRegionFactory.setPartitionAttributes(any(PartitionAttributes.class))).thenAnswer(
newSetter(PartitionAttributes.class, partitionAttributes, mockRegionFactory));
when(mockRegionFactory.setScope(any(Scope.class))).thenAnswer(
newSetter(Scope.class, scope, mockRegionFactory));
when(mockRegionFactory.setValueConstraint(any(Class.class))).thenAnswer(
newSetter(Class.class, valueConstraint, mockRegionFactory));
final RegionAttributes<K, V> mockRegionAttributes =
mock(RegionAttributes.class, mockName("MockRegionAttributes"));
when(mockRegionAttributes.getDataPolicy()).thenAnswer(newGetter(dataPolicy));
when(mockRegionAttributes.getDiskStoreName()).thenAnswer(newGetter(diskStoreName));
when(mockRegionAttributes.isDiskSynchronous()).thenAnswer(newGetter(diskSynchronous));
when(mockRegionAttributes.getIgnoreJTA()).thenAnswer(newGetter(ignoreJta));
when(mockRegionAttributes.getKeyConstraint()).thenAnswer(newGetter(keyConstraint));
when(mockRegionAttributes.getPartitionAttributes()).thenAnswer(newGetter(partitionAttributes));
when(mockRegionAttributes.getScope()).thenAnswer(newGetter(scope));
when(mockRegionAttributes.getValueConstraint()).thenAnswer(newGetter(valueConstraint));
when(mockRegionFactory.create(anyString())).thenAnswer(new Answer<Region<K, V>>() {
@Override
public Region<K, V> answer(InvocationOnMock invocation) throws Throwable {
return mockRegion(invocation.getArgumentAt(0, String.class), mockRegionAttributes);
}
});
return mockRegionFactory;
}
/* (non-Javadoc) */
@SuppressWarnings("unchecked")
protected static <K, V> Region<K, V> mockRegion(String name, RegionAttributes<K, V> regionAttributes) {
Region<K, V> mockRegion = mock(Region.class, mockName(name));
when(mockRegion.getName()).thenReturn(name);
when(mockRegion.getFullPath()).thenReturn(String.format("%1$s%2$s", Region.SEPARATOR, name));
when(mockRegion.getAttributes()).thenReturn(regionAttributes);
return mockRegion;
}
/* (non-Javadoc) */
protected static <R> Answer<R> newGetter(AtomicReference<R> returnValue) {
return invocation -> returnValue.get();
}
/* (non-Javadoc) */
protected static <T, R> Answer<R> newSetter(Class<T> parameterType, AtomicReference<T> argument, R returnValue) {
return invocation -> {
argument.set(invocation.getArgumentAt(0, parameterType));
return returnValue;
};
}
@Configuration
@SuppressWarnings("unused")
static abstract class ClientCacheConfiguration {
@Bean
ClientCache gemfireCache() {
return mockClientCache();
}
}
@Configuration
@SuppressWarnings("unused")
static abstract class ServerCacheConfiguration {
@Bean
Cache gemfireCache() {
return mockCache();
}
}
@EnableEntityDefinedRegions(basePackageClasses = NonEntity.class,
excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = ClientRegion.class),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = CollocatedPartitionRegionEntity.class) })
@SuppressWarnings("all")
static class AllServerPersistentEntitiesConfiguration extends ServerCacheConfiguration {
}
@EnableEntityDefinedRegions(basePackageClasses = NonEntity.class, strict = true,
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,
classes = { LocalRegion.class, PartitionRegion.class, ReplicateRegion.class }))
@SuppressWarnings("all")
static class ClientPersistentEntitiesConfiguration extends ClientCacheConfiguration {
}
@EnableEntityDefinedRegions(basePackageClasses = NonEntity.class,
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,
classes = { ClientRegion.class, LocalRegion.class, ReplicateRegion.class }))
@SuppressWarnings("all")
static class PeerPartitionRegionPersistentEntitiesConfiguration extends ServerCacheConfiguration {
@Bean @Lazy
DiskStore mockDiskStore() {
return mock(DiskStore.class, mockName("MockDiskStore"));
}
@Bean @Lazy
PartitionResolver mockPartitionResolver() {
return mock(PartitionResolver.class, mockName("MockPartitionResolver"));
}
}
}

View File

@@ -0,0 +1,37 @@
/*
* 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.config.annotation.test.entities;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.annotation.ClientRegion;
/**
* {@link ClientRegionEntity} persistent entity stored in the "Users" {@link org.apache.geode.cache.DataPolicy#NORMAL},
* client {@link org.apache.geode.cache.Region}.
*
* @author John Blum
* @since 1.9.0
*/
@ClientRegion(name = "Sessions", shortcut = ClientRegionShortcut.CACHING_PROXY)
public class ClientRegionEntity {
@Id
private String id;
}

View File

@@ -0,0 +1,38 @@
/*
* 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.config.annotation.test.entities;
import org.springframework.data.gemfire.mapping.annotation.PartitionRegion;
/**
* {@link CollocatedPartitionRegionEntity} persistent entity stored in the "ContactEvents"
* {@link org.apache.geode.cache.DataPolicy#PERSISTENT_PARTITION} {@link org.apache.geode.cache.Region}.
*
* @author John Blum
* @since 1.9.0
*/
@PartitionRegion(value = "ContactEvents", collocatedWith = "Customers", diskStoreName = "mockDiskStore",
diskSynchronous = false, ignoreJta = true, partitionResolverName = "mockPartitionResolver",
persistent = true, redundantCopies = 2)
public class CollocatedPartitionRegionEntity {
private String email;
private String phoneNumber;
}

View File

@@ -0,0 +1,38 @@
/*
* 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.config.annotation.test.entities;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.annotation.Region;
/**
* {@link GenericRegionEntity} persistent entity stored in the "GenericRegionEntity"
* {@link org.apache.geode.cache.DataPolicy#NORMAL}, {@link org.apache.geode.cache.Region}.
*
* @author John Blum
* @since 1.9.0
*/
@Region
public class GenericRegionEntity {
@Id
private Long id;
private String name;
}

View File

@@ -0,0 +1,36 @@
/*
* 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.config.annotation.test.entities;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.annotation.LocalRegion;
/**
* {@link LocalRegionEntity} persistent entity stored in the "LocalRegionEntity" {@link org.apache.geode.cache.DataPolicy#NORMAL},
* {@link org.apache.geode.cache.Scope#LOCAL} {@link org.apache.geode.cache.Region}.
*
* @author John Blum
* @since 1.9.0
*/
@LocalRegion
public class LocalRegionEntity {
@Id
private String id;
}

View File

@@ -0,0 +1,28 @@
/*
* 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.config.annotation.test.entities;
/**
* A non-persistent entity class.
*
* @author John Blum
* @since 1.9.0
*/
public class NonEntity {
}

View File

@@ -0,0 +1,44 @@
/*
* 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.config.annotation.test.entities;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.annotation.PartitionRegion;
/**
* {@link PartitionRegionEntity} persistent entity stored in the "Customers"
* {@link org.apache.geode.cache.DataPolicy#PERSISTENT_PARTITION} {@link org.apache.geode.cache.Region}.
*
* @author John Blum
* @since 1.9.0
*/
@PartitionRegion(name = "Customers", persistent = true, redundantCopies = 1,
fixedPartitions = {
@PartitionRegion.FixedPartition(name = "one", primary = true, numBuckets = 16),
@PartitionRegion.FixedPartition(name = "two", numBuckets = 21)
}
)
public class PartitionRegionEntity {
@Id
private Long id;
private String firstName;
private String lastName;
}

View File

@@ -0,0 +1,35 @@
/*
* 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.config.annotation.test.entities;
import org.springframework.data.gemfire.ScopeType;
import org.springframework.data.gemfire.mapping.annotation.ReplicateRegion;
/**
* {@link ReplicateRegionEntity} persistent entity stored in the "Accounts"
* {@link org.apache.geode.cache.DataPolicy#REPLICATE} {@link org.apache.geode.cache.Region}.
*
* @author John Blum
* @since 1.9.0
*/
@ReplicateRegion(name = "Accounts", scope = ScopeType.DISTRIBUTED_ACK)
public class ReplicateRegionEntity {
private String number;
}

View File

@@ -28,6 +28,7 @@ import java.math.BigInteger;
import org.junit.Test;
import org.springframework.data.gemfire.TestUtils;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.gemfire.mapping.model.GemfireSimpleTypeHolder;
import org.springframework.data.mapping.PersistentEntity;

View File

@@ -24,12 +24,13 @@ import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Test;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.util.ClassTypeInformation;
/**
* Unit tests for {@link GemfirePersistentEntity}.
*
*
* @author Oliver Gierke
* @author John Blum
*/

View File

@@ -17,7 +17,7 @@
package org.springframework.data.gemfire.repository.cdi;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.gemfire.repository.GemfireRepository;
import org.springframework.data.gemfire.repository.sample.Person;

View File

@@ -42,7 +42,7 @@ import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlReaderContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.gemfire.repository.GemfireRepository;
import org.springframework.data.gemfire.repository.support.GemfireRepositoryFactoryBean;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;

View File

@@ -17,17 +17,18 @@
package org.springframework.data.gemfire.repository.sample;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.gemfire.mapping.annotation.ReplicateRegion;
import org.springframework.util.Assert;
/**
* The Account class is an abstract data type (ADT) for modeling customer accounts.
*
* @author John Blum
* @see org.springframework.data.gemfire.mapping.Region
* @see Region
* @since 1.0.0
*/
@Region("Accounts")
@ReplicateRegion("Accounts")
@SuppressWarnings("unused")
public class Account {

View File

@@ -16,10 +16,10 @@
package org.springframework.data.gemfire.repository.sample;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
/**
*
*
* @author Oliver Gierke
*/
@Region("address")

View File

@@ -17,14 +17,14 @@
package org.springframework.data.gemfire.repository.sample;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
/**
* The Algorithm interface define abstract data type modeling a computer algorithm.
*
* @author John Blum
* @see org.springframework.data.annotation.Id
* @see org.springframework.data.gemfire.mapping.Region
* @see Region
* @since 1.4.0
*/
@Region("Algorithms")

View File

@@ -16,7 +16,7 @@
package org.springframework.data.gemfire.repository.sample;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.gemfire.repository.GemfireRepository;
import org.springframework.data.gemfire.repository.Query;

View File

@@ -17,7 +17,8 @@
package org.springframework.data.gemfire.repository.sample;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.gemfire.mapping.annotation.ReplicateRegion;
import org.springframework.util.ObjectUtils;
/**
@@ -25,11 +26,11 @@ import org.springframework.util.ObjectUtils;
*
* @author John Blum
* @see org.springframework.data.annotation.Id
* @see org.springframework.data.gemfire.mapping.Region
* @see Region
* @since 1.0.0
*/
@ReplicateRegion("Customers")
@SuppressWarnings("unused")
@Region("Customers")
public class Customer {
@Id

View File

@@ -16,7 +16,7 @@
package org.springframework.data.gemfire.repository.sample;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.gemfire.repository.GemfireRepository;
import org.springframework.data.gemfire.repository.Query;

View File

@@ -16,13 +16,13 @@
package org.springframework.data.gemfire.repository.sample;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
/**
* The GuestUser class represents an authorized restricted user of a service or computer system, etc.
*
* @author John Blum
* @see org.springframework.data.gemfire.mapping.Region
* @see Region
* @see org.springframework.data.gemfire.repository.sample.User
* @since 1.4.0
*/

View File

@@ -21,7 +21,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.util.ObjectUtils;
/**

View File

@@ -17,7 +17,7 @@
package org.springframework.data.gemfire.repository.sample;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.util.ObjectUtils;
/**
@@ -25,7 +25,7 @@ import org.springframework.util.ObjectUtils;
*
* @author John Blum
* @see org.springframework.data.annotation.Id
* @see org.springframework.data.gemfire.mapping.Region
* @see Region
* @since 1.4.0
*/
@Region("Plants")

View File

@@ -16,14 +16,14 @@
package org.springframework.data.gemfire.repository.sample;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.util.StringUtils;
/**
* The Programmer class is a User representing/modeling a software engineer/developer.
*
* @author John J. Blum
* @see org.springframework.data.gemfire.mapping.Region
* @see Region
* @see org.springframework.data.gemfire.repository.sample.User
* @since 1.4.0
*/

View File

@@ -16,7 +16,7 @@
package org.springframework.data.gemfire.repository.sample;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.gemfire.repository.GemfireRepository;
import org.springframework.data.gemfire.repository.Query;
@@ -25,7 +25,7 @@ import org.springframework.data.gemfire.repository.Query;
* from/to an underlying data store (GemFire).
*
* @author John Blum
* @see org.springframework.data.gemfire.mapping.Region
* @see Region
* @see org.springframework.data.gemfire.repository.GemfireRepository
* @see org.springframework.data.gemfire.repository.Query
* @since 1.4.0

View File

@@ -16,13 +16,13 @@
package org.springframework.data.gemfire.repository.sample;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
/**
* The RootUser class represents an authorized administrative user of a service or computer system, etc.
*
* @author John Blum
* @see org.springframework.data.gemfire.mapping.Region
* @see Region
* @see org.springframework.data.gemfire.repository.sample.User
* @since 1.4.0
*/

View File

@@ -19,7 +19,7 @@ package org.springframework.data.gemfire.repository.sample;
import java.util.Calendar;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
@@ -29,7 +29,7 @@ import org.springframework.util.ObjectUtils;
* @author John Blum
* @see java.lang.Comparable
* @see org.springframework.data.annotation.Id
* @see org.springframework.data.gemfire.mapping.Region
* @see Region
* @since 1.4.0
*/
@Region("Users")

View File

@@ -314,7 +314,7 @@ public class GemfireRepositoryFactoryUnitTests {
interface SampleCustomGemfireRepository extends GemfireRepository<Person, Long>, SampleCustomRepository<Person> {
}
@org.springframework.data.gemfire.mapping.Region("People")
@org.springframework.data.gemfire.mapping.annotation.Region("People")
interface PersonRepository extends GemfireRepository<Person, Long> {
}
}

View File

@@ -43,7 +43,7 @@ import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.CacheFactoryBean;
import org.springframework.data.gemfire.LocalRegionFactoryBean;
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.gemfire.repository.support.GemfireRepositoryFactoryBean;
import org.springframework.data.gemfire.test.support.IdentifierSequence;
import org.springframework.data.repository.CrudRepository;

View File

@@ -24,7 +24,7 @@ import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.gemfire.mapping.Region;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.gemfire.test.support.IdentifierSequence;
import org.springframework.data.gemfire.util.SpringUtils;
import org.springframework.util.Assert;

View File

@@ -33,7 +33,7 @@ import org.junit.Test;
public class ArrayUtilsUnitTests {
@Test
public void asArrayRetursEmptyArray() {
public void asArrayReturnsEmptyArray() {
Object[] array = ArrayUtils.asArray();
assertThat(array).isNotNull();
@@ -58,6 +58,34 @@ public class ArrayUtilsUnitTests {
assertThat(array).isEqualTo(new Object[] { 1 });
}
@Test
public void defaultIfEmptyWithNonNullNonEmptyArrayReturnsArray() {
Object[] array = { "test" };
Object[] defaultArray = { "tested" };
assertThat(ArrayUtils.defaultIfEmpty(array, defaultArray)).isSameAs(array);
}
@Test
public void defaultIfEmptyWithEmptyArrayReturnsDefaultArray() {
Object[] array = {};
Object[] defaultArray = { "tested" };
assertThat(ArrayUtils.defaultIfEmpty(array, defaultArray)).isSameAs(defaultArray);
}
@Test
public void defaultIfEmptyWithNullArrayReturnsDefaultArray() {
Object[] defaultArray = { "tested" };
assertThat(ArrayUtils.defaultIfEmpty(null, defaultArray)).isSameAs(defaultArray);
}
@Test
public void defaultIfEmptyWithNullArrayAndNullDefaultArrayReturnsNull() {
assertThat(ArrayUtils.defaultIfEmpty(null, null)).isNull();
}
@Test
public void getFirstWithNonNullArray() {
assertThat(ArrayUtils.getFirst(ArrayUtils.asArray(1, 2, 3))).isEqualTo(1);
@@ -187,6 +215,7 @@ public class ArrayUtilsUnitTests {
}
@Test
@SuppressWarnings("unchecked")
public void sortIsSuccessful() {
Comparable[] array = new Comparable[] { 2, 3, 1 };
Comparable[] sortedArray = ArrayUtils.sort(array);

View File

@@ -27,7 +27,9 @@ import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -59,6 +61,61 @@ public class CollectionUtilsUnitTests {
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void addAllIterableElementsToList() {
List<Integer> target = new ArrayList<Integer>(Arrays.asList(1, 2, 3));
Set<Integer> source = new HashSet<Integer>(Arrays.asList(1, 2, 3));
target = CollectionUtils.addAll(target, source);
assertThat(target).isNotNull();
assertThat(target.size()).isEqualTo(6);
assertThat(target).isEqualTo(Arrays.asList(1, 2, 3, 1, 2, 3));
}
@Test
public void addAllIterableElementsToSet() {
Set<Integer> target = new HashSet<Integer>(Arrays.asList(1, 2, 3));
Set<Integer> source = new HashSet<Integer>(Arrays.asList(1, 2, 3, 4, 5));
target = CollectionUtils.addAll(target, source);
assertThat(target).isNotNull();
assertThat(target.size()).isEqualTo(5);
assertThat(target).contains(1, 2, 3, 4, 5);
}
@Test
public void addIterableToNullCollection() {
exception.expect(IllegalArgumentException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage("Collection must not be null");
CollectionUtils.addAll(null, Collections.emptySet());
}
@Test
public void addEmptyIterableToCollection() {
Collection<Integer> target = new ArrayList<Integer>(Arrays.asList(1, 2, 3));
target = CollectionUtils.addAll(target, Collections.<Integer>emptyList());
assertThat(target).isNotNull();
assertThat(target.size()).isEqualTo(3);
assertThat(target).contains(1, 2, 3);
}
@Test
public void addNullIterableToCollection() {
Collection<Integer> target = new ArrayList<Integer>(Arrays.asList(1, 2, 3));
target = CollectionUtils.addAll(target, null);
assertThat(target).isNotNull();
assertThat(target.size()).isEqualTo(3);
assertThat(target).contains(1, 2, 3);
}
@Test
public void asSetContainsAllArrayElements() {
Object[] elements = { "a", "b", "c" };
@@ -67,7 +124,7 @@ public class CollectionUtilsUnitTests {
assertThat(set).isNotNull();
assertThat(set.size()).isEqualTo(elements.length);
assertThat(set).containsAll(Arrays.asList(elements));
assertThat(set.containsAll(Arrays.asList(elements))).isTrue();
}
@Test
@@ -78,7 +135,7 @@ public class CollectionUtilsUnitTests {
assertThat(set).isNotNull();
assertThat(set.size()).isEqualTo(2);
assertThat(set).containsAll(Arrays.asList(elements));
assertThat(set.containsAll(Arrays.asList(elements))).isTrue();
}
@Test(expected = UnsupportedOperationException.class)
@@ -99,6 +156,43 @@ public class CollectionUtilsUnitTests {
}
}
@Test
public void defaultIfEmptyWithNonNullNonEmptyIterableReturnsIterable() {
Iterable<Object> iterable = Collections.<Object>singleton(1);
Iterable<Object> defaultIterable = Collections.<Object>singleton(2);
assertThat(CollectionUtils.defaultIfEmpty(iterable, defaultIterable)).isSameAs(iterable);
}
@Test
public void defaultIfEmptyWithEmptyIterableReturnsDefault() {
Iterable<Object> iterable = Collections.emptySet();
Iterable<Object> defaultIterable = Collections.<Object>singleton(2);
assertThat(CollectionUtils.defaultIfEmpty(iterable, defaultIterable)).isSameAs(defaultIterable);
}
@Test
public void defaultIfEmptyWithNullIterableReturnsDefault() {
Iterable<?> defaultIterable = Collections.singleton(2);
assertThat(CollectionUtils.defaultIfEmpty(null, defaultIterable)).isSameAs(defaultIterable);
}
@Test
public void defaultIfEmptyWithNullIterableAndNullDefaultReturnsNull() {
assertThat(CollectionUtils.defaultIfEmpty((Iterable<?>) null, null)).isNull();
}
@Test
public void emptyIterableReturnsEmptyIterable() {
Iterable<?> iterable = CollectionUtils.emptyIterable();
assertThat(iterable).isNotNull();
assertThat(iterable.iterator()).isNotNull();
assertThat(iterable.iterator().hasNext()).isFalse();
}
@Test
@SuppressWarnings("unchecked")
public void iterableEnumeration() {