diff --git a/src/main/java/org/springframework/data/gemfire/GemfireAccessor.java b/src/main/java/org/springframework/data/gemfire/GemfireAccessor.java
index 0548db81..50d4e3ff 100644
--- a/src/main/java/org/springframework/data/gemfire/GemfireAccessor.java
+++ b/src/main/java/org/springframework/data/gemfire/GemfireAccessor.java
@@ -16,21 +16,22 @@
package org.springframework.data.gemfire;
+import com.gemstone.gemfire.GemFireCheckedException;
+import com.gemstone.gemfire.GemFireException;
+import com.gemstone.gemfire.cache.Region;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.util.Assert;
-import com.gemstone.gemfire.GemFireCheckedException;
-import com.gemstone.gemfire.GemFireException;
-import com.gemstone.gemfire.cache.Region;
-
/**
- * Base class for GemfireTemplate and GemfireInterceptor, defining common properties such as {@link Region}.
+ * {@link GemfireAccessor} is a base class for {@link GemfireTemplate} defining common operations and properties,
+ * such as {@link Region}.
+ *
+ * This class is not intended to be used directly.
*
- * Not intended to be used directly.
- *
* @author Costin Leau
* @author John Blum
* @see org.springframework.beans.factory.InitializingBean
@@ -66,7 +67,7 @@ public class GemfireAccessor implements InitializingBean {
}
public void afterPropertiesSet() {
- Assert.notNull(getRegion(), "The GemFire Cache Region is required.");
+ Assert.notNull(getRegion(), "Region is required");
}
/**
@@ -96,12 +97,11 @@ public class GemfireAccessor implements InitializingBean {
* org.springframework.dao hierarchy. Note that this particular implementation
* is called only for GemFire querying exception that do NOT extend from GemFire exception.
* May be overridden in subclasses.
- *
+ *
* @param ex GemFireException that occurred
* @return the corresponding DataAccessException instance
*/
public DataAccessException convertGemFireQueryException(RuntimeException ex) {
return GemfireCacheUtils.convertQueryExceptions(ex);
}
-
}
diff --git a/src/main/java/org/springframework/data/gemfire/GemfireTemplate.java b/src/main/java/org/springframework/data/gemfire/GemfireTemplate.java
index c3dbf140..cb16331d 100644
--- a/src/main/java/org/springframework/data/gemfire/GemfireTemplate.java
+++ b/src/main/java/org/springframework/data/gemfire/GemfireTemplate.java
@@ -21,13 +21,9 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;
+import java.util.List;
import java.util.Map;
-import org.springframework.dao.DataAccessException;
-import org.springframework.dao.InvalidDataAccessApiUsageException;
-import org.springframework.util.Assert;
-import org.springframework.util.ClassUtils;
-
import com.gemstone.gemfire.GemFireCheckedException;
import com.gemstone.gemfire.GemFireException;
import com.gemstone.gemfire.cache.Region;
@@ -40,6 +36,12 @@ import com.gemstone.gemfire.cache.query.QueryService;
import com.gemstone.gemfire.cache.query.SelectResults;
import com.gemstone.gemfire.internal.cache.LocalRegion;
+import org.springframework.dao.DataAccessException;
+import org.springframework.dao.InvalidDataAccessApiUsageException;
+import org.springframework.util.Assert;
+import org.springframework.util.ClassUtils;
+import org.springframework.util.StringUtils;
+
/**
* Helper class that simplifies GemFire data access code and converts {@link GemFireCheckedException} and
* {@link GemFireException} into Spring {@link DataAccessException}, following the org.springframework.dao
@@ -50,7 +52,7 @@ import com.gemstone.gemfire.internal.cache.LocalRegion;
* explicitly care about handling {@link Region} life-cycle exceptions.
* Typically used to implement data access or business logic services that use GemFire within their implementation but
* are GemFire-agnostic in their interface. The latter or code calling the latter only have to deal with business
- * objects, query objects, and org.springframework.dao exceptions.
+ * objects, query objects, and org.springframework.dao exceptions.
*
* @author Costin Leau
* @author John Blum
@@ -111,7 +113,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
* @see org.springframework.data.gemfire.GemfireOperations#containsKey(java.lang.Object)
*/
@Override
- public boolean containsKey(final Object key) {
+ public boolean containsKey(Object key) {
return getRegion().containsKey(key);
}
@@ -119,7 +121,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
* @see org.springframework.data.gemfire.GemfireOperations#containsKeyOnServer(java.lang.Object)
*/
@Override
- public boolean containsKeyOnServer(final Object key) {
+ public boolean containsKeyOnServer(Object key) {
return getRegion().containsKeyOnServer(key);
}
@@ -127,7 +129,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
* @see org.springframework.data.gemfire.GemfireOperations#containsValue(java.lang.Object)
*/
@Override
- public boolean containsValue(final Object value) {
+ public boolean containsValue(Object value) {
return getRegion().containsValue(value);
}
@@ -135,7 +137,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
* @see org.springframework.data.gemfire.GemfireOperations#containsValueForKey(java.lang.Object)
*/
@Override
- public boolean containsValueForKey(final Object key) {
+ public boolean containsValueForKey(Object key) {
return getRegion().containsValueForKey(key);
}
@@ -143,7 +145,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
* @see org.springframework.data.gemfire.GemfireOperations#create(K, V)
*/
@Override
- public void create(final K key, final V value) {
+ public void create(K key, V value) {
try {
getRegion().create(key, value);
}
@@ -156,7 +158,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
* @see org.springframework.data.gemfire.GemfireOperations#get(K)
*/
@Override
- public V get(final K key) {
+ public V get(K key) {
try {
return this.getRegion().get(key);
}
@@ -169,7 +171,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
* @see org.springframework.data.gemfire.GemfireOperations#getAll(java.util.Collection)
*/
@Override
- public Map getAll(final Collection> keys) {
+ public Map getAll(Collection> keys) {
try {
return this.getRegion().getAll(keys);
}
@@ -182,7 +184,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
* @see org.springframework.data.gemfire.GemfireOperations#put(K, V)
*/
@Override
- public V put(final K key, final V value) {
+ public V put(K key, V value) {
try {
return this.getRegion().put(key, value);
}
@@ -195,7 +197,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
* @see org.springframework.data.gemfire.GemfireOperations#putAll(java.util.Map)
*/
@Override
- public void putAll(final Map extends K, ? extends V> map) {
+ public void putAll(Map extends K, ? extends V> map) {
try {
this.getRegion().putAll(map);
}
@@ -208,7 +210,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
* @see org.springframework.data.gemfire.GemfireOperations#putIfAbsent(K, V)
*/
@Override
- public V putIfAbsent(final K key, final V value) {
+ public V putIfAbsent(K key, V value) {
try {
return this.getRegion().putIfAbsent(key, value);
}
@@ -221,7 +223,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
* @see org.springframework.data.gemfire.GemfireOperations#remove(K)
*/
@Override
- public V remove(final K key) {
+ public V remove(K key) {
try {
return this.getRegion().remove(key);
}
@@ -234,7 +236,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
* @see org.springframework.data.gemfire.GemfireOperations#replace(K, V)
*/
@Override
- public V replace(final K key, final V value) {
+ public V replace(K key, V value) {
try {
return this.getRegion().replace(key, value);
}
@@ -247,7 +249,7 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
* @see org.springframework.data.gemfire.GemfireOperations#replace(K, V, V)
*/
@Override
- public boolean replace(final K key, final V oldValue, final V newValue) {
+ public boolean replace(K key, V oldValue, V newValue) {
try {
return this.getRegion().replace(key, oldValue, newValue);
}
@@ -256,19 +258,20 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
}
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
* @see org.springframework.data.gemfire.GemfireOperations#query(java.lang.String)
*/
@Override
- public SelectResults query(final String query) {
+ public SelectResults query(String query) {
try {
return this.getRegion().query(query);
}
- catch (IndexInvalidException ex) {
- throw convertGemFireQueryException(ex);
+ catch (IndexInvalidException e) {
+ throw convertGemFireQueryException(e);
}
- catch (QueryInvalidException ex) {
- throw convertGemFireQueryException(ex);
+ catch (QueryInvalidException e) {
+ throw convertGemFireQueryException(e);
}
catch (GemFireCheckedException e) {
throw convertGemFireAccessException(e);
@@ -287,14 +290,15 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
}
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
* @see org.springframework.data.gemfire.GemfireOperations#find(java.lang.String, java.lang.Object)
*/
@Override
@SuppressWarnings("unchecked")
- public SelectResults find(final String queryString, final Object... params) throws InvalidDataAccessApiUsageException {
+ public SelectResults find(String queryString, Object... params) throws InvalidDataAccessApiUsageException {
try {
- QueryService queryService = lookupQueryService(getRegion());
+ QueryService queryService = resolveQueryService(getRegion());
Query query = queryService.newQuery(queryString);
Object result = query.execute(params);
@@ -302,8 +306,9 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
return (SelectResults) result;
}
else {
- throw new InvalidDataAccessApiUsageException(
- "Result object returned from GemfireCallback isn't a SelectResult: [" + result + "]");
+ throw new InvalidDataAccessApiUsageException(String.format(
+ "The result from executing query [%1$s] was not an instance of SelectResults [%2$s]",
+ queryString, result));
}
}
catch (IndexInvalidException ex) {
@@ -329,26 +334,28 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
}
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
* @see org.springframework.data.gemfire.GemfireOperations#findUnique(java.lang.String, java.lang.Object)
*/
@Override
@SuppressWarnings("unchecked")
- public T findUnique(final String queryString, final Object... params) throws InvalidDataAccessApiUsageException {
+ public T findUnique(String queryString, Object... params) throws InvalidDataAccessApiUsageException {
try {
- QueryService queryService = lookupQueryService(getRegion());
+ QueryService queryService = resolveQueryService(getRegion());
Query query = queryService.newQuery(queryString);
Object result = query.execute(params);
if (result instanceof SelectResults) {
SelectResults selectResults = (SelectResults) result;
+ List results = selectResults.asList();
- if (selectResults.asList().size() == 1) {
- result = selectResults.iterator().next();
+ if (results.size() == 1) {
+ result = results.get(0);
}
else {
throw new InvalidDataAccessApiUsageException(String.format(
- "The result returned from query (%1$s) is not unique: (%2$s).", queryString, result));
+ "The result returned from query [%1$s]) was not unique [%2$s]", queryString, result));
}
}
@@ -378,43 +385,51 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
}
/**
- * Returns the query service used by the template in its find methods.
- *
- * @param region region to find the local query service from
- * @return query service to use, local or generic
+ * Returns the {@link QueryService} used by this template in its query/finder methods.
+ *
+ * @param region {@link Region} used to acquire the {@link QueryService}.
+ * @return the {@link QueryService} that will perform the query.
* @see com.gemstone.gemfire.cache.Region
* @see com.gemstone.gemfire.cache.Region#getRegionService()
* @see com.gemstone.gemfire.cache.RegionService#getQueryService()
* @see com.gemstone.gemfire.cache.client.ClientCache#getLocalQueryService()
*/
- protected QueryService lookupQueryService(final Region, ?> region) {
- return (requiresLocalQueryService(region) ? ((ClientCache) region.getRegionService()).getLocalQueryService()
- : region.getRegionService().getQueryService());
+ protected QueryService resolveQueryService(Region, ?> region) {
+ return (region.getRegionService() instanceof ClientCache ? resolveClientQueryService(region)
+ : queryServiceFrom(region));
}
- /*
- * (non-Javadoc)
- * @see com.gemstone.gemfire.cache.Region
- * @see com.gemstone.gemfire.internal.cache.LocalRegion
- */
- /* package-private */ boolean isLocalWithNoServerProxy(final Region, ?> region) {
+ /* (non-Javadoc) */
+ QueryService resolveClientQueryService(Region, ?> region) {
+ ClientCache clientCache = (ClientCache) region.getRegionService();
+
+ return (requiresLocalQueryService(region) ? clientCache.getLocalQueryService()
+ : (requiresPooledQueryService(region) ? clientCache.getQueryService(poolNameFrom(region))
+ : queryServiceFrom(region)));
+ }
+
+ /* (non-Javadoc) */
+ boolean requiresLocalQueryService(Region, ?> region) {
+ return (Scope.LOCAL.equals(region.getAttributes().getScope()) && isLocalWithNoServerProxy(region));
+ }
+
+ /* (non-Javadoc) */
+ boolean isLocalWithNoServerProxy(Region, ?> region) {
return (region instanceof LocalRegion && !((LocalRegion) region).hasServerProxy());
}
- /*
- * (non-Javadoc)
- * @see #isLocalWithNoServerProxy(:Region)
- * @see com.gemstone.gemfire.cache.Region
- * @see com.gemstone.gemfire.cache.RegionAttributes
- * @see com.gemstone.gemfire.cache.RegionAttributes#getScope()
- * @see com.gemstone.gemfire.cache.Scope
- * @see com.gemstone.gemfire.cache.client.ClientCache
- * @see com.gemstone.gemfire.internal.cache.LocalRegion
- * @see com.gemstone.gemfire.internal.cache.LocalRegion#hasServerProxy()
- */
- private boolean requiresLocalQueryService(final Region, ?> region) {
- return (region.getRegionService() instanceof ClientCache && isLocalWithNoServerProxy(region)
- && Scope.LOCAL.equals(region.getAttributes().getScope()));
+ boolean requiresPooledQueryService(Region, ?> region) {
+ return StringUtils.hasText(poolNameFrom(region));
+ }
+
+ /* (non-Javadoc) */
+ QueryService queryServiceFrom(Region, ?> region) {
+ return region.getRegionService().getQueryService();
+ }
+
+ /* (non-Javadoc) */
+ String poolNameFrom(Region, ?> region) {
+ return region.getAttributes().getPoolName();
}
/*
@@ -433,9 +448,11 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
@Override
public T execute(GemfireCallback action, boolean exposeNativeRegion) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
+
try {
- Region, ?> regionToExpose = (exposeNativeRegion ? getRegion() : regionProxy);
- return action.doInGemfire(regionToExpose);
+ Region, ?> regionArgument = (exposeNativeRegion ? getRegion() : regionProxy);
+
+ return action.doInGemfire(regionArgument);
}
catch (IndexInvalidException ex) {
throw convertGemFireQueryException(ex);
@@ -472,9 +489,11 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
* @see #execute(GemfireCallback, boolean)
*/
@SuppressWarnings("unchecked")
- protected Region createRegionProxy(final Region region) {
- return (Region) Proxy.newProxyInstance(region.getClass().getClassLoader(),
- ClassUtils.getAllInterfacesForClass(region.getClass(), getClass().getClassLoader()),
+ protected Region createRegionProxy(Region region) {
+ Class> regionType = region.getClass();
+
+ return (Region) Proxy.newProxyInstance(regionType.getClassLoader(),
+ ClassUtils.getAllInterfacesForClass(regionType, getClass().getClassLoader()),
new RegionCloseSuppressingInvocationHandler(region));
}
@@ -516,5 +535,4 @@ public class GemfireTemplate extends GemfireAccessor implements GemfireOperation
}
}
}
-
}
diff --git a/src/main/java/org/springframework/data/gemfire/util/PropertiesBuilder.java b/src/main/java/org/springframework/data/gemfire/util/PropertiesBuilder.java
index 7683e19f..924e7e94 100644
--- a/src/main/java/org/springframework/data/gemfire/util/PropertiesBuilder.java
+++ b/src/main/java/org/springframework/data/gemfire/util/PropertiesBuilder.java
@@ -50,6 +50,18 @@ public class PropertiesBuilder implements FactoryBean {
return new PropertiesBuilder();
}
+ /**
+ * Factory method to create an instance of {@link PropertiesBuilder} initialized with the given {@link Properties}.
+ *
+ * @param properties {@link Properties} used as the default properties of the constructed {@link PropertiesBuilder}.
+ * @return an instance of {@link PropertiesBuilder} initialized with the given {@link Properties}.
+ * @see java.util.Properties
+ * @see #PropertiesBuilder(Properties)
+ */
+ public static PropertiesBuilder from(Properties properties) {
+ return new PropertiesBuilder(properties);
+ }
+
/**
* Constructs and initializes a {@link PropertiesBuilder} containing all properties
* from the given {@link InputStream}.
diff --git a/src/test/java/org/springframework/data/gemfire/GemfireTemplateIntegrationTest.java b/src/test/java/org/springframework/data/gemfire/GemfireTemplateIntegrationTest.java
deleted file mode 100644
index 585ddd58..00000000
--- a/src/test/java/org/springframework/data/gemfire/GemfireTemplateIntegrationTest.java
+++ /dev/null
@@ -1,336 +0,0 @@
-/*
- * 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;
-
-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 java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Calendar;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import javax.annotation.Resource;
-
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.dao.InvalidDataAccessApiUsageException;
-import org.springframework.data.gemfire.repository.sample.User;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
-import com.gemstone.gemfire.cache.Region;
-import com.gemstone.gemfire.cache.query.SelectResults;
-
-/**
- * The GemfireTemplateIntegrationTest class is a test suite of test cases testing the contract and functionality
- * of the SDG GemfireTemplate class for wrapping a GemFire Cache Region and performing Cache Region operations.
- *
- * @author John Blum
- * @see org.junit.Test
- * @see org.springframework.data.gemfire.GemfireTemplate
- * @see org.springframework.test.context.ContextConfiguration
- * @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
- * @since 1.4.0
- */
-@ContextConfiguration
-@RunWith(SpringJUnit4ClassRunner.class)
-@SuppressWarnings("unused")
-public class GemfireTemplateIntegrationTest {
-
- protected static final List TEST_USER_LIST = new ArrayList(11);
-
- static {
- TEST_USER_LIST.add(createUser("jonDoe"));
- TEST_USER_LIST.add(createUser("janeDoe", false));
- TEST_USER_LIST.add(createUser("pieDoe", false));
- TEST_USER_LIST.add(createUser("cookieDoe"));
- TEST_USER_LIST.add(createUser("jackHandy"));
- TEST_USER_LIST.add(createUser("mandyHandy", false));
- TEST_USER_LIST.add(createUser("randyHandy", false));
- TEST_USER_LIST.add(createUser("sandyHandy"));
- TEST_USER_LIST.add(createUser("imaPigg"));
- }
-
- @Resource(name = "Users")
- private Region users;
-
- @Autowired
- private GemfireTemplate usersTemplate;
-
- protected static User createUser(final String username) {
- return createUser(username, true);
- }
-
- protected static User createUser(final String username, final Boolean active) {
- return createUser(username, String.format("%1$s@companyx.com", username), Calendar.getInstance(), active);
- }
-
- protected static User createUser(final String username, final String email, final Calendar since, final Boolean active) {
- User user = new User(username);
- user.setActive(Boolean.TRUE.equals(active));
- user.setEmail(email);
- user.setSince(since);
- return user;
- }
-
- protected String getKey(final User user) {
- return (user != null ? user.getUsername() : null);
- }
-
- protected User getUser(final String username) {
- for (User user : TEST_USER_LIST) {
- if (user.getUsername().equals(username)) {
- return user;
- }
- }
-
- return null;
- }
-
- protected List getUsers(final String... usernames) {
- List users = new ArrayList(usernames.length);
- List usernameList = Arrays.asList(usernames);
-
- for (User user : TEST_USER_LIST) {
- if (usernameList.contains(user.getUsername())) {
- users.add(user);
- }
- }
-
- return users;
- }
-
- protected Map getUsersAsMap(final User... users) {
- Map userMap = new HashMap(users.length);
-
- for (User user : users) {
- userMap.put(getKey(user), user);
- }
-
- return userMap;
- }
-
- protected void assertNullEquals(final Object value1, final Object value2) {
- Assert.assertTrue(value1 == null ? value2 == null : value1.equals(value2));
- }
-
- @Before
- public void setup() {
- assertNotNull("The 'Users' Region was not properly configured and initialized!", users);
-
- if (users.isEmpty()) {
- for (User user : TEST_USER_LIST) {
- users.put(getKey(user), user);
- }
-
- assertFalse(users.isEmpty());
- assertEquals(TEST_USER_LIST.size(), users.size());
- }
- }
-
- @Test
- public void testContainsKey() {
- assertTrue(usersTemplate.containsKey(getKey(getUser("jonDoe"))));
- assertFalse(usersTemplate.containsKey("dukeNukem"));
- }
-
- @Test
- @Ignore
- public void testContainsKeyOnServer() {
- assertTrue(usersTemplate.containsKeyOnServer(getKey(getUser("jackHandy"))));
- assertFalse(usersTemplate.containsKeyOnServer("maxPayne"));
- }
-
- @Test
- public void testContainsValue() {
- assertTrue(usersTemplate.containsValue(getUser("pieDoe")));
- assertFalse(usersTemplate.containsValue(createUser("pieDough")));
- }
-
- @Test
- public void testContainsValueForKey() {
- assertTrue(usersTemplate.containsValueForKey(getKey(getUser("cookieDoe"))));
- assertFalse(usersTemplate.containsValueForKey("chocolateChipCookieDoe"));
- }
-
- @Test
- public void testCreate() {
- User bartSimpson = createUser("bartSimpson");
-
- usersTemplate.create(getKey(bartSimpson), bartSimpson);
-
- assertTrue(users.containsKey(getKey(bartSimpson)));
- assertTrue(users.containsValueForKey(getKey(bartSimpson)));
- assertTrue(users.containsValue(bartSimpson));
- assertEquals(bartSimpson, users.get(getKey(bartSimpson)));
- }
-
- @Test
- public void testGet() {
- assertEquals(users.get(getKey(getUser("imaPigg"))), usersTemplate.get(getKey(getUser("imaPigg"))));
- assertNullEquals(users.get("mrT"), usersTemplate.get("mrT"));
- }
-
- @Test
- public void testPut() {
- User peterGriffon = createUser("peterGriffon");
-
- assertNull(usersTemplate.put(getKey(peterGriffon), peterGriffon));
- assertEquals(peterGriffon, users.get(getKey(peterGriffon)));
- }
-
- @Test
- public void testPutIfAbsent() {
- User stewieGriffon = createUser("stewieGriffon");
-
- assertFalse(users.containsValue(stewieGriffon));
- assertNull(usersTemplate.putIfAbsent(getKey(stewieGriffon), stewieGriffon));
- assertTrue(users.containsValue(stewieGriffon));
- assertEquals(stewieGriffon, usersTemplate.putIfAbsent(getKey(stewieGriffon), createUser("megGriffon")));
- assertEquals(stewieGriffon, users.get(getKey(stewieGriffon)));
- }
-
- @Test
- public void testRemove() {
- User mandyHandy = users.get(getKey(getUser("mandyHandy")));
-
- assertNotNull(mandyHandy);
- assertEquals(mandyHandy, usersTemplate.remove(getKey(mandyHandy)));
- assertFalse(users.containsKey(getKey(mandyHandy)));
- assertFalse(users.containsValue(mandyHandy));
- assertFalse(users.containsKey("loisGriffon"));
- assertNull(usersTemplate.remove("loisGriffon"));
- assertFalse(users.containsKey("loisGriffon"));
- }
-
- @Test
- public void testReplace() {
- User randyHandy = users.get(getKey(getUser("randyHandy")));
- User lukeFluke = createUser("lukeFluke");
- User chrisGriffon = createUser("chrisGriffon");
-
- assertNotNull(randyHandy);
- assertEquals(randyHandy, usersTemplate.replace(getKey(randyHandy), lukeFluke));
- assertEquals(lukeFluke, users.get(getKey(randyHandy)));
- assertFalse(users.containsValue(randyHandy));
- assertFalse(users.containsValue(chrisGriffon));
- assertNull(usersTemplate.replace(getKey(chrisGriffon), chrisGriffon));
- assertFalse(users.containsValue(chrisGriffon));
- }
-
- @Test
- public void testReplaceOldValueWithNewValue() {
- User jackHandy = getUser("jackHandy");
- User imaPigg = getUser("imaPigg");
-
- assertTrue(users.containsValue(jackHandy));
- assertFalse(usersTemplate.replace(getKey(jackHandy), null, imaPigg));
- assertTrue(users.containsValue(jackHandy));
- assertEquals(jackHandy, users.get(getKey(jackHandy)));
- assertTrue(usersTemplate.replace(getKey(jackHandy), jackHandy, imaPigg));
- assertFalse(users.containsValue(jackHandy));
- assertEquals(imaPigg, users.get(getKey(jackHandy)));
- }
-
- @Test
- public void testGetAllReturnsNoResults() {
- List keys = Arrays.asList("keyOne", "keyTwo", "keyThree");
-
- Map actualUserMapping = usersTemplate.getAll(keys);
- Map expectedUserMapping = users.getAll(keys);
-
- assertEquals(expectedUserMapping, actualUserMapping);
- }
-
- @Test
- public void testGetAllReturnsResults() {
- List keys = Arrays.asList(getKey(getUser("jonDoe")), getKey(getUser("pieDoe")));
-
- Map actualUserMapping = usersTemplate.getAll(keys);
- Map expectedUserMapping = users.getAll(keys);
-
- assertEquals(actualUserMapping, expectedUserMapping);
- }
-
- @Test
- public void testPutAll() {
- User batMan = createUser("batMap");
- User spiderMan = createUser("spiderMan");
- User superMan = createUser("superMan");
-
- Map userMap = getUsersAsMap(batMan, spiderMan, superMan);
-
- assertFalse(users.keySet().containsAll(userMap.keySet()));
- assertFalse(users.values().containsAll(userMap.values()));
-
- usersTemplate.putAll(userMap);
-
- assertTrue(users.keySet().containsAll(userMap.keySet()));
- assertTrue(users.values().containsAll(userMap.values()));
- }
-
- @Test
- public void testQuery() {
- SelectResults queryResults = usersTemplate.query("username LIKE '%Doe'");
-
- assertNotNull(queryResults);
-
- List queriedUsers = queryResults.asList();
-
- assertNotNull(queriedUsers);
- assertFalse(queriedUsers.isEmpty());
- assertEquals(4, queriedUsers.size());
- assertTrue(queriedUsers.containsAll(getUsers("jonDoe", "janeDoe", "pieDoe", "cookieDoe")));
- }
-
- @Test
- public void testFind() {
- SelectResults findResults = usersTemplate.find("SELECT u FROM /Users u WHERE u.username LIKE $1 AND u.active = $2", "%Doe", true);
-
- assertNotNull(findResults);
-
- List usersFound = findResults.asList();
-
- assertNotNull(usersFound);
- assertFalse(usersFound.isEmpty());
- assertEquals(2, usersFound.size());
- assertTrue(usersFound.containsAll(getUsers("jonDoe", "cookieDoe")));
- }
-
- @Test
- public void testFindUniqueReturnsResult() {
- User jonDoe = usersTemplate.findUnique("SELECT u FROM /Users u WHERE u.username = $1", "jonDoe");
-
- assertNotNull(jonDoe);
- assertEquals(getUser("jonDoe"), jonDoe);
- }
-
- @Test(expected = InvalidDataAccessApiUsageException.class)
- public void testFindUniqueReturnsNoResult() {
- usersTemplate.findUnique("SELECT u FROM /Users u WHERE u.username = $1", "benDover");
- }
-
-}
diff --git a/src/test/java/org/springframework/data/gemfire/GemfireTemplateIntegrationTests.java b/src/test/java/org/springframework/data/gemfire/GemfireTemplateIntegrationTests.java
new file mode 100644
index 00000000..bccfb3a3
--- /dev/null
+++ b/src/test/java/org/springframework/data/gemfire/GemfireTemplateIntegrationTests.java
@@ -0,0 +1,399 @@
+/*
+ * 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;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assume.assumeThat;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.annotation.Resource;
+
+import com.gemstone.gemfire.cache.GemFireCache;
+import com.gemstone.gemfire.cache.Region;
+import com.gemstone.gemfire.cache.query.SelectResults;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.dao.InvalidDataAccessApiUsageException;
+import org.springframework.data.gemfire.repository.sample.User;
+import org.springframework.data.gemfire.util.CacheUtils;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+/**
+ * Integration tests for {@link GemfireTemplate}.
+ *
+ * @author John Blum
+ * @see org.junit.Test
+ * @see org.springframework.data.gemfire.GemfireTemplate
+ * @see org.springframework.test.context.ContextConfiguration
+ * @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
+ * @since 1.4.0
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration
+@SuppressWarnings("unused")
+public class GemfireTemplateIntegrationTests {
+
+ protected static final String DEFAULT_GEMFIRE_LOG_LEVEL = "warning";
+
+ protected static final List TEST_USERS = new ArrayList(9);
+
+ static {
+ TEST_USERS.add(newUser("jonDoe"));
+ TEST_USERS.add(newUser("janeDoe", false));
+ TEST_USERS.add(newUser("pieDoe", false));
+ TEST_USERS.add(newUser("cookieDoe"));
+ TEST_USERS.add(newUser("jackHandy"));
+ TEST_USERS.add(newUser("mandyHandy", false));
+ TEST_USERS.add(newUser("randyHandy", false));
+ TEST_USERS.add(newUser("sandyHandy"));
+ TEST_USERS.add(newUser("imaPigg"));
+ }
+
+ @Autowired
+ private GemFireCache gemfireCache;
+
+ @Autowired
+ private GemfireTemplate usersTemplate;
+
+ @Resource(name = "Users")
+ private Region users;
+
+ protected static User newUser(String username) {
+ return newUser(username, true);
+ }
+
+ protected static User newUser(String username, Boolean active) {
+ return newUser(username, String.format("%1$s@companyx.com", username), Calendar.getInstance(), active);
+ }
+
+ protected static User newUser(String username, String email, Calendar since, Boolean active) {
+ User user = new User(username);
+ user.setActive(Boolean.TRUE.equals(active));
+ user.setEmail(email);
+ user.setSince(since);
+ return user;
+ }
+
+ protected String getKey(User user) {
+ return (user != null ? user.getUsername() : null);
+ }
+
+ protected User getUser(String username) {
+ for (User user : TEST_USERS) {
+ if (user.getUsername().equals(username)) {
+ return user;
+ }
+ }
+
+ return null;
+ }
+
+ protected List getUsers(String... usernames) {
+ List usernameList = Arrays.asList(usernames);
+ List users = new ArrayList(usernames.length);
+
+ for (User user : TEST_USERS) {
+ if (usernameList.contains(user.getUsername())) {
+ users.add(user);
+ }
+ }
+
+ return users;
+ }
+
+ protected Map getUsersAsMap(String... usernames) {
+ return getUsersAsMap(getUsers(usernames));
+ }
+
+ protected Map getUsersAsMap(User... users) {
+ return getUsersAsMap(Arrays.asList(users));
+ }
+
+ protected Map getUsersAsMap(Iterable users) {
+ Map userMap = new HashMap();
+
+ for (User user : users) {
+ userMap.put(getKey(user), user);
+ }
+
+ return userMap;
+ }
+
+ protected void assertNullEquals(Object value1, Object value2) {
+ assertThat(value1 == null ? value2 == null : value1.equals(value2)).isTrue();
+ }
+
+ @Before
+ public void setup() {
+ assertThat(users).isNotNull();
+
+ if (users.isEmpty()) {
+ for (User user : TEST_USERS) {
+ users.put(getKey(user), user);
+ }
+
+ assertThat(users.isEmpty()).isFalse();
+ assertThat(users.size()).isEqualTo(TEST_USERS.size());
+ }
+ }
+
+ @Test
+ public void containsKey() {
+ assertThat(usersTemplate.containsKey(getKey(getUser("jonDoe")))).isTrue();
+ assertThat(usersTemplate.containsKey("dukeNukem")).isFalse();
+ }
+
+ @Test
+ public void containsKeyOnServer() {
+ assumeThat(CacheUtils.isClient(gemfireCache), is(true));
+ assertThat(usersTemplate.containsKeyOnServer(getKey(getUser("jackHandy")))).isTrue();
+ assertThat(usersTemplate.containsKeyOnServer("maxPayne")).isFalse();
+ }
+
+ @Test
+ public void containsValue() {
+ assertThat(usersTemplate.containsValue(getUser("pieDoe"))).isTrue();
+ assertThat(usersTemplate.containsValue(newUser("pieDough"))).isFalse();
+ }
+
+ @Test
+ public void containsValueForKey() {
+ assertThat(usersTemplate.containsValueForKey(getKey(getUser("cookieDoe")))).isTrue();
+ assertThat(usersTemplate.containsValueForKey("chocolateChipCookieDoe")).isFalse();
+ }
+
+ @Test
+ public void create() {
+ User bartSimpson = newUser("bartSimpson");
+
+ usersTemplate.create(getKey(bartSimpson), bartSimpson);
+
+ assertThat(users.containsKey(getKey(bartSimpson))).isTrue();
+ assertThat(users.containsValueForKey(getKey(bartSimpson))).isTrue();
+ assertThat(users.containsValue(bartSimpson)).isTrue();
+ assertThat(users.get(getKey(bartSimpson))).isEqualTo(bartSimpson);
+ }
+
+ @Test
+ public void get() {
+ String key = getKey(getUser("imaPigg"));
+
+ assertThat(usersTemplate.get(key)).isEqualTo(users.get(key));
+ assertNullEquals(users.get("mrT"), usersTemplate.get("mrT"));
+ }
+
+ @Test
+ public void put() {
+ User peterGriffon = newUser("peterGriffon");
+
+ assertThat(usersTemplate.put(getKey(peterGriffon), peterGriffon)).isNull();
+ assertThat(users.get(getKey(peterGriffon))).isEqualTo(peterGriffon);
+ }
+
+ @Test
+ public void putIfAbsent() {
+ User stewieGriffon = newUser("stewieGriffon");
+
+ assertThat(users.containsValue(stewieGriffon)).isFalse();
+ assertThat(usersTemplate.putIfAbsent(getKey(stewieGriffon), stewieGriffon)).isNull();
+ assertThat(users.containsValue(stewieGriffon)).isTrue();
+ assertThat(usersTemplate.putIfAbsent(getKey(stewieGriffon), newUser("megGriffon"))).isEqualTo(stewieGriffon);
+ assertThat(users.get(getKey(stewieGriffon))).isEqualTo(stewieGriffon);
+ }
+
+ @Test
+ public void remove() {
+ User mandyHandy = users.get(getKey(getUser("mandyHandy")));
+
+ assertThat(mandyHandy).isNotNull();
+ assertThat(usersTemplate.remove(getKey(mandyHandy))).isEqualTo(mandyHandy);
+ assertThat(users.containsKey(getKey(mandyHandy))).isFalse();
+ assertThat(users.containsValue(mandyHandy)).isFalse();
+ assertThat(users.containsKey("loisGriffon")).isFalse();
+ assertThat(usersTemplate.remove("loisGriffon")).isNull();
+ assertThat(users.containsKey("loisGriffon")).isFalse();
+ }
+
+ @Test
+ public void replace() {
+ User randyHandy = users.get(getKey(getUser("randyHandy")));
+ User lukeFluke = newUser("lukeFluke");
+ User chrisGriffon = newUser("chrisGriffon");
+
+ assertThat(randyHandy).isNotNull();
+ assertThat(usersTemplate.replace(getKey(randyHandy), lukeFluke)).isEqualTo(randyHandy);
+ assertThat(users.get(getKey(randyHandy))).isEqualTo(lukeFluke);
+ assertThat(users.containsValue(randyHandy)).isFalse();
+ assertThat(users.containsValue(chrisGriffon)).isFalse();
+ assertThat(usersTemplate.replace(getKey(chrisGriffon), chrisGriffon)).isNull();
+ assertThat(users.containsValue(chrisGriffon)).isFalse();
+ }
+
+ @Test
+ public void replaceOldValueWithNewValue() {
+ User jackHandy = getUser("jackHandy");
+ User imaPigg = getUser("imaPigg");
+
+ assertThat(users.containsValue(jackHandy)).isTrue();
+ assertThat(usersTemplate.replace(getKey(jackHandy), null, imaPigg)).isFalse();
+ assertThat(users.containsValue(jackHandy)).isTrue();
+ assertThat(users.get(getKey(jackHandy))).isEqualTo(jackHandy);
+ assertThat(usersTemplate.replace(getKey(jackHandy), jackHandy, imaPigg)).isTrue();
+ assertThat(users.containsValue(jackHandy)).isFalse();
+ assertThat(users.get(getKey(jackHandy))).isEqualTo(imaPigg);
+ }
+
+ @Test
+ public void getAllReturnsNoResults() {
+ List keys = Arrays.asList("keyOne", "keyTwo", "keyThree");
+ Map users = usersTemplate.getAll(keys);
+
+ assertThat(users).isNotNull();
+ assertThat(users).isEqualTo(this.users.getAll(keys));
+ }
+
+ @Test
+ public void getAllReturnsResults() {
+ Map users = usersTemplate.getAll(Arrays.asList(
+ getKey(getUser("jonDoe")), getKey(getUser("pieDoe"))));
+
+ assertThat(users).isNotNull();
+ assertThat(users).isEqualTo(getUsersAsMap(getUser("jonDoe"), getUser("pieDoe")));
+ }
+
+ @Test
+ public void putAll() {
+ User batMan = newUser("batMan");
+ User spiderMan = newUser("spiderMan");
+ User superMan = newUser("superMan");
+
+ Map userMap = getUsersAsMap(batMan, spiderMan, superMan);
+
+ assertThat(users.keySet().containsAll(userMap.keySet())).isFalse();
+ assertThat(users.values().containsAll(userMap.values())).isFalse();
+
+ usersTemplate.putAll(userMap);
+
+ assertThat(users.keySet().containsAll(userMap.keySet())).isTrue();
+ assertThat(users.values().containsAll(userMap.values())).isTrue();
+ }
+
+ @Test
+ public void query() {
+ SelectResults queryResults = usersTemplate.query("username LIKE '%Doe'");
+
+ assertThat(queryResults).isNotNull();
+
+ List usersFound = queryResults.asList();
+
+ assertThat(usersFound).isNotNull();
+ assertThat(usersFound.size()).isEqualTo(4);
+ assertThat(usersFound.containsAll(getUsers("jonDoe", "janeDoe", "pieDoe", "cookieDoe"))).isTrue();
+ }
+
+ @Test
+ public void find() {
+ SelectResults findResults = usersTemplate.find("SELECT u FROM /Users u WHERE u.username LIKE $1 AND u.active = $2", "%Doe", true);
+
+ assertThat(findResults).isNotNull();
+
+ List usersFound = findResults.asList();
+
+ assertThat(usersFound).isNotNull();
+ assertThat(usersFound.size()).isEqualTo(2);
+ assertThat(usersFound.containsAll(getUsers("jonDoe", "cookieDoe"))).isTrue();
+ }
+
+ @Test
+ public void findUniqueReturnsResult() {
+ User jonDoe = usersTemplate.findUnique("SELECT u FROM /Users u WHERE u.username = $1", "jonDoe");
+
+ assertThat(jonDoe).isNotNull();
+ assertThat(jonDoe).isEqualTo(getUser("jonDoe"));
+ }
+
+ @Test(expected = InvalidDataAccessApiUsageException.class)
+ public void findUniqueReturnsNoResult() {
+ usersTemplate.findUnique("SELECT u FROM /Users u WHERE u.username = $1", "benDover");
+ }
+
+ @Test(expected = InvalidDataAccessApiUsageException.class)
+ public void findUnqiueReturnsTooManyResults() {
+ usersTemplate.findUnique("SELECT u FROM /Users u WHERE u.username LIKE $1", "%Doe");
+ }
+
+ @Configuration
+ static class GemfireTemplateConfiguration {
+
+ Properties gemfireProperties() {
+ Properties gemfireProperties = new Properties();
+
+ gemfireProperties.setProperty("name", applicationName());
+ gemfireProperties.setProperty("mcast-port", "0");
+ gemfireProperties.setProperty("log-level", logLevel());
+ return gemfireProperties;
+ }
+
+ String applicationName() {
+ return GemfireTemplateIntegrationTests.class.getName();
+ }
+
+ String logLevel() {
+ return System.getProperty("gemfire.log-level", DEFAULT_GEMFIRE_LOG_LEVEL);
+ }
+
+ @Bean
+ CacheFactoryBean gemfireCache() {
+ CacheFactoryBean gemfireCache = new CacheFactoryBean();
+
+ gemfireCache.setClose(false);
+ gemfireCache.setProperties(gemfireProperties());
+
+ return gemfireCache;
+ }
+
+ @Bean(name = "Users")
+ LocalRegionFactoryBean