SGF-343 - Optimize the SDG implementation of CrudRepository.save(Iterable<S> enttiies) to use GemFire's Region.putAll(Map<K, V> values) operation.
This commit is contained in:
@@ -3,7 +3,9 @@ package org.springframework.data.gemfire.repository.support;
|
|||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.data.gemfire.GemfireCallback;
|
import org.springframework.data.gemfire.GemfireCallback;
|
||||||
@@ -18,10 +20,15 @@ import com.gemstone.gemfire.cache.Region;
|
|||||||
import com.gemstone.gemfire.cache.query.SelectResults;
|
import com.gemstone.gemfire.cache.query.SelectResults;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic repository implementation.
|
* Basic Repository implementation for GemFire.
|
||||||
*
|
*
|
||||||
* @author Oliver Gierke
|
* @author Oliver Gierke
|
||||||
* @author David Turanski
|
* @author David Turanski
|
||||||
|
* @author John Blum
|
||||||
|
* @see java.io.Serializable
|
||||||
|
* @see org.springframework.data.gemfire.GemfireTemplate
|
||||||
|
* @see org.springframework.data.gemfire.repository.GemfireRepository
|
||||||
|
* @see com.gemstone.gemfire.cache.Region
|
||||||
*/
|
*/
|
||||||
public class SimpleGemfireRepository<T, ID extends Serializable> implements GemfireRepository<T, ID> {
|
public class SimpleGemfireRepository<T, ID extends Serializable> implements GemfireRepository<T, ID> {
|
||||||
|
|
||||||
@@ -36,7 +43,6 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
|
|||||||
* @param entityInformation must not be {@literal null}.
|
* @param entityInformation must not be {@literal null}.
|
||||||
*/
|
*/
|
||||||
public SimpleGemfireRepository(GemfireTemplate template, EntityInformation<T, ID> entityInformation) {
|
public SimpleGemfireRepository(GemfireTemplate template, EntityInformation<T, ID> entityInformation) {
|
||||||
|
|
||||||
Assert.notNull(template);
|
Assert.notNull(template);
|
||||||
Assert.notNull(entityInformation);
|
Assert.notNull(entityInformation);
|
||||||
|
|
||||||
@@ -58,41 +64,63 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
|
|||||||
/*
|
/*
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
*
|
*
|
||||||
* @see
|
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
|
||||||
* org.springframework.data.repository.CrudRepository#save(java.lang.Iterable
|
|
||||||
* )
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public <U extends T> Iterable<U> save(Iterable<U> entities) {
|
public <U extends T> Iterable<U> save(Iterable<U> entities) {
|
||||||
List<U> result = new ArrayList<U>();
|
Map<ID, U> result = new HashMap<ID, U>();
|
||||||
|
|
||||||
for (U entity : entities) {
|
for (U entity : entities) {
|
||||||
result.add(save(entity));
|
result.put(entityInformation.getId(entity), entity);
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
|
template.putAll(result);
|
||||||
|
|
||||||
|
return result.values();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* (non-Javadoc)
|
||||||
|
*
|
||||||
|
* @see
|
||||||
|
* org.springframework.data.gemfire.repository.GemfireRepository#save(
|
||||||
|
* org.springframework.data.gemfire.repository.Wrapper)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public T save(Wrapper<T, ID> wrapper) {
|
||||||
|
return template.put(wrapper.getKey(), wrapper.getEntity());
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* (non-Javadoc)
|
||||||
|
*
|
||||||
|
* @see org.springframework.data.repository.CrudRepository#count()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public long count() {
|
||||||
|
SelectResults<Integer> results = template.find("SELECT count(*) FROM " + template.getRegion().getFullPath());
|
||||||
|
return (long) results.iterator().next();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
*
|
*
|
||||||
* @see org.springframework.data.repository.CrudRepository#findOne(java.io.
|
* @see org.springframework.data.repository.CrudRepository#exists(java.io.Serializable)
|
||||||
* Serializable)
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean exists(ID id) {
|
||||||
|
return (findOne(id) != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* (non-Javadoc)
|
||||||
|
*
|
||||||
|
* @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable)
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public T findOne(ID id) {
|
public T findOne(ID id) {
|
||||||
Object object = template.get(id);
|
return (T) template.get(id);
|
||||||
return (T) object;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
*
|
|
||||||
* @see org.springframework.data.repository.CrudRepository#exists(java.io.
|
|
||||||
* Serializable)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean exists(ID id) {
|
|
||||||
return findOne(id) != null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -102,13 +130,13 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Collection<T> findAll() {
|
public Collection<T> findAll() {
|
||||||
SelectResults<T> results = template.find("select * from " + template.getRegion().getFullPath());
|
SelectResults<T> results = template.find("SELECT * FROM " + template.getRegion().getFullPath());
|
||||||
return results.asList();
|
return results.asList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
* @see org.springframework.data.gemfire.repository.GemfireRepository.sor(:org.springframework.data.domain.Sort)
|
* @see org.springframework.data.gemfire.repository.GemfireRepository.sort(:org.springframework.data.domain.Sort)
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Iterable<T> findAll(final Sort sort) {
|
public Iterable<T> findAll(final Sort sort) {
|
||||||
@@ -124,15 +152,13 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
|
|||||||
/*
|
/*
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
*
|
*
|
||||||
* @see
|
* @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
|
||||||
* org.springframework.data.repository.CrudRepository#findAll(java.lang.
|
|
||||||
* Iterable)
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Collection<T> findAll(Iterable<ID> ids) {
|
public Collection<T> findAll(Iterable<ID> ids) {
|
||||||
|
|
||||||
List<ID> parameters = new ArrayList<ID>();
|
List<ID> parameters = new ArrayList<ID>();
|
||||||
|
|
||||||
for (ID id : ids) {
|
for (ID id : ids) {
|
||||||
parameters.add(id);
|
parameters.add(id);
|
||||||
}
|
}
|
||||||
@@ -142,33 +168,30 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
*
|
*
|
||||||
* @see org.springframework.data.repository.CrudRepository#count()
|
* @see org.springframework.data.repository.CrudRepository#delete(java.io.Serializable)
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public long count() {
|
public void delete(ID id) {
|
||||||
SelectResults<Integer> results = template.find("select count(*) from " + template.getRegion().getFullPath());
|
template.remove(id);
|
||||||
return (long)results.iterator().next();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
*
|
*
|
||||||
* @see
|
* @see
|
||||||
* org.springframework.data.repository.CrudRepository#delete(java.lang.Object
|
* org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
|
||||||
* )
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void delete(T entity) {
|
public void delete(T entity) {
|
||||||
template.remove(entityInformation.getId(entity));
|
delete(entityInformation.getId(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
*
|
*
|
||||||
* @see
|
* @see
|
||||||
* org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable
|
* org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
|
||||||
* )
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void delete(Iterable<? extends T> entities) {
|
public void delete(Iterable<? extends T> entities) {
|
||||||
@@ -184,7 +207,6 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void deleteAll() {
|
public void deleteAll() {
|
||||||
|
|
||||||
template.execute(new GemfireCallback<Void>() {
|
template.execute(new GemfireCallback<Void>() {
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
@@ -192,7 +214,8 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
|
|||||||
//clear() does not work for partitioned regions
|
//clear() does not work for partitioned regions
|
||||||
try {
|
try {
|
||||||
region.clear();
|
region.clear();
|
||||||
} catch (UnsupportedOperationException e) {
|
}
|
||||||
|
catch (UnsupportedOperationException e) {
|
||||||
for (Object key : region.keySet()) {
|
for (Object key : region.keySet()) {
|
||||||
region.remove(key);
|
region.remove(key);
|
||||||
}
|
}
|
||||||
@@ -203,26 +226,4 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
*
|
|
||||||
* @see org.springframework.data.repository.CrudRepository#delete(java.io.
|
|
||||||
* Serializable)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void delete(ID id) {
|
|
||||||
template.remove(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
*
|
|
||||||
* @see
|
|
||||||
* org.springframework.data.gemfire.repository.GemfireRepository#save(org
|
|
||||||
* .springframework.data.gemfire.repository.Wrapper)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public T save(Wrapper<T, ID> wrapper) {
|
|
||||||
return template.put(wrapper.getKey(), wrapper.getEntity());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,10 +58,11 @@ import com.gemstone.gemfire.cache.query.SelectResults;
|
|||||||
import com.gemstone.gemfire.cache.query.TypeMismatchException;
|
import com.gemstone.gemfire.cache.query.TypeMismatchException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The GemfireTemplateTest class is a test suite of test case testing the contract and functionality of the
|
* The GemfireTemplateTest class is a test suite of test cases testing the contract and functionality of the SDG
|
||||||
* GemfirTemplate class.
|
* GemfireTemplate class.
|
||||||
*
|
*
|
||||||
* @author Costin Leau
|
* @author Costin Leau
|
||||||
|
* @author David Turanski
|
||||||
* @author John Blum
|
* @author John Blum
|
||||||
* @see org.junit.Test
|
* @see org.junit.Test
|
||||||
* @see org.junit.runner.RunWith
|
* @see org.junit.runner.RunWith
|
||||||
@@ -69,8 +70,8 @@ import com.gemstone.gemfire.cache.query.TypeMismatchException;
|
|||||||
* @see org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer
|
* @see org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer
|
||||||
* @see org.springframework.test.context.ContextConfiguration
|
* @see org.springframework.test.context.ContextConfiguration
|
||||||
*/
|
*/
|
||||||
@ContextConfiguration(locations="basic-template.xml", initializers=GemfireTestApplicationContextInitializer.class)
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(locations="basic-template.xml", initializers=GemfireTestApplicationContextInitializer.class)
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class GemfireTemplateTest {
|
public class GemfireTemplateTest {
|
||||||
|
|
||||||
|
|||||||
@@ -19,13 +19,13 @@ import static org.hamcrest.Matchers.hasItems;
|
|||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.hamcrest.Matchers.not;
|
import static org.hamcrest.Matchers.not;
|
||||||
import static org.hamcrest.Matchers.nullValue;
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -48,6 +48,9 @@ import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
|
|||||||
* Integration tests for {@link SimpleGemfireRepository}.
|
* Integration tests for {@link SimpleGemfireRepository}.
|
||||||
*
|
*
|
||||||
* @author Oliver Gierke
|
* @author Oliver Gierke
|
||||||
|
* @author John Blum
|
||||||
|
* @see org.junit.Test
|
||||||
|
* @see org.springframework.data.gemfire.repository.support.SimpleGemfireRepository
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration("../../basic-template.xml")
|
@ContextConfiguration("../../basic-template.xml")
|
||||||
@@ -63,8 +66,8 @@ public class SimpleGemfireRepositoryIntegrationTest {
|
|||||||
|
|
||||||
RegionClearListener regionClearListener;
|
RegionClearListener regionClearListener;
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Before
|
@Before
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
simpleRegion.clear();
|
simpleRegion.clear();
|
||||||
regionClearListener = new RegionClearListener();
|
regionClearListener = new RegionClearListener();
|
||||||
@@ -128,6 +131,24 @@ public class SimpleGemfireRepositoryIntegrationTest {
|
|||||||
assertThat(result, not(hasItems(dave)));
|
assertThat(result, not(hasItems(dave)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSaveEntities() {
|
||||||
|
assertTrue(template.getRegion().isEmpty());
|
||||||
|
|
||||||
|
Person johnBlum = new Person(1l, "John", "Blum");
|
||||||
|
Person jonBloom = new Person(2l, "Jon", "Bloom");
|
||||||
|
Person juanBlume = new Person(3l, "Juan", "Blume");
|
||||||
|
|
||||||
|
repository.save(Arrays.asList(johnBlum, jonBloom, juanBlume));
|
||||||
|
|
||||||
|
assertFalse(template.getRegion().isEmpty());
|
||||||
|
assertEquals(3, template.getRegion().size());
|
||||||
|
|
||||||
|
assertEquals(johnBlum, template.get(johnBlum.id));
|
||||||
|
assertEquals(jonBloom, template.get(jonBloom.id));
|
||||||
|
assertEquals(juanBlume, template.get(juanBlume.id));
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
public static class RegionClearListener extends CacheListenerAdapter {
|
public static class RegionClearListener extends CacheListenerAdapter {
|
||||||
public boolean eventFired;
|
public boolean eventFired;
|
||||||
@@ -137,4 +158,5 @@ public class SimpleGemfireRepositoryIntegrationTest {
|
|||||||
eventFired = true;
|
eventFired = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,303 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2013 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.repository.support;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Mockito.doAnswer;
|
||||||
|
import static org.mockito.Mockito.doThrow;
|
||||||
|
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.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
import org.mockito.stubbing.Answer;
|
||||||
|
import org.springframework.data.gemfire.GemfireTemplate;
|
||||||
|
import org.springframework.data.gemfire.repository.Wrapper;
|
||||||
|
import org.springframework.data.gemfire.repository.sample.Animal;
|
||||||
|
import org.springframework.data.gemfire.test.support.CollectionUtils;
|
||||||
|
import org.springframework.data.repository.core.EntityInformation;
|
||||||
|
|
||||||
|
import com.gemstone.gemfire.cache.Region;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The SimpleGemfireRepositoryUnitTest class is a test suite of test cases testing the contract and functionality
|
||||||
|
* of the SimpleGemfireRepository class.
|
||||||
|
*
|
||||||
|
* @author John Blum
|
||||||
|
* @see org.junit.Test
|
||||||
|
* @see org.mockito.Mockito
|
||||||
|
* @see org.springframework.data.gemfire.repository.support.SimpleGemfireRepository
|
||||||
|
* @since 1.4.5
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public class SimpleGemfireRepositoryUnitTest {
|
||||||
|
|
||||||
|
protected Map<Long, Animal> asMap(Iterable<Animal> animals) {
|
||||||
|
Map<Long, Animal> animalMap = new HashMap<Long, Animal>();
|
||||||
|
for (Animal animal : animals) {
|
||||||
|
animalMap.put(animal.getId(), animal);
|
||||||
|
}
|
||||||
|
return animalMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Animal createAnimal(final String name) {
|
||||||
|
Animal animal = new Animal();
|
||||||
|
animal.setName(name);
|
||||||
|
return animal;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Animal createAnimal(final Long id, final String name) {
|
||||||
|
Animal animal = createAnimal(name);
|
||||||
|
animal.setId(id);
|
||||||
|
return animal;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected GemfireTemplate createGemfireTemplate(final Region<?, ?> region) {
|
||||||
|
return new GemfireTemplate(region);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected EntityInformation<Animal, Long> mockEntityInformation() {
|
||||||
|
EntityInformation<Animal, Long> mockEntityInformation = mock(EntityInformation.class);
|
||||||
|
|
||||||
|
doAnswer(new Answer<Long>() {
|
||||||
|
private final AtomicLong idSequence = new AtomicLong(0l);
|
||||||
|
@Override public Long answer(final InvocationOnMock invocation) throws Throwable {
|
||||||
|
Animal argument = Animal.class.cast(invocation.getArguments()[0]);
|
||||||
|
Long id = argument.getId();
|
||||||
|
id = (id != null ? id : idSequence.incrementAndGet());
|
||||||
|
argument.setId(id);
|
||||||
|
return id;
|
||||||
|
|
||||||
|
}
|
||||||
|
}).when(mockEntityInformation).getId(any(Animal.class));
|
||||||
|
|
||||||
|
return mockEntityInformation;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSave() {
|
||||||
|
Region<Long, Animal> mockRegion = mock(Region.class, "testSave");
|
||||||
|
|
||||||
|
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
|
||||||
|
createGemfireTemplate(mockRegion), mockEntityInformation());
|
||||||
|
|
||||||
|
Animal dog = repository.save(createAnimal("dog"));
|
||||||
|
|
||||||
|
assertNotNull(dog);
|
||||||
|
assertEquals(1l, dog.getId().longValue());
|
||||||
|
assertEquals("dog", dog.getName());
|
||||||
|
|
||||||
|
verify(mockRegion, times(1)).put(eq(1l), eq(dog));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSaveEntities() {
|
||||||
|
List<Animal> animals = new ArrayList<Animal>(3);
|
||||||
|
|
||||||
|
animals.add(createAnimal("bird"));
|
||||||
|
animals.add(createAnimal("cat"));
|
||||||
|
animals.add(createAnimal("dog"));
|
||||||
|
|
||||||
|
Region<Long, Animal> mockRegion = mock(Region.class, "testSaveAll");
|
||||||
|
|
||||||
|
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
|
||||||
|
createGemfireTemplate(mockRegion), mockEntityInformation());
|
||||||
|
|
||||||
|
Iterable<Animal> savedAnimals = repository.save(animals);
|
||||||
|
|
||||||
|
assertNotNull(savedAnimals);
|
||||||
|
|
||||||
|
verify(mockRegion, times(1)).putAll(eq(asMap(savedAnimals)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSaveWrapper() {
|
||||||
|
Animal dog = createAnimal(1l, "dog");
|
||||||
|
|
||||||
|
Wrapper dogWrapper = new Wrapper(dog, dog.getId());
|
||||||
|
|
||||||
|
Region<Long, Animal> mockRegion = mock(Region.class, "testSaveWrapper");
|
||||||
|
|
||||||
|
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
|
||||||
|
createGemfireTemplate(mockRegion), mockEntityInformation());
|
||||||
|
|
||||||
|
repository.save(dogWrapper);
|
||||||
|
|
||||||
|
verify(mockRegion, times(1)).put(eq(dog.getId()), eq(dog));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExists() {
|
||||||
|
final Animal dog = createAnimal(1l, "dog");
|
||||||
|
|
||||||
|
Region<Long, Animal> mockRegion = mock(Region.class, "testFindOne");
|
||||||
|
|
||||||
|
when(mockRegion.get(any(Long.class))).then(new Answer<Animal>() {
|
||||||
|
@Override public Animal answer(final InvocationOnMock invocation) throws Throwable {
|
||||||
|
return (dog.getId().equals(invocation.getArguments()[0]) ? dog : null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
|
||||||
|
createGemfireTemplate(mockRegion), mockEntityInformation());
|
||||||
|
|
||||||
|
assertTrue(repository.exists(1l));
|
||||||
|
assertFalse(repository.exists(10l));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindOne() {
|
||||||
|
final Animal dog = createAnimal(1l, "dog");
|
||||||
|
|
||||||
|
Region<Long, Animal> mockRegion = mock(Region.class, "testFindOne");
|
||||||
|
|
||||||
|
when(mockRegion.get(any(Long.class))).then(new Answer<Animal>() {
|
||||||
|
@Override public Animal answer(final InvocationOnMock invocation) throws Throwable {
|
||||||
|
return (dog.getId().equals(invocation.getArguments()[0]) ? dog : null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
|
||||||
|
createGemfireTemplate(mockRegion), mockEntityInformation());
|
||||||
|
|
||||||
|
assertEquals(dog, repository.findOne(1l));
|
||||||
|
assertNull(repository.findOne(10l));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAll() {
|
||||||
|
long id = 0;
|
||||||
|
|
||||||
|
final List<Animal> animals = new ArrayList<Animal>(3);
|
||||||
|
|
||||||
|
animals.add(createAnimal(++id, "bird"));
|
||||||
|
animals.add(createAnimal(++id, "cat"));
|
||||||
|
animals.add(createAnimal(++id, "dog"));
|
||||||
|
|
||||||
|
Region<Long, Animal> mockRegion = mock(Region.class, "testSaveAll");
|
||||||
|
|
||||||
|
when(mockRegion.getAll(any(Collection.class))).then(new Answer<Map<Long, Animal>>() {
|
||||||
|
@Override public Map<Long, Animal> answer(final InvocationOnMock invocation) throws Throwable {
|
||||||
|
Collection<Long> keys = (Collection<Long>) invocation.getArguments()[0];
|
||||||
|
Map<Long, Animal> result = new HashMap<Long, Animal>(keys.size());
|
||||||
|
for (Animal animal : animals) {
|
||||||
|
if (keys.contains(animal.getId())) {
|
||||||
|
result.put(animal.getId(), animal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
|
||||||
|
createGemfireTemplate(mockRegion), mockEntityInformation());
|
||||||
|
|
||||||
|
Collection<Animal> animalsFound = repository.findAll(Arrays.asList(1l, 3l));
|
||||||
|
|
||||||
|
assertNotNull(animalsFound);
|
||||||
|
assertEquals(2, animalsFound.size());
|
||||||
|
assertTrue(animalsFound.containsAll(CollectionUtils.subList(animals, 0, 2)));
|
||||||
|
|
||||||
|
verify(mockRegion, times(1)).getAll(eq(Arrays.asList(1l, 3l)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteById() {
|
||||||
|
Region<Long, Animal> mockRegion = mock(Region.class, "testDeleteById");
|
||||||
|
|
||||||
|
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
|
||||||
|
createGemfireTemplate(mockRegion), mockEntityInformation());
|
||||||
|
|
||||||
|
repository.delete(1l);
|
||||||
|
|
||||||
|
verify(mockRegion, times(1)).remove(eq(1l));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteEntity() {
|
||||||
|
Region<Long, Animal> mockRegion = mock(Region.class, "testDeleteEntity");
|
||||||
|
|
||||||
|
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
|
||||||
|
createGemfireTemplate(mockRegion), mockEntityInformation());
|
||||||
|
|
||||||
|
repository.delete(createAnimal(1l, "dog"));
|
||||||
|
|
||||||
|
verify(mockRegion, times(1)).remove(eq(1l));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteEntities() {
|
||||||
|
Region<Long, Animal> mockRegion = mock(Region.class, "testDeleteEntities");
|
||||||
|
|
||||||
|
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
|
||||||
|
createGemfireTemplate(mockRegion), mockEntityInformation());
|
||||||
|
|
||||||
|
repository.delete(Arrays.asList(createAnimal(1l, "bird"), createAnimal(2l, "cat"), createAnimal(3l, "dog")));
|
||||||
|
|
||||||
|
verify(mockRegion, times(1)).remove(eq(1l));
|
||||||
|
verify(mockRegion, times(1)).remove(eq(2l));
|
||||||
|
verify(mockRegion, times(1)).remove(eq(3l));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteAllWithClear() {
|
||||||
|
Region<Long, Animal> mockRegion = mock(Region.class, "testDeleteEntities");
|
||||||
|
|
||||||
|
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
|
||||||
|
createGemfireTemplate(mockRegion), mockEntityInformation());
|
||||||
|
|
||||||
|
repository.deleteAll();
|
||||||
|
|
||||||
|
verify(mockRegion, times(1)).clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteAllWithKeys() {
|
||||||
|
Region<Long, Animal> mockRegion = mock(Region.class, "testDeleteEntities");
|
||||||
|
|
||||||
|
doThrow(new UnsupportedOperationException("Not Implemented!")).when(mockRegion).clear();
|
||||||
|
when(mockRegion.keySet()).thenReturn(new HashSet<Long>(Arrays.asList(1l, 2l, 3l)));
|
||||||
|
|
||||||
|
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
|
||||||
|
createGemfireTemplate(mockRegion), mockEntityInformation());
|
||||||
|
|
||||||
|
repository.deleteAll();
|
||||||
|
|
||||||
|
verify(mockRegion, times(1)).clear();
|
||||||
|
verify(mockRegion, times(1)).remove(eq(1l));
|
||||||
|
verify(mockRegion, times(1)).remove(eq(2l));
|
||||||
|
verify(mockRegion, times(1)).remove(eq(3l));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2013 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.test.support;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The CollectionUtils class is a utility class for working with the Java Collections Framework.
|
||||||
|
*
|
||||||
|
* @author John Blum
|
||||||
|
* @see java.util.Collection
|
||||||
|
* @see java.util.Collections
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public abstract class CollectionUtils {
|
||||||
|
|
||||||
|
public static <T> Iterable<T> iterable(final Enumeration<T> enumeration) {
|
||||||
|
return new Iterable<T>() {
|
||||||
|
@Override public Iterator<T> iterator() {
|
||||||
|
return org.springframework.util.CollectionUtils.toIterator(enumeration);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Iterable<T> iterable(final Iterator<T> iterator) {
|
||||||
|
return new Iterable<T>() {
|
||||||
|
@Override public Iterator<T> iterator() {
|
||||||
|
return iterator;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> List<T> subList(final List<T> source, final int... indices) {
|
||||||
|
List<T> result = new ArrayList<T>(indices.length);
|
||||||
|
for (int index : indices) {
|
||||||
|
result.add(source.get(index));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,12 +2,21 @@
|
|||||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
xmlns:gfe="http://www.springframework.org/schema/gemfire"
|
xmlns:gfe="http://www.springframework.org/schema/gemfire"
|
||||||
xmlns:p="http://www.springframework.org/schema/p"
|
xmlns:p="http://www.springframework.org/schema/p"
|
||||||
|
xmlns:util="http://www.springframework.org/schema/util"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
|
xsi:schemaLocation="
|
||||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
|
||||||
|
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
|
||||||
">
|
">
|
||||||
|
|
||||||
<gfe:cache/>
|
<util:properties id="gemfireProperties">
|
||||||
|
<prop key="name">SpringGemFireBasicTemplate</prop>
|
||||||
|
<prop key="mcast-port">0</prop>
|
||||||
|
<prop key="log-level">warning</prop>
|
||||||
|
</util:properties>
|
||||||
|
|
||||||
|
<gfe:cache properties-ref="gemfireProperties"/>
|
||||||
|
|
||||||
<gfe:replicated-region id="simple"/>
|
<gfe:replicated-region id="simple"/>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user