SGF-623 - Polish.

This commit is contained in:
John Blum
2017-05-03 11:19:17 -07:00
parent 5391b7b5f0
commit 589b558dd1
15 changed files with 92 additions and 72 deletions

View File

@@ -13,17 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.repository;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.CrudRepository;
/**
* Gemfire-specific extension of the {@link CrudRepository} interface.
* GemFire specific extension of the Spring Data {@link CrudRepository} interface.
*
* @author Oliver Gierke
* @author John Blum
* @see java.io.Serializable
* @see org.springframework.data.repository.CrudRepository
*/
public interface GemfireRepository<T, ID> extends CrudRepository<T, ID> {

View File

@@ -13,14 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.repository;
import lombok.NonNull;
import lombok.Value;
/**
* Simple value object to hold an entity alongside an external key the entity shall be stored under.
*
* Simple value object holding an entity along with the external key in which the entity will be mapped.
*
* @author Oliver Gierke
*/
@Value
@@ -28,4 +29,5 @@ public class Wrapper<T, KEY> {
T entity;
@NonNull KEY key;
}

View File

@@ -21,8 +21,8 @@ import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.repository.core.support.PersistentEntityInformation;
/**
* Implementation of {@link GemfireEntityInformation} to return the region name stored in the backing
* {@link PersistentEntity}.
* Implementation of {@link GemfireEntityInformation} and Spring Data's {@link PersistentEntityInformation}
* that returns the Region name associated with the {@link PersistentEntity}.
*
* @author Oliver Gierke
* @author John Blum
@@ -53,5 +53,4 @@ public class DefaultGemfireEntityInformation<T, ID> extends PersistentEntityInfo
public String getRegionName() {
return persistentEntity.getRegionName();
}
}

View File

@@ -13,15 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.repository.query;
import org.apache.geode.cache.Region;
import org.springframework.data.repository.core.EntityInformation;
/**
* {@link EntityInformation} to capture Gemfire specific information.
* {@link EntityInformation} capturing GemFire specific information.
*
* @author Oliver Gierke
* @see org.springframework.data.repository.core.EntityInformation
*/
public interface GemfireEntityInformation<T, ID> extends EntityInformation<T, ID> {
@@ -31,4 +33,5 @@ public interface GemfireEntityInformation<T, ID> extends EntityInformation<T, ID
* @return the name of the {@link Region} the entity is held in.
*/
String getRegionName();
}

View File

@@ -16,7 +16,8 @@
package org.springframework.data.gemfire.repository.support;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.*;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
import java.io.Serializable;
import java.lang.reflect.Method;
@@ -82,9 +83,10 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
@Override
@SuppressWarnings("unchecked")
public <T, ID> GemfireEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
GemfirePersistentEntity<T> entity = (GemfirePersistentEntity<T>) mappingContext.getPersistentEntity(domainClass)
.orElseThrow(() -> newIllegalArgumentException("Unable to resolve PersistentEntity for type [%s]", domainClass));
.orElseThrow(() -> newIllegalArgumentException("Unable to resolve PersistentEntity for type [%s]",
domainClass));
return new DefaultGemfireEntityInformation<>(entity);
}
@@ -95,8 +97,9 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
*/
@Override
protected Object getTargetRepository(RepositoryInformation repositoryInformation) {
GemfireEntityInformation<?, Serializable> entityInformation = getEntityInformation(
repositoryInformation.getDomainType());
GemfireEntityInformation<?, Serializable> entityInformation =
getEntityInformation(repositoryInformation.getDomainType());
GemfireTemplate gemfireTemplate = getTemplate(repositoryInformation);
@@ -104,20 +107,21 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
}
GemfireTemplate getTemplate(RepositoryMetadata metadata) {
GemfirePersistentEntity<?> entity = mappingContext.getPersistentEntity(metadata.getDomainType())
.orElseThrow(() -> newIllegalArgumentException("Unable to resolve PersistentEntity for type [%s]",
metadata.getDomainType()));
String entityRegionName = entity.getRegionName();
String repositoryRegionName = getRepositoryRegionName(metadata.getRepositoryInterface());
String regionName = (StringUtils.hasText(repositoryRegionName) ? repositoryRegionName : entityRegionName);
String resolvedRegionName = StringUtils.hasText(repositoryRegionName) ? repositoryRegionName : entityRegionName;
Region<?, ?> region = regions.getRegion(regionName);
Region<?, ?> region = regions.getRegion(resolvedRegionName);
if (region == null) {
throw new IllegalStateException(String.format("No Region '%1$s' found for domain class %2$s;"
throw newIllegalStateException("No Region [%1$s] was found for domain class [%2$s];"
+ " Make sure you have configured a GemFire Region of that name in your application context",
regionName, metadata.getDomainType().getName()));
resolvedRegionName, metadata.getDomainType().getName());
}
Class<?> regionKeyType = region.getAttributes().getKeyConstraint();
@@ -125,7 +129,7 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
if (regionKeyType != null && entity.getIdProperty() != null) {
Assert.isTrue(regionKeyType.isAssignableFrom(entityIdType), String.format(
"The Region referenced only supports keys of type %1$s, but the entity to be stored has an id of type %2$s",
"The Region referenced only supports keys of type [%1$s], but the entity to be stored has an id of type [%2$s]",
regionKeyType.getName(), entityIdType.getName()));
}

View File

@@ -16,7 +16,6 @@
package org.springframework.data.gemfire.repository.support;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@@ -54,7 +53,7 @@ import org.springframework.util.Assert;
* @see org.apache.geode.cache.Cache
* @see org.apache.geode.cache.Region
*/
public class SimpleGemfireRepository<T, ID extends Serializable> implements GemfireRepository<T, ID> {
public class SimpleGemfireRepository<T, ID> implements GemfireRepository<T, ID> {
private final EntityInformation<T, ID> entityInformation;
@@ -80,7 +79,6 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
*/
@Override
public <U extends T> U save(U entity) {
ID id = entityInformation.getRequiredId(entity);
template.put(id, entity);
@@ -94,13 +92,10 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
*/
@Override
public <U extends T> Iterable<U> saveAll(Iterable<U> entities) {
Map<ID, U> entitiesToSave = new HashMap<>();
entities.forEach(entity -> {
entitiesToSave.put(entityInformation.getRequiredId(entity), entity);
});
entities.forEach(entity -> entitiesToSave.put(entityInformation.getRequiredId(entity), entity));
template.putAll(entitiesToSave);
return entitiesToSave.values();
@@ -113,7 +108,9 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
@Override
public T save(Wrapper<T, ID> wrapper) {
T entity = wrapper.getEntity();
template.put(wrapper.getKey(), entity);
return entity;
}
@@ -180,10 +177,9 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
*/
@Override
public Collection<T> findAllById(Iterable<ID> ids) {
List<ID> parameters = Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList());
List<ID> keys = Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList());
return CollectionUtils.<ID, T>nullSafeMap(template.getAll(parameters)).values().stream()
return CollectionUtils.<ID, T>nullSafeMap(template.getAll(keys)).values().stream()
.filter(Objects::nonNull).collect(Collectors.toList());
}

View File

@@ -10,9 +10,10 @@
* 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.client;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;

View File

@@ -16,7 +16,7 @@
package org.springframework.data.gemfire.repository.sample;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Optional;
@@ -27,20 +27,20 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* The AnimalRepositoryTest class is a test suite of test cases testing the functionality behind PR #55 involving
* persisting application domain object/entities to multiple Regions in GemFire's Cache.
* Integration test testing the functionality behind PR #55 involving persisting application domain object/entities
* to multiple Regions in a GemFire Cache.
*
* @author Stuart Williams
* @author John Blum
* @see org.junit.Test
* @see org.junit.runner.RunWith
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
* @since 1.4.0
* @see org.springframework.test.context.junit4.SpringRunner
* @link https://github.com/spring-projects/spring-data-gemfire/pull/55
* @since 1.4.0
*/
@ContextConfiguration
@RunWith(SpringRunner.class)
@ContextConfiguration
@SuppressWarnings("unused")
public class AnimalRepositoryTest {
@@ -58,7 +58,7 @@ public class AnimalRepositoryTest {
}
@Test
public void testEntityStoredInMultipleRegions() {
public void entityStoredInMultipleRegionsIsSuccessful() {
Animal felix = newAnimal(1, "Felix");
Animal leo = newAnimal(2, "Leo");
Animal cerberus = newAnimal(3, "Cerberus");

View File

@@ -39,15 +39,16 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
*/
public class IncompatibleRegionKeyEntityIdAnimalRepositoryTest {
protected static final String APPLICATION_CONTEXT_CONFIG_LOCATION = String.format("%1$s%2$s%1$s%3$s",
private static final String APPLICATION_CONTEXT_CONFIG_LOCATION = String.format("%1$s%2$s%1$s%3$s",
File.separator, AnimalRepositoryTest.class.getPackage().getName().replace('.', File.separatorChar),
"IncompatibleRegionKeyEntityIdAnimalRepositoryTest-context.xml");
String.format("%s-context.xml", IncompatibleRegionKeyEntityIdAnimalRepositoryTest.class.getSimpleName()));
@Test(expected = IllegalArgumentException.class)
public void storeAnimalHavingLongIdInRabbitsRegionWithStringKey() {
try {
ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext(
APPLICATION_CONTEXT_CONFIG_LOCATION);
ConfigurableApplicationContext applicationContext =
new ClassPathXmlApplicationContext(APPLICATION_CONTEXT_CONFIG_LOCATION);
applicationContext.getBean(RabbitRepository.class);
}
// NOTE the ClassCastException thrown from GemFire is unexpected; this is not correct and the identifying type
@@ -62,8 +63,9 @@ public class IncompatibleRegionKeyEntityIdAnimalRepositoryTest {
catch (BeanCreationException expected) {
//expected.printStackTrace(System.err);
assertTrue(expected.getCause() instanceof IllegalArgumentException);
assertEquals(String.format("The Region referenced only supports keys of type %1$s, but the entity to be stored has an id of type %2$s",
assertEquals(String.format("The Region referenced only supports keys of type [%1$s], but the entity to be stored has an id of type [%2$s]",
String.class.getName(), Long.class.getName()), expected.getCause().getMessage());
throw (IllegalArgumentException) expected.getCause();
}
}

View File

@@ -38,15 +38,16 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
*/
public class PlantRepositoryTest {
protected static final String APPLICATION_CONTEXT_CONFIG_LOCATION = String.format("%1$s%2$s%1$s%3$s",
private static final String APPLICATION_CONTEXT_CONFIG_LOCATION = String.format("%1$s%2$s%1$s%3$s",
File.separator, PlantRepositoryTest.class.getPackage().getName().replace('.', File.separatorChar),
"PlantRepositoryTest-context.xml");
@Test(expected = IllegalArgumentException.class)
public void storePlantHavingStringIdInPlantsRegionWithLongKey() {
try {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
APPLICATION_CONTEXT_CONFIG_LOCATION);
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext(APPLICATION_CONTEXT_CONFIG_LOCATION);
context.getBean(PlantRepository.class);
}
// NOTE technically, the IllegalArgumentException for incompatible Region 'Key' and Entity ID is thrown
@@ -54,8 +55,9 @@ public class PlantRepositoryTest {
catch (BeanCreationException expected) {
//expected.printStackTrace(System.err);
assertTrue(expected.getCause() instanceof IllegalArgumentException);
assertEquals(String.format("The Region referenced only supports keys of type %1$s, but the entity to be stored has an id of type %2$s",
assertEquals(String.format("The Region referenced only supports keys of type [%1$s], but the entity to be stored has an id of type [%2$s]",
Long.class.getName(), String.class.getName()), expected.getCause().getMessage());
throw (IllegalArgumentException) expected.getCause();
}
}

View File

@@ -17,7 +17,10 @@
package org.springframework.data.gemfire.repository.sample;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Collections;
@@ -34,11 +37,10 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.gemfire.repository.Wrapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
/**
* The SubRegionRepositoryTest class is a test suite of test cases testing the use of GemFire Repositories on GemFire
* Cache Subregions.
* Integration tests testing the use of GemFire Repositories on GemFire Cache Subregions.
*
* @author John Blum
* @see org.junit.Test
@@ -53,8 +55,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @link https://jira.springsource.org/browse/SGF-252
* @since 1.4.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration("subregionRepository.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@SuppressWarnings("unused")
public class SubRegionRepositoryIntegrationTest {

View File

@@ -221,7 +221,7 @@ public class GemfireRepositoryFactoryUnitTests {
exception.expect(IllegalArgumentException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage(String.format(
"The Region referenced only supports keys of type %1$s, but the entity to be stored has an id of type %2$s",
"The Region referenced only supports keys of type [%1$s], but the entity to be stored has an id of type [%2$s]",
String.class.getName(), Long.class.getName()));
gemfireRepositoryFactory.getTemplate(mockRepositoryMetadata);
@@ -248,7 +248,7 @@ public class GemfireRepositoryFactoryUnitTests {
exception.expect(IllegalStateException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage(String.format(
"No Region 'People' found for domain class %s; Make sure you have configured a GemFire Region of that name in your application context",
"No Region [People] was found for domain class [%s]; Make sure you have configured a GemFire Region of that name in your application context",
Person.class.getName()));
gemfireRepositoryFactory.getTemplate(mockRepositoryMetadata);

View File

@@ -16,7 +16,7 @@
package org.springframework.data.gemfire.repository.support;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.Collection;
@@ -58,7 +58,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@SuppressWarnings("unused")
public class SimpleGemfireRepositoryIntegrationTests {
protected static final String DEFAULT_GEMFIRE_LOG_LEVEL = "warning";
static final String DEFAULT_GEMFIRE_LOG_LEVEL = "warning";
@Autowired
private GemfireTemplate template;
@@ -177,7 +177,7 @@ public class SimpleGemfireRepositoryIntegrationTests {
@SuppressWarnings("rawtypes")
public static class RegionClearListener extends CacheListenerAdapter {
public volatile boolean eventFired;
volatile boolean eventFired;
@Override
public void afterRegionClear(RegionEvent ev) {

View File

@@ -16,7 +16,10 @@
package org.springframework.data.gemfire.repository.support;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.Serializable;
import java.util.ArrayList;
@@ -36,16 +39,15 @@ import org.springframework.data.gemfire.repository.GemfireRepository;
import org.springframework.data.gemfire.repository.sample.Customer;
import org.springframework.data.repository.core.support.ReflectionEntityInformation;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
/**
* The SimpleGemfireRepositoryTransactionalIntegrationTest class is a test suite of test cases testing
* the SimpleGemfireRepository class and SDC Repository abstraction implementation in the context of
* GemFire "Cache" Transactions.
* Integration tests testing the {@link SimpleGemfireRepository} class and SDC Repository abstraction implementation
* in the context of GemFire "Cache" Transactions.
*
* @author John Blum
* @see org.junit.Test
@@ -55,14 +57,14 @@ import org.springframework.transaction.support.TransactionTemplate;
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
* @since 1.6.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(SpringRunner.class)
@ContextConfiguration
@SuppressWarnings("unused")
public class SimpleGemfireRepositoryTransactionalIntegrationTest {
// TODO add additional test cases for SimpleGemfireRepository (Region operations) in the presence of Transactions!!!
protected static final AtomicLong ID_SEQUENCE = new AtomicLong(0l);
static final AtomicLong ID_SEQUENCE = new AtomicLong(0L);
@Autowired
private CustomerService customerService;
@@ -70,7 +72,7 @@ public class SimpleGemfireRepositoryTransactionalIntegrationTest {
@Resource(name = "Customers")
private Region customers;
protected static Customer createCustomer(final String firstName, final String lastName) {
static Customer createCustomer(String firstName, String lastName) {
Customer customer = new SerializableCustomer(firstName, lastName);
customer.setId(ID_SEQUENCE.incrementAndGet());
return customer;
@@ -146,7 +148,7 @@ public class SimpleGemfireRepositoryTransactionalIntegrationTest {
transactionTemplate = new TransactionTemplate(transactionManager);
}
public void saveAll(final Iterable<Customer> customers) {
void saveAll(final Iterable<Customer> customers) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override protected void doInTransactionWithoutResult(final TransactionStatus status) {
customerRepository.saveAll(customers);
@@ -154,7 +156,7 @@ public class SimpleGemfireRepositoryTransactionalIntegrationTest {
});
}
public void removeAllCausingTransactionRollback() {
void removeAllCausingTransactionRollback() {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override protected void doInTransactionWithoutResult(final TransactionStatus status) {
removeAll();
@@ -163,7 +165,7 @@ public class SimpleGemfireRepositoryTransactionalIntegrationTest {
});
}
public void removeAll() {
void removeAll() {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override protected void doInTransactionWithoutResult(final TransactionStatus status) {
customerRepository.deleteAll();
@@ -171,5 +173,4 @@ public class SimpleGemfireRepositoryTransactionalIntegrationTest {
});
}
}
}

View File

@@ -16,11 +16,19 @@
package org.springframework.data.gemfire.repository.support;
import static org.assertj.core.api.Assertions.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
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.util.ArrayList;
import java.util.Arrays;