Skip to content

Commit

Permalink
SGF-135: deleteAll() uses region.clear() if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
David Turanski committed Oct 17, 2012
1 parent 321a964 commit e97ab1d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Expand Up @@ -177,9 +177,13 @@ public void deleteAll() {
@Override
@SuppressWarnings("rawtypes")
public Void doInGemfire(Region region) {

for (Object key : region.keySet()) {
region.remove(key);
//clear() does not work for partitioned regions
try {
region.clear();
} catch (UnsupportedOperationException e) {
for (Object key : region.keySet()) {
region.remove(key);
}
}

return null;
Expand Down
Expand Up @@ -21,6 +21,8 @@
import java.util.Arrays;
import java.util.Collection;

import javax.annotation.Resource;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -32,7 +34,11 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.gemstone.gemfire.cache.CacheListener;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionEvent;
import com.gemstone.gemfire.cache.query.SelectResults;
import com.gemstone.gemfire.cache.util.CacheListenerAdapter;

/**
* Integration tests for {@link SimpleGemfireRepository}.
Expand All @@ -45,12 +51,20 @@ public class SimpleGemfireRepositoryIntegrationTest {

@Autowired
GemfireTemplate template;

@Resource(name="simple")
Region<?,?> simpleRegion;

SimpleGemfireRepository<Person, Long> repository;

@SuppressWarnings("rawtypes")
RegionClearListener regionClearListener;

@Before
public void setUp() {

regionClearListener = new RegionClearListener();
simpleRegion.getAttributesMutator().addCacheListener(regionClearListener);

EntityInformation<Person, Long> information = new ReflectionEntityInformation<Person, Long>(Person.class);
repository = new SimpleGemfireRepository<Person, Long>(template, information);
}
Expand All @@ -72,6 +86,13 @@ public void storeAndDeleteEntity() {
assertThat(repository.findOne(person.id), is(nullValue()));
assertThat(repository.findAll().size(), is(0));
}

@Test
public void testDeleteAllFiresClearEvent() {
assertFalse(regionClearListener.eventFired);
repository.deleteAll();
assertTrue(regionClearListener.eventFired);
}

@Test
public void queryRegion() throws Exception {
Expand Down Expand Up @@ -101,4 +122,12 @@ public void findAllWithGivenIds() {
assertThat(result, hasItems(carter, leroi));
assertThat(result, not(hasItems(dave)));
}

public static class RegionClearListener extends CacheListenerAdapter {
public boolean eventFired;
@Override
public void afterRegionClear(RegionEvent ev) {
eventFired = true;
}
}
}

0 comments on commit e97ab1d

Please sign in to comment.